forked from ianlancetaylor/cgosymbolizer
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcgomemprof.go
More file actions
128 lines (103 loc) · 2.9 KB
/
Copy pathcgomemprof.go
File metadata and controls
128 lines (103 loc) · 2.9 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
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
//go:build with_jemalloc && linux
// +build with_jemalloc,linux
package cgosymbolizer
// #cgo pkg-config: jemalloc
//
// #include "cgomemprof.h"
// #include "stdlib.h"
import "C"
import (
"fmt"
"sync"
"unsafe"
)
var mu sync.RWMutex
// IsJemallocProfEnabled returns true if jemalloc profiling is enabled.
func IsJemallocProfEnabled() bool {
enabled := C.IsJemallocProfEnabled()
return int(enabled) == 1
}
// IsJemallocStatsEnabled returns true if jemalloc stats is enabled.
func IsJemallocStatsEnabled() bool {
enabled := C.IsJemallocStatsEnabled()
return int(enabled) == 1
}
// EnableMemoryProfiling enables memory profiling at runtime.
func EnableMemoryProfiling() error {
mu.Lock()
defer mu.Unlock()
if code := C.EnableMemoryProfiling(); code != 0 {
return fmt.Errorf("EnableMemoryProfiling failed with code %d", code)
}
return nil
}
// DisableMemoryProfiling disables memory profiling at runtime.
func DisableMemoryProfiling() error {
mu.Lock()
defer mu.Unlock()
if code := C.DisableMemoryProfiling(); code != 0 {
return fmt.Errorf("DisableMemoryProfiling failed with code %d", code)
}
return nil
}
// DumpMemoryProfileIntoFile dumps memory profile into file.
func DumpMemoryProfileIntoFile(filename string) error {
mu.RLock()
defer mu.RUnlock()
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
if code := C.DumpMemoryProfileIntoFile(cfilename); code != 0 {
return fmt.Errorf("DumpMemoryProfileIntoFile failed with code %d", int(code))
}
return nil
}
func DumpStatsIntoFile(filename string, opts string) error {
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
copts := C.CString(opts)
defer C.free(unsafe.Pointer(copts))
if code := C.DumpStatsIntoFile(cfilename, copts); code != 0 {
return fmt.Errorf("DumpStatsIntoFile failed with code %d", int(code))
}
return nil
}
// GetSymbol returns the symbol name of the given address.
func GetSymbol(addr uint64) string {
result := C.GetSymbol(C.uintptr_t(addr))
r := C.GoString(result)
C.free(unsafe.Pointer(result))
return r
}
func GetAllocatedMemory() uint64 {
return uint64(C.GetAllocatedMemory())
}
func GetActiveMemory() uint64 {
return uint64(C.GetActiveMemory())
}
func GetMetadataMemory() uint64 {
return uint64(C.GetMetadataMemory())
}
func GetMetadataThpMemory() uint64 {
return uint64(C.GetMetadataThpMemory())
}
func GetResidentMemory() uint64 {
return uint64(C.GetResidentMemory())
}
func GetMappedMemory() uint64 {
return uint64(C.GetMappedMemory())
}
func GetRetainedMemory() uint64 {
return uint64(C.GetRetainedMemory())
}
func GetZeroReallocs() uint64 {
return uint64(C.GetZeroReallocs())
}
func GetBackgroundThreadNumThreads() uint64 {
return uint64(C.GetBackgroundThreadNumThreads())
}
func GetBackgroundThreadNumRuns() uint64 {
return uint64(C.GetBackgroundThreadNumRuns())
}
func GetBackgroundThreadRunInterval() uint64 {
return uint64(C.GetBackgroundThreadRunInterval())
}