From 4e6612be31035489050496b6e49efea318a51e66 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 15:15:45 +1100 Subject: [PATCH 01/14] Move logging out of lock in add_base --- src/stratifier.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index 28b1cb53..8952b228 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -697,7 +697,6 @@ static void add_base(ckpool_t *ckp, workbase_t *wb, bool *new_block) hex2bin(bin, sdata->lasthash, 32); swap_256(swap, bin); __bin2hex(sdata->lastswaphash, swap, 32); - LOGNOTICE("Block hash changed to %s", sdata->lastswaphash); sdata->blockchange_id = wb->id; } if (*new_block && ckp->logshares) { @@ -726,8 +725,10 @@ static void add_base(ckpool_t *ckp, workbase_t *wb, bool *new_block) sdata->current_workbase = wb; ck_wunlock(&sdata->workbase_lock); - if (*new_block) + if (*new_block) { + LOGNOTICE("Block hash changed to %s", sdata->lastswaphash); purge_share_hashtable(sdata, wb->id); + } send_workinfo(ckp, wb); From 6b30cfc9d04402e9e92d686400a297857d75603b Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 15:19:31 +1100 Subject: [PATCH 02/14] Move possible LOGERR usage ouf ot lock in __drop_client --- src/stratifier.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index 8952b228..02603fc3 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1171,16 +1171,19 @@ static void __drop_client(sdata_t *sdata, stratum_instance_t *client, user_insta static void _dec_instance_ref(sdata_t *sdata, stratum_instance_t *instance, const char *file, const char *func, const int line) { + int ref; + ck_wlock(&sdata->instance_lock); - if (unlikely(--instance->ref < 0)) { - LOGERR("Instance ref count dropped below zero from %s %s:%d", file, func, line); - instance->ref = 0; - } + ref = --instance->ref; /* See if there are any instances that were dropped that could not be * moved due to holding a reference and drop them now. */ - if (unlikely(instance->dropped && !instance->ref)) + if (unlikely(instance->dropped && !ref)) __drop_client(sdata, instance, instance->user_instance, instance->id); ck_wunlock(&sdata->instance_lock); + + /* This should never happen */ + if (unlikely(ref < 0)) + LOGERR("Instance ref count dropped below zero from %s %s:%d", file, func, line); } #define dec_instance_ref(sdata, instance) _dec_instance_ref(sdata, instance, __FILE__, __func__, __LINE__) From fd2369921a1f9bd9a5d8abc8669ea4aa2ccd839c Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 15:21:22 +1100 Subject: [PATCH 03/14] Move possible LOGINFO use out of lock usage in drop_client --- src/stratifier.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index 02603fc3..e4c283ec 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1326,6 +1326,7 @@ static void drop_client(sdata_t *sdata, int64_t id) stratum_instance_t *client, *tmp; user_instance_t *instance = NULL; time_t now_t = time(NULL); + int aged = 0, killed = 0; ckpool_t *ckp = NULL; bool dec = false; @@ -1356,7 +1357,7 @@ static void drop_client(sdata_t *sdata, int64_t id) continue; if (unlikely(client->ref)) continue; - LOGINFO("Ageing disconnected instance %ld to dead", client->id); + aged++; __del_disconnected(sdata, client); } @@ -1364,7 +1365,7 @@ static void drop_client(sdata_t *sdata, int64_t id) * counts for them. */ DL_FOREACH_SAFE(sdata->dead_instances, client, tmp) { if (!client->ref) { - LOGINFO("Stratifier discarding dead instance %ld", client->id); + killed++; __del_dead(sdata, client); dealloc(client->workername); dealloc(client->useragent); @@ -1373,6 +1374,11 @@ static void drop_client(sdata_t *sdata, int64_t id) } ck_wunlock(&sdata->instance_lock); + if (aged) + LOGINFO("Aged %d disconnected instances to dead", aged); + if (killed) + LOGINFO("Stratifier discarded %d dead instances", killed); + /* Decrease worker count outside of instance_lock to avoid recursive * locking */ if (dec) From 603306739edb0d20e23ef5f74e0b98ed327842d1 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 15:46:16 +1100 Subject: [PATCH 04/14] Move log usage out of lock in drop_client --- src/stratifier.c | 49 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index e4c283ec..0c20511d 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1140,10 +1140,11 @@ static stratum_instance_t *ref_instance_by_id(sdata_t *sdata, int64_t id) return instance; } -static void __drop_client(sdata_t *sdata, stratum_instance_t *client, user_instance_t *instance, - int64_t id) +/* Ret = 1 is disconnected, 2 is killed, 3 is workerless killed */ +static int __drop_client(sdata_t *sdata, stratum_instance_t *client, user_instance_t *instance) { stratum_instance_t *old_client = NULL; + int ret; HASH_DEL(sdata->stratum_instances, client); if (instance) @@ -1151,36 +1152,55 @@ static void __drop_client(sdata_t *sdata, stratum_instance_t *client, user_insta HASH_FIND(hh, sdata->disconnected_instances, &client->enonce1_64, sizeof(uint64_t), old_client); /* Only keep around one copy of the old client in server mode */ if (!client->ckp->proxy && !old_client && client->enonce1_64 && client->authorised) { - LOGNOTICE("Client %ld %s disconnected %s", id, client->workername, - client->dropped ? "lazily" : ""); + ret = 1; HASH_ADD(hh, sdata->disconnected_instances, enonce1_64, sizeof(uint64_t), client); sdata->stats.disconnected++; sdata->disconnected_generated++; client->disconnected_time = time(NULL); } else { - if (client->workername) { - LOGNOTICE("Client %ld %s dropped %s", id, client->workername, - client->dropped ? "lazily" : ""); - } else - LOGINFO("Workerless client %ld dropped %s", id, client->dropped ? "lazily" : ""); + if (client->workername) + ret = 2; + else + ret = 3; __add_dead(sdata, client); } + return ret; +} + +static void client_drop_message(int64_t client_id, int dropped, bool lazily) +{ + switch(dropped) { + case 0: + break; + case 1: + LOGNOTICE("Client %ld disconnected %s", client_id, lazily ? "lazily" : ""); + break; + case 2: + LOGNOTICE("Client %ld dropped %s", client_id, lazily ? "lazily" : ""); + break; + case 3: + LOGNOTICE("Workerless client %ld dropped %s", client_id, lazily ? "lazily" : ""); + break; + } } /* Decrease the reference count of instance. */ static void _dec_instance_ref(sdata_t *sdata, stratum_instance_t *instance, const char *file, const char *func, const int line) { - int ref; + int64_t client_id = instance->id; + int dropped = 0, ref; ck_wlock(&sdata->instance_lock); ref = --instance->ref; /* See if there are any instances that were dropped that could not be * moved due to holding a reference and drop them now. */ if (unlikely(instance->dropped && !ref)) - __drop_client(sdata, instance, instance->user_instance, instance->id); + dropped = __drop_client(sdata, instance, instance->user_instance); ck_wunlock(&sdata->instance_lock); + client_drop_message(client_id, dropped, true); + /* This should never happen */ if (unlikely(ref < 0)) LOGERR("Instance ref count dropped below zero from %s %s:%d", file, func, line); @@ -1323,10 +1343,10 @@ static void dec_worker(ckpool_t *ckp, user_instance_t *instance) static void drop_client(sdata_t *sdata, int64_t id) { + int dropped = 0, aged = 0, killed = 0; stratum_instance_t *client, *tmp; user_instance_t *instance = NULL; time_t now_t = time(NULL); - int aged = 0, killed = 0; ckpool_t *ckp = NULL; bool dec = false; @@ -1337,16 +1357,16 @@ static void drop_client(sdata_t *sdata, int64_t id) if (client) { instance = client->user_instance; if (client->authorised) { - client->authorised = false; dec = true; ckp = client->ckp; } /* If the client is still holding a reference, don't drop them * now but wait till the reference is dropped */ if (likely(!client->ref)) - __drop_client(sdata, client, instance, id); + dropped = __drop_client(sdata, client, instance); else client->dropped = true; + client->authorised = false; } /* Old disconnected instances will not have any valid shares so remove @@ -1374,6 +1394,7 @@ static void drop_client(sdata_t *sdata, int64_t id) } ck_wunlock(&sdata->instance_lock); + client_drop_message(id, dropped, false); if (aged) LOGINFO("Aged %d disconnected instances to dead", aged); if (killed) From 9396a8d39ed9daabd5cf4c9b25a2375c0a701074 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 15:55:40 +1100 Subject: [PATCH 05/14] Remove use of LOGDEBUG within locks in del dead/disconnected --- src/stratifier.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index 0c20511d..c16dd995 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -905,14 +905,12 @@ static void __add_dead(sdata_t *sdata, stratum_instance_t *client) static void __del_dead(sdata_t *sdata, stratum_instance_t *client) { - LOGDEBUG("Deleting dead instance %ld", client->id); DL_DELETE_INIT(sdata->dead_instances, client); sdata->stats.dead--; } static void __del_disconnected(sdata_t *sdata, stratum_instance_t *client) { - LOGDEBUG("Deleting disconnected instance %ld", client->id); HASH_DEL(sdata->disconnected_instances, client); sdata->stats.disconnected--; __add_dead(sdata, client); @@ -921,21 +919,29 @@ static void __del_disconnected(sdata_t *sdata, stratum_instance_t *client) static void drop_allclients(ckpool_t *ckp) { stratum_instance_t *client, *tmp; + int disconnects = 0, kills = 0; sdata_t *sdata = ckp->data; char buf[128]; ck_wlock(&sdata->instance_lock); HASH_ITER(hh, sdata->stratum_instances, client, tmp) { HASH_DEL(sdata->stratum_instances, client); + kills++; __add_dead(sdata, client); sprintf(buf, "dropclient=%ld", client->id); send_proc(ckp->connector, buf); } HASH_ITER(hh, sdata->disconnected_instances, client, tmp) { + disconnects++; __del_disconnected(sdata, client); } sdata->stats.users = sdata->stats.workers = 0; ck_wunlock(&sdata->instance_lock); + + if (disconnects) + LOGNOTICE("Disconnected %d instances", disconnects); + if (kills) + LOGNOTICE("Dropped %d instances", kills); } static void update_subscribe(ckpool_t *ckp) @@ -1229,6 +1235,7 @@ static uint64_t disconnected_sessionid_exists(sdata_t *sdata, const char *sessio { stratum_instance_t *instance, *tmp; uint64_t enonce1_64 = 0, ret = 0; + int64_t old_id = 0; int slen; if (!sessionid) @@ -1257,12 +1264,15 @@ static uint64_t disconnected_sessionid_exists(sdata_t *sdata, const char *sessio if (instance) { /* Delete the entry once we are going to use it since there * will be a new instance with the enonce1_64 */ + old_id = instance->id; __del_disconnected(sdata, instance); ret = enonce1_64; } out_unlock: ck_wunlock(&sdata->instance_lock); out: + if (ret) + LOGNOTICE("Reconnecting old instance %ld to instance %ld", old_id, id); return ret; } From d7417e9460686059dabd57b2b7494e4314a10ff4 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 15:58:03 +1100 Subject: [PATCH 06/14] Move use of LOGINFO out of __stratum_add_instance --- src/stratifier.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index c16dd995..340521af 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1226,7 +1226,6 @@ static stratum_instance_t *__stratum_add_instance(ckpool_t *ckp, int64_t id, int instance->diff = instance->old_diff = ckp->startdiff; instance->ckp = ckp; tv_time(&instance->ldc); - LOGINFO("Stratifier added instance %ld server %d", id, server); HASH_ADD_I64(sdata->stratum_instances, id, instance); return instance; } @@ -3305,6 +3304,7 @@ static void srecv_process(ckpool_t *ckp, char *buf) { sdata_t *sdata = ckp->data; stratum_instance_t *client; + bool added = false; smsg_t *msg; json_t *val; int server; @@ -3351,11 +3351,16 @@ static void srecv_process(ckpool_t *ckp, char *buf) ck_wlock(&sdata->instance_lock); client = __instance_by_id(sdata, msg->client_id); /* If client_id instance doesn't exist yet, create one */ - if (unlikely(!client)) + if (unlikely(!client)) { client = __stratum_add_instance(ckp, msg->client_id, server); + added = true; + } __inc_instance_ref(client); ck_wunlock(&sdata->instance_lock); + if (added) + LOGINFO("Stratifier added instance %ld server %d", client->id, server); + parse_instance_msg(sdata, msg, client); dec_instance_ref(sdata, client); out: From 6c2582fa3c12e5815059490c258ee504c18ae56b Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 15:59:49 +1100 Subject: [PATCH 07/14] Remove use of logdebug within __add_dead --- src/stratifier.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/stratifier.c b/src/stratifier.c index 340521af..32399207 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -897,7 +897,6 @@ static void update_base(ckpool_t *ckp, int prio) static void __add_dead(sdata_t *sdata, stratum_instance_t *client) { - LOGDEBUG("Adding dead instance %ld", client->id); DL_APPEND(sdata->dead_instances, client); sdata->stats.dead++; sdata->dead_generated++; From 41c74985ce12128d8e9dc6db2d13f3038fa95875 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 16:08:21 +1100 Subject: [PATCH 08/14] Print warning messages to console before attempting to log them in case of inability to write to the log --- src/ckpool.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index c2ca2348..ca07b3c2 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -81,6 +81,13 @@ void logmsg(int loglevel, const char *fmt, ...) { tm.tm_hour, tm.tm_min, tm.tm_sec); + if (loglevel <= LOG_WARNING) {\ + if (loglevel <= LOG_ERR && errno != 0) + fprintf(stderr, "%s %s with errno %d: %s\n", stamp, buf, errno, strerror(errno)); + else + fprintf(stderr, "%s %s\n", stamp, buf); + fflush(stderr); + } if (logfd) { char *msg; @@ -90,13 +97,6 @@ void logmsg(int loglevel, const char *fmt, ...) { ASPRINTF(&msg, "%s %s\n", stamp, buf); ckmsgq_add(global_ckp->logger, msg); } - if (loglevel <= LOG_WARNING) {\ - if (loglevel <= LOG_ERR && errno != 0) - fprintf(stderr, "%s %s with errno %d: %s\n", stamp, buf, errno, strerror(errno)); - else - fprintf(stderr, "%s %s\n", stamp, buf); - fflush(stderr); - } free(buf); } } From 15a4fd91a66120835ea580f9d175bbdbb3bfc8b3 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 18:36:42 +1100 Subject: [PATCH 09/14] Add ckdbq entries outside of the instance lock in update_workerstats --- src/stratifier.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index 32399207..b50ac34c 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -364,6 +364,14 @@ struct stratifier_data { typedef struct stratifier_data sdata_t; +typedef struct json_entry json_entry_t; + +struct json_entry{ + json_entry_t *next; + json_entry_t *prev; + json_t *val; +}; + /* Priority levels for generator messages */ #define GEN_LAX 0 #define GEN_NORMAL 1 @@ -3684,6 +3692,7 @@ out: * avoid floods of stat data coming at once. */ static void update_workerstats(ckpool_t *ckp, sdata_t *sdata) { + json_entry_t *json_list = NULL, *entry, *tmpentry; user_instance_t *user, *tmp; char cdfield[64]; time_t now_t; @@ -3716,8 +3725,8 @@ static void update_workerstats(ckpool_t *ckp, sdata_t *sdata) continue; DL_FOREACH(user->worker_instances, worker) { double ghs1, ghs5, ghs60, ghs1440; - int elapsed; json_t *val; + int elapsed; /* Send one lot of stats once the worker is idle if * they have submitted no shares in the last 10 minutes @@ -3744,10 +3753,19 @@ static void update_workerstats(ckpool_t *ckp, sdata_t *sdata) "createcode", __func__, "createinet", ckp->serverurl[0]); worker->notified_idle = worker->idle; - ckdbq_add(ckp, ID_WORKERSTATS, val); + entry = ckalloc(sizeof(json_entry_t)); + entry->val = val; + DL_APPEND(json_list, entry); } } ck_runlock(&sdata->instance_lock); + + /* Add all entries outside of the instance lock */ + DL_FOREACH_SAFE(json_list, entry, tmpentry) { + ckdbq_add(ckp, ID_WORKERSTATS, entry->val); + DL_DELETE(json_list, entry); + free(entry); + } } static void *statsupdate(void *arg) From 309079238ae831c178573fd9bcf931ec572c3baa Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 20:03:06 +1100 Subject: [PATCH 10/14] Differentiate lock contention from failing to get a response in failed auth message --- src/stratifier.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index b50ac34c..db836fa4 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -2187,6 +2187,7 @@ static int send_recv_auth(stratum_instance_t *client) ckpool_t *ckp = client->ckp; sdata_t *sdata = ckp->data; char *buf = NULL, *json_msg; + bool contended = false; char cdfield[64]; int ret = 1; json_t *val; @@ -2222,7 +2223,8 @@ static int send_recv_auth(stratum_instance_t *client) if (likely(!mutex_timedlock(&sdata->ckdb_lock, 3))) { buf = ckdb_msg_call(ckp, json_msg); mutex_unlock(&sdata->ckdb_lock); - } + } else + contended = true; free(json_msg); if (likely(buf)) { @@ -2266,7 +2268,10 @@ static int send_recv_auth(stratum_instance_t *client) json_decref(val); goto out; } - LOGWARNING("Got no auth response from ckdb :("); + if (contended) + LOGWARNING("Prolonged lock contention for ckdb while trying to authorise"); + else + LOGWARNING("Got no auth response from ckdb :("); out_fail: ret = -1; out: From 102ed8748b193b3dcdc088f8c42bfda011c6b192 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 21:15:34 +1100 Subject: [PATCH 11/14] Cache and use value of current_workbase in __stratum_notify --- src/stratifier.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index db836fa4..022dbc93 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -3010,20 +3010,20 @@ out: } /* Must enter with workbase_lock held */ -static json_t *__stratum_notify(sdata_t *sdata, bool clean) +static json_t *__stratum_notify(const workbase_t *wb, const bool clean) { json_t *val; JSON_CPACK(val, "{s:[ssssosssb],s:o,s:s}", "params", - sdata->current_workbase->idstring, - sdata->current_workbase->prevhash, - sdata->current_workbase->coinb1, - sdata->current_workbase->coinb2, - json_deep_copy(sdata->current_workbase->merkle_array), - sdata->current_workbase->bbversion, - sdata->current_workbase->nbit, - sdata->current_workbase->ntime, + wb->idstring, + wb->prevhash, + wb->coinb1, + wb->coinb2, + json_deep_copy(wb->merkle_array), + wb->bbversion, + wb->nbit, + wb->ntime, clean, "id", json_null(), "method", "mining.notify"); @@ -3035,7 +3035,7 @@ static void stratum_broadcast_update(sdata_t *sdata, bool clean) json_t *json_msg; ck_rlock(&sdata->workbase_lock); - json_msg = __stratum_notify(sdata, clean); + json_msg = __stratum_notify(sdata->current_workbase, clean); ck_runlock(&sdata->workbase_lock); stratum_broadcast(sdata, json_msg); @@ -3047,7 +3047,7 @@ static void stratum_send_update(sdata_t *sdata, int64_t client_id, bool clean) json_t *json_msg; ck_rlock(&sdata->workbase_lock); - json_msg = __stratum_notify(sdata, clean); + json_msg = __stratum_notify(sdata->current_workbase, clean); ck_runlock(&sdata->workbase_lock); stratum_add_send(sdata, json_msg, client_id); From 923eb44796bcd70c6e662e5e9bd154edc6589329 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 21:23:10 +1100 Subject: [PATCH 12/14] Cache value of ssends --- src/stratifier.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index 022dbc93..b80dda30 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1289,6 +1289,7 @@ static void stratum_broadcast(sdata_t *sdata, json_t *val) { stratum_instance_t *instance, *tmp; ckmsg_t *bulk_send = NULL; + ckmsgq_t *ssends; if (unlikely(!val)) { LOGERR("Sent null json to stratum_broadcast"); @@ -1316,13 +1317,15 @@ static void stratum_broadcast(sdata_t *sdata, json_t *val) if (!bulk_send) return; + ssends = sdata->ssends; + mutex_lock(sdata->ssends->lock); - if (sdata->ssends->msgs) - DL_CONCAT(sdata->ssends->msgs, bulk_send); + if (ssends->msgs) + DL_CONCAT(ssends->msgs, bulk_send); else - sdata->ssends->msgs = bulk_send; - pthread_cond_signal(sdata->ssends->cond); - mutex_unlock(sdata->ssends->lock); + ssends->msgs = bulk_send; + pthread_cond_signal(ssends->cond); + mutex_unlock(ssends->lock); } static void stratum_add_send(sdata_t *sdata, json_t *val, int64_t client_id) From 96280964ee474d01d7fc81d54d6ea29285530ffd Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 22:08:57 +1100 Subject: [PATCH 13/14] Read user and workerstats from logs on standalone mode outside of lock --- src/stratifier.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index b80dda30..79403fbe 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -2036,6 +2036,7 @@ static double dsps_from_key(json_t *val, const char *key) return ret; } +/* Enter holding a reference count */ static void read_userstats(ckpool_t *ckp, user_instance_t *instance) { char s[512]; @@ -2075,6 +2076,7 @@ static void read_userstats(ckpool_t *ckp, user_instance_t *instance) json_decref(val); } +/* Enter holding a reference count */ static void read_workerstats(ckpool_t *ckp, worker_instance_t *worker) { char s[512]; @@ -2120,10 +2122,10 @@ static user_instance_t *generate_user(ckpool_t *ckp, stratum_instance_t *client, const char *workername) { char *base_username = strdupa(workername), *username; + bool new_instance = false, new_worker = false; sdata_t *sdata = ckp->data; user_instance_t *instance; stratum_instance_t *tmp; - bool new = false; int len; username = strsep(&base_username, "._"); @@ -2139,12 +2141,9 @@ static user_instance_t *generate_user(ckpool_t *ckp, stratum_instance_t *client, /* New user instance. Secondary user id will be NULL */ instance = ckzalloc(sizeof(user_instance_t)); strcpy(instance->username, username); - new = true; - + new_instance = true; instance->id = sdata->user_instance_id++; HASH_ADD_STR(sdata->user_instances, username, instance); - if (CKP_STANDALONE(ckp)) - read_userstats(ckp, instance); } DL_FOREACH(instance->instances, tmp) { if (!safecmp(workername, tmp->workername)) { @@ -2160,15 +2159,19 @@ static user_instance_t *generate_user(ckpool_t *ckp, stratum_instance_t *client, worker->workername = strdup(workername); worker->instance = instance; DL_APPEND(instance->worker_instances, worker); - if (CKP_STANDALONE(ckp)) - read_workerstats(ckp, worker); + new_worker = true; worker->start_time = time(NULL); client->worker_instance = worker; } DL_APPEND(instance->instances, client); ck_wunlock(&sdata->instance_lock); - if (new && !ckp->proxy) { + if (CKP_STANDALONE(ckp) && new_instance) + read_userstats(ckp, instance); + if (CKP_STANDALONE(ckp) && new_worker) + read_workerstats(ckp, client->worker_instance); + + if (new_instance && !ckp->proxy) { /* Is this a btc address based username? */ if (len > 26 && len < 35) instance->btcaddress = test_address(ckp, username); From f01bb374b732cc26280599ae1ca9a1358743dfa6 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 26 Jan 2015 22:27:30 +1100 Subject: [PATCH 14/14] Dump the user stats to log in statsupdate outside of lock --- src/stratifier.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index 79403fbe..5d1b2cfb 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -366,12 +366,20 @@ typedef struct stratifier_data sdata_t; typedef struct json_entry json_entry_t; -struct json_entry{ +struct json_entry { json_entry_t *next; json_entry_t *prev; json_t *val; }; +typedef struct char_entry char_entry_t; + +struct char_entry { + char_entry_t *next; + char_entry_t *prev; + char *buf; +}; + /* Priority levels for generator messages */ #define GEN_LAX 0 #define GEN_NORMAL 1 @@ -3798,6 +3806,7 @@ static void *statsupdate(void *arg) double tdiff, per_tdiff; char suffix1[16], suffix5[16], suffix15[16], suffix60[16], cdfield[64]; char suffix360[16], suffix1440[16], suffix10080[16]; + char_entry_t *char_list = NULL, *char_t, *chartmp_t; user_instance_t *instance, *tmpuser; stratum_instance_t *client, *tmp; double sps1, sps5, sps15, sps60; @@ -3926,14 +3935,24 @@ static void *statsupdate(void *arg) } s = json_dumps(val, JSON_NO_UTF8 | JSON_PRESERVE_ORDER); fprintf(fp, "%s\n", s); - if (!idle) - LOGNOTICE("User %s:%s", instance->username, s); + if (!idle) { + char_t = ckalloc(sizeof(char_entry_t)); + ASPRINTF(&char_t->buf, "User %s:%s", instance->username, s); + DL_APPEND(char_list, char_t); + } dealloc(s); json_decref(val); fclose(fp); } ck_runlock(&sdata->instance_lock); + DL_FOREACH_SAFE(char_list, char_t, chartmp_t) { + LOGNOTICE("%s", char_t->buf); + DL_DELETE(char_list, char_t); + free(char_t->buf); + dealloc(char_t); + } + ghs1 = stats->dsps1 * nonces; suffix_string(ghs1, suffix1, 16, 0); sps1 = stats->sps1;