Remove unused assignments
[nmdb.git] / utils / nmdb-stats.c
blobf3cd6f20e5f052bea3e46a1ce636f29c709e1734
2 /* nmdb-stats.c
3 * Queries the stats of a nmdb server.
4 * Alberto Bertogli (albertito@blitiri.com.ar)
5 */
7 #include <stdio.h> /* printf() */
8 #include <stdint.h> /* uint64_t */
9 #include <string.h> /* strcmp() */
10 #include <stdlib.h> /* atoi() */
11 #include <arpa/inet.h> /* htonl() and friends */
13 #include "nmdb.h"
16 /* ntohll() is not standard, so we define it using an UGLY trick because there
17 * is no standard way to check for endianness at runtime! (this is the same as
18 * the one in nmdb/parse.c, the infraestructure to keep these common is not
19 * worth it)*/
20 static uint64_t ntohll(uint64_t x)
22 static int endianness = 0;
24 /* determine the endianness by checking how htonl() behaves; use -1
25 * for little endian and 1 for big endian */
26 if (endianness == 0) {
27 if (htonl(1) == 1)
28 endianness = 1;
29 else
30 endianness = -1;
33 if (endianness == 1) {
34 /* big endian */
35 return x;
38 /* little endian */
39 return ( ntohl( (x >> 32) & 0xFFFFFFFF ) | \
40 ( (uint64_t) ntohl(x & 0xFFFFFFFF) ) << 32 );
43 #define MAX_STATS_SIZE 64
45 static void help(void)
47 printf("Use: nmdb-stats [ tipc port | [tcp|udp|sctp] host port ]\n");
50 int main(int argc, char **argv)
52 int i, j, k;
53 int rv;
54 uint64_t stats[MAX_STATS_SIZE];
55 unsigned int nservers = 0, nstats = 0;
56 nmdb_t *db;
58 db = nmdb_init();
60 if (argc < 3) {
61 help();
62 return 1;
65 if (strcmp("tipc", argv[1]) == 0) {
66 rv = nmdb_add_tipc_server(db, atoi(argv[2]));
67 } else {
68 if (argc != 4) {
69 help();
70 return 1;
73 if (strcmp("tcp", argv[1]) == 0) {
74 rv = nmdb_add_tcp_server(db, argv[2], atoi(argv[3]));
75 } else if (strcmp("udp", argv[1]) == 0) {
76 rv = nmdb_add_udp_server(db, argv[2], atoi(argv[3]));
77 } else if (strcmp("sctp", argv[1]) == 0) {
78 rv = nmdb_add_sctp_server(db, argv[2], atoi(argv[3]));
79 } else {
80 help();
81 return 1;
85 if (!rv) {
86 perror("Error adding server");
87 return 1;
90 rv = nmdb_stats(db, (unsigned char *) stats, sizeof(stats),
91 &nservers, &nstats);
92 if (rv <= 0) {
93 printf("Error %d\n", rv);
94 return 1;
97 /* Macro to simplify showing the fields */
98 #define shst(s, pos) \
99 do { \
100 printf("\t%ju\t%s\n", ntohll(stats[j + pos]), s); \
101 } while(0)
103 /* The following assumes it can be more than one server. This can
104 * never happen with the current code, but it can be useful as an
105 * example in the future. */
106 for (i = 0; i < nservers; i++) {
107 printf("stats for server %d:\n", i);
109 j = nstats * i;
111 /* note they are not necessarily in numerical order */
112 shst("cache get", 0);
113 shst("cache set", 1);
114 shst("cache del", 2);
115 shst("cache cas", 3);
116 shst("cache incr", 4);
118 shst("db get", 5);
119 shst("db set", 6);
120 shst("db del", 7);
121 shst("db cas", 8);
122 shst("db incr", 9);
123 shst("db firstkey", 21);
124 shst("db nextkey", 22);
126 shst("cache hits", 10);
127 shst("cache misses", 11);
129 shst("db hits", 12);
130 shst("db misses", 13);
132 shst("msg tipc", 14);
133 shst("msg tcp", 15);
134 shst("msg udp", 16);
135 shst("msg sctp", 17);
137 shst("version mismatch", 18);
138 shst("broken requests", 19);
139 shst("unknown requests", 20);
141 /* if there are any fields we don't know, show them anyway */
142 for (k = 23; k < nstats; k++) {
143 shst("unknown field", k);
146 printf("\n");
149 nmdb_free(db);
151 return 0;