Encapsulate buffer allocation in buffered writer

This commit is contained in:
Yinan Zhang
2020-02-03 15:56:13 -08:00
parent bdc08b5158
commit 9cac3fa8f5
6 changed files with 215 additions and 80 deletions

View File

@@ -5,23 +5,114 @@
#include "jemalloc/internal/buf_writer.h"
#include "jemalloc/internal/malloc_io.h"
void
buf_writer_flush(buf_writer_t *buf_writer) {
assert(buf_writer->buf_end <= buf_writer->buf_size);
buf_writer->buf[buf_writer->buf_end] = '\0';
if (buf_writer->write_cb == NULL) {
buf_writer->write_cb = je_malloc_message != NULL ?
je_malloc_message : wrtmessage;
static void *
buf_writer_allocate_internal_buf(tsdn_t *tsdn, size_t buf_len) {
#ifdef JEMALLOC_JET
if (buf_len > SC_LARGE_MAXCLASS) {
return NULL;
}
#else
assert(buf_len <= SC_LARGE_MAXCLASS);
#endif
return iallocztm(tsdn, buf_len, sz_size2index(buf_len), false, NULL,
true, arena_get(tsdn, 0, false), true);
}
static void
buf_writer_free_internal_buf(tsdn_t *tsdn, void *buf) {
if (buf != NULL) {
idalloctm(tsdn, buf, NULL, NULL, true, true);
}
}
static write_cb_t buf_writer_cb;
static void
buf_writer_assert(buf_writer_t *buf_writer) {
if (buf_writer->buf != NULL) {
assert(buf_writer->public_write_cb == buf_writer_cb);
assert(buf_writer->public_cbopaque == buf_writer);
assert(buf_writer->private_write_cb != buf_writer_cb);
assert(buf_writer->private_cbopaque != buf_writer);
assert(buf_writer->buf_size > 0);
} else {
assert(buf_writer->public_write_cb != buf_writer_cb);
assert(buf_writer->public_cbopaque != buf_writer);
assert(buf_writer->private_write_cb == NULL);
assert(buf_writer->private_cbopaque == NULL);
assert(buf_writer->buf_size == 0);
}
}
bool
buf_writer_init(tsdn_t *tsdn, buf_writer_t *buf_writer, write_cb_t *write_cb,
void *cbopaque, char *buf, size_t buf_len) {
assert(buf_len >= 2);
if (buf != NULL) {
buf_writer->buf = buf;
buf_writer->internal_buf = false;
} else {
buf_writer->buf = buf_writer_allocate_internal_buf(tsdn,
buf_len);
buf_writer->internal_buf = true;
}
buf_writer->write_cb(buf_writer->cbopaque, buf_writer->buf);
buf_writer->buf_end = 0;
if (buf_writer->buf != NULL) {
buf_writer->public_write_cb = buf_writer_cb;
buf_writer->public_cbopaque = buf_writer;
buf_writer->private_write_cb = write_cb;
buf_writer->private_cbopaque = cbopaque;
buf_writer->buf_size = buf_len - 1; /* Allowing for '\0'. */
buf_writer_assert(buf_writer);
return false;
} else {
buf_writer->public_write_cb = write_cb;
buf_writer->public_cbopaque = cbopaque;
buf_writer->private_write_cb = NULL;
buf_writer->private_cbopaque = NULL;
buf_writer->buf_size = 0;
buf_writer_assert(buf_writer);
return true;
}
}
write_cb_t *
buf_writer_get_write_cb(buf_writer_t *buf_writer) {
buf_writer_assert(buf_writer);
return buf_writer->public_write_cb;
}
void *
buf_writer_get_cbopaque(buf_writer_t *buf_writer) {
buf_writer_assert(buf_writer);
return buf_writer->public_cbopaque;
}
void
buf_writer_flush(buf_writer_t *buf_writer) {
buf_writer_assert(buf_writer);
if (buf_writer->buf == NULL) {
return;
}
assert(buf_writer->buf_end <= buf_writer->buf_size);
buf_writer->buf[buf_writer->buf_end] = '\0';
if (buf_writer->private_write_cb == NULL) {
buf_writer->private_write_cb = je_malloc_message != NULL ?
je_malloc_message : wrtmessage;
}
assert(buf_writer->private_write_cb != NULL);
buf_writer->private_write_cb(buf_writer->private_cbopaque,
buf_writer->buf);
buf_writer->buf_end = 0;
}
static void
buf_writer_cb(void *buf_writer_arg, const char *s) {
buf_writer_t *buf_writer = (buf_writer_t *)buf_writer_arg;
size_t i, slen, n, s_remain, buf_remain;
buf_writer_assert(buf_writer);
assert(buf_writer->buf != NULL);
assert(buf_writer->buf_end <= buf_writer->buf_size);
size_t i, slen, n, s_remain, buf_remain;
for (i = 0, slen = strlen(s); i < slen; i += n) {
if (buf_writer->buf_end == buf_writer->buf_size) {
buf_writer_flush(buf_writer);
@@ -34,3 +125,12 @@ buf_writer_cb(void *buf_writer_arg, const char *s) {
}
assert(i == slen);
}
void
buf_writer_terminate(tsdn_t *tsdn, buf_writer_t *buf_writer) {
buf_writer_assert(buf_writer);
buf_writer_flush(buf_writer);
if (buf_writer->internal_buf) {
buf_writer_free_internal_buf(tsdn, buf_writer->buf);
}
}

View File

@@ -3740,19 +3740,12 @@ je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque,
if (config_debug) {
stats_print(write_cb, cbopaque, opts);
} else {
char *buf = (char *)iallocztm(tsdn, STATS_PRINT_BUFSIZE,
sz_size2index(STATS_PRINT_BUFSIZE), false, NULL, true,
arena_get(TSDN_NULL, 0, true), true);
if (buf == NULL) {
stats_print(write_cb, cbopaque, opts);
} else {
buf_writer_t buf_writer;
buf_writer_init(&buf_writer, write_cb, cbopaque, buf,
STATS_PRINT_BUFSIZE);
stats_print(buf_writer_cb, &buf_writer, opts);
buf_writer_flush(&buf_writer);
idalloctm(tsdn, buf, NULL, NULL, true, true);
}
buf_writer_t buf_writer;
buf_writer_init(tsdn, &buf_writer, write_cb, cbopaque, NULL,
STATS_PRINT_BUFSIZE);
stats_print(buf_writer_get_write_cb(&buf_writer),
buf_writer_get_cbopaque(&buf_writer), opts);
buf_writer_terminate(tsdn, &buf_writer);
}
check_entry_exit_locking(tsdn);

View File

@@ -629,19 +629,12 @@ prof_log_stop(tsdn_t *tsdn) {
struct prof_emitter_cb_arg_s arg;
arg.fd = fd;
char *buf = (char *)iallocztm(tsdn, PROF_LOG_STOP_BUFSIZE,
sz_size2index(PROF_LOG_STOP_BUFSIZE), false, NULL, true,
arena_get(TSDN_NULL, 0, true), true);
buf_writer_t buf_writer;
if (buf == NULL) {
emitter_init(&emitter, emitter_output_json_compact,
prof_emitter_write_cb, &arg);
} else {
buf_writer_init(&buf_writer, prof_emitter_write_cb, &arg, buf,
PROF_LOG_STOP_BUFSIZE);
emitter_init(&emitter, emitter_output_json_compact,
buf_writer_cb, &buf_writer);
}
buf_writer_init(tsdn, &buf_writer, prof_emitter_write_cb, &arg, NULL,
PROF_LOG_STOP_BUFSIZE);
emitter_init(&emitter, emitter_output_json_compact,
buf_writer_get_write_cb(&buf_writer),
buf_writer_get_cbopaque(&buf_writer));
emitter_begin(&emitter);
prof_log_emit_metadata(&emitter);
@@ -650,10 +643,7 @@ prof_log_stop(tsdn_t *tsdn) {
prof_log_emit_allocs(tsd, &emitter);
emitter_end(&emitter);
if (buf != NULL) {
buf_writer_flush(&buf_writer);
idalloctm(tsdn, buf, NULL, NULL, true, true);
}
buf_writer_terminate(tsdn, &buf_writer);
/* Reset global state. */
if (log_tables_initialized) {

View File

@@ -462,20 +462,13 @@ dump_bt(emitter_t *emitter, prof_tctx_t *tctx) {
void
prof_recent_alloc_dump(tsd_t *tsd, void (*write_cb)(void *, const char *),
void *cbopaque) {
char *buf = (char *)iallocztm(tsd_tsdn(tsd), PROF_RECENT_PRINT_BUFSIZE,
sz_size2index(PROF_RECENT_PRINT_BUFSIZE), false, NULL, true,
arena_get(tsd_tsdn(tsd), 0, false), true);
emitter_t emitter;
buf_writer_t buf_writer;
if (buf == NULL) {
emitter_init(&emitter, emitter_output_json_compact, write_cb,
cbopaque);
} else {
buf_writer_init(&buf_writer, write_cb, cbopaque, buf,
PROF_RECENT_PRINT_BUFSIZE);
emitter_init(&emitter, emitter_output_json_compact,
buf_writer_cb, &buf_writer);
}
buf_writer_init(tsd_tsdn(tsd), &buf_writer, write_cb, cbopaque, NULL,
PROF_RECENT_PRINT_BUFSIZE);
emitter_t emitter;
emitter_init(&emitter, emitter_output_json_compact,
buf_writer_get_write_cb(&buf_writer),
buf_writer_get_cbopaque(&buf_writer));
emitter_begin(&emitter);
malloc_mutex_lock(tsd_tsdn(tsd), &prof_recent_alloc_mtx);
@@ -535,10 +528,7 @@ prof_recent_alloc_dump(tsd_t *tsd, void (*write_cb)(void *, const char *),
malloc_mutex_unlock(tsd_tsdn(tsd), &prof_recent_alloc_mtx);
emitter_end(&emitter);
if (buf != NULL) {
buf_writer_flush(&buf_writer);
idalloctm(tsd_tsdn(tsd), buf, NULL, NULL, true, true);
}
buf_writer_terminate(tsd_tsdn(tsd), &buf_writer);
}
#undef PROF_RECENT_PRINT_BUFSIZE