Skip to content

Commit 48de62d

Browse files
authored
Merge pull request #29 from mackerelio/darwin-parse-page-size
[darwin] parse page size in vm_stat output
2 parents 18b73dd + 76554a6 commit 48de62d

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

memory/memory_darwin.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ type Stats struct {
6161
}
6262

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

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

0 commit comments

Comments
 (0)