4 Copyright (C) Andrew Tridgell 2006
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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;
49 enum my_functions {FUNC_INCR=1, FUNC_FETCH=2};
52 ctdb call function to increment an integer
54 static int incr_func(struct ctdb_call_info *call)
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;
61 call->new_data->dptr = talloc_size(call, 4);
62 call->new_data->dsize = 4;
63 *(uint32_t *)call->new_data->dptr = 0;
65 call->new_data = &call->record_data;
67 (*(uint32_t *)call->new_data->dptr)++;
72 ctdb call function to fetch a record
74 static int fetch_func(struct ctdb_call_info *call)
76 call->reply_data = &call->record_data;
82 static int msg_plus, msg_minus;
85 handler for messages in bench_ring()
87 static void ring_message_handler(struct ctdb_context *ctdb, uint32_t srvid,
88 TDB_DATA data, void *private_data)
90 int incr = *(int *)data.dptr;
91 int *count = (int *)private_data;
94 dest = (ctdb_get_vnn(ctdb) + incr) % ctdb_get_num_nodes(ctdb);
95 ctdb_send_message(ctdb, dest, srvid, data);
104 benchmark sending messages in a ring around the nodes
106 static void bench_ring(struct ctdb_context *ctdb, struct event_context *ev)
108 int vnn=ctdb_get_vnn(ctdb);
112 /* two messages are injected into the ring, moving
113 in opposite directions */
117 data.dptr = (uint8_t *)&incr;
118 data.dsize = sizeof(incr);
120 for (i=0;i<num_msgs;i++) {
122 dest = (ctdb_get_vnn(ctdb) + incr) % ctdb_get_num_nodes(ctdb);
123 ctdb_send_message(ctdb, dest, 0, data);
126 dest = (ctdb_get_vnn(ctdb) + incr) % ctdb_get_num_nodes(ctdb);
127 ctdb_send_message(ctdb, dest, 0, data);
133 while (end_timer() < timelimit) {
134 if (vnn == 0 && msg_count % 10000 == 0) {
135 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\r",
136 msg_count/end_timer(), msg_plus, msg_minus);
142 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\n",
143 msg_count/end_timer(), msg_plus, msg_minus);
150 int main(int argc, const char *argv[])
152 struct ctdb_context *ctdb;
153 struct ctdb_db_context *ctdb_db;
155 struct poptOption popt_options[] = {
158 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
159 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
160 { "num-msgs", 'n', POPT_ARG_INT, &num_msgs, 0, "num_msgs", "integer" },
164 const char **extra_argv;
168 struct event_context *ev;
170 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
172 while ((opt = poptGetNextOpt(pc)) != -1) {
175 fprintf(stderr, "Invalid option %s: %s\n",
176 poptBadOption(pc, 0), poptStrerror(opt));
181 /* setup the remaining options for the main program to use */
182 extra_argv = poptGetArgs(pc);
185 while (extra_argv[extra_argc]) extra_argc++;
188 ev = event_context_init(NULL);
190 /* initialise ctdb */
191 ctdb = ctdb_cmdline_init(ev);
193 /* attach to a specific database */
194 ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666);
196 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
200 /* setup a ctdb call function */
201 ret = ctdb_set_call(ctdb_db, incr_func, FUNC_INCR);
202 ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
204 /* start the protocol running */
205 ret = ctdb_start(ctdb);
207 ctdb_set_message_handler(ctdb, 0, ring_message_handler,&msg_count);
209 /* wait until all nodes are connected (should not be needed
210 outside of test code) */
211 ctdb_connect_wait(ctdb);
213 bench_ring(ctdb, ev);