-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmaps.go
145 lines (133 loc) · 3.46 KB
/
smaps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-License-Identifier: Apache-2.0
package smaps
import (
"bufio"
"io"
"os"
"strconv"
"strings"
)
type Identifier string
const (
UnknownIdentifier Identifier = "unknown"
)
type Smap struct {
Identifier Identifier
Address string
Permissions string
Offset string
Dev string
Inode string
IsPath bool
Size int64
KernelPageSize int64
MMUPageSize int64
Rss int64
Pss int64
PssDirty int64
SharedClean int64
SharedDirty int64
PrivateClean int64
PrivateDirty int64
Referenced int64
Swap int64
}
type Smaps map[Identifier][]Smap
func parseSize(sizeStr string) int64 {
parts := strings.Fields(sizeStr)
if len(parts) != 2 {
return 0
}
value, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return 0
}
unit := strings.ToLower(parts[1])
switch unit {
case "kb":
return int64(value * 1024)
case "mb":
return int64(value * 1024 * 1024)
case "gb":
return int64(value * 1024 * 1024 * 1024)
default:
return 0
}
}
func parse(reader io.Reader, smaps Smaps) {
scanner := bufio.NewScanner(reader)
var line string
var fields []string
var prefixIndex int
for scanner.Scan() {
line = scanner.Text()
fields = strings.Fields(line)
if len(fields) >= 5 {
smap := Smap{
Address: fields[0],
Permissions: fields[1],
Offset: fields[2],
Dev: fields[3],
Inode: fields[4],
}
if len(fields) > 5 {
identifier := strings.Join(fields[5:], " ")
smap.IsPath = !strings.HasPrefix(identifier, "[") || !strings.HasSuffix(identifier, "]")
if smap.IsPath {
smap.Identifier = Identifier(identifier)
} else {
smap.Identifier = Identifier(identifier[1 : len(identifier)-1])
}
} else {
smap.IsPath = false
smap.Identifier = UnknownIdentifier
}
for scanner.Scan() {
line = scanner.Text()
if strings.HasPrefix(line, "VmFlags:") {
break
}
prefixIndex = strings.Index(line, ":")
switch line[:prefixIndex] {
case "Size":
smap.Size = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "KernelPageSize":
smap.KernelPageSize = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "MMUPageSize":
smap.MMUPageSize = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "Rss":
smap.Rss = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "Pss":
smap.Pss = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "Pss_Dirty":
smap.PssDirty = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "Shared_Clean":
smap.SharedClean = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "Shared_Dirty":
smap.SharedDirty = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "Private_Clean":
smap.PrivateClean = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "Private_Dirty":
smap.PrivateDirty = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "Referenced":
smap.Referenced = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
case "Swap":
smap.Swap = parseSize(strings.TrimSpace(line[prefixIndex+1:]))
}
}
if smapArray, ok := smaps[smap.Identifier]; ok {
smaps[smap.Identifier] = append(smapArray, smap)
} else {
smaps[smap.Identifier] = []Smap{smap}
}
}
}
}
func Parse(file *os.File) (Smaps, error) {
smaps := make(Smaps)
_, err := file.Seek(0, 0)
if err == nil {
parse(file, smaps)
}
return smaps, err
}