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