Skip to content

Commit

Permalink
update pingserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Yao Yue committed Feb 26, 2017
1 parent 722002f commit 5f1f3ad
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 64 deletions.
57 changes: 10 additions & 47 deletions src/server/pingserver/admin/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,27 @@
#include "util/procinfo.h"

#include <cc_mm.h>
#include <cc_print.h>

#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) {
log_warn("%s has already been setup, overwrite",
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;
Expand All @@ -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;
Expand Down
14 changes: 1 addition & 13 deletions src/server/pingserver/admin/process.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
#pragma once

#include <cc_metric.h>

/* 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);
2 changes: 1 addition & 1 deletion src/server/pingserver/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion src/server/pingserver/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down
2 changes: 0 additions & 2 deletions src/server/pingserver/stats.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "admin/process.h"
#include "data/process.h"

#include "core/core.h"
Expand All @@ -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;
Expand Down

0 comments on commit 5f1f3ad

Please sign in to comment.