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