From 5f1f3adcb6e114bf64134e696d142ec1f7e2ea1a Mon Sep 17 00:00:00 2001 From: Yao Yue Date: Sat, 25 Feb 2017 13:28:33 -0800 Subject: [PATCH] update pingserver --- src/server/pingserver/admin/process.c | 57 +++++---------------------- src/server/pingserver/admin/process.h | 14 +------ src/server/pingserver/main.c | 2 +- src/server/pingserver/stats.c | 1 - src/server/pingserver/stats.h | 2 - 5 files changed, 12 insertions(+), 64 deletions(-) diff --git a/src/server/pingserver/admin/process.c b/src/server/pingserver/admin/process.c index 547e6e09e..ff1dbba51 100644 --- a/src/server/pingserver/admin/process.c +++ b/src/server/pingserver/admin/process.c @@ -4,32 +4,18 @@ #include "util/procinfo.h" #include -#include #define PINGSERVER_ADMIN_MODULE_NAME "pingserver::admin" -#define METRIC_PRINT_FMT "STAT %s %s\r\n" -#define METRIC_PRINT_LEN 64 /* > 5("STAT ") + 32 (name) + 20 (value) + CRLF */ -#define METRIC_DESCRIBE_FMT "%33s %15s %s\r\n" -#define METRIC_DESCRIBE_LEN 120 /* 34 (name) + 16 (type) + 68 (description) + CRLF */ -#define METRIC_FOOTER CRLF -#define METRIC_END "END\r\n" -#define METRIC_END_LEN (sizeof(METRIC_END) - 1) - -#define VERSION_PRINT_FMT "VERSION %s\r\n" -#define VERSION_PRINT_LEN 30 - extern struct stats stats; extern unsigned int nmetric; static bool admin_init = false; -static admin_process_metrics_st *admin_metrics = NULL; -static char *stats_buf = NULL; -static char version_buf[VERSION_PRINT_LEN]; -static size_t stats_len; +static char *buf = NULL; +static size_t cap; void -admin_process_setup(admin_process_metrics_st *metrics) +admin_process_setup(void) { log_info("set up the %s module", PINGSERVER_ADMIN_MODULE_NAME); if (admin_init) { @@ -37,10 +23,8 @@ admin_process_setup(admin_process_metrics_st *metrics) PINGSERVER_ADMIN_MODULE_NAME); } - admin_metrics = metrics; - - stats_len = METRIC_PRINT_LEN * nmetric; - stats_buf = cc_alloc(stats_len + METRIC_END_LEN); + cap = METRIC_PRINT_LEN * nmetric + METRIC_END_LEN; + buf = cc_alloc(cap); /* TODO: check return status of cc_alloc */ admin_init = true; @@ -54,49 +38,28 @@ admin_process_teardown(void) log_warn("%s has never been setup", PINGSERVER_ADMIN_MODULE_NAME); } - admin_metrics = NULL; admin_init = false; } static void _admin_stats(struct response *rsp, struct request *req) { - size_t offset = 0; - struct metric *metrics = (struct metric *)&stats; - - INCR(admin_metrics, stats); - procinfo_update(); - for (int i = 0; i < nmetric; ++i) { - offset += metric_print(stats_buf + offset, stats_len - offset, - METRIC_PRINT_FMT, &metrics[i]); - } - strcpy(stats_buf + offset, METRIC_END); - - rsp->type = RSP_GENERIC; - rsp->data.data = stats_buf; - rsp->data.len = offset + METRIC_END_LEN; -} - -static void -_admin_version(struct response *rsp, struct request *req) -{ - INCR(admin_metrics, version); - - rsp->type = RSP_GENERIC; - cc_snprintf(version_buf, VERSION_PRINT_LEN, VERSION_PRINT_FMT, VERSION_STRING); - rsp->data = str2bstr(version_buf); + rsp->data.data = buf; + rsp->data.len = print_stats(buf, cap, (struct metric *)&stats, nmetric); } void admin_process_request(struct response *rsp, struct request *req) { + rsp->type = RSP_GENERIC; + switch (req->type) { case REQ_STATS: _admin_stats(rsp, req); break; case REQ_VERSION: - _admin_version(rsp, req); + rsp->data = str2bstr(VERSION_PRINTED); break; default: rsp->type = RSP_INVALID; diff --git a/src/server/pingserver/admin/process.h b/src/server/pingserver/admin/process.h index a55409919..361230004 100644 --- a/src/server/pingserver/admin/process.h +++ b/src/server/pingserver/admin/process.h @@ -1,16 +1,4 @@ #pragma once -#include - -/* name type description */ -#define ADMIN_PROCESS_METRIC(ACTION) \ - ACTION( stats, METRIC_COUNTER, "# stats requests" )\ - ACTION( stats_ex, METRIC_COUNTER, "# stats errors" )\ - ACTION( version, METRIC_COUNTER, "# version requests" ) - -typedef struct { - ADMIN_PROCESS_METRIC(METRIC_DECLARE) -} admin_process_metrics_st; - -void admin_process_setup(admin_process_metrics_st *metrics); +void admin_process_setup(void); void admin_process_teardown(void); diff --git a/src/server/pingserver/main.c b/src/server/pingserver/main.c index 46b8b4208..cde8b254b 100644 --- a/src/server/pingserver/main.c +++ b/src/server/pingserver/main.c @@ -112,7 +112,7 @@ setup(void) procinfo_setup(&stats.procinfo); parse_setup(&stats.parse_req, NULL); compose_setup(NULL, &stats.compose_rsp); - admin_process_setup(&stats.admin_process); + admin_process_setup(); core_setup(&setting.admin, &setting.server, &setting.worker, &stats.server, &stats.worker); diff --git a/src/server/pingserver/stats.c b/src/server/pingserver/stats.c index 0262e805e..89a9ce44e 100644 --- a/src/server/pingserver/stats.c +++ b/src/server/pingserver/stats.c @@ -2,7 +2,6 @@ struct stats stats = { { PROCINFO_METRIC(METRIC_INIT) }, - { ADMIN_PROCESS_METRIC(METRIC_INIT) }, { PARSE_REQ_METRIC(METRIC_INIT) }, { COMPOSE_RSP_METRIC(METRIC_INIT) }, { CORE_SERVER_METRIC(METRIC_INIT) }, diff --git a/src/server/pingserver/stats.h b/src/server/pingserver/stats.h index 5073bf1c7..644139256 100644 --- a/src/server/pingserver/stats.h +++ b/src/server/pingserver/stats.h @@ -1,6 +1,5 @@ #pragma once -#include "admin/process.h" #include "data/process.h" #include "core/core.h" @@ -18,7 +17,6 @@ struct stats { /* perf info */ procinfo_metrics_st procinfo; /* application modules */ - admin_process_metrics_st admin_process; parse_req_metrics_st parse_req; compose_rsp_metrics_st compose_rsp; server_metrics_st server;