From b2a9fbbc5cdbed1db4d1d5007d4561c2a4e9e291 Mon Sep 17 00:00:00 2001 From: "roberto@goyle" Date: Tue, 17 Apr 2012 22:26:32 +0200 Subject: [PATCH] set offload threading stacksize to 512k --- protocol.c | 17 +++++++++++++++++ uwsgi.c | 3 +++ uwsgi.h | 3 +++ 3 files changed, 23 insertions(+) diff --git a/protocol.c b/protocol.c index 2f0b57fe..f45116d1 100644 --- a/protocol.c +++ b/protocol.c @@ -1803,6 +1803,10 @@ void *uwsgi_static_offload_thread(void *req) { free(uof_req->hvec); free(uof_req); + pthread_mutex_lock(&uwsgi.static_offload_thread_lock); + uwsgi.workers[uwsgi.mywid].static_offload_threads--; + pthread_mutex_unlock(&uwsgi.static_offload_thread_lock); + return NULL; } @@ -1853,6 +1857,15 @@ int uwsgi_file_serve(struct wsgi_request *wsgi_req, char *document_root, uint16_ // Ok, the file must be served as static from uWSGI if (uwsgi.static_offload_to_thread) { + pthread_mutex_lock(&uwsgi.static_offload_thread_lock); + uint64_t offload_thread_count = uwsgi.workers[uwsgi.mywid].static_offload_threads; + pthread_mutex_unlock(&uwsgi.static_offload_thread_lock); + + if (offload_thread_count > (uint64_t) uwsgi.static_offload_to_thread) { + uwsgi_log_verbose("OVERLOAD !!! unable to offload static file serving\n"); + return uwsgi_real_file_serve(wsgi_req, real_filename, real_filename_len, &st); + } + struct uwsgi_offload_request *uor = uwsgi_malloc(sizeof(struct uwsgi_offload_request)); // buffer @@ -1876,6 +1889,10 @@ int uwsgi_file_serve(struct wsgi_request *wsgi_req, char *document_root, uint16_ // avoid closing the connection wsgi_req->fd_closed = 1; + pthread_mutex_lock(&uwsgi.static_offload_thread_lock); + uwsgi.workers[uwsgi.mywid].static_offload_threads++; + pthread_mutex_unlock(&uwsgi.static_offload_thread_lock); + if (pthread_create(&uor->tid, &uwsgi.static_offload_thread_attr, uwsgi_static_offload_thread, (void *) uor)) { uwsgi_error("pthread_create()"); // bad condition, better to exit... diff --git a/uwsgi.c b/uwsgi.c index 150889fb..d139c322 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -1869,6 +1869,9 @@ int uwsgi_start(void *v_argv) { if (uwsgi.static_offload_to_thread) { pthread_attr_init(&uwsgi.static_offload_thread_attr); pthread_attr_setdetachstate(&uwsgi.static_offload_thread_attr, PTHREAD_CREATE_DETACHED); + // 512K should be enough... + pthread_attr_setstacksize(&uwsgi.static_offload_thread_attr, 512*1024); + pthread_mutex_init(&uwsgi.static_offload_thread_lock, NULL); } // end of generic initialization diff --git a/uwsgi.h b/uwsgi.h index ee07f8cb..a6a5aa25 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1282,6 +1282,7 @@ struct uwsgi_server { int check_static_docroot; int static_offload_to_thread; pthread_attr_t static_offload_thread_attr; + pthread_mutex_t static_offload_thread_lock; char *daemonize; char *daemonize2; @@ -1873,6 +1874,8 @@ struct uwsgi_worker { uint64_t avg_response_time; + uint64_t static_offload_threads; + char name[0xff]; };