From 56adcf2cc928180e8c201c74af66b674806d1f74 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 23 Dec 2012 18:00:29 +0100 Subject: [PATCH] MINOR: tools: simplify the use of the int to ascii macros These macros (U2H, U2A, LIM2A, ...) have been used with an explicit index for the local storage variable, making it difficult to change log formats and causing a few issues from time to time. Let's have a single macro with a rotating index so that up to 10 conversions may be used in a single call. --- include/common/standard.h | 81 +++++++++++----------- src/dumpstats.c | 138 +++++++++++++++++++------------------- src/standard.c | 5 +- 3 files changed, 114 insertions(+), 110 deletions(-) diff --git a/include/common/standard.h b/include/common/standard.h index 221d8b880d..b1db8213bd 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -41,6 +41,8 @@ # define ULLONG_MAX (LLONG_MAX * 2ULL + 1) #endif +/* number of itoa_str entries */ +#define NB_ITOA_STR 10 /****** string-specific macros and functions ******/ /* if a > max, then bound to . The macro returns the new */ @@ -62,6 +64,8 @@ enum { STD_OP_GE = 4, STD_OP_LT = 5, }; +extern int itoa_idx; /* index of next itoa_str to use */ + /* * copies at most chars from to . Last char is always * set to 0, unless is 0. The number of chars copied is returned @@ -123,32 +127,6 @@ char *lltoa(long long n, char *dst, size_t size); */ char *utoa_pad(unsigned int n, char *dst, size_t size); -/* Fast macros to convert up to 10 different parameters inside a same call of - * expression. - */ -#define U2A0(n) ({ ultoa_r((n), itoa_str[0], sizeof(itoa_str[0])); }) -#define U2A1(n) ({ ultoa_r((n), itoa_str[1], sizeof(itoa_str[1])); }) -#define U2A2(n) ({ ultoa_r((n), itoa_str[2], sizeof(itoa_str[2])); }) -#define U2A3(n) ({ ultoa_r((n), itoa_str[3], sizeof(itoa_str[3])); }) -#define U2A4(n) ({ ultoa_r((n), itoa_str[4], sizeof(itoa_str[4])); }) -#define U2A5(n) ({ ultoa_r((n), itoa_str[5], sizeof(itoa_str[5])); }) -#define U2A6(n) ({ ultoa_r((n), itoa_str[6], sizeof(itoa_str[6])); }) -#define U2A7(n) ({ ultoa_r((n), itoa_str[7], sizeof(itoa_str[7])); }) -#define U2A8(n) ({ ultoa_r((n), itoa_str[8], sizeof(itoa_str[8])); }) -#define U2A9(n) ({ ultoa_r((n), itoa_str[9], sizeof(itoa_str[9])); }) - -/* The same macros provide HTML encoding of numbers */ -#define U2H0(n) ({ ulltoh_r((n), itoa_str[0], sizeof(itoa_str[0])); }) -#define U2H1(n) ({ ulltoh_r((n), itoa_str[1], sizeof(itoa_str[1])); }) -#define U2H2(n) ({ ulltoh_r((n), itoa_str[2], sizeof(itoa_str[2])); }) -#define U2H3(n) ({ ulltoh_r((n), itoa_str[3], sizeof(itoa_str[3])); }) -#define U2H4(n) ({ ulltoh_r((n), itoa_str[4], sizeof(itoa_str[4])); }) -#define U2H5(n) ({ ulltoh_r((n), itoa_str[5], sizeof(itoa_str[5])); }) -#define U2H6(n) ({ ulltoh_r((n), itoa_str[6], sizeof(itoa_str[6])); }) -#define U2H7(n) ({ ulltoh_r((n), itoa_str[7], sizeof(itoa_str[7])); }) -#define U2H8(n) ({ ulltoh_r((n), itoa_str[8], sizeof(itoa_str[8])); }) -#define U2H9(n) ({ ulltoh_r((n), itoa_str[9], sizeof(itoa_str[9])); }) - /* * This function simply returns a locally allocated string containing the ascii * representation for number 'n' in decimal, unless n is 0 in which case it @@ -159,19 +137,44 @@ char *utoa_pad(unsigned int n, char *dst, size_t size); */ extern const char *limit_r(unsigned long n, char *buffer, int size, const char *alt); -/* Fast macros to convert up to 10 different parameters inside a same call of - * expression. Warning! they share the same vectors as U2A*! - */ -#define LIM2A0(n, alt) ({ limit_r((n), itoa_str[0], sizeof(itoa_str[0]), (alt)); }) -#define LIM2A1(n, alt) ({ limit_r((n), itoa_str[1], sizeof(itoa_str[1]), (alt)); }) -#define LIM2A2(n, alt) ({ limit_r((n), itoa_str[2], sizeof(itoa_str[2]), (alt)); }) -#define LIM2A3(n, alt) ({ limit_r((n), itoa_str[3], sizeof(itoa_str[3]), (alt)); }) -#define LIM2A4(n, alt) ({ limit_r((n), itoa_str[4], sizeof(itoa_str[4]), (alt)); }) -#define LIM2A5(n, alt) ({ limit_r((n), itoa_str[5], sizeof(itoa_str[5]), (alt)); }) -#define LIM2A6(n, alt) ({ limit_r((n), itoa_str[6], sizeof(itoa_str[6]), (alt)); }) -#define LIM2A7(n, alt) ({ limit_r((n), itoa_str[7], sizeof(itoa_str[7]), (alt)); }) -#define LIM2A8(n, alt) ({ limit_r((n), itoa_str[8], sizeof(itoa_str[8]), (alt)); }) -#define LIM2A9(n, alt) ({ limit_r((n), itoa_str[9], sizeof(itoa_str[9]), (alt)); }) +/* returns a locally allocated string containing the ASCII representation of + * the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same + * function call (eg: printf), shared with the other similar functions making + * use of itoa_str[]. + */ +static inline const char *U2A(unsigned long n) +{ + const char *ret = ultoa_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0])); + if (++itoa_idx >= NB_ITOA_STR) + itoa_idx = 0; + return ret; +} + +/* returns a locally allocated string containing the HTML representation of + * the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same + * function call (eg: printf), shared with the other similar functions making + * use of itoa_str[]. + */ +static inline const char *U2H(unsigned long long n) +{ + const char *ret = ulltoh_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0])); + if (++itoa_idx >= NB_ITOA_STR) + itoa_idx = 0; + return ret; +} + +/* returns a locally allocated string containing the HTML representation of + * the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same + * function call (eg: printf), shared with the other similar functions making + * use of itoa_str[]. + */ +static inline const char *LIM2A(unsigned long n, const char *alt) +{ + const char *ret = limit_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0]), alt); + if (++itoa_idx >= NB_ITOA_STR) + itoa_idx = 0; + return ret; +} /* * Returns non-zero if character is a hex digit (0-9, a-f, A-F), else zero. diff --git a/src/dumpstats.c b/src/dumpstats.c index 526349b506..3a890f3c15 100644 --- a/src/dumpstats.c +++ b/src/dumpstats.c @@ -1839,14 +1839,14 @@ static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px) "Current connection rate:%s/s" "Current session rate:%s/s" "", - U2H0(read_freq_ctr(&px->fe_sess_per_sec)), - U2H1(read_freq_ctr(&px->fe_conn_per_sec)), - U2H2(read_freq_ctr(&px->fe_sess_per_sec))); + U2H(read_freq_ctr(&px->fe_sess_per_sec)), + U2H(read_freq_ctr(&px->fe_conn_per_sec)), + U2H(read_freq_ctr(&px->fe_sess_per_sec))); if (px->mode == PR_MODE_HTTP) chunk_appendf(&trash, "Current request rate:%s/s", - U2H3(read_freq_ctr(&px->fe_req_per_sec))); + U2H(read_freq_ctr(&px->fe_req_per_sec))); chunk_appendf(&trash, "" @@ -1855,20 +1855,20 @@ static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px) "Max connection rate:%s/s" "Max session rate:%s/s" "", - U2H0(px->fe_counters.sps_max), - U2H1(px->fe_counters.cps_max), - U2H2(px->fe_counters.sps_max)); + U2H(px->fe_counters.sps_max), + U2H(px->fe_counters.cps_max), + U2H(px->fe_counters.sps_max)); if (px->mode == PR_MODE_HTTP) chunk_appendf(&trash, "Max request rate:%s/s", - U2H3(px->fe_counters.p.http.rps_max)); + U2H(px->fe_counters.p.http.rps_max)); chunk_appendf(&trash, "" /* sessions rate : limit */ "%s", - LIM2A4(px->fe_sps_lim, "-")); + LIM2A(px->fe_sps_lim, "-")); chunk_appendf(&trash, /* sessions: current, max, limit, total */ @@ -1877,10 +1877,10 @@ static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px) "Cum. connections:%s" "Cum. sessions:%s" "", - U2H0(px->feconn), U2H1(px->fe_counters.conn_max), U2H2(px->maxconn), - U2H3(px->fe_counters.cum_sess), - U2H4(px->fe_counters.cum_conn), - U2H5(px->fe_counters.cum_sess)); + U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn), + U2H(px->fe_counters.cum_sess), + U2H(px->fe_counters.cum_conn), + U2H(px->fe_counters.cum_sess)); /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */ if (px->mode == PR_MODE_HTTP) { @@ -1895,17 +1895,17 @@ static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px) "- other responses:%s" "Intercepted requests:%s" "", - U2H0(px->fe_counters.p.http.cum_req), - U2H1(px->fe_counters.p.http.rsp[1]), - U2H2(px->fe_counters.p.http.rsp[2]), - U2H3(px->fe_counters.p.http.comp_rsp), + U2H(px->fe_counters.p.http.cum_req), + U2H(px->fe_counters.p.http.rsp[1]), + U2H(px->fe_counters.p.http.rsp[2]), + U2H(px->fe_counters.p.http.comp_rsp), px->fe_counters.p.http.rsp[2] ? (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0, - U2H4(px->fe_counters.p.http.rsp[3]), - U2H5(px->fe_counters.p.http.rsp[4]), - U2H6(px->fe_counters.p.http.rsp[5]), - U2H7(px->fe_counters.p.http.rsp[0]), - U2H8(px->fe_counters.intercepted_req)); + U2H(px->fe_counters.p.http.rsp[3]), + U2H(px->fe_counters.p.http.rsp[4]), + U2H(px->fe_counters.p.http.rsp[5]), + U2H(px->fe_counters.p.http.rsp[0]), + U2H(px->fe_counters.intercepted_req)); } chunk_appendf(&trash, @@ -1915,13 +1915,13 @@ static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px) /* bytes : in */ "%s" "", - U2H7(px->fe_counters.bytes_in)); + U2H(px->fe_counters.bytes_in)); chunk_appendf(&trash, /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */ "%s%s
compression: in=%lld out=%lld bypassed=%lld savings=%d%%
%s", (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "":"", - U2H0(px->fe_counters.bytes_out), + U2H(px->fe_counters.bytes_out), px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp, px->fe_counters.comp_in ? (int)((px->fe_counters.comp_in - px->fe_counters.comp_out)*100/px->fe_counters.comp_in) : 0, @@ -1939,8 +1939,8 @@ static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px) /* rest of server: nothing */ "" "", - U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp), - U2H2(px->fe_counters.failed_req), + U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp), + U2H(px->fe_counters.failed_req), px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP"); } @@ -2073,8 +2073,8 @@ static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, st "%s%s" "", (flags & ST_SHLGNDS)?"":"", - U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn), - U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out)); + U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn), + U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out)); chunk_appendf(&trash, /* denied: req, resp */ @@ -2088,8 +2088,8 @@ static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, st /* rest of server: nothing */ "" "", - U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp), - U2H2(l->counters->failed_req), + U2H(l->counters->denied_req), U2H(l->counters->denied_resp), + U2H(l->counters->failed_req), (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL"); } else { /* CSV mode */ @@ -2226,8 +2226,8 @@ static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, in "%s%s" "", (flags & ST_SHLGNDS) ? "" : "", - U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"), - U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max)); + U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"), + U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max)); chunk_appendf(&trash, @@ -2236,9 +2236,9 @@ static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, in "%s
" "" "", - U2H0(sv->cur_sess), U2H1(sv->counters.cur_sess_max), LIM2A2(sv->maxconn, "-"), - U2H3(sv->counters.cum_sess), - U2H4(sv->counters.cum_sess)); + U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"), + U2H(sv->counters.cum_sess), + U2H(sv->counters.cum_sess)); /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */ if (px->mode == PR_MODE_HTTP) { @@ -2255,20 +2255,20 @@ static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, in "" "" "", - U2H0(tot), - U2H1(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0, - U2H2(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0, - U2H3(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0, - U2H4(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0, - U2H5(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0, - U2H6(sv->counters.p.http.rsp[0]), tot ? (int)(100*sv->counters.p.http.rsp[0] / tot) : 0); + U2H(tot), + U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0, + U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0, + U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0, + U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0, + U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0, + U2H(sv->counters.p.http.rsp[0]), tot ? (int)(100*sv->counters.p.http.rsp[0] / tot) : 0); } chunk_appendf(&trash, "
Cum. sessions:%s
- HTTP 5xx responses:%s(%d%%)
- other responses:%s(%d%%)
" /* sessions: lbtot */ "%s", - U2H1(sv->counters.cum_lbconn)); + U2H(sv->counters.cum_lbconn)); chunk_appendf(&trash, /* bytes : in, out */ @@ -2282,10 +2282,10 @@ static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, in /* warnings: retries, redispatches */ "%lld%lld" "", - U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out), - U2H2(sv->counters.failed_secu), - U2H3(sv->counters.failed_conns), - U2H6(sv->counters.failed_resp), + U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out), + U2H(sv->counters.failed_secu), + U2H(sv->counters.failed_conns), + U2H(sv->counters.failed_resp), sv->counters.cli_aborts, sv->counters.srv_aborts, sv->counters.retries, sv->counters.redispatches); @@ -2403,7 +2403,7 @@ static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, in "", px->id, sv->id, sv->nbpend, sv->counters.nbpend_max, - sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess, + sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess, sv->counters.bytes_in, sv->counters.bytes_out, sv->counters.failed_secu, sv->counters.failed_conns, sv->counters.failed_resp, @@ -2441,7 +2441,7 @@ static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, in chunk_appendf(&trash, "%s," "%d,%d,%d,", - LIM2A0(sv->maxqueue, ""), + LIM2A(sv->maxqueue, ""), relative_pid, px->uuid, sv->puid); /* throttle */ @@ -2570,8 +2570,8 @@ static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, in "%s%s" "", (flags & ST_SHLGNDS)?"":"", - U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max), - U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max)); + U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max), + U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max)); chunk_appendf(&trash, /* sessions: current, max, limit, total */ @@ -2579,9 +2579,9 @@ static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, in "%s
" "" "", - U2H0(px->beconn), U2H1(px->be_counters.conn_max), U2H2(px->fullconn), - U2H3(px->be_counters.cum_conn), - U2H4(px->be_counters.cum_conn)); + U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn), + U2H(px->be_counters.cum_conn), + U2H(px->be_counters.cum_conn)); /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */ if (px->mode == PR_MODE_HTTP) { @@ -2596,17 +2596,17 @@ static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, in "" "" "", - U2H0(px->be_counters.p.http.cum_req), - U2H1(px->be_counters.p.http.rsp[1]), - U2H2(px->be_counters.p.http.rsp[2]), - U2H3(px->be_counters.p.http.comp_rsp), + U2H(px->be_counters.p.http.cum_req), + U2H(px->be_counters.p.http.rsp[1]), + U2H(px->be_counters.p.http.rsp[2]), + U2H(px->be_counters.p.http.comp_rsp), px->be_counters.p.http.rsp[2] ? (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0, - U2H4(px->be_counters.p.http.rsp[3]), - U2H5(px->be_counters.p.http.rsp[4]), - U2H6(px->be_counters.p.http.rsp[5]), - U2H7(px->be_counters.p.http.rsp[0]), - U2H8(px->be_counters.intercepted_req)); + U2H(px->be_counters.p.http.rsp[3]), + U2H(px->be_counters.p.http.rsp[4]), + U2H(px->be_counters.p.http.rsp[5]), + U2H(px->be_counters.p.http.rsp[0]), + U2H(px->be_counters.intercepted_req)); } chunk_appendf(&trash, @@ -2616,14 +2616,14 @@ static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, in /* bytes: in */ "" "", - U2H7(px->be_counters.cum_lbconn), - U2H8(px->be_counters.bytes_in)); + U2H(px->be_counters.cum_lbconn), + U2H(px->be_counters.bytes_in)); chunk_appendf(&trash, /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */ "", (px->be_counters.comp_in || px->be_counters.comp_byp) ? "":"", - U2H0(px->be_counters.bytes_out), + U2H(px->be_counters.bytes_out), px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp, px->be_counters.comp_in ? (int)((px->be_counters.comp_in - px->be_counters.comp_out)*100/px->be_counters.comp_in) : 0, @@ -2645,9 +2645,9 @@ static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, in "" "" "", - U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp), - U2H2(px->be_counters.failed_conns), - U2H5(px->be_counters.failed_resp), + U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp), + U2H(px->be_counters.failed_conns), + U2H(px->be_counters.failed_resp), px->be_counters.cli_aborts, px->be_counters.srv_aborts, px->be_counters.retries, px->be_counters.redispatches, diff --git a/src/standard.c b/src/standard.c index 10c25a3029..13ec4ad999 100644 --- a/src/standard.c +++ b/src/standard.c @@ -25,7 +25,7 @@ #include #include -/* enough to store 10 integers of : +/* enough to store NB_ITOA_STR integers of : * 2^64-1 = 18446744073709551615 or * -2^63 = -9223372036854775808 * @@ -33,7 +33,8 @@ * '' around digits at positions 3N+1 in order * to add spacing at up to 6 positions : 18 446 744 073 709 551 615 */ -char itoa_str[10][171]; +char itoa_str[NB_ITOA_STR][171]; +int itoa_idx = 0; /* index of next itoa_str to use */ /* * unsigned long long ASCII representation -- 2.47.3
Cum. sessions:%s
- other responses:%s
Intercepted requests:%s
%s%s%s
compression: in=%lld out=%lld bypassed=%lld savings=%d%%
%s
%s %s %d%d%d