forked from cockroachdb/cockroach
/
runtime_jemalloc.go
138 lines (126 loc) · 4.13 KB
/
runtime_jemalloc.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
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
// +build !stdmalloc
package status
// #cgo CPPFLAGS: -DJEMALLOC_NO_DEMANGLE
// #cgo LDFLAGS: -ljemalloc
// // On macOS, je_zone_register is run at init time to register
// // jemalloc with the system allocator. Unfortunately, all the
// // machinery for this is in a single file, and is not referenced
// // elsewhere, causing the linker to omit the file's symbols.
// // Manually force the presence of these symbols on macOS.
// #cgo darwin LDFLAGS: -u_je_zone_register
// #cgo dragonfly freebsd LDFLAGS: -lm
// #cgo linux LDFLAGS: -lrt -lm -lpthread
//
// #include <jemalloc/jemalloc.h>
//
// // https://github.com/jemalloc/jemalloc/wiki/Use-Case:-Introspection-Via-mallctl*()
// // https://github.com/jemalloc/jemalloc/blob/4.5.0/src/stats.c#L960:L969
//
// typedef struct {
// size_t Allocated;
// size_t Active;
// size_t Metadata;
// size_t Resident;
// size_t Mapped;
// size_t Retained;
// } JemallocStats;
//
// int jemalloc_get_stats(JemallocStats *stats) {
// // Update the statistics cached by je_mallctl.
// uint64_t epoch = 1;
// size_t sz = sizeof(epoch);
// je_mallctl("epoch", &epoch, &sz, &epoch, sz);
//
// int err;
//
// sz = sizeof(&stats->Allocated);
// err = je_mallctl("stats.allocated", &stats->Allocated, &sz, NULL, 0);
// if (err != 0) {
// return err;
// }
// sz = sizeof(&stats->Active);
// err = je_mallctl("stats.active", &stats->Active, &sz, NULL, 0);
// if (err != 0) {
// return err;
// }
// sz = sizeof(&stats->Metadata);
// err = je_mallctl("stats.metadata", &stats->Metadata, &sz, NULL, 0);
// if (err != 0) {
// return err;
// }
// sz = sizeof(&stats->Resident);
// err = je_mallctl("stats.resident", &stats->Resident, &sz, NULL, 0);
// if (err != 0) {
// return err;
// }
// sz = sizeof(&stats->Mapped);
// err = je_mallctl("stats.mapped", &stats->Mapped, &sz, NULL, 0);
// if (err != 0) {
// return err;
// }
// sz = sizeof(&stats->Retained);
// err = je_mallctl("stats.retained", &stats->Retained, &sz, NULL, 0);
// if (err != 0) {
// return err;
// }
// return err;
// }
import "C"
import (
"math"
"reflect"
"strings"
"github.com/dustin/go-humanize"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
func init() {
if getCgoMemStats != nil {
panic("getCgoMemStats is already set")
}
getCgoMemStats = getJemallocStats
}
func getJemallocStats(ctx context.Context) (uint, uint, error) {
var js C.JemallocStats
// TODO(marc): should we panic here? Failure on fetching the stats may be a problem.
if _, err := C.jemalloc_get_stats(&js); err != nil {
return 0, 0, err
}
if log.V(2) {
// Summary of jemalloc stats:
v := reflect.ValueOf(js)
t := v.Type()
stats := make([]string, v.NumField())
for i := 0; i < v.NumField(); i++ {
stats[i] = t.Field(i).Name + ": " + humanize.IBytes(uint64(v.Field(i).Interface().(C.size_t)))
}
log.Infof(ctx, "jemalloc stats: %s", strings.Join(stats, " "))
}
// NB: the `!V(MaxInt32)` condition is a workaround to not spew this to the
// logs whenever log interception (log spy) is active. If we refactored
// je_malloc_stats_print to return a string that we can `log.Infof` instead,
// we wouldn't need this.
if log.V(3) && !log.V(math.MaxInt32) {
// Detailed jemalloc stats (very verbose, includes per-arena stats).
C.je_malloc_stats_print(nil, nil, nil)
}
return uint(js.Allocated), uint(js.Resident), nil
}
// Used to force allocation in tests. 'import "C"' is not supported in tests.
func allocateMemory() {
// Empirically, 8KiB is not enough, but 16KiB is.
C.malloc(16 << 10)
}