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