summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMax Romanov <max.romanov@nginx.com>2020-09-29 22:58:04 +0300
committerMax Romanov <max.romanov@nginx.com>2020-09-29 22:58:04 +0300
commit61eba6eef1558ed741d2bf5f1f3f5bc0481e587a (patch)
tree07b3c9dc16872261158624a6d1d0594d1eca05c2
parentc721a5378dd467054f672a40e677be56bad53c4d (diff)
downloadunit-61eba6eef1558ed741d2bf5f1f3f5bc0481e587a.tar.gz
unit-61eba6eef1558ed741d2bf5f1f3f5bc0481e587a.tar.bz2
Wrapping libunit's malloc() and free() calls for logging purposes.
This change aids heap usage analysis in applications. The alloc and free functions are also required for lvlhash due to the upcoming threading support, because using main nxt_memalign() and nxt_free() isn't safe in a multithreaded app environment. The reason is that these functions may use thread-local structures which aren't initialized properly in applications.
-rw-r--r--src/nxt_lvlhsh.c14
-rw-r--r--src/nxt_malloc.c16
-rw-r--r--src/nxt_unit.c133
3 files changed, 98 insertions, 65 deletions
diff --git a/src/nxt_lvlhsh.c b/src/nxt_lvlhsh.c
index ec433341..d10dbc58 100644
--- a/src/nxt_lvlhsh.c
+++ b/src/nxt_lvlhsh.c
@@ -1015,17 +1015,3 @@ nxt_lvlhsh_retrieve(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto,
return NULL;
}
-
-
-void *
-nxt_lvlhsh_alloc(void *data, size_t size)
-{
- return nxt_memalign(size, size);
-}
-
-
-void
-nxt_lvlhsh_free(void *data, void *p)
-{
- nxt_free(p);
-}
diff --git a/src/nxt_malloc.c b/src/nxt_malloc.c
index 910ef7cd..fed58e96 100644
--- a/src/nxt_malloc.c
+++ b/src/nxt_malloc.c
@@ -81,6 +81,22 @@ nxt_realloc(void *p, size_t size)
}
+/* nxt_lvlhsh_* functions moved here to avoid references from nxt_lvlhsh.c. */
+
+void *
+nxt_lvlhsh_alloc(void *data, size_t size)
+{
+ return nxt_memalign(size, size);
+}
+
+
+void
+nxt_lvlhsh_free(void *data, void *p)
+{
+ nxt_free(p);
+}
+
+
#if (NXT_DEBUG)
void
diff --git a/src/nxt_unit.c b/src/nxt_unit.c
index b7c01f95..a4015ce5 100644
--- a/src/nxt_unit.c
+++ b/src/nxt_unit.c
@@ -184,6 +184,10 @@ static nxt_unit_request_info_t *nxt_unit_request_hash_find(
nxt_unit_ctx_t *ctx, uint32_t stream, int remove);
static char * nxt_unit_snprint_prefix(char *p, char *end, pid_t pid, int level);
+static void *nxt_unit_lvlhsh_alloc(void *data, size_t size);
+static void nxt_unit_lvlhsh_free(void *data, void *p);
+static void *nxt_unit_malloc(size_t size);
+static void nxt_unit_free(void *p);
static int nxt_unit_memcasecmp(const void *p1, const void *p2, size_t length);
@@ -532,7 +536,7 @@ nxt_unit_create(nxt_unit_init_t *init)
nxt_unit_impl_t *lib;
nxt_unit_callbacks_t *cb;
- lib = malloc(sizeof(nxt_unit_impl_t) + init->request_data_size);
+ lib = nxt_unit_malloc(sizeof(nxt_unit_impl_t) + init->request_data_size);
if (nxt_slow_path(lib == NULL)) {
nxt_unit_alert(NULL, "failed to allocate unit struct");
@@ -587,7 +591,7 @@ nxt_unit_create(nxt_unit_init_t *init)
fail:
- free(lib);
+ nxt_unit_free(lib);
return NULL;
}
@@ -711,7 +715,7 @@ nxt_unit_lib_release(nxt_unit_impl_t *lib)
nxt_unit_mmaps_destroy(&lib->incoming);
nxt_unit_mmaps_destroy(&lib->outgoing);
- free(lib);
+ nxt_unit_free(lib);
}
}
@@ -1389,7 +1393,7 @@ nxt_unit_request_check_response_port(nxt_unit_request_info_t *req,
return NXT_UNIT_AGAIN;
}
- port_impl = malloc(sizeof(nxt_unit_port_impl_t));
+ port_impl = nxt_unit_malloc(sizeof(nxt_unit_port_impl_t));
if (nxt_slow_path(port_impl == NULL)) {
nxt_unit_alert(ctx, "check_response_port: malloc(%d) failed",
(int) sizeof(nxt_unit_port_impl_t));
@@ -1413,7 +1417,7 @@ nxt_unit_request_check_response_port(nxt_unit_request_info_t *req,
pthread_mutex_unlock(&lib->mutex);
- free(port);
+ nxt_unit_free(port);
return NXT_UNIT_ERROR;
}
@@ -1427,7 +1431,7 @@ nxt_unit_request_check_response_port(nxt_unit_request_info_t *req,
pthread_mutex_unlock(&lib->mutex);
- free(port);
+ nxt_unit_free(port);
return NXT_UNIT_ERROR;
}
@@ -1635,7 +1639,7 @@ nxt_unit_request_info_get(nxt_unit_ctx_t *ctx)
if (nxt_queue_is_empty(&ctx_impl->free_req)) {
pthread_mutex_unlock(&ctx_impl->mutex);
- req_impl = malloc(sizeof(nxt_unit_request_info_impl_t)
+ req_impl = nxt_unit_malloc(sizeof(nxt_unit_request_info_impl_t)
+ lib->request_data_size);
if (nxt_slow_path(req_impl == NULL)) {
return NULL;
@@ -1723,7 +1727,7 @@ nxt_unit_request_info_free(nxt_unit_request_info_impl_t *req_impl)
nxt_queue_remove(&req_impl->link);
if (req_impl != &ctx_impl->req) {
- free(req_impl);
+ nxt_unit_free(req_impl);
}
}
@@ -1742,7 +1746,7 @@ nxt_unit_websocket_frame_get(nxt_unit_ctx_t *ctx)
if (nxt_queue_is_empty(&ctx_impl->free_ws)) {
pthread_mutex_unlock(&ctx_impl->mutex);
- ws_impl = malloc(sizeof(nxt_unit_websocket_frame_impl_t));
+ ws_impl = nxt_unit_malloc(sizeof(nxt_unit_websocket_frame_impl_t));
if (nxt_slow_path(ws_impl == NULL)) {
return NULL;
}
@@ -1788,7 +1792,7 @@ nxt_unit_websocket_frame_free(nxt_unit_websocket_frame_impl_t *ws_impl)
{
nxt_queue_remove(&ws_impl->link);
- free(ws_impl);
+ nxt_unit_free(ws_impl);
}
@@ -2325,7 +2329,7 @@ nxt_unit_mmap_buf_get(nxt_unit_ctx_t *ctx)
if (ctx_impl->free_buf == NULL) {
pthread_mutex_unlock(&ctx_impl->mutex);
- mmap_buf = malloc(sizeof(nxt_unit_mmap_buf_t));
+ mmap_buf = nxt_unit_malloc(sizeof(nxt_unit_mmap_buf_t));
if (nxt_slow_path(mmap_buf == NULL)) {
return NULL;
}
@@ -2643,7 +2647,7 @@ nxt_unit_free_outgoing_buf(nxt_unit_mmap_buf_t *mmap_buf)
}
if (mmap_buf->free_ptr != NULL) {
- free(mmap_buf->free_ptr);
+ nxt_unit_free(mmap_buf->free_ptr);
mmap_buf->free_ptr = NULL;
}
@@ -2685,7 +2689,7 @@ nxt_unit_read_buf_get_impl(nxt_unit_ctx_impl_t *ctx_impl)
return rbuf;
}
- rbuf = malloc(sizeof(nxt_unit_read_buf_t));
+ rbuf = nxt_unit_malloc(sizeof(nxt_unit_read_buf_t));
if (nxt_fast_path(rbuf != NULL)) {
rbuf->ctx_impl = ctx_impl;
@@ -3044,7 +3048,7 @@ nxt_unit_request_preread(nxt_unit_request_info_t *req, size_t size)
return NULL;
}
- mmap_buf->free_ptr = malloc(size);
+ mmap_buf->free_ptr = nxt_unit_malloc(size);
if (nxt_slow_path(mmap_buf->free_ptr == NULL)) {
nxt_unit_req_alert(req, "preread: failed to allocate buf memory");
nxt_unit_mmap_buf_release(mmap_buf);
@@ -3327,7 +3331,7 @@ nxt_unit_websocket_retain(nxt_unit_websocket_frame_t *ws)
size = ws_impl->buf->buf.end - ws_impl->buf->buf.start;
- b = malloc(size);
+ b = nxt_unit_malloc(size);
if (nxt_slow_path(b == NULL)) {
return NXT_UNIT_ERROR;
}
@@ -3835,7 +3839,7 @@ nxt_unit_get_outgoing_buf(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port,
mmap_buf->plain_ptr = local_buf;
} else {
- mmap_buf->free_ptr = malloc(size + sizeof(nxt_port_msg_t));
+ mmap_buf->free_ptr = nxt_unit_malloc(size + sizeof(nxt_port_msg_t));
if (nxt_slow_path(mmap_buf->free_ptr == NULL)) {
return NXT_UNIT_ERROR;
}
@@ -4005,7 +4009,7 @@ nxt_unit_process_release(nxt_unit_process_t *process)
if (c == 1) {
nxt_unit_debug(NULL, "destroy process #%d", (int) process->pid);
- free(process);
+ nxt_unit_free(process);
}
}
@@ -4022,7 +4026,7 @@ nxt_unit_mmaps_destroy(nxt_unit_mmaps_t *mmaps)
munmap(mm->hdr, PORT_MMAP_SIZE);
}
- free(mmaps->elts);
+ nxt_unit_free(mmaps->elts);
}
pthread_mutex_destroy(&mmaps->mutex);
@@ -4294,8 +4298,8 @@ nxt_unit_lvlhsh_pid_test(nxt_lvlhsh_query_t *lhq, void *data)
static const nxt_lvlhsh_proto_t lvlhsh_processes_proto nxt_aligned(64) = {
NXT_LVLHSH_DEFAULT,
nxt_unit_lvlhsh_pid_test,
- nxt_lvlhsh_alloc,
- nxt_lvlhsh_free,
+ nxt_unit_lvlhsh_alloc,
+ nxt_unit_lvlhsh_free,
};
@@ -4324,7 +4328,7 @@ nxt_unit_process_get(nxt_unit_impl_t *lib, pid_t pid)
return process;
}
- process = malloc(sizeof(nxt_unit_process_t));
+ process = nxt_unit_malloc(sizeof(nxt_unit_process_t));
if (nxt_slow_path(process == NULL)) {
nxt_unit_alert(NULL, "failed to allocate process for #%d", (int) pid);
@@ -4349,7 +4353,7 @@ nxt_unit_process_get(nxt_unit_impl_t *lib, pid_t pid)
default:
nxt_unit_alert(NULL, "process %d insert failed", (int) pid);
- free(process);
+ nxt_unit_free(process);
process = NULL;
break;
}
@@ -4920,7 +4924,8 @@ nxt_unit_ctx_alloc(nxt_unit_ctx_t *ctx, void *data)
lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit);
- new_ctx = malloc(sizeof(nxt_unit_ctx_impl_t) + lib->request_data_size);
+ new_ctx = nxt_unit_malloc(sizeof(nxt_unit_ctx_impl_t)
+ + lib->request_data_size);
if (nxt_slow_path(new_ctx == NULL)) {
nxt_unit_alert(ctx, "failed to allocate context");
@@ -4929,7 +4934,7 @@ nxt_unit_ctx_alloc(nxt_unit_ctx_t *ctx, void *data)
rc = nxt_unit_ctx_init(lib, new_ctx, data);
if (nxt_slow_path(rc != NXT_UNIT_OK)) {
- free(new_ctx);
+ nxt_unit_free(new_ctx);
return NULL;
}
@@ -5008,7 +5013,7 @@ nxt_unit_ctx_free(nxt_unit_ctx_impl_t *ctx_impl)
while (ctx_impl->free_buf != NULL) {
mmap_buf = ctx_impl->free_buf;
nxt_unit_mmap_buf_unlink(mmap_buf);
- free(mmap_buf);
+ nxt_unit_free(mmap_buf);
}
nxt_queue_each(req_impl, &ctx_impl->free_req,
@@ -5035,7 +5040,7 @@ nxt_unit_ctx_free(nxt_unit_ctx_impl_t *ctx_impl)
}
if (ctx_impl != &lib->main_ctx) {
- free(ctx_impl);
+ nxt_unit_free(ctx_impl);
}
nxt_unit_lib_release(lib);
@@ -5220,7 +5225,7 @@ nxt_inline void nxt_unit_port_release(nxt_unit_port_t *port)
: sizeof(nxt_port_queue_t));
}
- free(port_impl);
+ nxt_unit_free(port_impl);
}
}
@@ -5336,7 +5341,7 @@ nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue)
process->next_port_id = port->id.id + 1;
}
- new_port = malloc(sizeof(nxt_unit_port_impl_t));
+ new_port = nxt_unit_malloc(sizeof(nxt_unit_port_impl_t));
if (nxt_slow_path(new_port == NULL)) {
nxt_unit_alert(ctx, "add_port: %d,%d malloc() failed",
port->id.pid, port->id.id);
@@ -5351,7 +5356,7 @@ nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue)
nxt_unit_alert(ctx, "add_port: %d,%d hash_add failed",
port->id.pid, port->id.id);
- free(new_port);
+ nxt_unit_free(new_port);
new_port = NULL;
@@ -6016,8 +6021,8 @@ nxt_unit_port_hash_test(nxt_lvlhsh_query_t *lhq, void *data)
static const nxt_lvlhsh_proto_t lvlhsh_ports_proto nxt_aligned(64) = {
NXT_LVLHSH_DEFAULT,
nxt_unit_port_hash_test,
- nxt_lvlhsh_alloc,
- nxt_lvlhsh_free,
+ nxt_unit_lvlhsh_alloc,
+ nxt_unit_lvlhsh_free,
};
@@ -6115,8 +6120,8 @@ nxt_unit_request_hash_test(nxt_lvlhsh_query_t *lhq, void *data)
static const nxt_lvlhsh_proto_t lvlhsh_requests_proto nxt_aligned(64) = {
NXT_LVLHSH_DEFAULT,
nxt_unit_request_hash_test,
- nxt_lvlhsh_alloc,
- nxt_lvlhsh_free,
+ nxt_unit_lvlhsh_alloc,
+ nxt_unit_lvlhsh_free,
};
@@ -6346,24 +6351,61 @@ nxt_unit_snprint_prefix(char *p, char *end, pid_t pid, int level)
}
-/* The function required by nxt_lvlhsh_alloc() and nxt_lvlvhsh_free(). */
-
-void *
-nxt_memalign(size_t alignment, size_t size)
+static void *
+nxt_unit_lvlhsh_alloc(void *data, size_t size)
{
- void *p;
- nxt_err_t err;
+ int err;
+ void *p;
- err = posix_memalign(&p, alignment, size);
+ err = posix_memalign(&p, size, size);
if (nxt_fast_path(err == 0)) {
+ nxt_unit_debug(NULL, "posix_memalign(%d, %d): %p",
+ (int) size, (int) size, p);
return p;
}
+ nxt_unit_alert(NULL, "posix_memalign(%d, %d) failed: %s (%d)",
+ (int) size, (int) size, strerror(err), err);
return NULL;
}
+static void
+nxt_unit_lvlhsh_free(void *data, void *p)
+{
+ nxt_unit_free(p);
+}
+
+
+static void *
+nxt_unit_malloc(size_t size)
+{
+ void *p;
+
+ p = malloc(size);
+
+ if (nxt_fast_path(p != NULL)) {
+ nxt_unit_debug(NULL, "malloc(%d): %p", (int) size, p);
+
+ } else {
+ nxt_unit_alert(NULL, "malloc(%d) failed: %s (%d)",
+ (int) size, strerror(errno), errno);
+ }
+
+ return p;
+}
+
+
+static void
+nxt_unit_free(void *p)
+{
+ nxt_unit_debug(NULL, "free(%p)", p);
+
+ free(p);
+}
+
+
static int
nxt_unit_memcasecmp(const void *p1, const void *p2, size_t length)
{
@@ -6390,14 +6432,3 @@ nxt_unit_memcasecmp(const void *p1, const void *p2, size_t length)
return 0;
}
-
-
-#if (NXT_DEBUG)
-
-void
-nxt_free(void *p)
-{
- free(p);
-}
-
-#endif