-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract types and parsing into subpackages (#9)
- Loading branch information
Showing
6 changed files
with
322 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/TimRots/gutil-linux | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package irq | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"regexp" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const PATH_PROC_INTERRUPTS = "/proc/interrupts" | ||
|
||
type Interrupt struct { | ||
Irq string | ||
Total int | ||
Name string | ||
} | ||
|
||
func IrqStat() (Interrupts []Interrupt, err error) { | ||
var f *os.File | ||
if f, err = os.Open(PATH_PROC_INTERRUPTS); err != nil { | ||
return | ||
} | ||
defer f.Close() | ||
|
||
var ( | ||
active_cpu_count int | ||
irq string | ||
name []string | ||
) | ||
scanner := bufio.NewScanner(f) | ||
for ln := 0; scanner.Scan(); ln++ { | ||
if ln == 0 { | ||
// First line shows the active cpus | ||
active_cpu_count = strings.Count(scanner.Text(), "CPU") | ||
continue | ||
} | ||
|
||
// Match all non-whitespace character sequences | ||
i, total, words := 0, 0, regexp.MustCompile(`[\S]+`).FindAllString(scanner.Text(), -1) | ||
for _, word := range words { | ||
// As the lines are variadic in length we use active_cpu_count and | ||
// i count to determine how to parse the value based on the word its position. | ||
i++ | ||
switch { | ||
case i == 1: | ||
irq = strings.ReplaceAll(string(word), ":", "") | ||
case i <= active_cpu_count+1: | ||
numint, _ := strconv.Atoi(word) | ||
total += numint | ||
case i > active_cpu_count+1: | ||
name = append(name, string(word)) | ||
} | ||
} | ||
Interrupts = append(Interrupts, Interrupt{irq, total, strings.Join(name, " ")}) | ||
irq, name = "", nil | ||
} | ||
|
||
// Sort slice by number | ||
sort.Slice(Interrupts, func(y, z int) bool { | ||
return Interrupts[y].Total > Interrupts[z].Total | ||
}) | ||
|
||
return | ||
} |
Oops, something went wrong.