Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor metric print (#134) #141

Merged
merged 3 commits into from
Feb 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/protocol/admin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(SOURCE
format.c
compose.c
parse.c
request.c
Expand Down
1 change: 1 addition & 0 deletions src/protocol/admin/admin_include.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "compose.h"
#include "format.h"
#include "parse.h"
#include "process.h"
#include "request.h"
Expand Down
18 changes: 18 additions & 0 deletions src/protocol/admin/format.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "format.h"

#include <cc_print.h>

size_t
print_stats(char *buf, size_t cap, struct metric *metrics, unsigned int nmetric)
{
size_t offset = 0;

/* TODO: report error if running out of space in buf */
for (int i = 0; i < nmetric; ++i) {
offset += metric_print(buf + offset, cap - offset, METRIC_PRINT_FMT,
&metrics[i]);
}
offset += cc_scnprintf(buf + offset, cap - offset, METRIC_END);

return offset;
}
14 changes: 14 additions & 0 deletions src/protocol/admin/format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <cc_metric.h>

#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_END "END\r\n"
#define METRIC_END_LEN (sizeof(METRIC_END) - 1)

#define VERSION_PRINTED "VERSION " VERSION_STRING "\r\n"

size_t print_stats(char *buf, size_t cap, struct metric *metrics, unsigned int nmetric);
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
57 changes: 10 additions & 47 deletions src/server/slimcache/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 SLIMCACHE_ADMIN_MODULE_NAME "slimcache::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", SLIMCACHE_ADMIN_MODULE_NAME);
if (admin_init) {
log_warn("%s has already been setup, overwrite",
SLIMCACHE_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", SLIMCACHE_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/slimcache/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/slimcache/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ setup(void)
klog_setup(&setting.klog, &stats.klog);
cuckoo_setup(&setting.cuckoo, &stats.cuckoo);
process_setup(&setting.process, &stats.process);
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/slimcache/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
struct stats stats = {
{ PROCINFO_METRIC(METRIC_INIT) },
{ PROCESS_METRIC(METRIC_INIT) },
{ ADMIN_PROCESS_METRIC(METRIC_INIT) },
{ PARSE_REQ_METRIC(METRIC_INIT) },
{ COMPOSE_RSP_METRIC(METRIC_INIT) },
{ KLOG_METRIC(METRIC_INIT) },
Expand Down
2 changes: 0 additions & 2 deletions src/server/slimcache/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 @@ -19,7 +18,6 @@ struct stats {
procinfo_metrics_st procinfo;
/* application modules */
process_metrics_st process;
admin_process_metrics_st admin_process;
parse_req_metrics_st parse_req;
compose_rsp_metrics_st compose_rsp;
klog_metrics_st klog;
Expand Down
Loading