-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathskynet_mprof.diff
214 lines (203 loc) · 5.84 KB
/
skynet_mprof.diff
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c
index 44c7183..c29c3f6 100644
--- a/skynet-src/malloc_hook.c
+++ b/skynet-src/malloc_hook.c
@@ -9,6 +9,44 @@
#include "skynet.h"
#include "atomic.h"
+#ifdef MEMORY_PROFILE
+#include <errno.h>
+#include <execinfo.h>
+#include <unistd.h>
+#include "skynet_handle.h"
+#include "skynet_imp.h"
+
+static const int HEADER_LEN = 2;
+
+inline static void
+put_bigendian_u16(uint8_t* buf, uint16_t v) {
+ buf[0] = (uint8_t)(v >> 8);
+ buf[1] = (uint8_t)v;
+}
+
+uint32_t mprof_svc_handle = -1;
+int mprof_notify_fd = -1;
+
+static void
+mprof_block_send(const char* req, int len) {
+ while (true) {
+ int n = write(mprof_notify_fd, req, len);
+ if (n < 0) {
+ if (errno != EINTR) {
+ fprintf(stderr, "mprof: block send error %s.", strerror(errno));
+ }
+ continue;
+ }
+ assert(n == len);
+ return;
+ }
+}
+
+static const int MAX_STACK_SIZE = 32;
+
+static int bt_init_status = 0;
+
+#endif
// turn on MEMORY_CHECK can do more memory check, such as double free
// #define MEMORY_CHECK
@@ -28,6 +66,9 @@ struct mem_cookie {
#ifdef MEMORY_CHECK
uint32_t dogtag;
#endif
+#ifdef MEMORY_PROFILE
+ uint32_t sampling;
+#endif
};
#define SLOT_SIZE 0x10000
@@ -85,8 +126,27 @@ update_xmalloc_stat_free(uint32_t handle, size_t __n) {
}
}
+static void malloc_oom(size_t size) {
+ fprintf(stderr, "xmalloc: Out of memory trying to allocate %zu bytes\n",
+ size);
+ fflush(stderr);
+ abort();
+}
+
+#ifdef MEMORY_PROFILE
+inline static void* fill_prefix(char* ptr, int sampling);
+
inline static void*
-fill_prefix(char* ptr) {
+mprof_malloc(size_t size) {
+ void* buffer = je_malloc(size + PREFIX_SIZE);
+ if(!buffer) malloc_oom(size);
+ buffer = fill_prefix(buffer, 0);
+ return buffer;
+}
+#endif
+
+inline static void*
+fill_prefix(char* ptr, int sampling) {
uint32_t handle = skynet_current_handle();
size_t size = je_malloc_usable_size(ptr);
struct mem_cookie *p = (struct mem_cookie *)(ptr + size - sizeof(struct mem_cookie));
@@ -94,6 +154,39 @@ fill_prefix(char* ptr) {
#ifdef MEMORY_CHECK
uint32_t dogtag = MEMORY_ALLOCTAG;
memcpy(&p->dogtag, &dogtag, sizeof(dogtag));
+#endif
+#ifdef MEMORY_PROFILE
+ p->sampling = 0;
+ if (sampling && handle < (uint32_t)(-THREAD_MONITOR) && handle != mprof_svc_handle && mprof_notify_fd >= 0) {
+ void* stack_trace[MAX_STACK_SIZE];
+ if (bt_init_status == 0) { // uninit
+ bt_init_status = 1; // initing
+ backtrace(stack_trace, MAX_STACK_SIZE);
+ bt_init_status = 2; // init done
+ } else if (bt_init_status == 2) {
+ int stack_depth = backtrace(stack_trace, MAX_STACK_SIZE);
+ if (stack_depth > 2) {
+ p->sampling = 1;
+ stack_depth -= 2; // exclude stack_trace[0], stack_trace[1]
+ char* cmd = "malloc ";
+ size_t cmdlen = strlen(cmd);
+ size_t buf_sz = HEADER_LEN + cmdlen + sizeof(ptr) + sizeof(size) + sizeof(stack_depth) + stack_depth * sizeof(void*);
+ void* buffer = mprof_malloc(buf_sz);
+ int offset = HEADER_LEN;
+ memcpy(buffer + offset, cmd, cmdlen);
+ offset += cmdlen;
+ memcpy(buffer + offset, &ptr, sizeof(ptr));
+ offset += sizeof(ptr);
+ memcpy(buffer + offset, &size, sizeof(size));
+ offset += sizeof(size);
+ memcpy(buffer + offset, &stack_depth, sizeof(stack_depth));
+ offset += sizeof(stack_depth);
+ memcpy(buffer + offset, &stack_trace[1], stack_depth * sizeof(void*));
+ put_bigendian_u16(buffer, buf_sz - HEADER_LEN);
+ mprof_block_send(buffer, buf_sz);
+ }
+ }
+ }
#endif
update_xmalloc_stat_alloc(handle, size);
return ptr;
@@ -114,18 +207,27 @@ clean_prefix(char* ptr) {
assert(dogtag == MEMORY_ALLOCTAG); // memory out of bounds
dogtag = MEMORY_FREETAG;
memcpy(&p->dogtag, &dogtag, sizeof(dogtag));
+#endif
+#ifdef MEMORY_PROFILE
+ if (p->sampling) {
+ char* cmd = "free ";
+ size_t cmdlen = strlen(cmd);
+ size_t buf_sz = HEADER_LEN + cmdlen + sizeof(ptr) + sizeof(size);
+ void* buffer = mprof_malloc(buf_sz);
+ int offset = HEADER_LEN;
+ memcpy(buffer + offset, cmd, cmdlen);
+ offset += cmdlen;
+ memcpy(buffer + offset, &ptr, sizeof(ptr));
+ offset += sizeof(ptr);
+ memcpy(buffer + offset, &size, sizeof(size));
+ put_bigendian_u16(buffer, buf_sz - HEADER_LEN);
+ mprof_block_send(buffer, buf_sz);
+ }
#endif
update_xmalloc_stat_free(handle, size);
return ptr;
}
-static void malloc_oom(size_t size) {
- fprintf(stderr, "xmalloc: Out of memory trying to allocate %zu bytes\n",
- size);
- fflush(stderr);
- abort();
-}
-
void
memory_info_dump(const char* opts) {
je_malloc_stats_print(0,0, opts);
@@ -185,7 +287,7 @@ void *
skynet_malloc(size_t size) {
void* ptr = je_malloc(size + PREFIX_SIZE);
if(!ptr) malloc_oom(size);
- return fill_prefix(ptr);
+ return fill_prefix(ptr, 1);
}
void *
@@ -195,7 +297,7 @@ skynet_realloc(void *ptr, size_t size) {
void* rawptr = clean_prefix(ptr);
void *newptr = je_realloc(rawptr, size+PREFIX_SIZE);
if(!newptr) malloc_oom(size);
- return fill_prefix(newptr);
+ return fill_prefix(newptr, 1);
}
void
@@ -209,28 +311,28 @@ void *
skynet_calloc(size_t nmemb,size_t size) {
void* ptr = je_calloc(nmemb + ((PREFIX_SIZE+size-1)/size), size );
if(!ptr) malloc_oom(size);
- return fill_prefix(ptr);
+ return fill_prefix(ptr, 1);
}
void *
skynet_memalign(size_t alignment, size_t size) {
void* ptr = je_memalign(alignment, size + PREFIX_SIZE);
if(!ptr) malloc_oom(size);
- return fill_prefix(ptr);
+ return fill_prefix(ptr, 1);
}
void *
skynet_aligned_alloc(size_t alignment, size_t size) {
void* ptr = je_aligned_alloc(alignment, size + (size_t)((PREFIX_SIZE + alignment -1) & ~(alignment-1)));
if(!ptr) malloc_oom(size);
- return fill_prefix(ptr);
+ return fill_prefix(ptr, 1);
}
int
skynet_posix_memalign(void **memptr, size_t alignment, size_t size) {
int err = je_posix_memalign(memptr, alignment, size + PREFIX_SIZE);
if (err) malloc_oom(size);
- fill_prefix(*memptr);
+ fill_prefix(*memptr, 1);
return err;
}