From 54ad139d6a4b52c4ad83ac6f08600ed2ffdd570e Mon Sep 17 00:00:00 2001 From: kanoi Date: Thu, 18 Jun 2015 09:56:23 +1000 Subject: [PATCH 01/10] php - fix missing variable log message --- pool/page_userinfo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pool/page_userinfo.php b/pool/page_userinfo.php index 358edbb6..95e12ca4 100644 --- a/pool/page_userinfo.php +++ b/pool/page_userinfo.php @@ -19,7 +19,7 @@ function douserinfo($data, $user) $ans = getUserInfo($user); - $pg = '

Block Acclaim

'.$pg; + $pg = '

Block Acclaim

'; $pg .= "\n"; $pg .= ""; $pg .= ""; From 0e2f020705c3ac851f1f1a8c26f885d8938f22fb Mon Sep 17 00:00:00 2001 From: kanoi Date: Mon, 6 Jul 2015 20:15:07 +1000 Subject: [PATCH 02/10] php - add sort radios to the workers page --- pool/inc.php | 165 +++++++++++++++++++----------------------- pool/page.php | 6 ++ pool/page_workers.php | 23 ++++-- 3 files changed, 96 insertions(+), 98 deletions(-) diff --git a/pool/inc.php b/pool/inc.php index a6126a68..a426580f 100644 --- a/pool/inc.php +++ b/pool/inc.php @@ -2,15 +2,15 @@ # function GBaseJS() { - $g = "function hasCan(){var c0=document.getElementById('can0');c=document.getElementById('can');return !!(c0&&c&&c.getContext&&c.getContext('2d'));} -function sep(d){ans={};var ar=d.split('\\t');var l=ar.length;for(var i=0;i0){ans=c[0].substr(e+1)}}}return ans} +function scnv(n,v){var d=new Date();d.setTime(d.getTime()+(864*Math.pow(10,8)));document.cookie=n+'='+v+';expires='+d.toUTCString()+';path=/'} function ni(e,o){if(e){if(o==0){e.defd=e.style.display;e.style.display='none'}else{e.style.display=e.defd}}} -function domin(o){var e=document.getElementById('minicb');if(e){if(o==0){e.checked=true}else{e.checked=false}};for(var i=0;i<10;i++){e=document.getElementById('mini'+i);ni(e,o)}} +function domin(o){var e=document.getElementById('minicb');if(e){if(o==0){e.checked=true}else{e.checked=false}}for(var i=0;i<10;i++){e=document.getElementById('mini'+i);ni(e,o)}} function mini(){var hm=gcn('mini');if(hm==''){domin(1)}else{domin(0)}} function md(e){var c='';if(e.checked){c='y'}scnv('mini',c);mini()} function tim(){var e=document.getElementById('ftr');if(e){var now=new Date(),t=document.createTextNode(now.toLocaleString());e.appendChild(t)}} -function jst(){var e=document.getElementById('jst');if(e){e.style.visibility='hidden'}}"; +function jst(){var e=document.getElementById('jst');if(e){e.style.visibility='hidden'}} +"; return $h; } # function HeadCSS($iCrap = false) { - $h = "input[type=checkbox] {vertical-align: -2px;} -form {display: inline-block;} -html, body {height: 100%; font-family:Arial, Verdana, sans-serif; font-size:12pt; background-color:#eeffff; text-align: center; background-repeat: no-repeat; background-position: center;} -.page {min-height: 100%; height: auto !important; height: 100%; margin: 0 auto -50px; position: relative;} -div.jst {color:red; font-weight: bold; font-size: 8; text-align: center; vertical-align: top;} -div.accwarn {color:red; font-weight: bold; font-size: 8; text-align: center; vertical-align: top;} -div.topd {background-color:#cff; border-color: #cff; border-style: solid; border-width: 9px;} -.topdes {color:blue; text-align: right;} -.topdesl {color:blue; text-align: left;} -.topwho {color:black; font-weight: bold; margin-right: 8px;} -.topdat {margin-left: 8px; margin-right: 24px; color:green; font-weight: bold;} -span.nb {white-space: pre;} -span.login {float: right; margin-left: 8px; margin-right: 24px;} + $h = "input[type=checkbox] {vertical-align:-2px;} +form {display:inline-block;} +html, body {height:100%;font-family:Arial, Verdana, sans-serif;font-size:12pt;background-color:#eeffff;text-align:center;background-repeat:no-repeat;background-position:center;} +.page {min-height:100%;height:auto !important;height:100%;margin:0 auto -50px;position:relative;} +div.jst {color:red;font-weight:bold;font-size:8;text-align:center;vertical-align:top;} +div.accwarn {color:red;font-weight:bold;font-size:8;text-align:center;vertical-align:top;} +div.topd {background-color:#cff;border-color:#cff;border-style:solid;border-width:9px;} +.topdes {color:blue;text-align:right;} +.topdesl {color:blue;text-align:left;} +.topwho {color:black;font-weight:bold;margin-right:8px;} +.topdat {margin-left:8px;margin-right:24px;color:green;font-weight:bold;} +span.nb {white-space:pre;} +span.login {float:right;margin-left:8px;margin-right:24px;} span.hil {color:blue;} span.user {color:green;} span.addr {color:brown;} -span.warn {color:orange; font-weight:bold;} -span.urg {color:red; font-weight:bold;} -span.err {color:red; font-weight:bold; font-size:120%;} -span.alert {color:red; font-weight:bold; font-size:250%;} -input.tiny {width: 0px; height: 0px; margin: 0px; padding: 0px; outline: none; border: 0px;} -#n42 {margin:0; position: relative; color:#ffffff; background:#0077ee;} -#n42 a {color:#fff; text-decoration:none; padding: 6px; display:block;} -#n42 td {min-width: 100px; float: left; vertical-align: top; padding: 0px 2px;} -#n42 td.navboxr {float: right;} -#n42 td.nav {position: relative;} -#n42 td.ts {border-width: 1px; border-color: #0022ee; border-style: solid none none none;}"; +span.warn {color:orange;font-weight:bold;} +span.urg {color:red;font-weight:bold;} +span.err {color:red;font-weight:bold;font-size:120%;} +span.alert {color:red;font-weight:bold;font-size:250%;} +input.tiny {width:0px;height:0px;margin:0px;padding:0px;outline:none;border:0px;} +#n42 {margin:0;position:relative;color:#ffffff;background:#0077ee;} +#n42 a {color:#fff;text-decoration:none;padding:6px;display:block;} +#n42 td {min-width:100px;float:left;vertical-align:top;padding:0px 2px;} +#n42 td.navboxr {float:right;} +#n42 td.nav {position:relative;} +#n42 td.ts {border-width:1px;border-color:#0022ee;border-style:solid none none none;} +"; if (!$iCrap) { - $h .= " -#n42 div.sub {left: 0px; z-index: 42; position: absolute; visibility: hidden;} + $h .= "#n42 div.sub {left:0px;z-index:42;position:absolute;visibility:hidden;} #n42 td.nav:hover {background:#0099ee;} -#n42 td.nav:hover div.sub {background:#0077ee; visibility: visible;}"; +#n42 td.nav:hover div.sub {background:#0077ee;visibility:visible;} +"; } - $h .= " -h1 {margin-top: 20px; float:middle; font-size: 20px;} -.foot, .push {height: 50px;} -.title {background-color: #909090;} -.even {background-color: #cccccc;} -.odd {background-color: #a8a8a8;} -.hid {display: none;} -.dl {text-align: left; padding: 2px 8px;} -.dr {text-align: right; padding: 2px 8px;} -.dc {text-align: center; padding: 2px 8px;} -.dls {text-align: left; padding: 2px 8px; text-decoration:line-through; font-weight:lighter;} -.drs {text-align: right; padding: 2px 8px; text-decoration:line-through; font-weight:lighter;} -.dcs {text-align: center; padding: 2px 8px; text-decoration:line-through; font-weight:lighter;} + $h .= "h1 {margin-top:20px;float:middle;font-size:20px;} +.foot, .push {height:50px;} +.title {background-color:#909090;} +.even {background-color:#cccccc;} +.odd {background-color:#a8a8a8;} +.hid {display:none;} +.dl {text-align:left;padding:2px 8px;} +.dr {text-align:right;padding:2px 8px;} +.dc {text-align:center;padding:2px 8px;} +.dls {text-align:left;padding:2px 8px;text-decoration:line-through;font-weight:lighter;} +.drs {text-align:right;padding:2px 8px;text-decoration:line-through;font-weight:lighter;} +.dcs {text-align:center;padding:2px 8px;text-decoration:line-through;font-weight:lighter;} .st0 {font-weight:bold;} -.st1 {color:red; font-weight:bold;} -.st2 {color:green; font-weight:bold;} -.st3 {color:blue; font-weight:bold;} -.fthi {color:red; font-size:7px;} -.ftlo {color:green; font-size:7px;} -.ft {color:blue; font-size:7px;} -.ftl {text-align: left; color:blue; font-size:7px; display:inline-block; width:20%; white-space: nowrap;} -.ftm {text-align: middle; font-size:10pt; display:inline-block; width:60%; white-space: nowrap;} -.ftr {text-align: rigth; color:blue; font-size:7px; display:inline-block; width:20%; white-space: nowrap;}"; +.st1 {color:red;font-weight:bold;} +.st2 {color:green;font-weight:bold;} +.st3 {color:blue;font-weight:bold;} +.fthi {color:red;font-size:7px;} +.ftlo {color:green;font-size:7px;} +.ft {color:blue;font-size:7px;} +.ftl {text-align:left;color:blue;font-size:7px;display:inline-block;width:20%;white-space:nowrap;} +.ftm {text-align:middle;font-size:10pt;display:inline-block;width:60%;white-space:nowrap;} +.ftr {text-align:rigth;color:blue;font-size:7px;display:inline-block;width:20%;white-space:nowrap;} +"; return $h; } # +function SortJS() +{ + $s = "function sot(t,s,o){if(t && s>=0){var rs=t.rows;if(rs && rs.length>2){var all=[], rn, ok=true;for(rn=1;rn diff --git a/pool/page.php b/pool/page.php index 20e42f78..6a64d2eb 100644 --- a/pool/page.php +++ b/pool/page.php @@ -66,6 +66,11 @@ function addTips() addCSS($tcss); } # +function addSort() +{ + $s = SortJS(); + addScript($s); +} # function makeURL($page) { @@ -75,6 +80,7 @@ function makeURL($page) $page = '?k='.$page; return "/index.php$page"; } +# function makeLink($page, $rest = '') { $href = ""; $pg .= ''; - $pg .= ''; + $pg .= ""; $pg .= ''; - $pg .= ''; + $pg .= ""; $pg .= ''; $pg .= ''; - $pg .= ''; + $pg .= ""; $pg .= "\n"; + addSort(); return $pg; } # @@ -86,14 +88,15 @@ function workuser($data, $user, &$offset, &$totshare, &$totdiff, $ld = ' '; $pg .= ""; - $pg .= ''; + $pg .= "'; $shareacc = number_format($all[$i]['w_shareacc'], 0); $totshare += $all[$i]['w_shareacc']; $diffacc = number_format($all[$i]['w_diffacc'], 0); + $ds = round($all[$i]['w_diffacc']); $totdiff += $all[$i]['w_diffacc']; $pg .= ""; - $pg .= ""; + $pg .= ""; $dtot = $all[$i]['w_diffacc'] + $all[$i]['w_diffinv']; if ($dtot > 0) @@ -113,13 +116,17 @@ function workuser($data, $user, &$offset, &$totshare, &$totdiff, $uhr = $all[$i]['w_uhr']; if ($uhr == '?') + { $uhr = '?GHs'; + $su = 0; + } else { $totrate += $uhr; $uhr = dsprate($uhr); + $su = round($uhr); } - $pg .= ""; + $pg .= ""; $pg .= "\n"; @@ -137,7 +144,7 @@ function worktotal($offset, $totshare, $totdiff, $totinvalid, $totrate, $blockac $row = 'even'; else $row = 'odd'; - $pg .= ""; + $pg .= ""; $shareacc = number_format($totshare, 0); $pg .= ""; $diffacc = number_format($totdiff, 0); From a86b3f7aaa629e7da2dbb56bd4406671df68faa2 Mon Sep 17 00:00:00 2001 From: kanoi Date: Mon, 6 Jul 2015 23:50:13 +1000 Subject: [PATCH 03/10] php - store worker sort choice in a cookie --- pool/inc.php | 4 +++- pool/page_workers.php | 14 ++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pool/inc.php b/pool/inc.php index a426580f..9710fe8b 100644 --- a/pool/inc.php +++ b/pool/inc.php @@ -139,7 +139,9 @@ input.tiny {width:0px;height:0px;margin:0px;padding:0px;outline:none;border:0px; function SortJS() { $s = "function sot(t,s,o){if(t && s>=0){var rs=t.rows;if(rs && rs.length>2){var all=[], rn, ok=true;for(rn=1;rn"; + $pg .= ""; $pg .= ''; - $pg .= ""; + $pg .= ""; $pg .= ''; - $pg .= ""; + $pg .= ""; $pg .= ''; $pg .= ''; - $pg .= ""; + $pg .= ""; $pg .= "\n"; - addSort(); return $pg; } # @@ -194,6 +194,8 @@ function doworker($data, $user) } $pg .= "
UserWork DiffLast Share$r,2,'n');\">:Last ShareSharesDiff$r,4,'r');\">:DiffInvalidBlock %Hash Rate$r,7,'r');\" checked>:Hash Rate
$ld'.howlongago($lst).'".howlongago($lst).'$shareacc$diffacc$diffacc$uhr$uhr
Total:
Total: $offset$shareaccWorker Name:<$r id=srtwrk data-sf=s0>Work Diff$r,2,'n');\">:Last Share<$r id=srtlst data-sf=n2>:Last ShareShares$r,4,'r');\">:Diff<$r id=srtdiff data-sf=r4>:DiffInvalidBlock %$r,7,'r');\" checked>:Hash Rate<$r id=srtrate data-sf=r7>:Hash Rate
\n"; + $pg .= "\n"; return "

Workers$title

".$pg; } From 8c9164e9503c1ce712cbe2758f6dc48a4e3b0ac2 Mon Sep 17 00:00:00 2001 From: kanoi Date: Wed, 8 Jul 2015 18:03:07 +1000 Subject: [PATCH 04/10] php - hard rate sort was prefix based - fix to be absolute --- pool/page_workers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pool/page_workers.php b/pool/page_workers.php index 052fe133..d87ea691 100644 --- a/pool/page_workers.php +++ b/pool/page_workers.php @@ -122,9 +122,9 @@ function workuser($data, $user, &$offset, &$totshare, &$totdiff, } else { + $su = round($uhr); $totrate += $uhr; $uhr = dsprate($uhr); - $su = round($uhr); } $pg .= "$uhr"; From c9245673af3d1f9efc26ff07f41278a7cde52943 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 12 Jul 2015 11:23:15 +1000 Subject: [PATCH 05/10] Make read_socket_line use a timeout pointer allowing the value to be used for reentrant calls --- src/ckpool.c | 18 ++++++++++++------ src/ckpool.h | 2 +- src/generator.c | 16 +++++++++++----- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index ccb708c3..4d00084e 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -495,7 +495,7 @@ void empty_buffer(connsock_t *cs) /* Read from a socket into cs->buf till we get an '\n', converting it to '\0' * and storing how much extra data we've received, to be moved to the beginning * of the buffer for use on the next receive. */ -int read_socket_line(connsock_t *cs, float timeout) +int read_socket_line(connsock_t *cs, float *timeout) { int fd = cs->fd, ret = -1; char *eom = NULL; @@ -519,7 +519,12 @@ int read_socket_line(connsock_t *cs, float timeout) tv_time(&start); rewait: - ret = wait_read_select(fd, eom ? 0 : timeout); + if (*timeout <= 0) { + LOGDEBUG("Timed out in read_socket_line"); + ret = 0; + goto out; + } + ret = wait_read_select(fd, eom ? 0 : *timeout); if (ret < 1) { if (!ret) { if (eom) @@ -531,7 +536,7 @@ rewait: } tv_time(&now); diff = tvdiff(&now, &start); - timeout -= diff; + *timeout -= diff; while (42) { char readbuf[PAGESIZE] = {}; int backoff = 1; @@ -543,7 +548,7 @@ rewait: if (eom) break; /* Have we used up all the timeout yet? */ - if (timeout > 0 && (errno == EAGAIN || errno == EWOULDBLOCK || !ret)) + if (*timeout > 0 && (errno == EAGAIN || errno == EWOULDBLOCK || !ret)) goto rewait; LOGERR("Failed to recv in read_socket_line"); goto out; @@ -724,6 +729,7 @@ json_t *json_rpc_call(connsock_t *cs, const char *rpc_req) char *http_req = NULL; json_error_t err_val; json_t *val = NULL; + float timeout = 60; int len, ret; if (unlikely(cs->fd < 0)) { @@ -766,7 +772,7 @@ json_t *json_rpc_call(connsock_t *cs, const char *rpc_req) LOGWARNING("Failed to write to socket in json_rpc_call"); goto out_empty; } - ret = read_socket_line(cs, 5); + ret = read_socket_line(cs, &timeout); if (ret < 1) { LOGWARNING("Failed to read socket line in json_rpc_call"); goto out_empty; @@ -776,7 +782,7 @@ json_t *json_rpc_call(connsock_t *cs, const char *rpc_req) goto out_empty; } do { - ret = read_socket_line(cs, 5); + ret = read_socket_line(cs, &timeout); if (ret < 1) { LOGWARNING("Failed to read http socket lines in json_rpc_call"); goto out_empty; diff --git a/src/ckpool.h b/src/ckpool.h index f5bfdb20..d859ce89 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -242,7 +242,7 @@ ckpool_t *global_ckp; bool ping_main(ckpool_t *ckp); void empty_buffer(connsock_t *cs); -int read_socket_line(connsock_t *cs, float timeout); +int read_socket_line(connsock_t *cs, float *timeout); void _send_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line); #define send_proc(pi, msg) _send_proc(pi, msg, __FILE__, __func__, __LINE__) char *_send_recv_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line); diff --git a/src/generator.c b/src/generator.c index 90f054f4..576c8709 100644 --- a/src/generator.c +++ b/src/generator.c @@ -517,8 +517,9 @@ static char *cached_proxy_line(proxy_instance_t *proxi) static char *next_proxy_line(connsock_t *cs, proxy_instance_t *proxi) { char *buf = cached_proxy_line(proxi); + float timeout = 10; - if (!buf && read_socket_line(cs, 5) > 0) + if (!buf && read_socket_line(cs, &timeout) > 0) buf = strdup(cs->buf); return buf; } @@ -534,9 +535,10 @@ static void append_proxy_line(proxy_instance_t *proxi, const char *buf) /* Get a new line from the connsock and return a copy of it */ static char *new_proxy_line(connsock_t *cs) { + float timeout = 10; char *buf = NULL; - if (read_socket_line(cs, 5) < 1) + if (read_socket_line(cs, &timeout) < 1) goto out; buf = strdup(cs->buf); out: @@ -719,6 +721,7 @@ out: static bool passthrough_stratum(connsock_t *cs, proxy_instance_t *proxi) { json_t *req, *val = NULL, *res_val, *err_val; + float timeout = 10; bool ret = false; JSON_CPACK(req, "{s:s,s:[s]}", @@ -730,7 +733,7 @@ static bool passthrough_stratum(connsock_t *cs, proxy_instance_t *proxi) LOGWARNING("Failed to send message in passthrough_stratum"); goto out; } - if (read_socket_line(cs, 5) < 1) { + if (read_socket_line(cs, &timeout) < 1) { LOGWARNING("Failed to receive line in passthrough_stratum"); goto out; } @@ -1259,6 +1262,7 @@ static void *proxy_recv(void *arg) notify_instance_t *ni, *tmp; share_msg_t *share, *tmpshare; int retries = 0, ret; + float timeout; time_t now; now = time(NULL); @@ -1286,12 +1290,13 @@ static void *proxy_recv(void *arg) /* If we don't get an update within 10 minutes the upstream pool * has likely stopped responding. */ + timeout = 10; do { if (cs->fd == -1) { ret = -1; break; } - ret = read_socket_line(cs, 5); + ret = read_socket_line(cs, &timeout); } while (ret == 0 && ++retries < 120); if (ret < 1) { @@ -1393,10 +1398,11 @@ static void *passthrough_recv(void *arg) rename_proc("passrecv"); while (42) { + float timeout = 60; int ret; do { - ret = read_socket_line(cs, 60); + ret = read_socket_line(cs, &timeout); } while (ret == 0); if (ret < 1) { From f7914958896df38a9107d23d27d81321955f09df Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 12 Jul 2015 12:31:28 +1000 Subject: [PATCH 06/10] Move failure warnings into write_length, importing source details and adding info about type of failure --- src/libckpool.c | 25 +++++++++++++++---------- src/libckpool.h | 3 ++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/libckpool.c b/src/libckpool.c index 398b1a1c..a2714c2f 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -1003,20 +1003,27 @@ out: return ret; } -int write_length(int sockd, const void *buf, int len) +int _write_length(int sockd, const void *buf, int len, const char *file, const char *func, const int line) { int ret, ofs = 0; if (unlikely(len < 1)) { - LOGWARNING("Invalid write length of %d requested in write_length", len); + LOGWARNING("Invalid write length of %d requested in write_length from %s %s:%d", + len, file, func, line); return -1; } - if (unlikely(sockd < 0)) + if (unlikely(sockd < 0)) { + LOGWARNING("Attempt to write to invalidated sock in write_length from %s %s:%d", + file, func, line); return -1; + } while (len) { ret = write(sockd, buf + ofs, len); - if (unlikely(ret < 0)) + if (unlikely(ret < 0)) { + LOGERR("Failed to write %d bytes in write_length from %s %s:%d", + len, file, func, line); return -1; + } ofs += ret; len -= ret; } @@ -1049,10 +1056,9 @@ bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *fu LOGERR("Select1 failed in send_unix_msg (%d)", ern); goto out; } - ret = write_length(sockd, &msglen, 4); + ret = _write_length(sockd, &msglen, 4, file, func, line); if (unlikely(ret < 4)) { - ern = errno; - LOGERR("Failed to write 4 byte length in send_unix_msg (%d)", ern); + LOGERR("Failed to write 4 byte length in send_unix_msg"); goto out; } ret = wait_write_select(sockd, 5); @@ -1061,10 +1067,9 @@ bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *fu LOGERR("Select2 failed in send_unix_msg (%d)", ern); goto out; } - ret = write_length(sockd, buf, len); + ret = _write_length(sockd, buf, len, file, func, line); if (unlikely(ret < 0)) { - ern = errno; - LOGERR("Failed to write %d bytes in send_unix_msg (%d)", len, ern); + LOGERR("Failed to write %d bytes in send_unix_msg", len); goto out; } retval = true; diff --git a/src/libckpool.h b/src/libckpool.h index dc28e18d..10611916 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -499,7 +499,8 @@ char *_recv_unix_msg(int sockd, int timeout1, int timeout2, const char *file, co #define recv_unix_msg_tmo(sockd, tmo) _recv_unix_msg(sockd, tmo, RECV_UNIX_TIMEOUT2, __FILE__, __func__, __LINE__) #define recv_unix_msg_tmo2(sockd, tmo1, tmo2) _recv_unix_msg(sockd, tmo1, tmo2, __FILE__, __func__, __LINE__) int wait_write_select(int sockd, float timeout); -int write_length(int sockd, const void *buf, int len); +#define write_length(sockd, buf, len) _write_length(sockd, buf, len, __FILE__, __func__, __LINE__) +int _write_length(int sockd, const void *buf, int len, const char *file, const char *func, const int line); bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *func, const int line); #define send_unix_msg(sockd, buf) _send_unix_msg(sockd, buf, __FILE__, __func__, __LINE__) bool _send_unix_data(int sockd, const struct msghdr *msg, const char *file, const char *func, const int line); From aa8460c0ba92bb92d0afaf80f0df518f4d5724fc Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 12 Jul 2015 12:40:23 +1000 Subject: [PATCH 07/10] Set default unix read timeout back to 5 seconds except for ckpmsg and ckdb which need the 30s defaults, and double the write timeout --- src/ckdb.c | 2 +- src/libckpool.c | 8 ++++---- src/libckpool.h | 10 ++++++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index 7a469159..d8eba810 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -3743,7 +3743,7 @@ static void *socketer(__maybe_unused void *arg) cmdnum = CMD_UNSET; - buf = recv_unix_msg(sockd); + buf = recv_unix_msg_tmo2(sockd, RECV_UNIX_TIMEOUT1, RECV_UNIX_TIMEOUT2); // Once we've read the message setnow(&now); if (buf) { diff --git a/src/libckpool.c b/src/libckpool.c index a2714c2f..a72dba24 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -1050,7 +1050,7 @@ bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *fu goto out; } msglen = htole32(len); - ret = wait_write_select(sockd, 5); + ret = wait_write_select(sockd, UNIX_WRITE_TIMEOUT); if (unlikely(ret < 1)) { ern = errno; LOGERR("Select1 failed in send_unix_msg (%d)", ern); @@ -1061,7 +1061,7 @@ bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *fu LOGERR("Failed to write 4 byte length in send_unix_msg"); goto out; } - ret = wait_write_select(sockd, 5); + ret = wait_write_select(sockd, UNIX_WRITE_TIMEOUT); if (unlikely(ret < 1)) { ern = errno; LOGERR("Select2 failed in send_unix_msg (%d)", ern); @@ -1089,7 +1089,7 @@ bool _send_unix_data(int sockd, const struct msghdr *msg, const char *file, cons LOGWARNING("Null message sent to send_unix_data"); goto out; } - ret = wait_write_select(sockd, 5); + ret = wait_write_select(sockd, UNIX_WRITE_TIMEOUT); if (unlikely(ret < 1)) { LOGERR("Select1 failed in send_unix_data"); goto out; @@ -1112,7 +1112,7 @@ bool _recv_unix_data(int sockd, struct msghdr *msg, const char *file, const char bool retval = false; int ret; - ret = wait_read_select(sockd, 5); + ret = wait_read_select(sockd, UNIX_READ_TIMEOUT); if (unlikely(ret < 1)) { LOGERR("Select1 failed in recv_unix_data"); goto out; diff --git a/src/libckpool.h b/src/libckpool.h index 10611916..26aff16d 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -236,6 +236,12 @@ void logmsg(int loglevel, const char *fmt, ...); #define PAGESIZE (4096) +/* Default timeouts for unix socket reads and writes in seconds. Set write + * timeout to double the read timeout in case of one read blocking the next + * writer. */ +#define UNIX_READ_TIMEOUT 5 +#define UNIX_WRITE_TIMEOUT 10 + /* Share error values */ enum share_err { @@ -495,8 +501,8 @@ int read_length(int sockd, void *buf, int len); char *_recv_unix_msg(int sockd, int timeout1, int timeout2, const char *file, const char *func, const int line); #define RECV_UNIX_TIMEOUT1 30 #define RECV_UNIX_TIMEOUT2 5 -#define recv_unix_msg(sockd) _recv_unix_msg(sockd, RECV_UNIX_TIMEOUT1, RECV_UNIX_TIMEOUT2, __FILE__, __func__, __LINE__) -#define recv_unix_msg_tmo(sockd, tmo) _recv_unix_msg(sockd, tmo, RECV_UNIX_TIMEOUT2, __FILE__, __func__, __LINE__) +#define recv_unix_msg(sockd) _recv_unix_msg(sockd, UNIX_READ_TIMEOUT, UNIX_READ_TIMEOUT, __FILE__, __func__, __LINE__) +#define recv_unix_msg_tmo(sockd, tmo) _recv_unix_msg(sockd, tmo, UNIX_READ_TIMEOUT, __FILE__, __func__, __LINE__) #define recv_unix_msg_tmo2(sockd, tmo1, tmo2) _recv_unix_msg(sockd, tmo1, tmo2, __FILE__, __func__, __LINE__) int wait_write_select(int sockd, float timeout); #define write_length(sockd, buf, len) _write_length(sockd, buf, len, __FILE__, __func__, __LINE__) From f9a17d04c3ad40b2a683e6355cf6da5b69ab4009 Mon Sep 17 00:00:00 2001 From: kanoi Date: Sun, 12 Jul 2015 14:33:21 +1000 Subject: [PATCH 08/10] Update log messages to millisecond accuracy --- src/ckdb.c | 12 +++++++----- src/ckpool.c | 14 ++++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index d8eba810..e568ccdd 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -561,7 +561,8 @@ void logmsg(int loglevel, const char *fmt, ...) int logfd = 0; char *buf = NULL; struct tm tm; - time_t now_t; + tv_t now_tv; + int ms; va_list ap; char stamp[128]; char *extra = EMPTY; @@ -572,8 +573,9 @@ void logmsg(int loglevel, const char *fmt, ...) if (loglevel > global_ckp->loglevel) return; - now_t = time(NULL); - localtime_r(&now_t, &tm); + tv_time(&now_tv); + ms = (int)(now_tv.tv_usec / 1000); + localtime_r(&(now_tv.tv_sec), &tm); minoff = tm.tm_gmtoff / 60; if (minoff < 0) { tzch = '-'; @@ -591,13 +593,13 @@ void logmsg(int loglevel, const char *fmt, ...) tzch, hroff); } snprintf(stamp, sizeof(stamp), - "[%d-%02d-%02d %02d:%02d:%02d%s]", + "[%d-%02d-%02d %02d:%02d:%02d.%03d%s]", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, - tm.tm_sec, + tm.tm_sec, ms, tzinfo); if (!fmt) { diff --git a/src/ckpool.c b/src/ckpool.c index 4d00084e..7f2473b3 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -64,7 +64,8 @@ void logmsg(int loglevel, const char *fmt, ...) { int logfd = global_ckp->logfd; char *buf = NULL; struct tm tm; - time_t now_t; + tv_t now_tv; + int ms; va_list ap; char stamp[128]; @@ -72,16 +73,17 @@ void logmsg(int loglevel, const char *fmt, ...) { VASPRINTF(&buf, fmt, ap); va_end(ap); - now_t = time(NULL); - localtime_r(&now_t, &tm); - sprintf(stamp, "[%d-%02d-%02d %02d:%02d:%02d]", + tv_time(&now_tv); + ms = (int)(now_tv.tv_usec / 1000); + localtime_r(&(now_tv.tv_sec), &tm); + sprintf(stamp, "[%d-%02d-%02d %02d:%02d:%02d.%03d]", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, - tm.tm_sec); - if (loglevel <= LOG_WARNING) {\ + tm.tm_sec, ms); + 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 From e94115d6e28b7407f5b90b1f3b6016e30cdfd106 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 12 Jul 2015 14:56:12 +1000 Subject: [PATCH 09/10] Allow _send_recv_proc to take custom timeouts --- src/ckpool.c | 7 ++++--- src/ckpool.h | 5 +++-- src/libckpool.c | 6 +++--- src/libckpool.h | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index 4d00084e..f7ec1fbe 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -640,7 +640,8 @@ out: /* Send a single message to a process instance and retrieve the response, then * close the socket. */ -char *_send_recv_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line) +char *_send_recv_proc(proc_instance_t *pi, int writetimeout, int readtimedout, + const char *msg, const char *file, const char *func, const int line) { char *path = pi->us.path, *buf = NULL; int sockd; @@ -670,10 +671,10 @@ char *_send_recv_proc(proc_instance_t *pi, const char *msg, const char *file, co LOGWARNING("Failed to open socket %s in send_recv_proc", path); goto out; } - if (unlikely(!send_unix_msg(sockd, msg))) + if (unlikely(!_send_unix_msg(sockd, msg, writetimeout, file, func, line))) LOGWARNING("Failed to send %s to socket %s", msg, path); else - buf = recv_unix_msg(sockd); + buf = _recv_unix_msg(sockd, readtimedout, readtimedout, file, func, line); Close(sockd); out: if (unlikely(!buf)) diff --git a/src/ckpool.h b/src/ckpool.h index d859ce89..12ee8612 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -245,8 +245,9 @@ void empty_buffer(connsock_t *cs); int read_socket_line(connsock_t *cs, float *timeout); void _send_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line); #define send_proc(pi, msg) _send_proc(pi, msg, __FILE__, __func__, __LINE__) -char *_send_recv_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line); -#define send_recv_proc(pi, msg) _send_recv_proc(pi, msg, __FILE__, __func__, __LINE__) +char *_send_recv_proc(proc_instance_t *pi, int writetimeout, int readtimedout, + const char *msg, const char *file, const char *func, const int line); +#define send_recv_proc(pi, msg) _send_recv_proc(pi, UNIX_WRITE_TIMEOUT, UNIX_READ_TIMEOUT, msg, __FILE__, __func__, __LINE__) char *_send_recv_ckdb(const ckpool_t *ckp, const char *msg, const char *file, const char *func, const int line); #define send_recv_ckdb(ckp, msg) _send_recv_ckdb(ckp, msg, __FILE__, __func__, __LINE__) char *_ckdb_msg_call(const ckpool_t *ckp, const char *msg, const char *file, const char *func, diff --git a/src/libckpool.c b/src/libckpool.c index a72dba24..50358ab9 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -1030,7 +1030,7 @@ int _write_length(int sockd, const void *buf, int len, const char *file, const c return ofs; } -bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *func, const int line) +bool _send_unix_msg(int sockd, const char *buf, int timeout, const char *file, const char *func, const int line) { uint32_t msglen, len; bool retval = false; @@ -1050,7 +1050,7 @@ bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *fu goto out; } msglen = htole32(len); - ret = wait_write_select(sockd, UNIX_WRITE_TIMEOUT); + ret = wait_write_select(sockd, timeout); if (unlikely(ret < 1)) { ern = errno; LOGERR("Select1 failed in send_unix_msg (%d)", ern); @@ -1061,7 +1061,7 @@ bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *fu LOGERR("Failed to write 4 byte length in send_unix_msg"); goto out; } - ret = wait_write_select(sockd, UNIX_WRITE_TIMEOUT); + ret = wait_write_select(sockd, timeout); if (unlikely(ret < 1)) { ern = errno; LOGERR("Select2 failed in send_unix_msg (%d)", ern); diff --git a/src/libckpool.h b/src/libckpool.h index 26aff16d..aed291dc 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -507,8 +507,8 @@ char *_recv_unix_msg(int sockd, int timeout1, int timeout2, const char *file, co int wait_write_select(int sockd, float timeout); #define write_length(sockd, buf, len) _write_length(sockd, buf, len, __FILE__, __func__, __LINE__) int _write_length(int sockd, const void *buf, int len, const char *file, const char *func, const int line); -bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *func, const int line); -#define send_unix_msg(sockd, buf) _send_unix_msg(sockd, buf, __FILE__, __func__, __LINE__) +bool _send_unix_msg(int sockd, const char *buf, int timeout, const char *file, const char *func, const int line); +#define send_unix_msg(sockd, buf) _send_unix_msg(sockd, buf, UNIX_WRITE_TIMEOUT, __FILE__, __func__, __LINE__) bool _send_unix_data(int sockd, const struct msghdr *msg, const char *file, const char *func, const int line); #define send_unix_data(sockd, msg) _send_unix_data(sockd, msg, __FILE__, __func__, __LINE__) bool _recv_unix_data(int sockd, struct msghdr *msg, const char *file, const char *func, const int line); From 53656e12847fc8ba34549cad13f24846878563c8 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 12 Jul 2015 15:04:41 +1000 Subject: [PATCH 10/10] Json RPC calls can take a while to return so use a custom timeout when talking to the generator from the stratifier --- src/ckpool.c | 8 ++++---- src/ckpool.h | 8 +++++--- src/stratifier.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index f7ec1fbe..3b968f7d 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -359,7 +359,7 @@ static int send_procmsg(proc_instance_t *pi, const char *buf) } sockd = open_unix_client(path); if (unlikely(sockd < 0)) { - LOGWARNING("Failed to open socket %s in send_recv_proc", path); + LOGWARNING("Failed to open socket %s in send_procmsg", path); goto out; } if (unlikely(!send_unix_msg(sockd, buf))) @@ -640,8 +640,8 @@ out: /* Send a single message to a process instance and retrieve the response, then * close the socket. */ -char *_send_recv_proc(proc_instance_t *pi, int writetimeout, int readtimedout, - const char *msg, const char *file, const char *func, const int line) +char *_send_recv_proc(proc_instance_t *pi, const char *msg, int writetimeout, int readtimedout, + const char *file, const char *func, const int line) { char *path = pi->us.path, *buf = NULL; int sockd; @@ -727,10 +727,10 @@ char *_ckdb_msg_call(const ckpool_t *ckp, const char *msg, const char *file, co json_t *json_rpc_call(connsock_t *cs, const char *rpc_req) { + float timeout = RPC_TIMEOUT; char *http_req = NULL; json_error_t err_val; json_t *val = NULL; - float timeout = 60; int len, ret; if (unlikely(cs->fd < 0)) { diff --git a/src/ckpool.h b/src/ckpool.h index 12ee8612..573ddc8f 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -19,6 +19,8 @@ #include "libckpool.h" #include "uthash.h" +#define RPC_TIMEOUT 60 + struct ckpool_instance; typedef struct ckpool_instance ckpool_t; @@ -245,9 +247,9 @@ void empty_buffer(connsock_t *cs); int read_socket_line(connsock_t *cs, float *timeout); void _send_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line); #define send_proc(pi, msg) _send_proc(pi, msg, __FILE__, __func__, __LINE__) -char *_send_recv_proc(proc_instance_t *pi, int writetimeout, int readtimedout, - const char *msg, const char *file, const char *func, const int line); -#define send_recv_proc(pi, msg) _send_recv_proc(pi, UNIX_WRITE_TIMEOUT, UNIX_READ_TIMEOUT, msg, __FILE__, __func__, __LINE__) +char *_send_recv_proc(proc_instance_t *pi, const char *msg, int writetimeout, int readtimedout, + const char *file, const char *func, const int line); +#define send_recv_proc(pi, msg) _send_recv_proc(pi, msg, UNIX_WRITE_TIMEOUT, UNIX_READ_TIMEOUT, __FILE__, __func__, __LINE__) char *_send_recv_ckdb(const ckpool_t *ckp, const char *msg, const char *file, const char *func, const int line); #define send_recv_ckdb(ckp, msg) _send_recv_ckdb(ckp, msg, __FILE__, __func__, __LINE__) char *_ckdb_msg_call(const ckpool_t *ckp, const char *msg, const char *file, const char *func, diff --git a/src/stratifier.c b/src/stratifier.c index 0d3382de..243a9df9 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -832,7 +832,7 @@ static char *__send_recv_generator(ckpool_t *ckp, const char *msg, const int pri set = true; } else set = false; - buf = send_recv_proc(ckp->generator, msg); + buf = _send_recv_proc(ckp->generator, msg, UNIX_WRITE_TIMEOUT, RPC_TIMEOUT, __FILE__, __func__, __LINE__); if (unlikely(!buf)) buf = strdup("failed"); if (set)