Implement Virtual List View (VLV)
[Samba.git] / ctdb / server / ctdb_statistics.c
blob4cf8f9e2726a1aa51a3634ae0057212c1cd72c1c
1 /*
2 ctdb statistics code
4 Copyright (C) Ronnie Sahlberg 2010
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.
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.
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/>.
20 #include "replace.h"
21 #include "system/network.h"
22 #include "system/time.h"
24 #include <talloc.h>
25 #include <tevent.h>
27 #include "lib/util/debug.h"
28 #include "lib/util/samba_util.h"
30 #include "ctdb_private.h"
32 #include "common/logging.h"
34 static void ctdb_statistics_update(struct tevent_context *ev,
35 struct tevent_timer *te,
36 struct timeval t, void *p)
38 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
40 memmove(&ctdb->statistics_history[1], &ctdb->statistics_history[0], (MAX_STAT_HISTORY-1)*sizeof(struct ctdb_statistics));
41 memcpy(&ctdb->statistics_history[0], &ctdb->statistics_current, sizeof(struct ctdb_statistics));
42 ctdb->statistics_history[0].statistics_current_time = timeval_current();
45 bzero(&ctdb->statistics_current, sizeof(struct ctdb_statistics));
46 ctdb->statistics_current.statistics_start_time = timeval_current();
48 tevent_add_timer(ctdb->ev, ctdb,
49 timeval_current_ofs(ctdb->tunable.stat_history_interval, 0),
50 ctdb_statistics_update, ctdb);
53 int ctdb_statistics_init(struct ctdb_context *ctdb)
55 bzero(&ctdb->statistics, sizeof(struct ctdb_statistics));
56 ctdb->statistics.statistics_start_time = timeval_current();
58 bzero(&ctdb->statistics_current, sizeof(struct ctdb_statistics));
59 ctdb->statistics_current.statistics_start_time = timeval_current();
61 bzero(ctdb->statistics_history, sizeof(ctdb->statistics_history));
63 tevent_add_timer(ctdb->ev, ctdb,
64 timeval_current_ofs(ctdb->tunable.stat_history_interval, 0),
65 ctdb_statistics_update, ctdb);
66 return 0;
70 int32_t ctdb_control_get_stat_history(struct ctdb_context *ctdb,
71 struct ctdb_req_control_old *c,
72 TDB_DATA *outdata)
74 int len;
75 struct ctdb_statistics_list_old *s;
77 len = offsetof(struct ctdb_statistics_list_old, stats) +
78 MAX_STAT_HISTORY*sizeof(struct ctdb_statistics);
80 s = talloc_size(outdata, len);
81 if (s == NULL) {
82 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate statistics history structure\n"));
83 return -1;
86 s->num = MAX_STAT_HISTORY;
87 memcpy(&s->stats[0], &ctdb->statistics_history[0], sizeof(ctdb->statistics_history));
89 outdata->dsize = len;
90 outdata->dptr = (uint8_t *)s;
92 return 0;