From 0715031b0068695574ebae1596e6efa300cf1fd0 Mon Sep 17 00:00:00 2001 From: Roberto De Ioris Date: Fri, 29 Nov 2013 07:39:12 +0100 Subject: [PATCH] prepare for the new sharedarea api --- core/uwsgi.c | 10 +-- plugins/python/python_plugin.c | 2 +- plugins/python/uwsgi_pymodule.c | 153 ++------------------------------ uwsgi.h | 18 +++- uwsgiconfig.py | 2 +- 5 files changed, 28 insertions(+), 157 deletions(-) diff --git a/core/uwsgi.c b/core/uwsgi.c index 98a15f08..a9567138 100644 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -259,7 +259,7 @@ static struct uwsgi_option uwsgi_base_options[] = { {"lock-engine", required_argument, 0, "set the lock engine", uwsgi_opt_set_str, &uwsgi.lock_engine, 0}, {"ftok", required_argument, 0, "set the ipcsem key via ftok() for avoiding duplicates", uwsgi_opt_set_str, &uwsgi.ftok, 0}, {"persistent-ipcsem", no_argument, 0, "do not remove ipcsem's on shutdown", uwsgi_opt_true, &uwsgi.persistent_ipcsem, 0}, - {"sharedarea", required_argument, 'A', "create a raw shared memory area of specified pages", uwsgi_opt_set_int, &uwsgi.sharedareasize, 0}, + {"sharedarea", required_argument, 'A', "create a raw shared memory area of specified pages (note: it supports keyval too)", uwsgi_opt_add_string_list, &uwsgi.sharedareas_list, 0}, {"safe-fd", required_argument, 0, "do not close the specified file descriptor", uwsgi_opt_safe_fd, NULL, 0}, {"fd-safe", required_argument, 0, "do not close the specified file descriptor", uwsgi_opt_safe_fd, NULL, 0}, @@ -2548,12 +2548,8 @@ int uwsgi_start(void *v_argv) { // allocate rpc structures uwsgi_rpc_init(); - // setup sharedarea - if (uwsgi.sharedareasize > 0) { - uwsgi.sharedarea = uwsgi_calloc_shared(uwsgi.page_size * uwsgi.sharedareasize); - uwsgi_log("shared area mapped at %p, you can access it with uwsgi.sharedarea* functions.\n", uwsgi.sharedarea); - uwsgi.sa_lock = uwsgi_rwlock_init("sharedarea"); - } + // initialize sharedareas + uwsgi_sharedareas_init(); uwsgi.snmp_lock = uwsgi_lock_init("snmp"); diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 95cbc726..b88c8295 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -833,7 +833,7 @@ void init_uwsgi_embedded_module() { init_uwsgi_module_spooler(new_uwsgi_module); } - if (uwsgi.sharedareasize > 0 && uwsgi.sharedarea) { + if (uwsgi.sharedareas) { init_uwsgi_module_sharedarea(new_uwsgi_module); } diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index c336b4e8..e20fb226 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -1429,33 +1429,11 @@ PyObject *py_uwsgi_extract(PyObject * self, PyObject * args) { PyObject *py_uwsgi_sharedarea_inclong(PyObject * self, PyObject * args) { uint64_t pos = 0; uint64_t value = 1; - uint64_t current_value = 0; - - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } if (!PyArg_ParseTuple(args, "l|l:sharedarea_inclong", &pos, &value)) { return NULL; } - if (pos + 8 >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - UWSGI_RELEASE_GIL - - uwsgi_wlock(uwsgi.sa_lock); - - memcpy(¤t_value, uwsgi.sharedarea + pos, 8); - value = current_value + value; - memcpy(uwsgi.sharedarea + pos, &value, 8); - - uwsgi_rwunlock(uwsgi.sa_lock); - UWSGI_GET_GIL - return PyInt_FromLong(value); } @@ -1464,30 +1442,10 @@ PyObject *py_uwsgi_sharedarea_writelong(PyObject * self, PyObject * args) { uint64_t pos = 0; uint64_t value = 0; - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } - if (!PyArg_ParseTuple(args, "ll:sharedarea_writelong", &pos, &value)) { return NULL; } - if (pos + 8 >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - UWSGI_RELEASE_GIL - - uwsgi_wlock(uwsgi.sa_lock); - - memcpy(uwsgi.sharedarea + pos, &value, 8); - - uwsgi_rwunlock(uwsgi.sa_lock); - - UWSGI_GET_GIL - return PyInt_FromLong(value); } @@ -1497,31 +1455,10 @@ PyObject *py_uwsgi_sharedarea_write(PyObject * self, PyObject * args) { char *value; Py_ssize_t value_len = 0; - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } - if (!PyArg_ParseTuple(args, "ls#:sharedarea_write", &pos, &value, &value_len)) { return NULL; } - if (pos + value_len >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - UWSGI_RELEASE_GIL - - uwsgi_wlock(uwsgi.sa_lock); - - memcpy(uwsgi.sharedarea + pos, value, value_len); - - - uwsgi_rwunlock(uwsgi.sa_lock); - - UWSGI_GET_GIL - return PyInt_FromLong(value_len); @@ -1531,32 +1468,10 @@ PyObject *py_uwsgi_sharedarea_writebyte(PyObject * self, PyObject * args) { uint64_t pos = 0; char value; - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } - - if (!PyArg_ParseTuple(args, "lb:sharedarea_writebyte", &pos, &value)) { return NULL; } - if (pos >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - UWSGI_RELEASE_GIL - - uwsgi_wlock(uwsgi.sa_lock); - - uwsgi.sharedarea[pos] = value; - - - uwsgi_rwunlock(uwsgi.sa_lock); - - UWSGI_GET_GIL - return PyInt_FromLong(value); } @@ -1565,30 +1480,11 @@ PyObject *py_uwsgi_sharedarea_readlong(PyObject * self, PyObject * args) { uint64_t pos = 0; uint64_t value; - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } - if (!PyArg_ParseTuple(args, "l:sharedarea_readlong", &pos)) { return NULL; } - if (pos + 8 >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - - UWSGI_RELEASE_GIL - - uwsgi_wlock(uwsgi.sa_lock); - - memcpy(&value, uwsgi.sharedarea + pos, 8); - - - uwsgi_rwunlock(uwsgi.sa_lock); - - UWSGI_GET_GIL + value = 1; return PyLong_FromLong(value); @@ -1597,32 +1493,15 @@ PyObject *py_uwsgi_sharedarea_readlong(PyObject * self, PyObject * args) { PyObject *py_uwsgi_sharedarea_readbyte(PyObject * self, PyObject * args) { uint64_t pos = 0; - - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } + uint8_t byte; if (!PyArg_ParseTuple(args, "l:sharedarea_readbyte", &pos)) { return NULL; } - if (pos >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } + byte = 1; - UWSGI_RELEASE_GIL - - uwsgi_wlock(uwsgi.sa_lock); - - char value = uwsgi.sharedarea[pos]; - - uwsgi_rwunlock(uwsgi.sa_lock); - - UWSGI_GET_GIL - - return PyInt_FromLong(value); + return PyInt_FromLong(byte); } @@ -1630,38 +1509,20 @@ PyObject *py_uwsgi_sharedarea_read(PyObject * self, PyObject * args) { uint64_t pos = 0; uint64_t len = 1; - if (uwsgi.sharedareasize <= 0) { - Py_INCREF(Py_None); - return Py_None; - } - if (!PyArg_ParseTuple(args, "l|l:sharedarea_read", &pos, &len)) { return NULL; } - if (pos + len >= uwsgi.page_size * uwsgi.sharedareasize) { - Py_INCREF(Py_None); - return Py_None; - } - +/* PyObject *ret = PyString_FromStringAndSize(NULL, len); #ifdef PYTHREE char *storage = PyBytes_AsString(ret); #else char *storage = PyString_AS_STRING(ret); #endif +*/ - UWSGI_RELEASE_GIL - - uwsgi_wlock(uwsgi.sa_lock); - - memcpy(storage, uwsgi.sharedarea + pos, len); - - uwsgi_rwunlock(uwsgi.sa_lock); - - UWSGI_GET_GIL - - return ret; + return NULL; } PyObject *py_uwsgi_spooler_freq(PyObject * self, PyObject * args) { diff --git a/uwsgi.h b/uwsgi.h index 175e3917..82ed4ce6 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -711,6 +711,16 @@ struct uwsgi_hash_algo *uwsgi_hash_algo_get(char *); void uwsgi_hash_algo_register(char *, uint32_t(*)(char *, uint64_t)); void uwsgi_hash_algo_register_all(void); +struct uwsgi_sharedarea { + int id; + int pages; + int fd; + struct uwsgi_lock_item *lock; + uint64_t max_pos; + uint64_t updates; + uint64_t hits; +}; + // maintain alignment here !!! struct uwsgi_cache_item { // item specific flags @@ -2304,8 +2314,9 @@ struct uwsgi_server { int vec_size; // shared area - char *sharedarea; - uint64_t sharedareasize; + struct uwsgi_string_list *sharedareas_list; + int sharedareas_cnt; + struct uwsgi_sharedarea **sharedareas; // avoid thundering herd in threaded modes pthread_mutex_t thunder_mutex; @@ -2588,6 +2599,7 @@ struct uwsgi_server { int (*wait_write_hook) (int, int); int (*wait_read_hook) (int, int); + int (*wait_milliseconds_hook) (int); struct uwsgi_string_list *schemes; @@ -4546,6 +4558,8 @@ void uwsgi_protocols_register(void); void uwsgi_build_plugin(char *dir); +void uwsgi_sharedareas_init(); + #ifdef __cplusplus } #endif diff --git a/uwsgiconfig.py b/uwsgiconfig.py index 288704a9..9dfb06b5 100644 --- a/uwsgiconfig.py +++ b/uwsgiconfig.py @@ -573,7 +573,7 @@ class uConf(object): 'core/setup_utils', 'core/clock', 'core/init', 'core/buffer', 'core/reader', 'core/writer', 'core/alarm', 'core/cron', 'core/hooks', 'core/plugins', 'core/lock', 'core/cache', 'core/daemons', 'core/errors', 'core/hash', 'core/master_events', 'core/chunked', 'core/queue', 'core/event', 'core/signal', 'core/strings', 'core/progress', 'core/timebomb', 'core/ini', 'core/fsmon', 'core/mount', - 'core/metrics', 'core/plugins_builder', + 'core/metrics', 'core/plugins_builder', 'core/sharedarea', 'core/rpc', 'core/gateway', 'core/loop', 'core/cookie', 'core/querystring', 'core/rb_timers', 'core/transformations', 'core/uwsgi'] # add protocols self.gcc_list.append('proto/base')