Skip to content

Commit

Permalink
Merge pull request #29 from mackerelio/darwin-parse-page-size
Browse files Browse the repository at this point in the history
[darwin] parse page size in vm_stat output
  • Loading branch information
susisu authored Oct 11, 2021
2 parents 18b73dd + 76554a6 commit 48de62d
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions memory/memory_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ type Stats struct {
}

// References:
// - https://support.apple.com/en-us/HT201464#memory
// - https://developer.apple.com/library/content/documentation/Performance/Conceptual/ManagingMemoryStats/Articles/AboutMemoryStats.html
// - https://opensource.apple.com/source/system_cmds/system_cmds-790/vm_stat.tproj/
// - https://support.apple.com/guide/activity-monitor/view-memory-usage-actmntr1004/10.14/mac/11.0
// - https://opensource.apple.com/source/system_cmds/system_cmds-880.60.2/vm_stat.tproj/
func collectMemoryStats(out io.Reader) (*Stats, error) {
scanner := bufio.NewScanner(out)
if !scanner.Scan() {
return nil, fmt.Errorf("failed to scan output of vm_stat")
}
line := scanner.Text()
if !strings.HasPrefix(line, "Mach Virtual Memory Statistics:") {
var pageSize uint64
if _, err := fmt.Sscanf(line, "Mach Virtual Memory Statistics: (page size of %d bytes)", &pageSize); err != nil {
return nil, fmt.Errorf("unexpected output of vm_stat: %s", line)
}

Expand All @@ -95,7 +95,7 @@ func collectMemoryStats(out io.Reader) (*Stats, error) {
if ptr := memStats[line[:i]]; ptr != nil {
val := strings.TrimRight(strings.TrimSpace(line[i+1:]), ".")
if v, err := strconv.ParseUint(val, 10, 64); err == nil {
*ptr = v * 4096
*ptr = v * pageSize
}
}
}
Expand Down

0 comments on commit 48de62d

Please sign in to comment.