git » nmdb » commit 00e67ab

Add a more complete stats structure and fill it where appropriate.

author Alberto Bertogli
2007-10-11 04:11:07 UTC
committer Alberto Bertogli
2007-10-11 04:30:15 UTC
parent d743e437504418ef1ae8047929e6eb133613930a

Add a more complete stats structure and fill it where appropriate.

There's still no way of getting the stats, that will come in following
patches.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

nmdb/Makefile +1 -1
nmdb/common.h +2 -5
nmdb/main.c +2 -10
nmdb/parse.c +27 -13
nmdb/sctp.c +2 -0
nmdb/stats.c +31 -0
nmdb/stats.h +39 -0
nmdb/tcp.c +1 -0
nmdb/tipc.c +2 -0
nmdb/udp.c +2 -0

diff --git a/nmdb/Makefile b/nmdb/Makefile
index c4e8ece..878c730 100644
--- a/nmdb/Makefile
+++ b/nmdb/Makefile
@@ -28,7 +28,7 @@ endif
 PREFIX=/usr/local
 
 
-OBJS = cache.o dbloop.o queue.o log.o net.o parse.o main.o
+OBJS = cache.o dbloop.o queue.o log.o net.o parse.o stats.o main.o
 LIBS = -levent -lpthread -lrt
 
 
diff --git a/nmdb/common.h b/nmdb/common.h
index 1a872c7..a1b129e 100644
--- a/nmdb/common.h
+++ b/nmdb/common.h
@@ -31,11 +31,8 @@ struct settings {
 extern struct settings settings;
 
 /* Statistics */
-struct stats {
-	unsigned long net_version_mismatch;
-	unsigned long net_broken_req;
-	unsigned long net_unk_req;
-};
+#include "stats.h"
 extern struct stats stats;
+
 #endif
 
diff --git a/nmdb/main.c b/nmdb/main.c
index 5bb3cad..3f29fd2 100644
--- a/nmdb/main.c
+++ b/nmdb/main.c
@@ -12,6 +12,7 @@
 #include "common.h"
 #include "net-const.h"
 #include "log.h"
+#include "stats.h"
 
 #define DEFDBNAME "database"
 
@@ -152,15 +153,6 @@ static int load_settings(int argc, char **argv)
 }
 
 
-static void init_stats(void)
-{
-	stats.net_version_mismatch = 0;
-	stats.net_broken_req = 0;
-	stats.net_unk_req = 0;
-	return;
-}
-
-
 int main(int argc, char **argv)
 {
 	struct cache *cd;
@@ -177,7 +169,7 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	init_stats();
+	stats_init(&stats);
 
 	cd = cache_create(settings.numobjs, 0);
 	if (cd == NULL) {
diff --git a/nmdb/parse.c b/nmdb/parse.c
index 368e67e..29a9489 100644
--- a/nmdb/parse.c
+++ b/nmdb/parse.c
@@ -159,31 +159,43 @@ int parse_message(struct req_info *req,
 	req->payload = payload;
 	req->psize = psize;
 
-	if (cmd == REQ_CACHE_GET)
+	if (cmd == REQ_CACHE_GET) {
+		stats.cache_get++;
 		parse_get(req, 0);
-	else if (cmd == REQ_CACHE_SET)
+	} else if (cmd == REQ_CACHE_SET) {
+		stats.cache_set++;
 		parse_set(req, 0, 0);
-	else if (cmd == REQ_CACHE_DEL)
+	} else if (cmd == REQ_CACHE_DEL) {
+		stats.cache_del++;
 		parse_del(req, 0, 0);
-	else if (cmd == REQ_GET)
+	} else if (cmd == REQ_GET) {
+		stats.db_get++;
 		parse_get(req, 1);
-	else if (cmd == REQ_SET_SYNC)
+	} else if (cmd == REQ_SET_SYNC) {
+		stats.db_set++;
 		parse_set(req, 1, 0);
-	else if (cmd == REQ_DEL_SYNC)
+	} else if (cmd == REQ_DEL_SYNC) {
+		stats.db_del++;
 		parse_del(req, 1, 0);
-	else if (cmd == REQ_SET_ASYNC)
+	} else if (cmd == REQ_SET_ASYNC) {
+		stats.db_set++;
 		parse_set(req, 1, 1);
-	else if (cmd == REQ_DEL_ASYNC)
+	} else if (cmd == REQ_DEL_ASYNC) {
+		stats.db_del++;
 		parse_del(req, 1, 1);
-	else if (cmd == REQ_CACHE_CAS)
+	} else if (cmd == REQ_CACHE_CAS) {
+		stats.cache_cas++;
 		parse_cas(req, 0);
-	else if (cmd == REQ_CAS)
+	} else if (cmd == REQ_CAS) {
+		stats.db_cas++;
 		parse_cas(req, 1);
-	else if (cmd == REQ_CACHE_INCR)
+	} else if (cmd == REQ_CACHE_INCR) {
+		stats.cache_incr++;
 		parse_incr(req, 0);
-	else if (cmd == REQ_INCR)
+	} else if (cmd == REQ_INCR) {
+		stats.db_incr++;
 		parse_incr(req, 1);
-	else {
+	} else {
 		stats.net_unk_req++;
 		req->reply_err(req, ERR_UNKREQ);
 	}
@@ -213,6 +225,7 @@ static void parse_get(struct req_info *req, int impact_db)
 	hit = cache_get(cache_table, key, ksize, &val, &vsize);
 
 	if (!hit && !impact_db) {
+		stats.cache_misses++;
 		req->reply_mini(req, REP_CACHE_MISS);
 		return;
 	} else if (!hit && impact_db) {
@@ -228,6 +241,7 @@ static void parse_get(struct req_info *req, int impact_db)
 		queue_signal(op_queue);
 		return;
 	} else {
+		stats.cache_hits++;
 		req->reply_long(req, REP_CACHE_HIT, val, vsize);
 		return;
 	}
diff --git a/nmdb/sctp.c b/nmdb/sctp.c
index 42fdb4c..7b02da3 100644
--- a/nmdb/sctp.c
+++ b/nmdb/sctp.c
@@ -217,6 +217,8 @@ void sctp_recv(int fd, short event, void *arg)
 		goto exit;
 	}
 
+	stats.msg_sctp++;
+
 	req.fd = fd;
 	req.type = REQTYPE_SCTP;
 	req.clisa = (struct sockaddr *) &clisa;
diff --git a/nmdb/stats.c b/nmdb/stats.c
new file mode 100644
index 0000000..8cd85ee
--- /dev/null
+++ b/nmdb/stats.c
@@ -0,0 +1,31 @@
+
+#include "stats.h"
+
+void stats_init(struct stats *s)
+{
+	s->cache_get = 0;
+	s->cache_set = 0;
+	s->cache_del = 0;
+	s->cache_cas = 0;
+	s->cache_incr = 0;
+
+	s->db_get = 0;
+	s->db_set = 0;
+	s->db_del = 0;
+	s->db_cas = 0;
+	s->db_incr = 0;
+
+	s->cache_misses = 0;
+	s->cache_hits = 0;
+
+	s->msg_tipc = 0;
+	s->msg_tcp = 0;
+	s->msg_udp = 0;
+	s->msg_sctp = 0;
+
+	s->net_version_mismatch = 0;
+	s->net_broken_req = 0;
+	s->net_unk_req = 0;
+}
+
+
diff --git a/nmdb/stats.h b/nmdb/stats.h
new file mode 100644
index 0000000..5bf3e8b
--- /dev/null
+++ b/nmdb/stats.h
@@ -0,0 +1,39 @@
+
+#ifndef _STATS_H
+#define _STATS_H
+
+/* Statistics structure */
+struct stats {
+	unsigned long cache_get;
+	unsigned long cache_set;
+	unsigned long cache_del;
+	unsigned long cache_cas;
+	unsigned long cache_incr;
+
+	unsigned long db_get;
+	unsigned long db_set;
+	unsigned long db_del;
+	unsigned long db_cas;
+	unsigned long db_incr;
+
+	unsigned long cache_hits;
+	unsigned long cache_misses;
+
+	unsigned long db_hits;
+	unsigned long db_misses;
+
+
+	unsigned long msg_tipc;
+	unsigned long msg_tcp;
+	unsigned long msg_udp;
+	unsigned long msg_sctp;
+
+	unsigned long net_version_mismatch;
+	unsigned long net_broken_req;
+	unsigned long net_unk_req;
+};
+
+void stats_init(struct stats *s);
+
+#endif
+
diff --git a/nmdb/tcp.c b/nmdb/tcp.c
index dc98c26..e511087 100644
--- a/nmdb/tcp.c
+++ b/nmdb/tcp.c
@@ -421,6 +421,7 @@ static void process_buf(struct tcp_socket *tcpsock,
 	}
 
 	/* The buffer is complete, parse it as usual. */
+	stats.msg_tcp++;
 	if (parse_message(&(tcpsock->req), buf + 4, len - 4)) {
 		goto exit;
 	} else {
diff --git a/nmdb/tipc.c b/nmdb/tipc.c
index 658a056..c821120 100644
--- a/nmdb/tipc.c
+++ b/nmdb/tipc.c
@@ -201,6 +201,8 @@ void tipc_recv(int fd, short event, void *arg)
 		goto exit;
 	}
 
+	stats.msg_tipc++;
+
 	req.fd = fd;
 	req.type = REQTYPE_TIPC;
 	req.clisa = (struct sockaddr *) &clisa;
diff --git a/nmdb/udp.c b/nmdb/udp.c
index c9c51ea..2adf54f 100644
--- a/nmdb/udp.c
+++ b/nmdb/udp.c
@@ -201,6 +201,8 @@ void udp_recv(int fd, short event, void *arg)
 		goto exit;
 	}
 
+	stats.msg_udp++;
+
 	req.fd = fd;
 	req.type = REQTYPE_UDP;
 	req.clisa = (struct sockaddr *) &clisa;