mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-06-16 02:15:48 +00:00
first attempt in implementing per-core harakiri
This commit is contained in:
+3
-1
@@ -948,7 +948,9 @@ next:
|
||||
// ok a worker died...
|
||||
uwsgi.workers[thewid].pid = 0;
|
||||
// only to be safe :P
|
||||
uwsgi.workers[thewid].harakiri = 0;
|
||||
for(i=0;i<uwsgi.cores;i++) {
|
||||
uwsgi.workers[thewid].cores[i].harakiri = 0;
|
||||
}
|
||||
|
||||
// ok, if we are reloading or dying, just continue the master loop
|
||||
// as soon as all of the workers have pid == 0, the action (exit, or reload) is triggered
|
||||
|
||||
+16
-12
@@ -157,21 +157,25 @@ void uwsgi_master_check_idle() {
|
||||
}
|
||||
|
||||
int uwsgi_master_check_workers_deadline() {
|
||||
int i;
|
||||
int i,j;
|
||||
int ret = 0;
|
||||
for (i = 1; i <= uwsgi.numproc; i++) {
|
||||
/* first check for harakiri */
|
||||
if (uwsgi.workers[i].harakiri > 0) {
|
||||
if (uwsgi.workers[i].harakiri < (time_t) uwsgi.current_time) {
|
||||
trigger_harakiri(i);
|
||||
ret = 1;
|
||||
for(j=0;j<uwsgi.cores;j++) {
|
||||
/* first check for harakiri */
|
||||
if (uwsgi.workers[i].cores[j].harakiri > 0) {
|
||||
if (uwsgi.workers[i].cores[j].harakiri < (time_t) uwsgi.current_time) {
|
||||
trigger_harakiri(i);
|
||||
ret = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* then user-defined harakiri */
|
||||
if (uwsgi.workers[i].user_harakiri > 0) {
|
||||
if (uwsgi.workers[i].user_harakiri < (time_t) uwsgi.current_time) {
|
||||
trigger_harakiri(i);
|
||||
ret = 1;
|
||||
/* then user-defined harakiri */
|
||||
if (uwsgi.workers[i].cores[j].user_harakiri > 0) {
|
||||
if (uwsgi.workers[i].cores[j].user_harakiri < (time_t) uwsgi.current_time) {
|
||||
trigger_harakiri(i);
|
||||
ret = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// then for evil memory checkers
|
||||
|
||||
+5
-5
@@ -632,7 +632,7 @@ void uwsgi_fixup_fds(int wid, int muleid, struct uwsgi_gateway *ug) {
|
||||
}
|
||||
|
||||
int uwsgi_respawn_worker(int wid) {
|
||||
|
||||
int i;
|
||||
int respawns = uwsgi.workers[wid].respawn_count;
|
||||
// the workers is not accepting (obviously)
|
||||
uwsgi.workers[wid].accepting = 0;
|
||||
@@ -641,8 +641,10 @@ int uwsgi_respawn_worker(int wid) {
|
||||
// ... same for update time
|
||||
uwsgi.workers[wid].last_spawn = uwsgi.current_time;
|
||||
// ... and memory/harakiri
|
||||
uwsgi.workers[wid].harakiri = 0;
|
||||
uwsgi.workers[wid].user_harakiri = 0;
|
||||
for(i=0;i<uwsgi.cores;i++) {
|
||||
uwsgi.workers[wid].cores[i].harakiri = 0;
|
||||
uwsgi.workers[wid].cores[i].user_harakiri = 0;
|
||||
}
|
||||
uwsgi.workers[wid].pending_harakiri = 0;
|
||||
uwsgi.workers[wid].rss_size = 0;
|
||||
uwsgi.workers[wid].vsz_size = 0;
|
||||
@@ -660,8 +662,6 @@ int uwsgi_respawn_worker(int wid) {
|
||||
// this is required for various checks
|
||||
uwsgi.workers[wid].delta_requests = 0;
|
||||
|
||||
int i;
|
||||
|
||||
if (uwsgi.threaded_logger) {
|
||||
pthread_mutex_lock(&uwsgi.threaded_logger_lock);
|
||||
}
|
||||
|
||||
+2
-2
@@ -477,7 +477,7 @@ int uwsgi_postbuffer_do_in_mem(struct wsgi_request *wsgi_req) {
|
||||
|
||||
while (remains > 0) {
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
inc_harakiri(uwsgi.harakiri_options.workers);
|
||||
inc_harakiri(wsgi_req, uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
ssize_t rlen = wsgi_req->socket->proto_read_body(wsgi_req, ptr, remains);
|
||||
@@ -551,7 +551,7 @@ int uwsgi_postbuffer_do_in_disk(struct wsgi_request *wsgi_req) {
|
||||
|
||||
// during post buffering we need to constantly reset the harakiri
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
inc_harakiri(uwsgi.harakiri_options.workers);
|
||||
inc_harakiri(wsgi_req, uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
// we use the already available post buffering buffer to read chunks....
|
||||
|
||||
+1
-1
@@ -702,7 +702,7 @@ static int uwsgi_router_simple_math_divide(struct uwsgi_route *ur, char *arg) {
|
||||
// harakiri router
|
||||
static int uwsgi_router_harakiri_func(struct wsgi_request *wsgi_req, struct uwsgi_route *route) {
|
||||
if (route->custom > 0) {
|
||||
set_user_harakiri(route->custom);
|
||||
set_user_harakiri(wsgi_req, route->custom);
|
||||
}
|
||||
return UWSGI_ROUTE_NEXT;
|
||||
}
|
||||
|
||||
+3
-3
@@ -46,7 +46,7 @@ int uwsgi_signal_handler(uint8_t sig) {
|
||||
uwsgi.workers[uwsgi.mywid].signum = sig;
|
||||
uwsgi.workers[uwsgi.mywid].signals++;
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
set_harakiri(NULL, uwsgi.harakiri_options.workers);
|
||||
}
|
||||
}
|
||||
else if (uwsgi.muleid > 0) {
|
||||
@@ -67,8 +67,8 @@ int uwsgi_signal_handler(uint8_t sig) {
|
||||
|
||||
if (uwsgi.mywid > 0) {
|
||||
uwsgi.workers[uwsgi.mywid].sig = 0;
|
||||
if (uwsgi.workers[uwsgi.mywid].harakiri > 0) {
|
||||
set_harakiri(0);
|
||||
if (uwsgi.workers[uwsgi.mywid].harakiri_total > 0) {
|
||||
set_harakiri(NULL, 0);
|
||||
}
|
||||
}
|
||||
else if (uwsgi.muleid > 0) {
|
||||
|
||||
+27
-17
@@ -42,9 +42,9 @@ int check_hex(char *str, int len) {
|
||||
}
|
||||
|
||||
// increase worker harakiri
|
||||
void inc_harakiri(int sec) {
|
||||
void inc_harakiri(struct wsgi_request *wsgi_req, int sec) {
|
||||
if (uwsgi.master_process) {
|
||||
uwsgi.workers[uwsgi.mywid].harakiri += sec;
|
||||
uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].harakiri += sec;
|
||||
}
|
||||
else {
|
||||
alarm(uwsgi.harakiri_options.workers + sec);
|
||||
@@ -52,12 +52,22 @@ void inc_harakiri(int sec) {
|
||||
}
|
||||
|
||||
// set worker harakiri
|
||||
void set_harakiri(int sec) {
|
||||
if (sec == 0) {
|
||||
uwsgi.workers[uwsgi.mywid].harakiri = 0;
|
||||
void set_harakiri(struct wsgi_request *wsgi_req, int sec) {
|
||||
if (!wsgi_req) {
|
||||
if (sec == 0) {
|
||||
uwsgi.workers[uwsgi.mywid].harakiri_total = 0;
|
||||
}
|
||||
else {
|
||||
uwsgi.workers[uwsgi.mywid].harakiri_total = uwsgi_now() + sec;
|
||||
}
|
||||
}
|
||||
else {
|
||||
uwsgi.workers[uwsgi.mywid].harakiri = uwsgi_now() + sec;
|
||||
if (sec == 0) {
|
||||
uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].harakiri = 0;
|
||||
}
|
||||
else {
|
||||
uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].harakiri = uwsgi_now() + sec;
|
||||
}
|
||||
}
|
||||
if (!uwsgi.master_process) {
|
||||
alarm(sec);
|
||||
@@ -65,7 +75,7 @@ void set_harakiri(int sec) {
|
||||
}
|
||||
|
||||
// set user harakiri
|
||||
void set_user_harakiri(int sec) {
|
||||
void set_user_harakiri(struct wsgi_request *wsgi_req, int sec) {
|
||||
if (!uwsgi.master_process) {
|
||||
uwsgi_log("!!! unable to set user harakiri without the master process !!!\n");
|
||||
return;
|
||||
@@ -79,8 +89,8 @@ void set_user_harakiri(int sec) {
|
||||
struct uwsgi_spooler *uspool = uwsgi.i_am_a_spooler;
|
||||
uspool->user_harakiri = 0;
|
||||
}
|
||||
else {
|
||||
uwsgi.workers[uwsgi.mywid].user_harakiri = 0;
|
||||
else if (wsgi_req) {
|
||||
uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].user_harakiri = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -91,8 +101,8 @@ void set_user_harakiri(int sec) {
|
||||
struct uwsgi_spooler *uspool = uwsgi.i_am_a_spooler;
|
||||
uspool->user_harakiri = uwsgi_now() + sec;
|
||||
}
|
||||
else {
|
||||
uwsgi.workers[uwsgi.mywid].user_harakiri = uwsgi_now() + sec;
|
||||
else if (wsgi_req) {
|
||||
uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].user_harakiri = uwsgi_now() + sec;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1099,13 +1109,13 @@ void uwsgi_close_request(struct wsgi_request *wsgi_req) {
|
||||
}
|
||||
|
||||
// leave harakiri mode
|
||||
if (uwsgi.workers[uwsgi.mywid].harakiri > 0) {
|
||||
set_harakiri(0);
|
||||
if (uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].harakiri > 0) {
|
||||
set_harakiri(wsgi_req, 0);
|
||||
}
|
||||
|
||||
// leave user harakiri mode
|
||||
if (uwsgi.workers[uwsgi.mywid].user_harakiri > 0) {
|
||||
set_user_harakiri(0);
|
||||
if (uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].user_harakiri > 0) {
|
||||
set_user_harakiri(wsgi_req, 0);
|
||||
}
|
||||
|
||||
if (!wsgi_req->do_not_account) {
|
||||
@@ -1362,7 +1372,7 @@ int wsgi_req_async_recv(struct wsgi_request *wsgi_req) {
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
set_harakiri(wsgi_req, uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -1394,7 +1404,7 @@ int wsgi_req_recv(int queue, struct wsgi_request *wsgi_req) {
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
set_harakiri(wsgi_req, uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
#ifdef UWSGI_ROUTING
|
||||
|
||||
+3
-3
@@ -1458,7 +1458,7 @@ void what_i_am_doing() {
|
||||
#else
|
||||
ctime_r((const time_t *) &wsgi_req->start_of_request_in_sec, ctime_storage);
|
||||
#endif
|
||||
if (uwsgi.harakiri_options.workers > 0 && uwsgi.workers[uwsgi.mywid].harakiri < uwsgi_now()) {
|
||||
if (uwsgi.harakiri_options.workers > 0 && uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].harakiri < uwsgi_now()) {
|
||||
uwsgi_log("HARAKIRI: --- uWSGI worker %d core %d (pid: %d) WAS managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, i, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime_storage);
|
||||
}
|
||||
else {
|
||||
@@ -1475,14 +1475,14 @@ void what_i_am_doing() {
|
||||
#else
|
||||
ctime_r((const time_t *) &wsgi_req->start_of_request_in_sec, ctime_storage);
|
||||
#endif
|
||||
if (uwsgi.harakiri_options.workers > 0 && uwsgi.workers[uwsgi.mywid].harakiri < uwsgi_now()) {
|
||||
if (uwsgi.harakiri_options.workers > 0 && uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].harakiri < uwsgi_now()) {
|
||||
uwsgi_log("HARAKIRI: --- uWSGI worker %d (pid: %d) WAS managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime_storage);
|
||||
}
|
||||
else {
|
||||
uwsgi_log("SIGUSR2: --- uWSGI worker %d (pid: %d) is managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime_storage);
|
||||
}
|
||||
}
|
||||
else if (uwsgi.harakiri_options.workers > 0 && uwsgi.workers[uwsgi.mywid].harakiri < uwsgi_now() && uwsgi.workers[uwsgi.mywid].sig) {
|
||||
else if (uwsgi.harakiri_options.workers > 0 && uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].harakiri < uwsgi_now() && uwsgi.workers[uwsgi.mywid].sig) {
|
||||
uwsgi_log("HARAKIRI: --- uWSGI worker %d (pid: %d) WAS handling signal %d ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, uwsgi.workers[uwsgi.mywid].signum);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ static PyObject *py_uwsgi_asyncio_accept(PyObject *self, PyObject *args) {
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
set_harakiri(wsgi_req, uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
uwsgi.async_proto_fd_table[wsgi_req->fd] = wsgi_req;
|
||||
|
||||
@@ -227,7 +227,7 @@ edge:
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
set_harakiri(wsgi_req, uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -389,7 +389,7 @@ retry:
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
set_harakiri(wsgi_req, uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
// spawn the new goroutine
|
||||
|
||||
@@ -211,7 +211,7 @@ edge:
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
set_harakiri(wsgi_req, uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
// hack to easily pass wsgi_req pointer to the greenlet
|
||||
|
||||
@@ -86,9 +86,11 @@ XS(XS_signal) {
|
||||
XS(XS_set_user_harakiri) {
|
||||
dXSARGS;
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
|
||||
psgi_check_args(1);
|
||||
|
||||
set_user_harakiri( SvIV(ST(0)) );
|
||||
set_user_harakiri(wsgi_req, SvIV(ST(0)) );
|
||||
|
||||
XSRETURN_UNDEF;
|
||||
}
|
||||
|
||||
@@ -237,7 +237,7 @@ void uwsgi_disconnect(struct wsgi_request *);
|
||||
|
||||
int uwsgi_ready_fd(struct wsgi_request *);
|
||||
|
||||
void set_user_harakiri(int);
|
||||
void set_user_harakiri(struct wsgi_request *, int);
|
||||
|
||||
int uwsgi_metric_set(char *, char *, int64_t);
|
||||
int uwsgi_metric_inc(char *, char *, int64_t);
|
||||
@@ -918,7 +918,10 @@ uwsgi.chunked_read_nb = uwsgi_pypy_chunked_read_nb
|
||||
"""
|
||||
uwsgi.set_user_harakiri(sec)
|
||||
"""
|
||||
uwsgi.set_user_harakiri = lambda x: lib.set_user_harakiri(x)
|
||||
def uwsgi_pypy_set_user_harakiri(x):
|
||||
wsgi_req = uwsgi_pypy_current_wsgi_req()
|
||||
lib.set_user_harakiri(wsgi_req, x)
|
||||
uwsgi.set_user_harakiri = uwsgi_pypy_set_user_harakiri
|
||||
|
||||
|
||||
print "Initialized PyPy with Python", sys.version
|
||||
|
||||
@@ -888,12 +888,13 @@ PyObject *py_uwsgi_log(PyObject * self, PyObject * args) {
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_set_user_harakiri(PyObject * self, PyObject * args) {
|
||||
struct wsgi_request *wsgi_req = py_current_wsgi_req();
|
||||
int sec = 0;
|
||||
if (!PyArg_ParseTuple(args, "i:set_user_harakiri", &sec)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
set_user_harakiri(sec);
|
||||
set_user_harakiri(wsgi_req, sec);
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
|
||||
@@ -442,8 +442,8 @@ void uwsgi_after_request_wsgi(struct wsgi_request *wsgi_req) {
|
||||
if (up.after_req_hook) {
|
||||
if (uwsgi.harakiri_no_arh) {
|
||||
// leave harakiri mode
|
||||
if (uwsgi.workers[uwsgi.mywid].harakiri > 0)
|
||||
set_harakiri(0);
|
||||
if (uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].harakiri > 0)
|
||||
set_harakiri(wsgi_req, 0);
|
||||
}
|
||||
UWSGI_GET_GIL
|
||||
PyObject *arh = python_call(up.after_req_hook, up.after_req_hook_args, 0, NULL);
|
||||
|
||||
@@ -110,7 +110,8 @@ static VALUE rack_uwsgi_warning(VALUE *class, VALUE rbmessage) {
|
||||
|
||||
static VALUE rack_uwsgi_user_harakiri(VALUE *class, VALUE sec) {
|
||||
Check_Type(sec, T_FIXNUM);
|
||||
set_user_harakiri(NUM2INT(sec));
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req();
|
||||
set_user_harakiri(wsgi_req, NUM2INT(sec));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
|
||||
@@ -235,7 +235,7 @@ PyObject *py_uwsgi_tornado_accept(PyObject *self, PyObject *args) {
|
||||
|
||||
// enter harakiri mode
|
||||
if (uwsgi.harakiri_options.workers > 0) {
|
||||
set_harakiri(uwsgi.harakiri_options.workers);
|
||||
set_harakiri(wsgi_req, uwsgi.harakiri_options.workers);
|
||||
}
|
||||
|
||||
uwsgi.async_proto_fd_table[wsgi_req->fd] = wsgi_req;
|
||||
|
||||
@@ -2863,7 +2863,6 @@ struct uwsgi_shared {
|
||||
|
||||
struct uwsgi_core {
|
||||
|
||||
//time_t harakiri;
|
||||
|
||||
uint64_t requests;
|
||||
uint64_t failed_requests;
|
||||
@@ -2889,6 +2888,10 @@ struct uwsgi_core {
|
||||
char *post_buf;
|
||||
|
||||
struct wsgi_request req;
|
||||
|
||||
// uWSGI 2.1
|
||||
time_t harakiri;
|
||||
time_t user_harakiri;
|
||||
};
|
||||
|
||||
struct uwsgi_worker {
|
||||
@@ -2904,8 +2907,9 @@ struct uwsgi_worker {
|
||||
uint64_t delta_requests;
|
||||
uint64_t failed_requests;
|
||||
|
||||
time_t harakiri;
|
||||
time_t user_harakiri;
|
||||
// renamed in 2.1 (was 'harakiri')
|
||||
time_t harakiri_total;
|
||||
time_t user_harakiri_unused;
|
||||
uint64_t harakiri_count;
|
||||
int pending_harakiri;
|
||||
|
||||
@@ -3042,11 +3046,11 @@ void uwsgi_curse(int, int);
|
||||
void uwsgi_curse_mule(int, int);
|
||||
void uwsgi_destroy_processes(void);
|
||||
|
||||
void set_harakiri(int);
|
||||
void set_user_harakiri(int);
|
||||
void set_harakiri(struct wsgi_request *, int);
|
||||
void set_user_harakiri(struct wsgi_request *, int);
|
||||
void set_mule_harakiri(int);
|
||||
void set_spooler_harakiri(int);
|
||||
void inc_harakiri(int);
|
||||
void inc_harakiri(struct wsgi_request *, int);
|
||||
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uint16_t uwsgi_swap16(uint16_t);
|
||||
|
||||
Reference in New Issue
Block a user