From c317588c6d704834925ed4485d4f720989f234c1 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 23 Jan 2015 18:15:58 +1100 Subject: [PATCH 1/9] Change ckdb userstats to update every minute instead of every 10 minutes --- src/stratifier.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index c69221a1..ac965da5 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -3497,8 +3497,8 @@ out: dec_instance_ref(sdata, client); } -/* Called every 20 seconds, we send the updated stats to ckdb of those users - * who have gone 10 minutes between updates. This ends up staggering stats to +/* Called 32 times per min, we send the updated stats to ckdb of those users + * who have gone 1 minute between updates. This ends up staggering stats to * avoid floods of stat data coming at once. */ static void update_workerstats(ckpool_t *ckp, sdata_t *sdata) { @@ -3815,10 +3815,10 @@ static void *statsupdate(void *arg) "createinet", ckp->serverurl[0]); ckdbq_add(ckp, ID_POOLSTATS, val); - /* Update stats 3 times per minute for smooth values, displaying - * status every minute. */ - for (i = 0; i < 3; i++) { - cksleep_ms_r(&stats->last_update, 20000); + /* Update stats 32 times per minute to divide up userstats for + * ckdb, displaying status every minute. */ + for (i = 0; i < 32; i++) { + cksleep_ms_r(&stats->last_update, 1875); cksleep_prepare_r(&stats->last_update); update_workerstats(ckp, sdata); @@ -3827,18 +3827,18 @@ static void *statsupdate(void *arg) stats->accounted_diff_shares += stats->unaccounted_diff_shares; stats->accounted_rejects += stats->unaccounted_rejects; - decay_time(&stats->sps1, stats->unaccounted_shares, 20, 60); - decay_time(&stats->sps5, stats->unaccounted_shares, 20, 300); - decay_time(&stats->sps15, stats->unaccounted_shares, 20, 900); - decay_time(&stats->sps60, stats->unaccounted_shares, 20, 3600); - - decay_time(&stats->dsps1, stats->unaccounted_diff_shares, 20, 60); - decay_time(&stats->dsps5, stats->unaccounted_diff_shares, 20, 300); - decay_time(&stats->dsps15, stats->unaccounted_diff_shares, 20, 900); - decay_time(&stats->dsps60, stats->unaccounted_diff_shares, 20, 3600); - decay_time(&stats->dsps360, stats->unaccounted_diff_shares, 20, 21600); - decay_time(&stats->dsps1440, stats->unaccounted_diff_shares, 20, 86400); - decay_time(&stats->dsps10080, stats->unaccounted_diff_shares, 20, 604800); + decay_time(&stats->sps1, stats->unaccounted_shares, 1.875, 60); + decay_time(&stats->sps5, stats->unaccounted_shares, 1.875, 300); + decay_time(&stats->sps15, stats->unaccounted_shares, 1.875, 900); + decay_time(&stats->sps60, stats->unaccounted_shares, 1.875, 3600); + + decay_time(&stats->dsps1, stats->unaccounted_diff_shares, 1.875, 60); + decay_time(&stats->dsps5, stats->unaccounted_diff_shares, 1.875, 300); + decay_time(&stats->dsps15, stats->unaccounted_diff_shares, 1.875, 900); + decay_time(&stats->dsps60, stats->unaccounted_diff_shares, 1.875, 3600); + decay_time(&stats->dsps360, stats->unaccounted_diff_shares, 1.875, 21600); + decay_time(&stats->dsps1440, stats->unaccounted_diff_shares, 1.875, 86400); + decay_time(&stats->dsps10080, stats->unaccounted_diff_shares, 1.875, 604800); stats->unaccounted_shares = stats->unaccounted_diff_shares = From 3bce852a286b5647291b86c079ff71d7254d342a Mon Sep 17 00:00:00 2001 From: ckolivas Date: Sat, 24 Jan 2015 09:44:43 +1100 Subject: [PATCH 2/9] Null the source pointer that calls jsonp_free generically as a sanity precaution. --- src/jansson-2.6/src/jansson_private.h | 4 +++- src/jansson-2.6/src/memory.c | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/jansson-2.6/src/jansson_private.h b/src/jansson-2.6/src/jansson_private.h index 3ca4dffa..9de9c1eb 100644 --- a/src/jansson-2.6/src/jansson_private.h +++ b/src/jansson-2.6/src/jansson_private.h @@ -81,7 +81,9 @@ int jsonp_dtostr(char *buffer, size_t size, double value); /* Wrappers for custom memory functions */ void* jsonp_malloc(size_t size); -void jsonp_free(void *ptr); +void _jsonp_free(void **ptr); +#define jsonp_free(ptr) _jsonp_free((void *)&(ptr)) + char *jsonp_strndup(const char *str, size_t length); char *jsonp_strdup(const char *str); char *jsonp_eolstrdup(const char *str); diff --git a/src/jansson-2.6/src/memory.c b/src/jansson-2.6/src/memory.c index 0be24380..5a00bafc 100644 --- a/src/jansson-2.6/src/memory.c +++ b/src/jansson-2.6/src/memory.c @@ -25,12 +25,13 @@ void *jsonp_malloc(size_t size) return (*do_malloc)(size); } -void jsonp_free(void *ptr) +void _jsonp_free(void **ptr) { - if(!ptr) + if(!*ptr) return; - (*do_free)(ptr); + (*do_free)(*ptr); + *ptr = NULL; } char *jsonp_strdup(const char *str) From 7cd351782ebbbf04ef33edd72ad860ae09b4bc08 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 24 Jan 2015 10:21:38 +1100 Subject: [PATCH 3/9] Push version to 0.8.3 --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index d183ca0b..a403b1f5 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT(ckpool, 0.8.2, kernel@kolivas.org) +AC_INIT(ckpool, 0.8.3, kernel@kolivas.org) AC_CANONICAL_SYSTEM AC_CONFIG_MACRO_DIR([m4]) From 39fcdb8848d3da646aa6312ec0a1e622fc4c4e36 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 24 Jan 2015 10:52:41 +1100 Subject: [PATCH 4/9] Change stratifier disconnect/drop message to not appear to be the server doing the disconnecting --- src/stratifier.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index ac965da5..916b171c 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1117,17 +1117,17 @@ 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("Disconnecting client %ld %s %s", id, client->workername, + LOGNOTICE("Client %ld %s disconnected %s", id, client->workername, client->dropped ? "lazily" : ""); HASH_ADD(hh, sdata->disconnected_instances, enonce1_64, sizeof(uint64_t), client); sdata->stats.disconnected++; client->disconnected_time = time(NULL); } else { - if (client->workername) - LOGNOTICE("Dropping client %ld %s %s", id, client->workername, + if (client->workername) { + LOGNOTICE("Client %ld %s dropped %s", id, client->workername, client->dropped ? "lazily" : ""); - else - LOGINFO("Dropping workerless client %ld %s", id, client->dropped ? "lazily" : ""); + } else + LOGINFO("Workerless client %ld dropped %s", id, client->dropped ? "lazily" : ""); __add_dead(sdata, client); } } From cced9e43b2b4cbeaf05254fec1737427258a5250 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 24 Jan 2015 11:32:47 +1100 Subject: [PATCH 5/9] Add a helper function to libckpool for setting a json object --- src/libckpool.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libckpool.h b/src/libckpool.h index 65fb4ae4..1b24d9c2 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -370,6 +370,14 @@ static inline void _json_set_bool(json_t *val, const char *key, bool boolean, } #define json_set_bool(val, key, boolean) _json_set_bool(val, key, boolean, __FILE__, __func__, __LINE__) +static inline void _json_set_object(json_t *val, const char *key, json_t *object, + const char *file, const char *func, const int line) +{ + if (unlikely(json_object_set_new_nocheck(val, key, object))) + LOGERR("Failed to set json object from %s %s:%d", file, func, line); +} +#define json_set_object(val, key, object) _json_set_object(val, key, object, __FILE__, __func__, __LINE__) + void rename_proc(const char *name); void create_pthread(pthread_t *thread, void *(*start_routine)(void *), void *arg); void join_pthread(pthread_t thread); From 0e5c5af1e6d03679288d65f0634d8ea34aa8e56d Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 24 Jan 2015 12:34:22 +1100 Subject: [PATCH 6/9] Create basic stratifier stats reporting on command --- src/ckpool.c | 6 +++++ src/stratifier.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/ckpool.c b/src/ckpool.c index 5f775fc5..bac50095 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -349,6 +349,12 @@ retry: } execv(ckp->initial_args[0], (char *const *)ckp->initial_args); } + } else if (cmdmatch(buf, "stratifierstats")) { + char *msg; + + LOGDEBUG("Listener received stratifierstats request"); + msg = send_recv_proc(ckp->stratifier, "stats"); + send_unix_msg(sockd, msg); } else { LOGINFO("Listener received unhandled message: %s", buf); send_unix_msg(sockd, "unknown"); diff --git a/src/stratifier.c b/src/stratifier.c index 916b171c..e041c269 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -302,6 +302,7 @@ struct stratifier_data { /* For the hashtable of all workbases */ workbase_t *workbases; workbase_t *current_workbase; + int workbases_generated; int64_t workbase_id; int64_t blockchange_id; @@ -323,6 +324,10 @@ struct stratifier_data { stratum_instance_t *disconnected_instances; stratum_instance_t *dead_instances; + int stratum_generated; + int disconnected_generated; + int dead_generated; + user_instance_t *user_instances; /* Protects both stratum and user instances */ @@ -656,6 +661,7 @@ static void add_base(ckpool_t *ckp, workbase_t *wb, bool *new_block) * we set workbase_id from it. In server mode the stratifier is * setting the workbase_id */ ck_wlock(&sdata->workbase_lock); + sdata->workbases_generated++; if (!ckp->proxy) wb->id = sdata->workbase_id++; else @@ -867,6 +873,7 @@ static void __add_dead(sdata_t *sdata, stratum_instance_t *client) LOGDEBUG("Adding dead instance %ld", client->id); LL_PREPEND(sdata->dead_instances, client); sdata->stats.dead++; + sdata->dead_generated++; } static void __del_dead(sdata_t *sdata, stratum_instance_t *client) @@ -1121,6 +1128,7 @@ static void __drop_client(sdata_t *sdata, stratum_instance_t *client, user_insta client->dropped ? "lazily" : ""); 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) { @@ -1156,6 +1164,7 @@ static stratum_instance_t *__stratum_add_instance(ckpool_t *ckp, int64_t id, int stratum_instance_t *instance = ckzalloc(sizeof(stratum_instance_t)); sdata_t *sdata = ckp->data; + sdata->stratum_generated++; instance->id = id; instance->server = server; instance->diff = instance->old_diff = ckp->startdiff; @@ -1501,6 +1510,55 @@ static void broadcast_ping(sdata_t *sdata) stratum_broadcast(sdata, json_msg); } +#define SAFE_HASH_OVERHEAD(HASHLIST) (HASHLIST ? HASH_OVERHEAD(hh, HASHLIST) : 0) + +static char *stratifier_stats(sdata_t *sdata) +{ + json_t *val = json_object(), *subval; + int objects, generated; + int64_t memsize; + char *buf; + + ck_rlock(&sdata->workbase_lock); + objects = HASH_COUNT(sdata->workbases); + memsize = SAFE_HASH_OVERHEAD(sdata->workbases) + sizeof(workbase_t) * objects; + generated = sdata->workbases_generated; + ck_runlock(&sdata->workbase_lock); + + JSON_CPACK(subval, "{si,si,si}", "count", objects, "memory", memsize, "generated", generated); + json_set_object(val, "workbases", subval); + + ck_rlock(&sdata->instance_lock); + objects = HASH_COUNT(sdata->user_instances); + memsize = SAFE_HASH_OVERHEAD(sdata->user_instances) + sizeof(stratum_instance_t) * objects; + JSON_CPACK(subval, "{si,si}", "count", objects, "memory", memsize); + json_set_object(val, "users", subval); + + objects = HASH_COUNT(sdata->stratum_instances); + memsize = SAFE_HASH_OVERHEAD(sdata->stratum_instances); + generated = sdata->stratum_generated; + JSON_CPACK(subval, "{si,si,si}", "count", objects, "memory", memsize, "generated", generated); + json_set_object(val, "clients", subval); + + objects = sdata->stats.disconnected; + generated = sdata->disconnected_generated; + memsize = sizeof(stratum_instance_t) * sdata->stats.disconnected; + JSON_CPACK(subval, "{si,si,si}", "count", objects, "memory", memsize, "generated", generated); + json_set_object(val, "disconnected", subval); + + objects = sdata->stats.dead; + generated = sdata->dead_generated; + memsize = sizeof(stratum_instance_t) * sdata->stats.dead; + JSON_CPACK(subval, "{si,si,si}", "count", objects, "memory", memsize, "generated", generated); + json_set_object(val, "dead", subval); + ck_runlock(&sdata->instance_lock); + + buf = json_dumps(val, JSON_NO_UTF8 | JSON_PRESERVE_ORDER); + json_decref(val); + LOGNOTICE("Stratifier stats: %s", buf); + return buf; +} + static int stratum_loop(ckpool_t *ckp, proc_instance_t *pi) { int sockd, ret = 0, selret = 0; @@ -1566,6 +1624,15 @@ retry: Close(sockd); goto retry; } + if (cmdmatch(buf, "stats")) { + char *msg; + + LOGDEBUG("Stratifier received stats request"); + msg = stratifier_stats(sdata); + send_unix_msg(sockd, msg); + Close(sockd); + goto retry; + } Close(sockd); LOGDEBUG("Stratifier received request: %s", buf); From b188ec8a9cbf6bfbc066abfdee08840c541279d1 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 24 Jan 2015 12:38:39 +1100 Subject: [PATCH 7/9] Add share stats to stratifierstats --- src/stratifier.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/stratifier.c b/src/stratifier.c index e041c269..635dbbbe 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -336,6 +336,8 @@ struct stratifier_data { share_t *shares; cklock_t share_lock; + int64_t shares_generated; + /* Linked list of block solves, added to during submission, removed on * accept/reject. It is likely we only ever have one solve on here but * you never know... */ @@ -1549,9 +1551,19 @@ static char *stratifier_stats(sdata_t *sdata) objects = sdata->stats.dead; generated = sdata->dead_generated; memsize = sizeof(stratum_instance_t) * sdata->stats.dead; + ck_runlock(&sdata->instance_lock); + JSON_CPACK(subval, "{si,si,si}", "count", objects, "memory", memsize, "generated", generated); json_set_object(val, "dead", subval); - ck_runlock(&sdata->instance_lock); + + ck_rlock(&sdata->share_lock); + generated = sdata->shares_generated; + objects = HASH_COUNT(sdata->shares); + memsize = SAFE_HASH_OVERHEAD(sdata->shares) + sizeof(share_t) * objects; + ck_runlock(&sdata->share_lock); + + JSON_CPACK(subval, "{si,si,si}", "count", objects, "memory", memsize, "generated", generated); + json_set_object(val, "shares", subval); buf = json_dumps(val, JSON_NO_UTF8 | JSON_PRESERVE_ORDER); json_decref(val); @@ -2599,6 +2611,7 @@ static bool new_share(sdata_t *sdata, const uchar *hash, int64_t wb_id) share = ckzalloc(sizeof(share_t)); memcpy(share->hash, hash, 32); share->workbase_id = wb_id; + sdata->shares_generated++; HASH_ADD(hh, sdata->shares, hash, 32, share); ret = true; out_unlock: From a44d00bf43ae2a4d7b661bd7f3c422a1bbdc0fdd Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 24 Jan 2015 13:07:50 +1100 Subject: [PATCH 8/9] Maintain a count of messages used in ckmsgq --- src/ckpool.c | 1 + src/ckpool.h | 1 + 2 files changed, 2 insertions(+) diff --git a/src/ckpool.c b/src/ckpool.c index bac50095..82623475 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -183,6 +183,7 @@ void ckmsgq_add(ckmsgq_t *ckmsgq, void *data) msg->data = data; mutex_lock(ckmsgq->lock); + ckmsgq->messages++; DL_APPEND(ckmsgq->msgs, msg); pthread_cond_signal(ckmsgq->cond); mutex_unlock(ckmsgq->lock); diff --git a/src/ckpool.h b/src/ckpool.h index 83424fc5..27116b5c 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -38,6 +38,7 @@ struct ckmsgq { pthread_cond_t *cond; ckmsg_t *msgs; void (*func)(ckpool_t *, void *); + int64_t messages; }; typedef struct ckmsgq ckmsgq_t; From 8ec6f3ce116ddbe6ae1fdfc47b2a0cda2c0347b7 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 24 Jan 2015 14:14:49 +1100 Subject: [PATCH 9/9] Add ckmsgq stats to stratifierstats. --- src/stratifier.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/stratifier.c b/src/stratifier.c index 635dbbbe..978551e6 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1514,6 +1514,21 @@ static void broadcast_ping(sdata_t *sdata) #define SAFE_HASH_OVERHEAD(HASHLIST) (HASHLIST ? HASH_OVERHEAD(hh, HASHLIST) : 0) +static void ckmsgq_stats(ckmsgq_t *ckmsgq, int size, json_t **val) +{ + int objects, generated; + int64_t memsize; + ckmsg_t *msg; + + mutex_lock(ckmsgq->lock); + DL_COUNT(ckmsgq->msgs, msg, objects); + generated = ckmsgq->messages; + mutex_unlock(ckmsgq->lock); + + memsize = (sizeof(ckmsg_t) + size) * objects; + JSON_CPACK(*val, "{si,si,si}", "count", objects, "memory", memsize, "generated", generated); +} + static char *stratifier_stats(sdata_t *sdata) { json_t *val = json_object(), *subval; @@ -1565,6 +1580,16 @@ static char *stratifier_stats(sdata_t *sdata) JSON_CPACK(subval, "{si,si,si}", "count", objects, "memory", memsize, "generated", generated); json_set_object(val, "shares", subval); + ckmsgq_stats(sdata->ssends, sizeof(smsg_t), &subval); + json_set_object(val, "ssends", subval); + /* Don't know exactly how big the string is so just count the pointer for now */ + ckmsgq_stats(sdata->srecvs, sizeof(char *), &subval); + json_set_object(val, "srecvs", subval); + ckmsgq_stats(sdata->ckdbq, sizeof(char *), &subval); + json_set_object(val, "ckdbq", subval); + ckmsgq_stats(sdata->stxnq, sizeof(json_params_t), &subval); + json_set_object(val, "stxnq", subval); + buf = json_dumps(val, JSON_NO_UTF8 | JSON_PRESERVE_ORDER); json_decref(val); LOGNOTICE("Stratifier stats: %s", buf);