1b0c30bc6ab9e6ac4b791a54ffc9e5d18d87ff67
[vlendec/samba-autobuild/.git] / ctdb / tests / src / ctdb_bench.c
1 /* 
2    simple ctdb benchmark
3
4    Copyright (C) Andrew Tridgell  2006
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 "replace.h"
21 #include "system/filesys.h"
22 #include "system/network.h"
23
24 #include <popt.h>
25 #include <talloc.h>
26 #include <tevent.h>
27
28 #include "lib/util/time.h"
29 #include "lib/util/debug.h"
30
31 #include "common/cmdline.h"
32
33 #include "ctdb_logging.h"
34 #include "ctdb_private.h"
35 #include "ctdb_client.h"
36
37 static struct timeval tp1,tp2;
38
39 static void start_timer(void)
40 {
41         gettimeofday(&tp1,NULL);
42 }
43
44 static double end_timer(void)
45 {
46         gettimeofday(&tp2,NULL);
47         return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
48                 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
49 }
50
51
52 static int timelimit = 10;
53 static int num_records = 10;
54 static int num_nodes;
55
56 enum my_functions {FUNC_INCR=1, FUNC_FETCH=2};
57
58 /*
59   ctdb call function to increment an integer
60 */
61 static int incr_func(struct ctdb_call_info *call)
62 {
63         if (call->record_data.dsize == 0) {
64                 call->new_data = talloc(call, TDB_DATA);
65                 if (call->new_data == NULL) {
66                         return CTDB_ERR_NOMEM;
67                 }
68                 call->new_data->dptr = talloc_size(call, 4);
69                 call->new_data->dsize = 4;
70                 *(uint32_t *)call->new_data->dptr = 0;
71         } else {
72                 call->new_data = &call->record_data;
73         }
74         (*(uint32_t *)call->new_data->dptr)++;
75         return 0;
76 }
77
78 /*
79   ctdb call function to fetch a record
80 */
81 static int fetch_func(struct ctdb_call_info *call)
82 {
83         call->reply_data = &call->record_data;
84         return 0;
85 }
86
87
88 struct bench_data {
89         struct ctdb_context *ctdb;
90         struct tevent_context *ev;
91         int msg_count;
92         int msg_plus, msg_minus;
93 };
94
95 /*
96   handler for messages in bench_ring()
97 */
98 static void ring_message_handler(uint64_t srvid, TDB_DATA data,
99                                  void *private_data)
100 {
101         struct bench_data *bdata = talloc_get_type_abort(
102                 private_data, struct bench_data);
103         int incr = *(int *)data.dptr;
104         int dest;
105
106         bdata->msg_count++;
107         dest = (ctdb_get_pnn(bdata->ctdb) + num_nodes + incr) % num_nodes;
108         ctdb_client_send_message(bdata->ctdb, dest, srvid, data);
109         if (incr == 1) {
110                 bdata->msg_plus++;
111         } else {
112                 bdata->msg_minus++;
113         }
114 }
115
116
117 static void send_start_messages(struct ctdb_context *ctdb, int incr)
118 {
119         /* two messages are injected into the ring, moving
120            in opposite directions */
121         int dest;
122         TDB_DATA data;
123                 
124         data.dptr = (uint8_t *)&incr;
125         data.dsize = sizeof(incr);
126
127         dest = (ctdb_get_pnn(ctdb) + num_nodes + incr) % num_nodes;
128         ctdb_client_send_message(ctdb, dest, 0, data);
129 }
130
131 static void each_second(struct tevent_context *ev, struct tevent_timer *te,
132                         struct timeval t, void *private_data)
133 {
134         struct bench_data *bdata = talloc_get_type_abort(
135                 private_data, struct bench_data);
136
137         /* we kickstart the ring into action by inserting messages from node
138            with pnn 0.
139            it may happen that some other node does not yet have ctdb_bench
140            running in which case the ring is broken and the messages are lost.
141            if so, once every second try again to restart the ring
142         */
143         if (bdata->msg_plus == 0) {
144 //              printf("no messages recevied, try again to kickstart the ring in forward direction...\n");
145                 send_start_messages(bdata->ctdb, 1);
146         }
147         if (bdata->msg_minus == 0) {
148 //              printf("no messages recevied, try again to kickstart the ring in reverse direction...\n");
149                 send_start_messages(bdata->ctdb, -1);
150         }
151         tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
152                          each_second, bdata);
153 }
154
155 static void dummy_event(struct tevent_context *ev, struct tevent_timer *te,
156                         struct timeval t, void *private_data)
157 {
158         struct bench_data *bdata = talloc_get_type_abort(
159                 private_data, struct bench_data);
160
161         tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
162                          dummy_event, bdata);
163 }
164
165 /*
166   benchmark sending messages in a ring around the nodes
167 */
168 static void bench_ring(struct bench_data *bdata)
169 {
170         int pnn = ctdb_get_pnn(bdata->ctdb);
171
172         if (pnn == 0) {
173                 tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
174                                  each_second, bdata);
175         } else {
176                 tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
177                                  dummy_event, bdata);
178         }
179
180         start_timer();
181         while (end_timer() < timelimit) {
182                 if (pnn == 0 && bdata->msg_count % 10000 == 0 && end_timer() > 0) {
183                         printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\r",
184                                bdata->msg_count/end_timer(),
185                                bdata->msg_plus, bdata->msg_minus);
186                         fflush(stdout);
187                 }
188                 tevent_loop_once(bdata->ev);
189         }
190
191         printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\n",
192                bdata->msg_count/end_timer(),
193                bdata->msg_plus, bdata->msg_minus);
194 }
195
196 /*
197   main program
198 */
199 int main(int argc, const char *argv[])
200 {
201         struct ctdb_context *ctdb;
202         struct ctdb_db_context *ctdb_db;
203
204         struct poptOption popt_options[] = {
205                 POPT_AUTOHELP
206                 POPT_CTDB_CMDLINE
207                 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
208                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
209                 { NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
210                 POPT_TABLEEND
211         };
212         int opt;
213         const char **extra_argv;
214         int extra_argc = 0;
215         int ret;
216         poptContext pc;
217         struct tevent_context *ev;
218         struct bench_data *bdata;
219
220         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
221
222         while ((opt = poptGetNextOpt(pc)) != -1) {
223                 switch (opt) {
224                 default:
225                         fprintf(stderr, "Invalid option %s: %s\n", 
226                                 poptBadOption(pc, 0), poptStrerror(opt));
227                         exit(1);
228                 }
229         }
230
231         /* setup the remaining options for the main program to use */
232         extra_argv = poptGetArgs(pc);
233         if (extra_argv) {
234                 extra_argv++;
235                 while (extra_argv[extra_argc]) extra_argc++;
236         }
237
238         if (num_nodes == 0) {
239                 printf("You must specify the number of nodes\n");
240                 exit(1);
241         }
242
243         ev = tevent_context_init(NULL);
244
245         /* initialise ctdb */
246         ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
247         if (ctdb == NULL) {
248                 exit(1);
249         }
250
251         /* attach to a specific database */
252         ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
253                               false, 0);
254         if (!ctdb_db) {
255                 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
256                 exit(1);
257         }
258
259         /* setup a ctdb call function */
260         ret = ctdb_set_call(ctdb_db, incr_func,  FUNC_INCR);
261         if (ret != 0) {
262                 DEBUG(DEBUG_DEBUG,("ctdb_set_call() failed, ignoring return code %d\n", ret));
263         }
264         ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
265         if (ret != 0) {
266                 DEBUG(DEBUG_DEBUG,("ctdb_set_call() failed, ignoring return code %d\n", ret));
267         }
268
269         bdata = talloc_zero(ctdb, struct bench_data);
270         if (bdata == NULL) {
271                 goto error;
272         }
273         bdata->ctdb = ctdb;
274         bdata->ev = ev;
275
276         if (ctdb_client_set_message_handler(ctdb, 0, ring_message_handler, bdata))
277                 goto error;
278
279         printf("Waiting for cluster\n");
280         while (1) {
281                 uint32_t recmode=1;
282                 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
283                 if (recmode == 0) break;
284                 tevent_loop_once(ev);
285         }
286
287         bench_ring(bdata);
288
289 error:
290         return 0;
291 }