From 48ee5c1e8476d372fa7f63e67e0ba92ea8440d8e Mon Sep 17 00:00:00 2001 From: kanoi Date: Sat, 10 Sep 2016 22:12:35 +1000 Subject: [PATCH] ckdb - use io_msg for db *_fill counter messages --- src/ckdb.c | 15 ++------------- src/ckdb.h | 19 ++++++++++++++++++- src/ckdb_data.c | 39 ++++++++++++++++++++++++++++++++++++++ src/ckdb_dbio.c | 50 +++++++++++++++++++++---------------------------- 4 files changed, 80 insertions(+), 43 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index a357b6b9..fabae6c6 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -1055,19 +1055,8 @@ static void *iomsgs(void *consol) return NULL; } -#define io_msg(stamp, msg, errn, logfd, logerr) \ - _io_msg(stamp, msg, false, errn, logfd, false, logerr, true, true, \ - WHERE_FFL_HERE) -#define cr_msg(stamp, msg) \ - _io_msg(stamp, msg, true, 0, false, true, false, false, true, WHERE_FFL_HERE) -#define lf_msg(stamp, msg) \ - _io_msg(stamp, msg, true, 0, false, true, false, true, true, WHERE_FFL_HERE) -#define err_msg(stamp, msg, errn) \ - _io_msg(stamp, msg, true, errn, false, false, true, true, true, WHERE_FFL_HERE) - -static void _io_msg(bool stamp, char *msg, bool alloc, int errn, bool logfd, - bool logout, bool logerr, bool eol, bool flush, - WHERE_FFL_ARGS) +void _io_msg(bool stamp, char *msg, bool alloc, int errn, bool logfd, + bool logout, bool logerr, bool eol, bool flush, WHERE_FFL_ARGS) { K_ITEM *fio_item = NULL, *cio_item = NULL; bool msgused = false; diff --git a/src/ckdb.h b/src/ckdb.h index 60bb6a38..2a7b5eb2 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -58,7 +58,7 @@ #define DB_VLOCK "1" #define DB_VERSION "1.0.7" -#define CKDB_VERSION DB_VERSION"-2.452" +#define CKDB_VERSION DB_VERSION"-2.453" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -756,6 +756,20 @@ enum cmd_values { // CCLs are every ... #define ROLL_S 3600 +#define io_msg(stamp, msg, errn, logfd, logerr) \ + _io_msg(stamp, msg, false, errn, logfd, false, logerr, true, true, \ + WHERE_FFL_HERE) +#define cr_msg(stamp, msg) \ + _io_msg(stamp, msg, true, 0, false, true, false, false, true, WHERE_FFL_HERE) +#define lf_msg(stamp, msg) \ + _io_msg(stamp, msg, true, 0, false, true, false, true, true, WHERE_FFL_HERE) +#define err_msg(stamp, msg, errn) \ + _io_msg(stamp, msg, true, errn, false, false, true, true, true, WHERE_FFL_HERE) + +extern void _io_msg(bool stamp, char *msg, bool alloc, int errn, bool logfd, + bool logout, bool logerr, bool eol, bool flush, + WHERE_FFL_ARGS); + #define LOGQUE(_msg, _db) log_queue_message(_msg, _db) #define LOGFILE(_msg, _prefix) rotating_log_nolock(_msg, _prefix) #define LOGDUP "dup." @@ -3066,6 +3080,9 @@ extern void free_marks_data(K_ITEM *item); #define free_seqset_data(_item) _free_seqset_data(_item) extern void _free_seqset_data(K_ITEM *item); +#define pcom(_n, _buf, _siz) _pcom(_n, _buf, _siz, WHERE_FFL_HERE); +extern void _pcom(int n, char *buf, size_t bufsiz, WHERE_FFL_ARGS); + // Data copy functions #define COPY_DATA(_new, _old) memcpy(_new, _old, sizeof(*(_new))) diff --git a/src/ckdb_data.c b/src/ckdb_data.c index 10fbc597..06f4ca60 100644 --- a/src/ckdb_data.c +++ b/src/ckdb_data.c @@ -192,6 +192,45 @@ void _free_seqset_data(K_ITEM *item) } } +static void pcom2(int n, char **buf, size_t *siz) +{ + size_t len; + + if (*siz > 1) { + if (n < 1000) { + len = snprintf(*buf, *siz, "%d", n); + } else { + pcom2(n/1000, buf, siz); + len = snprintf(*buf, *siz, ",%03d", n % 1000); + } + if (len > 0) { + *siz -= len; + *buf += len; + } + } +} + +void _pcom(int n, char *buf, size_t bufsiz, WHERE_FFL_ARGS) +{ + size_t siz = bufsiz; + + // a random limit that should never occur + if (siz < 4) { + quithere(1, "%s() bufsiz (%d) too small" WHERE_FFL, + __func__, (int)siz, WHERE_FFL_PASS); + } + + if (n < 0) { + *(buf++) = '-'; + siz--; + n = -n; + } + + *buf = '\0'; + + pcom2(n, &buf, &siz); +} + /* Data copy functions (added here as needed) All pointers need to be initialised since DUP_POINTER will free them */ diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index 2231fbf5..b44ac45b 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -9,17 +9,6 @@ #include "ckdb.h" -// Doesn't work with negative numbers ... -void pcom(int n) -{ - if (n < 1000) - printf("%d", n); - else { - pcom(n/1000); - printf(",%03d", n % 1000); - } -} - char *pqerrmsg(PGconn *conn) { char *ptr, *buf = strdup(PQerrorMessage(conn)); @@ -3275,6 +3264,7 @@ unparam: bool workinfo_fill(PGconn *conn) { + char tickbuf[256], pcombuf[64]; char ndiffbin[TXT_SML+1]; ExecStatusType rescode; PGresult *res; @@ -3290,8 +3280,8 @@ bool workinfo_fill(PGconn *conn) LOGDEBUG("%s(): select", __func__); - printf(TICK_PREFIX"wi 0\r"); - fflush(stdout); + STRNCPY(tickbuf, TICK_PREFIX"wi 0"); + cr_msg(false, tickbuf); APPEND_REALLOC_INIT(sel, off, len); APPEND_REALLOC(sel, off, len, @@ -3459,10 +3449,10 @@ bool workinfo_fill(PGconn *conn) } if (n == 0 || ((n+1) % 100000) == 0) { - printf(TICK_PREFIX"wi "); - pcom(n+1); - putchar('\r'); - fflush(stdout); + pcom(n+1, pcombuf, sizeof(pcombuf)); + snprintf(tickbuf, sizeof(tickbuf), + TICK_PREFIX"wi %s", pcombuf); + cr_msg(false, tickbuf); } tick(); n++; @@ -4007,6 +3997,7 @@ unparam: bool shares_fill(PGconn *conn) { + char tickbuf[256], pcombuf[64]; ExecStatusType rescode; PGresult *res; K_TREE_CTX ctx[1]; @@ -4055,8 +4046,8 @@ bool shares_fill(PGconn *conn) LOGWARNING("%s(): loading from workinfoid>=%"PRId64, __func__, workinfoid); - printf(TICK_PREFIX"sh 0\r"); - fflush(stdout); + STRNCPY(tickbuf, TICK_PREFIX"sh 0"); + cr_msg(false, tickbuf); sel = "declare sh cursor for select " "workinfoid,userid,workername,clientid,enonce1,nonce2,nonce," @@ -4218,10 +4209,10 @@ bool shares_fill(PGconn *conn) k_add_head(shares_hi_store, item); if (n == 0 || ((n+1) % 100000) == 0) { - printf(TICK_PREFIX"sh "); - pcom(n+1); - putchar('\r'); - fflush(stdout); + pcom(n+1, pcombuf, sizeof(pcombuf)); + snprintf(tickbuf, sizeof(tickbuf), + TICK_PREFIX"sh %s", pcombuf); + cr_msg(false, tickbuf); } tick(); n++; @@ -8333,6 +8324,7 @@ unparam: bool markersummary_fill(PGconn *conn) { + char tickbuf[256], pcombuf[64]; ExecStatusType rescode; PGresult *res; K_ITEM *item = NULL, *p_item, *wm_item = NULL; @@ -8434,8 +8426,8 @@ bool markersummary_fill(PGconn *conn) params[0], cd, amt, what); } - printf(TICK_PREFIX"ms 0\r"); - fflush(stdout); + STRNCPY(tickbuf, TICK_PREFIX"ms 0"); + cr_msg(false, tickbuf); res = PQexec(conn, "Begin", CKPQ_READ); rescode = PQresultStatus(res); @@ -8637,10 +8629,10 @@ bool markersummary_fill(PGconn *conn) userinfo_update(NULL, NULL, row, false); if (n == 0 || ((n+1) % 100000) == 0) { - printf(TICK_PREFIX"ms "); - pcom(n+1); - putchar('\r'); - fflush(stdout); + pcom(n+1, pcombuf, sizeof(pcombuf)); + snprintf(tickbuf, sizeof(tickbuf), + TICK_PREFIX"ms %s", pcombuf); + cr_msg(false, tickbuf); } tick(); n++;