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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
30 static struct timeval tp1,tp2;
32 static void start_timer(void)
34 gettimeofday(&tp1,NULL);
37 static double end_timer(void)
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));
45 static int timelimit = 10;
46 static int num_records = 10;
47 static int num_msgs = 1;
48 static uint32_t num_nodes;
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;
95 dest = (ctdb_get_vnn(ctdb) + incr) % num_nodes;
96 ctdb_send_message(ctdb, dest, srvid, data);
105 benchmark sending messages in a ring around the nodes
107 static void bench_ring(struct ctdb_context *ctdb, struct event_context *ev)
109 int vnn=ctdb_get_vnn(ctdb);
113 /* two messages are injected into the ring, moving
114 in opposite directions */
118 data.dptr = (uint8_t *)&incr;
119 data.dsize = sizeof(incr);
121 for (i=0;i<num_msgs;i++) {
123 dest = (ctdb_get_vnn(ctdb) + incr) % num_nodes;
124 ctdb_send_message(ctdb, dest, 0, data);
127 dest = (ctdb_get_vnn(ctdb) + incr) % num_nodes;
128 ctdb_send_message(ctdb, dest, 0, data);
134 while (end_timer() < timelimit) {
135 if (vnn == 0 && msg_count % 10000 == 0) {
136 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\r",
137 msg_count/end_timer(), msg_plus, msg_minus);
143 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\n",
144 msg_count/end_timer(), msg_plus, msg_minus);
148 handler for reconfigure message
150 static void reconfigure_handler(struct ctdb_context *ctdb, uint64_t srvid,
151 TDB_DATA data, void *private_data)
153 int *ready = (int *)private_data;
161 int main(int argc, const char *argv[])
163 struct ctdb_context *ctdb;
164 struct ctdb_db_context *ctdb_db;
166 struct poptOption popt_options[] = {
169 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
170 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
171 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
175 const char **extra_argv;
179 struct event_context *ev;
182 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
184 while ((opt = poptGetNextOpt(pc)) != -1) {
187 fprintf(stderr, "Invalid option %s: %s\n",
188 poptBadOption(pc, 0), poptStrerror(opt));
193 /* setup the remaining options for the main program to use */
194 extra_argv = poptGetArgs(pc);
197 while (extra_argv[extra_argc]) extra_argc++;
200 ev = event_context_init(NULL);
202 /* initialise ctdb */
203 ctdb = ctdb_cmdline_client(ev);
205 ctdb_set_message_handler(ctdb, CTDB_SRVID_RECONFIGURE, reconfigure_handler,
208 /* attach to a specific database */
209 ctdb_db = ctdb_attach(ctdb, "test.tdb");
211 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
215 /* setup a ctdb call function */
216 ret = ctdb_set_call(ctdb_db, incr_func, FUNC_INCR);
217 ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
219 if (ctdb_set_message_handler(ctdb, 0, ring_message_handler,&msg_count))
222 printf("Waiting for cluster\n");
223 while (!cluster_ready) {
227 ctdb_get_connected_nodes(ctdb, timeval_zero(), ctdb, &num_nodes);
229 bench_ring(ctdb, ev);