|
8 | 8 | #define lib_misc_c
|
9 | 9 | #define LUA_LIB
|
10 | 10 |
|
| 11 | +#include <stdio.h> |
| 12 | +#include <errno.h> |
| 13 | + |
11 | 14 | #include "lua.h"
|
12 | 15 | #include "lmisclib.h"
|
| 16 | +#include "lauxlib.h" |
13 | 17 |
|
14 | 18 | #include "lj_obj.h"
|
15 | 19 | #include "lj_str.h"
|
16 | 20 | #include "lj_tab.h"
|
17 | 21 | #include "lj_lib.h"
|
| 22 | +#include "lj_gc.h" |
| 23 | +#include "lj_err.h" |
| 24 | + |
| 25 | +#include "lj_memprof.h" |
18 | 26 |
|
19 | 27 | /* ------------------------------------------------------------------------ */
|
20 | 28 |
|
@@ -67,8 +75,168 @@ LJLIB_CF(misc_getmetrics)
|
67 | 75 |
|
68 | 76 | #include "lj_libdef.h"
|
69 | 77 |
|
| 78 | +/* ----- misc.memprof module ---------------------------------------------- */ |
| 79 | + |
| 80 | +#define LJLIB_MODULE_misc_memprof |
| 81 | + |
| 82 | +/* |
| 83 | +** Yep, 8Mb. Tuned in order not to bother the platform with too often flushes. |
| 84 | +*/ |
| 85 | +#define STREAM_BUFFER_SIZE (8 * 1024 * 1024) |
| 86 | + |
| 87 | +/* Structure given as ctx to memprof writer and on_stop callback. */ |
| 88 | +struct memprof_ctx { |
| 89 | + /* Output file stream for data. */ |
| 90 | + FILE *stream; |
| 91 | + /* Profiled global_State for lj_mem_free at on_stop callback. */ |
| 92 | + global_State *g; |
| 93 | + /* Buffer for data. */ |
| 94 | + uint8_t buf[STREAM_BUFFER_SIZE]; |
| 95 | +}; |
| 96 | + |
| 97 | +/* |
| 98 | +** Default buffer writer function. |
| 99 | +** Just call fwrite to the corresponding FILE. |
| 100 | +*/ |
| 101 | +static size_t buffer_writer_default(const void **buf_addr, size_t len, |
| 102 | + void *opt) |
| 103 | +{ |
| 104 | + struct memprof_ctx *ctx = opt; |
| 105 | + FILE *stream = ctx->stream; |
| 106 | + const void * const buf_start = *buf_addr; |
| 107 | + const void *data = *buf_addr; |
| 108 | + size_t write_total = 0; |
| 109 | + |
| 110 | + lua_assert(len <= STREAM_BUFFER_SIZE); |
| 111 | + |
| 112 | + for (;;) { |
| 113 | + const size_t written = fwrite(data, 1, len - write_total, stream); |
| 114 | + |
| 115 | + if (LJ_UNLIKELY(written == 0)) { |
| 116 | + /* Re-tries write in case of EINTR. */ |
| 117 | + if (errno != EINTR) { |
| 118 | + /* Will be freed as whole chunk later. */ |
| 119 | + *buf_addr = NULL; |
| 120 | + return write_total; |
| 121 | + } |
| 122 | + |
| 123 | + errno = 0; |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + write_total += written; |
| 128 | + lua_assert(write_total <= len); |
| 129 | + |
| 130 | + if (write_total == len) |
| 131 | + break; |
| 132 | + |
| 133 | + data = (uint8_t *)data + (ptrdiff_t)written; |
| 134 | + } |
| 135 | + |
| 136 | + *buf_addr = buf_start; |
| 137 | + return write_total; |
| 138 | +} |
| 139 | + |
| 140 | +/* Default on stop callback. Just close the corresponding stream. */ |
| 141 | +static int on_stop_cb_default(void *opt, uint8_t *buf) |
| 142 | +{ |
| 143 | + struct memprof_ctx *ctx = opt; |
| 144 | + FILE *stream = ctx->stream; |
| 145 | + UNUSED(buf); |
| 146 | + lj_mem_free(ctx->g, ctx, sizeof(*ctx)); |
| 147 | + return fclose(stream); |
| 148 | +} |
| 149 | + |
| 150 | +/* local started, err, errno = misc.memprof.start(fname) */ |
| 151 | +LJLIB_CF(misc_memprof_start) |
| 152 | +{ |
| 153 | + struct lj_memprof_options opt = {0}; |
| 154 | + const char *fname = strdata(lj_lib_checkstr(L, 1)); |
| 155 | + struct memprof_ctx *ctx; |
| 156 | + int memprof_status; |
| 157 | + |
| 158 | + /* |
| 159 | + ** FIXME: more elegant solution with ctx. |
| 160 | + ** Throws in case of OOM. |
| 161 | + */ |
| 162 | + ctx = lj_mem_new(L, sizeof(*ctx)); |
| 163 | + opt.ctx = ctx; |
| 164 | + opt.buf = ctx->buf; |
| 165 | + opt.writer = buffer_writer_default; |
| 166 | + opt.on_stop = on_stop_cb_default; |
| 167 | + opt.len = STREAM_BUFFER_SIZE; |
| 168 | + |
| 169 | + ctx->g = G(L); |
| 170 | + ctx->stream = fopen(fname, "wb"); |
| 171 | + |
| 172 | + if (ctx->stream == NULL) { |
| 173 | + lj_mem_free(ctx->g, ctx, sizeof(*ctx)); |
| 174 | + return luaL_fileresult(L, 0, fname); |
| 175 | + } |
| 176 | + |
| 177 | + memprof_status = lj_memprof_start(L, &opt); |
| 178 | + |
| 179 | + if (LJ_UNLIKELY(memprof_status != PROFILE_SUCCESS)) { |
| 180 | + if (memprof_status == PROFILE_ERRIO) { |
| 181 | + fclose(ctx->stream); |
| 182 | + lj_mem_free(ctx->g, ctx, sizeof(*ctx)); |
| 183 | + } |
| 184 | + switch (memprof_status) { |
| 185 | + case PROFILE_ERRUSE: |
| 186 | + lua_pushnil(L); |
| 187 | + lua_pushstring(L, err2msg(LJ_ERR_PROF_MISUSE)); |
| 188 | + lua_pushinteger(L, EINVAL); |
| 189 | + return 3; |
| 190 | + case PROFILE_ERRRUN: |
| 191 | + lua_pushnil(L); |
| 192 | + lua_pushstring(L, err2msg(LJ_ERR_PROF_ISRUNNING)); |
| 193 | + lua_pushinteger(L, EINVAL); |
| 194 | + return 3; |
| 195 | + case PROFILE_ERRIO: |
| 196 | + return luaL_fileresult(L, 0, fname); |
| 197 | + default: |
| 198 | + lua_assert(0); |
| 199 | + return 0; |
| 200 | + } |
| 201 | + } |
| 202 | + lua_pushboolean(L, 1); |
| 203 | + return 1; |
| 204 | +} |
| 205 | + |
| 206 | +/* local stopped, err, errno = misc.memprof.stop() */ |
| 207 | +LJLIB_CF(misc_memprof_stop) |
| 208 | +{ |
| 209 | + int status = lj_memprof_stop(L); |
| 210 | + if (status != PROFILE_SUCCESS) { |
| 211 | + switch (status) { |
| 212 | + case PROFILE_ERRUSE: |
| 213 | + lua_pushnil(L); |
| 214 | + lua_pushstring(L, err2msg(LJ_ERR_PROF_MISUSE)); |
| 215 | + lua_pushinteger(L, EINVAL); |
| 216 | + return 3; |
| 217 | + case PROFILE_ERRRUN: |
| 218 | + lua_pushnil(L); |
| 219 | + lua_pushstring(L, err2msg(LJ_ERR_PROF_NOTRUNNING)); |
| 220 | + lua_pushinteger(L, EINVAL); |
| 221 | + return 3; |
| 222 | + case PROFILE_ERRIO: |
| 223 | + return luaL_fileresult(L, 0, NULL); |
| 224 | + default: |
| 225 | + lua_assert(0); |
| 226 | + return 0; |
| 227 | + } |
| 228 | + } |
| 229 | + lua_pushboolean(L, 1); |
| 230 | + return 1; |
| 231 | +} |
| 232 | + |
| 233 | +#include "lj_libdef.h" |
| 234 | + |
| 235 | +/* ------------------------------------------------------------------------ */ |
| 236 | + |
70 | 237 | LUALIB_API int luaopen_misc(struct lua_State *L)
|
71 | 238 | {
|
72 | 239 | LJ_LIB_REG(L, LUAM_MISCLIBNAME, misc);
|
| 240 | + LJ_LIB_REG(L, LUAM_MISCLIBNAME ".memprof", misc_memprof); |
73 | 241 | return 1;
|
74 | 242 | }
|
0 commit comments