From 941cba9b3bf31b23351281e040fc1b992dfeeb6d Mon Sep 17 00:00:00 2001 From: kanoi Date: Thu, 20 Nov 2014 10:15:33 +1100 Subject: [PATCH] ckdb - convert sharesummary workername to a ptr - reduce ram ... lots --- src/ckdb.c | 9 +++++++-- src/ckdb.h | 37 ++++++++++++++++++++++--------------- src/ckdb_cmd.c | 2 +- src/ckdb_data.c | 11 +++++------ src/ckdb_dbio.c | 20 +++++++++++++++----- 5 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index 11cead07..ffede0a4 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -1057,6 +1057,11 @@ static void free_sharesummary_data(K_ITEM *item) SHARESUMMARY *sharesummary; DATA_SHARESUMMARY(sharesummary, item); + if (sharesummary->workername) { + LIST_MEM_SUB(sharesummary_free, sharesummary->workername); + free(sharesummary->workername); + sharesummary->workername = NULL; + } SET_CREATEBY(sharesummary_free, sharesummary->createby, EMPTY); SET_CREATECODE(sharesummary_free, sharesummary->createcode, EMPTY); SET_CREATEINET(sharesummary_free, sharesummary->createinet, EMPTY); @@ -1595,7 +1600,7 @@ static void summarise_blocks() // Add up the sharesummaries, abort if any SUMMARY_NEW looksharesummary.workinfoid = wi_finish; looksharesummary.userid = MAXID; - looksharesummary.workername[0] = '\0'; + looksharesummary.workername = EMPTY; INIT_SHARESUMMARY(&ss_look); ss_look.data = (void *)(&looksharesummary); @@ -2872,7 +2877,7 @@ static void compare_summaries(K_TREE *leftsum, char *leftname, looksharesummary.workinfoid = confirm_first_workinfoid; looksharesummary.userid = -1; - looksharesummary.workername[0] = '\0'; + looksharesummary.workername = EMPTY; INIT_SHARESUMMARY(&look); look.data = (void *)(&looksharesummary); diff --git a/src/ckdb.h b/src/ckdb.h index b81f98c8..bb269752 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -52,7 +52,7 @@ #define DB_VLOCK "1" #define DB_VERSION "0.9.5" -#define CKDB_VERSION DB_VERSION"-0.647" +#define CKDB_VERSION DB_VERSION"-0.648" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -347,15 +347,26 @@ enum cmd_values { // The size strdup will allocate multiples of #define MEMBASE 4 +#define LIST_MEM_ADD(_list, _fld) do { \ + size_t __siz; \ + __siz = strlen(_fld) + 1; \ + if (__siz % MEMBASE) \ + __siz += MEMBASE - (__siz % MEMBASE); \ + _list->ram += (int)__siz; \ + } while (0) + +#define LIST_MEM_SUB(_list, _fld) do { \ + size_t __siz; \ + __siz = strlen(_fld) + 1; \ + if (__siz % MEMBASE) \ + __siz += MEMBASE - (__siz % MEMBASE); \ + _list->ram -= (int)__siz; \ + } while (0) + #define SET_POINTER(_list, _fld, _val, _def) do { \ - size_t _siz; \ if ((_fld) && ((_fld) != EMPTY) && ((_fld) != (_def))) { \ - if (_list) { \ - _siz = strlen(_fld) + 1; \ - if (_siz % MEMBASE) \ - _siz += MEMBASE - (_siz % MEMBASE); \ - _list->ram -= (int)_siz; \ - } \ + if (_list) \ + LIST_MEM_SUB(_list, _fld); \ free(_fld); \ } \ if (!(_val) || !(*(_val))) \ @@ -364,12 +375,8 @@ enum cmd_values { if (((_val) == (_def)) || (strcmp(_val, _def) == 0)) \ (_fld) = (_def); \ else { \ - if (_list) { \ - _siz = strlen(_val) + 1; \ - if (_siz % MEMBASE) \ - _siz += MEMBASE - (_siz % MEMBASE); \ - _list->ram += (int)_siz; \ - } \ + if (_list) \ + LIST_MEM_ADD(_list, _val); \ _fld = strdup(_val); \ if (!(_fld)) \ quithere(1, "malloc OOM"); \ @@ -936,7 +943,7 @@ extern K_STORE *shareerrors_store; // SHARESUMMARY typedef struct sharesummary { int64_t userid; - char workername[TXT_BIG+1]; + char *workername; int64_t workinfoid; double diffacc; double diffsta; diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index c331eca2..e51db263 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -3076,7 +3076,7 @@ static char *cmd_pplns(__maybe_unused PGconn *conn, char *cmd, char *id, looksharesummary.workinfoid = workinfoid; looksharesummary.userid = MAXID; - looksharesummary.workername[0] = '\0'; + looksharesummary.workername = EMPTY; INIT_SHARESUMMARY(&ss_look); ss_look.data = (void *)(&looksharesummary); K_RLOCK(sharesummary_free); diff --git a/src/ckdb_data.c b/src/ckdb_data.c index b799648d..e176acfe 100644 --- a/src/ckdb_data.c +++ b/src/ckdb_data.c @@ -710,7 +710,7 @@ void workerstatus_ready() // The last one looksharesummary.userid = workerstatus->userid; - STRNCPY(looksharesummary.workername, workerstatus->workername); + looksharesummary.workername = workerstatus->workername; looksharesummary.workinfoid = MAXID; ss_look.data = (void *)(&looksharesummary); K_RLOCK(sharesummary_free); @@ -1376,7 +1376,7 @@ bool workinfo_age(PGconn *conn, int64_t workinfoid, char *poolinstance, // Find the first matching sharesummary looksharesummary.workinfoid = workinfoid; looksharesummary.userid = -1; - looksharesummary.workername[0] = '\0'; + looksharesummary.workername = EMPTY; ok = true; ss_tot = ss_already = ss_failed = shares_tot = shares_dumped = 0; @@ -1607,7 +1607,7 @@ K_ITEM *find_sharesummary(int64_t userid, char *workername, int64_t workinfoid) K_ITEM look; sharesummary.userid = userid; - STRNCPY(sharesummary.workername, workername); + sharesummary.workername = workername; sharesummary.workinfoid = workinfoid; INIT_SHARESUMMARY(&look); @@ -1643,7 +1643,7 @@ void auto_age_older(PGconn *conn, int64_t workinfoid, char *poolinstance, // Find the oldest 'unaged' sharesummary < workinfoid and >= prev_found looksharesummary.workinfoid = prev_found; looksharesummary.userid = -1; - looksharesummary.workername[0] = '\0'; + looksharesummary.workername = EMPTY; INIT_SHARESUMMARY(&look); look.data = (void *)(&looksharesummary); @@ -1956,8 +1956,7 @@ void set_block_share_counters() if (sharesummary->workinfoid <= pool.workinfoid) { // Skip back to the next worker looksharesummary.userid = sharesummary->userid; - STRNCPY(looksharesummary.workername, - sharesummary->workername); + looksharesummary.workername = sharesummary->workername; looksharesummary.workinfoid = -1; ss_look.data = (void *)(&looksharesummary); ss_item = find_before_in_ktree(sharesummary_root, &ss_look, diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index c8d1d864..f4e4681a 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -2719,7 +2719,8 @@ bool _sharesummary_update(PGconn *conn, SHARES *s_row, SHAREERRORS *e_row, K_ITE K_WUNLOCK(sharesummary_free); DATA_SHARESUMMARY(row, item); row->userid = userid; - STRNCPY(row->workername, workername); + row->workername = strdup(workername); + LIST_MEM_ADD(sharesummary_free, row->workername); row->workinfoid = workinfoid; zero_sharesummary(row, sharecreatedate, diff); row->inserted = false; @@ -2948,15 +2949,15 @@ late: PQfinish(conn); // We keep the new item no matter what 'ok' is, since it will be inserted later - K_WLOCK(sharesummary_free); if (new) { + K_WLOCK(sharesummary_free); sharesummary_root = add_to_ktree(sharesummary_root, item, cmp_sharesummary); sharesummary_workinfoid_root = add_to_ktree(sharesummary_workinfoid_root, item, cmp_sharesummary_workinfoid); k_add_head(sharesummary_store, item); + K_WUNLOCK(sharesummary_free); } - K_WUNLOCK(sharesummary_free); return ok; } @@ -3010,6 +3011,7 @@ bool sharesummary_fill(PGconn *conn) for (i = 0; i < n; i++) { item = k_unlink_head(sharesummary_free); DATA_SHARESUMMARY(row, item); + row->workername = NULL; if (everyone_die) { ok = false; @@ -3026,7 +3028,8 @@ bool sharesummary_fill(PGconn *conn) PQ_GET_FLD(res, i, "workername", field, ok); if (!ok) break; - TXT_TO_STR("workername", field, row->workername); + row->workername = strdup(field); + LIST_MEM_ADD(sharesummary_free, row->workername); PQ_GET_FLD(res, i, "workinfoid", field, ok); if (!ok) @@ -3153,8 +3156,15 @@ bool sharesummary_fill(PGconn *conn) tick(); } - if (!ok) + if (!ok) { + DATA_SHARESUMMARY(row, item); + if (row->workername) { + LIST_MEM_SUB(sharesummary_free, row->workername); + free(row->workername); + row->workername = NULL; + } k_add_head(sharesummary_free, item); + } PQclear(res);