From 4d55315487fb1342ef4bd0fcf3d37d24e2b25b6e Mon Sep 17 00:00:00 2001 From: Thomas Porzelt Date: Thu, 14 Aug 2008 01:05:15 +0200 Subject: [PATCH 1/2] added first counters --- src/actions.c | 5 ++++- src/connection.c | 1 + src/plugin_core.c | 31 +++++++++++++++++++++++++++++++ src/server.c | 2 ++ src/server.h | 15 +++++++++++++++ src/utils.c | 19 +++++++++++++++++++ src/utils.h | 3 +++ 7 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/actions.c b/src/actions.c index f0564cb..3ef0953 100644 --- a/src/actions.c +++ b/src/actions.c @@ -20,7 +20,7 @@ void action_release(server *srv, action *a) { case ACTION_TSETTING: release_option(srv, &a->value.setting); break; - case ACTION_TFUNCTION: + case ACTION_TFUNCTION: if (a->value.function.free) { a->value.function.free(srv, a->value.function.param); } @@ -155,6 +155,9 @@ action_result action_execute(server *srv, connection *con) { action_stack_pop(srv, as); continue; } + + srv->stats.actions_executed++; + switch (a->type) { case ACTION_TSETTING: con->options[a->value.setting.ndx] = a->value.setting.value; diff --git a/src/connection.c b/src/connection.c index 252eba7..3cdb25a 100644 --- a/src/connection.c +++ b/src/connection.c @@ -299,6 +299,7 @@ void connection_state_machine(server *srv, connection *con) { } connection_set_state(srv, con, CON_STATE_HANDLE_REQUEST_HEADER); request_validate_header(srv, con); + srv->stats.requests++; break; case CON_STATE_HANDLE_REQUEST_HEADER: diff --git a/src/plugin_core.c b/src/plugin_core.c index 8d2a8df..5414caa 100644 --- a/src/plugin_core.c +++ b/src/plugin_core.c @@ -1,6 +1,7 @@ #include "base.h" #include "plugin_core.h" +#include "utils.h" static action* core_list(server *srv, plugin* p, option *opt) { action *a; @@ -176,6 +177,10 @@ static action_result core_handle_test(server *srv, connection *con, gpointer par GHashTableIter iter; gpointer k, v; GList *hv; + GString *str; + guint64 uptime; + guint64 avg1, avg2, avg3; + gchar suffix1[2] = {0,0}, suffix2[2] = {0,0}, suffix3[2] = {0,0}; UNUSED(param); if (con->state != CON_STATE_HANDLE_REQUEST_HEADER) return ACTION_GO_ON; @@ -187,6 +192,32 @@ static action_result core_handle_test(server *srv, connection *con, gpointer par chunkqueue_append_mem(con->out, GSTR_LEN(con->request.uri.path)); chunkqueue_append_mem(con->out, CONST_STR_LEN("\r\nquery: ")); chunkqueue_append_mem(con->out, GSTR_LEN(con->request.uri.query)); + + chunkqueue_append_mem(con->out, CONST_STR_LEN("\r\n\r\nactions executed: ")); + uptime = (guint64)(ev_now(srv->loop) - srv->started); + if (uptime == 0) + uptime = 1; + avg1 = srv->stats.actions_executed; + suffix1[0] = counter_format(&avg1, 1000); + avg2 = srv->stats.actions_executed / uptime; + suffix2[0] = counter_format(&avg2, 1000); + avg3 = srv->stats.actions_executed / srv->stats.requests; + suffix3[0] = counter_format(&avg3, 1000); + str = g_string_sized_new(0); + g_string_printf(str, + "%"G_GUINT64_FORMAT"%s (%"G_GUINT64_FORMAT"%s/s, %"G_GUINT64_FORMAT"%s/req)", + avg1, suffix1, avg2, suffix2, avg3, suffix3 + ); + chunkqueue_append_string(con->out, str); + chunkqueue_append_mem(con->out, CONST_STR_LEN("\r\nrequests: ")); + avg1 = srv->stats.requests; + suffix1[0] = counter_format(&avg1, 1000); + avg2 = srv->stats.requests / uptime; + suffix2[0] = counter_format(&avg2, 1000); + str = g_string_sized_new(0); + g_string_printf(str, "%"G_GUINT64_FORMAT"%s (%"G_GUINT64_FORMAT"%s/s)", avg1, suffix1, avg2, suffix2); + chunkqueue_append_string(con->out, str); + chunkqueue_append_mem(con->out, CONST_STR_LEN("\r\n\r\n--- headers ---\r\n")); g_hash_table_iter_init(&iter, con->request.headers->table); while (g_hash_table_iter_next(&iter, &k, &v)) { diff --git a/src/server.c b/src/server.c index 6934161..250591f 100644 --- a/src/server.c +++ b/src/server.c @@ -297,6 +297,8 @@ void server_start(server *srv) { ev_io_start(srv->loop, &sock->watcher); } + srv->started = ev_now(srv->loop); + log_thread_start(srv); ev_loop(srv->loop, 0); diff --git a/src/server.h b/src/server.h index dda6c6f..9b4c41b 100644 --- a/src/server.h +++ b/src/server.h @@ -21,6 +21,18 @@ struct server_socket { ev_io watcher; }; +struct statistics_t; +typedef struct statistics_t statistics_t; + +struct statistics_t { + guint64 bytes_out; /** bytes transfered, outgoing */ + guint64 bytes_int; /** bytes transfered, incoming */ + + guint64 requests; /** processed requests */ + + guint64 actions_executed; /** actions executed */ +}; + struct server { guint32 magic; /** server magic version, check against LIGHTTPD_SERVER_MAGIC in plugins */ server_state state; @@ -60,6 +72,9 @@ struct server { GAsyncQueue *log_queue; GThread *log_thread; GMutex *log_mutex; /* manage access for the logs hashtable */ + + ev_tstamp started; + statistics_t stats; }; diff --git a/src/utils.c b/src/utils.c index d68b1c0..4ab8e31 100644 --- a/src/utils.c +++ b/src/utils.c @@ -236,3 +236,22 @@ gchar *http_status_string(guint status_code) { default: return "unknown status"; } } + + +gchar counter_format(guint64 *count, guint factor) { + gchar suffix = 0; + + if (*count > factor) { *count /= factor; suffix = 'k'; + if (*count > factor) { *count /= factor; suffix = 'm'; + if (*count > factor) { *count /= factor; suffix = 'g'; + if (*count > factor) { *count /= factor; suffix = 't'; + if (*count > factor) { *count /= factor; suffix = 'p'; + if (*count > factor) { *count /= factor; suffix = 'e'; } + } + } + } + } + } + + return suffix; +} diff --git a/src/utils.h b/src/utils.h index d86cb2c..38093c9 100644 --- a/src/utils.h +++ b/src/utils.h @@ -19,4 +19,7 @@ LI_API void path_simplify(GString *path); LI_API gchar *http_status_string(guint status_code); +/* */ +LI_API gchar counter_format(guint64 *count, guint factor); + #endif From 1ab2d0d6b360ce0250c3a95c6b3c444555a3d0c8 Mon Sep 17 00:00:00 2001 From: Thomas Porzelt Date: Thu, 14 Aug 2008 01:08:03 +0200 Subject: [PATCH 2/2] small change to config parser: comment now included in noise set --- src/config_parser.rl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config_parser.rl b/src/config_parser.rl index 9a59cd8..9fc85b5 100644 --- a/src/config_parser.rl +++ b/src/config_parser.rl @@ -664,9 +664,9 @@ line = ( line_sane | line_weird | line_insane ); ws = ( '\t' | ' ' ); - noise = ( ws | line ); - comment = ( '#' (any - line)* line ); + noise = ( ws | line | comment ); + block = ( '{' >block_start ); # basic types