From 046824dae7ce4e18332dded0a1ee5bddd220644d Mon Sep 17 00:00:00 2001 From: "roberto@quantal64" Date: Mon, 3 Sep 2012 14:11:42 +0200 Subject: [PATCH] fixed usage of mmap() --- core/init.c | 29 ++++++++++++++++++++++++----- core/utils.c | 3 ++- core/uwsgi.c | 4 ---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/core/init.c b/core/init.c index 86f908b8..c5f53f67 100644 --- a/core/init.c +++ b/core/init.c @@ -214,16 +214,26 @@ void uwsgi_setup_workers() { // allocate memory for cores uwsgi.workers[i].cores = (struct uwsgi_core *) uwsgi_calloc_shared(sizeof(struct uwsgi_core) * uwsgi.cores); + // this is a trick for avoiding too much memory areas + void *ts = uwsgi_calloc_shared(sizeof(void *) * uwsgi.max_apps * uwsgi.cores); + void *buffers = uwsgi_malloc_shared(uwsgi.buffer_size * uwsgi.cores); + void *hvec = uwsgi_malloc_shared(sizeof(struct iovec) * uwsgi.vec_size * uwsgi.cores); + void *post_buf = NULL; + if (uwsgi.post_buffering > 0) + post_buf = uwsgi_malloc_shared(uwsgi.post_buffering_bufsize * uwsgi.cores); + + for (j = 0; j < uwsgi.cores; j++) { // allocate shared memory for thread states (required for some language, like python) - uwsgi.workers[i].cores[j].ts = uwsgi_calloc_shared(sizeof(void *) * uwsgi.max_apps); + uwsgi.workers[i].cores[j].ts = ts + ((sizeof(void *) * uwsgi.max_apps) * j); // raw per-request buffer - uwsgi.workers[i].cores[j].buffer = uwsgi_malloc_shared(uwsgi.buffer_size); + uwsgi.workers[i].cores[j].buffer = buffers + (uwsgi.buffer_size * j); // iovec for uwsgi vars - uwsgi.workers[i].cores[j].hvec = uwsgi_malloc_shared(sizeof(struct iovec) * uwsgi.vec_size); - if (uwsgi.post_buffering > 0) - uwsgi.workers[i].cores[j].post_buf = uwsgi_malloc_shared(uwsgi.post_buffering_bufsize); + uwsgi.workers[i].cores[j].hvec = hvec + ((sizeof(struct iovec) * uwsgi.vec_size) * j); + if (post_buf) + uwsgi.workers[i].cores[j].post_buf = post_buf + (uwsgi.post_buffering_bufsize *j); } + // master does not need to following steps... if (i == 0) continue; uwsgi.workers[i].signal_pipe[0] = -1; @@ -232,4 +242,13 @@ void uwsgi_setup_workers() { snprintf(uwsgi.workers[i].snapshot_name, 0xff, "uWSGI snapshot %d", i); } + uint64_t total_memory = (sizeof(struct uwsgi_app) * uwsgi.max_apps) + (sizeof(struct uwsgi_core) * uwsgi.cores) + (sizeof(void *) * uwsgi.max_apps * uwsgi.cores) + + (uwsgi.buffer_size * uwsgi.cores) + (sizeof(struct iovec) * uwsgi.vec_size * uwsgi.cores); + if (uwsgi.post_buffering > 0) { + total_memory += (uwsgi.post_buffering_bufsize * uwsgi.cores); + } + + total_memory *= (uwsgi.numproc + uwsgi.master_process); + uwsgi_log("mapped %llu bytes (%llu KB) for %d cores\n", total_memory, total_memory / 1024, uwsgi.cores*uwsgi.numproc); + } diff --git a/core/utils.c b/core/utils.c index 3d2f4182..810789b7 100644 --- a/core/utils.c +++ b/core/utils.c @@ -3133,7 +3133,8 @@ void *uwsgi_malloc_shared(size_t size) { void *addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); - if (addr == NULL) { + if (addr == MAP_FAILED) { + uwsgi_log("unable to allocate %llu bytes (%lluMB)\n", (unsigned long long )size, (unsigned long long) (size/(1024*1024))); uwsgi_error("mmap()"); exit(1); } diff --git a/core/uwsgi.c b/core/uwsgi.c index 16bb19b2..cdbd05eb 100644 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -2075,10 +2075,6 @@ int uwsgi_start(void *v_argv) { uwsgi_log("cores allocated...\n"); #endif - if (uwsgi.cores > 1) { - uwsgi_log("allocated %llu bytes (%llu KB) for %d cores per worker.\n", (uint64_t) (sizeof(struct uwsgi_core) * uwsgi.cores), (uint64_t) ((sizeof(struct uwsgi_core) * uwsgi.cores) / 1024), uwsgi.cores); - } - if (uwsgi.vhost) { uwsgi_log("VirtualHosting mode enabled.\n"); }