-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
56 lines (48 loc) · 1.26 KB
/
utils.go
File metadata and controls
56 lines (48 loc) · 1.26 KB
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
package browserpm
import (
"context"
"crypto/rand"
"encoding/hex"
"os"
"runtime"
"github.com/shirou/gopsutil/v3/process"
)
// getEnvOrDefault gets the environment variable or returns the default value.
func getEnvOrDefault(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
// generateID generates a random ID.
func generateID() string {
b := make([]byte, 16)
rand.Read(b)
return hex.EncodeToString(b)
}
// getProcessMemory gets the process memory information.
func getProcessMemory(ctx context.Context, pid int32) (rss, vms, exclusive uint64) {
p, err := process.NewProcess(pid)
if err != nil {
return 0, 0, 0
}
// Basic memory information (available on all platforms).
if mi, err := p.MemoryInfo(); err == nil {
rss = mi.RSS
vms = mi.VMS
exclusive = rss // default fallback strategy
}
// Linux: use PSS (smaps_rollup) if available.
// MemoryMapsWithContext(ctx, true) will read smaps_rollup directly on Linux >= 4.15.
if runtime.GOOS == "linux" {
if maps, err := p.MemoryMapsWithContext(ctx, true); err == nil {
if maps != nil && len(*maps) > 0 {
if (*maps)[0].Pss > 0 {
// KB to Bytes
exclusive = (*maps)[0].Pss * 1024
}
}
}
}
return rss, vms, exclusive
}