243a4f2789be6eaf7b56a73c6cfacd47e913506e
[sahlberg/ctdb.git] / server / ctdb_statistics.c
1 /* 
2    ctdb statistics code
3
4    Copyright (C) Ronnie Sahlberg 2010
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include <string.h>
22 #include "lib/tevent/tevent.h"
23 #include "../include/ctdb_private.h"
24
25 static void ctdb_statistics_update(struct event_context *ev, struct timed_event *te, 
26                                    struct timeval t, void *p)
27 {
28         struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
29
30         memmove(&ctdb->statistics_history[1], &ctdb->statistics_history[0], (MAX_STAT_HISTORY-1)*sizeof(struct ctdb_statistics));
31         memcpy(&ctdb->statistics_history[0], &ctdb->statistics_current, sizeof(struct ctdb_statistics));
32         ctdb->statistics_history[0].statistics_current_time = timeval_current();
33
34
35         bzero(&ctdb->statistics_current, sizeof(struct ctdb_statistics));
36         ctdb->statistics_current.statistics_start_time = timeval_current();
37
38         event_add_timed(ctdb->ev, ctdb, timeval_current_ofs(10, 0), ctdb_statistics_update, ctdb);
39 }
40
41 int ctdb_statistics_init(struct ctdb_context *ctdb)
42 {
43         bzero(&ctdb->statistics, sizeof(struct ctdb_statistics));
44
45         bzero(&ctdb->statistics_current, sizeof(struct ctdb_statistics));
46         ctdb->statistics_current.statistics_start_time = timeval_current();
47
48         bzero(ctdb->statistics_history, sizeof(ctdb->statistics_history));
49
50         event_add_timed(ctdb->ev, ctdb, timeval_current_ofs(10, 0), ctdb_statistics_update, ctdb);
51         return 0;
52 }
53
54
55 int32_t ctdb_control_get_stat_history(struct ctdb_context *ctdb, 
56                                       struct ctdb_req_control *c,
57                                       TDB_DATA *outdata)
58 {
59         int len;
60         struct ctdb_statistics_wire *stat;
61
62         len = offsetof(struct ctdb_statistics_wire, stats) + MAX_STAT_HISTORY*sizeof(struct ctdb_statistics);
63
64         stat = talloc_size(outdata, len);
65         if (stat == NULL) {
66                 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate statistics history structure\n"));
67                 return -1;
68         }
69
70         stat->num = MAX_STAT_HISTORY;
71         memcpy(&stat->stats[0], &ctdb->statistics_history[0], sizeof(ctdb->statistics_history));
72
73         outdata->dsize = len;
74         outdata->dptr  = (uint8_t *)stat;
75
76         return 0;
77 }