-
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 (#25)
During report creation, we need to generate information about current process state - for example: memory information On Linux - we have a really nice way to get those attributes. We should read /proc/meminfo and /proc/self/status to get information about the process. Next we need to convert data from the file into attributes
- Loading branch information
1 parent
7771f60
commit 1164657
Showing
2 changed files
with
115 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,108 @@ | ||
package bt | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
memPath = "/proc/meminfo" | ||
procPath = "/proc/self/status" | ||
) | ||
|
||
var ( | ||
paths = []string{memPath, procPath} | ||
mapper = 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", | ||
"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() { | ||
for _, path := range paths { | ||
readFile(path) | ||
} | ||
} | ||
|
||
func readFile(path string) { | ||
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 { | ||
if attr, exists := mapper[values[0]]; exists { | ||
value, err := getValue(values[1]) | ||
if err != nil { | ||
continue | ||
} | ||
Options.Attributes[attr] = value | ||
} | ||
} | ||
} | ||
} | ||
|
||
func getValue(value string) (string, error) { | ||
value = strings.TrimSpace(value) | ||
if strings.HasSuffix(value, "kB") { | ||
value = strings.TrimSuffix(value, " kB") | ||
|
||
atoi, err := strconv.ParseInt(value, 10, 64) | ||
if err != nil && Options.DebugBacktrace { | ||
log.Printf("readFile err: %v", err) | ||
return "", err | ||
} | ||
atoi *= 1024 | ||
return fmt.Sprintf("%d", atoi), err | ||
} | ||
|
||
return value, nil | ||
} |