-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(attributes): add proc status and mem info for linux
- Loading branch information
1 parent
7771f60
commit d47eedd
Showing
2 changed files
with
111 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package bt | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"log" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
memPath = "/proc/meminfo" | ||
procPath = "/proc/self/status" | ||
) | ||
|
||
var ( | ||
paths = []string{memPath, procPath} | ||
memMap = map[string]string{ | ||
"MemTotal": "system.memory.total", | ||
"MemFree": "system.memory.free", | ||
"MemAvailable": "system.memory.available", | ||
"Buffers": "system.memory.buffers", | ||
"Cached": "system.memory.cached", | ||
"SwapCached": "system.memory.swap.cached", | ||
"Active": "system.memory.active", | ||
"Inactive": "system.memory.inactive", | ||
"SwapTotal": "system.memory.swap.total", | ||
"SwapFree": "system.memory.swap.free", | ||
"Dirty": "system.memory.dirty", | ||
"Writeback": "system.memory.writeback", | ||
"Slab": "system.memory.slab", | ||
"VmallocTotal": "system.memory.vmalloc.total", | ||
"VmallocUsed": "system.memory.vmalloc.used", | ||
"VmallocChunk": "system.memory.vmalloc.chunk", | ||
} | ||
|
||
procMap = map[string]string{ | ||
"nonvoluntary_ctxt_switches": " sched.cs.involuntary", | ||
"voluntary_ctxt_switches": "sched.cs.voluntary", | ||
"FDSize": "descriptor.count", | ||
"VmData": "vm.data.size", | ||
"VmLck": "vm.locked.size", | ||
"VmPTE": "vm.pte.size", | ||
"VmHWM": "vm.rss.peak", | ||
"VmRSS": "vm.rss.size", | ||
"VmLib": "vm.shared.size", | ||
"VmStk": "vm.stack.size", | ||
"VmSwap": "vm.swap.size", | ||
"VmPeak": "vm.vma.peak", | ||
"VmSize": "vm.vma.size", | ||
} | ||
) | ||
|
||
func readMemProcInfo() { | ||
done := make(chan bool, 2) | ||
for _, path := range paths { | ||
go readFile(path, done) | ||
} | ||
|
||
// wait for readFile to finish | ||
for i := 0; i < 2; i++ { | ||
<-done | ||
} | ||
} | ||
|
||
func readFile(path string, done chan bool) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer file.Close() | ||
|
||
reader := bufio.NewReader(file) | ||
for { | ||
l, _, err := reader.ReadLine() | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} else { | ||
if Options.DebugBacktrace { | ||
log.Printf("readFile err: %v", err) | ||
} | ||
break | ||
} | ||
} | ||
|
||
values := strings.Split(string(l), ":") | ||
if len(values) == 2 { | ||
trimmedValue := strings.TrimSpace(values[1]) | ||
if path == memPath { | ||
mapValues(values[0], trimmedValue, memMap) | ||
} else { | ||
mapValues(values[0], trimmedValue, procMap) | ||
} | ||
} | ||
} | ||
done <- true | ||
} | ||
|
||
func mapValues(key string, value string, mapper map[string]string) { | ||
if attr, exists := mapper[key]; exists { | ||
Options.Attributes[attr] = value | ||
} | ||
} |