4 Copyright (C) Andrew Tridgell 2006
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/>.
21 #include "lib/events/events.h"
22 #include "system/filesys.h"
25 #include "ctdb_protocol.h"
26 #include "ctdb_private.h"
31 static struct timeval tp1,tp2;
33 static void start_timer(void)
35 gettimeofday(&tp1,NULL);
38 static double end_timer(void)
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));
46 static int timelimit = 10;
47 static int num_records = 10;
50 enum my_functions {FUNC_INCR=1, FUNC_FETCH=2};
53 ctdb call function to increment an integer
55 static int incr_func(struct ctdb_call_info *call)
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;
62 call->new_data->dptr = talloc_size(call, 4);
63 call->new_data->dsize = 4;
64 *(uint32_t *)call->new_data->dptr = 0;
66 call->new_data = &call->record_data;
68 (*(uint32_t *)call->new_data->dptr)++;
73 ctdb call function to fetch a record
75 static int fetch_func(struct ctdb_call_info *call)
77 call->reply_data = &call->record_data;
83 static int msg_plus, msg_minus;
86 handler for messages in bench_ring()
88 static void ring_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
89 TDB_DATA data, void *private_data)
91 int incr = *(int *)data.dptr;
92 int *count = (int *)private_data;
96 dest = (ctdb_get_pnn(ctdb) + num_nodes + incr) % num_nodes;
97 ctdb_send_message(ctdb, dest, srvid, data);
106 void send_start_messages(struct ctdb_context *ctdb, int incr)
108 /* two messages are injected into the ring, moving
109 in opposite directions */
113 data.dptr = (uint8_t *)&incr;
114 data.dsize = sizeof(incr);
116 dest = (ctdb_get_pnn(ctdb) + num_nodes + incr) % num_nodes;
117 ctdb_send_message(ctdb, dest, 0, data);
120 static void each_second(struct event_context *ev, struct timed_event *te,
121 struct timeval t, void *private_data)
123 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
125 /* we kickstart the ring into action by inserting messages from node
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
132 // printf("no messages recevied, try again to kickstart the ring in forward direction...\n");
133 send_start_messages(ctdb, 1);
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);
139 event_add_timed(ctdb->ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
142 static void dummy_event(struct event_context *ev, struct timed_event *te,
143 struct timeval t, void *private_data)
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);
150 benchmark sending messages in a ring around the nodes
152 static void bench_ring(struct ctdb_context *ctdb, struct event_context *ev)
154 int pnn=ctdb_get_pnn(ctdb);
157 event_add_timed(ctdb->ev, ctdb, timeval_current_ofs(1, 0), each_second, ctdb);
159 event_add_timed(ctdb->ev, ctdb, timeval_current_ofs(1, 0), dummy_event, ctdb);
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);
172 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\n",
173 msg_count/end_timer(), msg_plus, msg_minus);
179 int main(int argc, const char *argv[])
181 struct ctdb_context *ctdb;
182 struct ctdb_db_context *ctdb_db;
184 struct poptOption popt_options[] = {
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" },
193 const char **extra_argv;
197 struct event_context *ev;
199 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
201 while ((opt = poptGetNextOpt(pc)) != -1) {
204 fprintf(stderr, "Invalid option %s: %s\n",
205 poptBadOption(pc, 0), poptStrerror(opt));
210 /* setup the remaining options for the main program to use */
211 extra_argv = poptGetArgs(pc);
214 while (extra_argv[extra_argc]) extra_argc++;
217 ev = event_context_init(NULL);
219 /* initialise ctdb */
220 ctdb = ctdb_cmdline_client(ev);
222 /* attach to a specific database */
223 ctdb_db = ctdb_attach(ctdb, "test.tdb", false, 0);
225 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
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);
233 if (ctdb_set_message_handler(ctdb, 0, ring_message_handler,&msg_count))
236 printf("Waiting for cluster\n");
239 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
240 if (recmode == 0) break;
244 bench_ring(ctdb, ev);