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 "system/filesys.h"
24 #include "ctdb_client.h"
25 #include "ctdb_private.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;
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
, uint64_t srvid
,
88 TDB_DATA data
, void *private_data
)
90 int incr
= *(int *)data
.dptr
;
91 int *count
= (int *)private_data
;
95 dest
= (ctdb_get_pnn(ctdb
) + num_nodes
+ incr
) % num_nodes
;
96 ctdb_client_send_message(ctdb
, dest
, srvid
, data
);
105 static void send_start_messages(struct ctdb_context
*ctdb
, int incr
)
107 /* two messages are injected into the ring, moving
108 in opposite directions */
112 data
.dptr
= (uint8_t *)&incr
;
113 data
.dsize
= sizeof(incr
);
115 dest
= (ctdb_get_pnn(ctdb
) + num_nodes
+ incr
) % num_nodes
;
116 ctdb_client_send_message(ctdb
, dest
, 0, data
);
119 static void each_second(struct event_context
*ev
, struct timed_event
*te
,
120 struct timeval t
, void *private_data
)
122 struct ctdb_context
*ctdb
= talloc_get_type(private_data
, struct ctdb_context
);
124 /* we kickstart the ring into action by inserting messages from node
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
131 // printf("no messages recevied, try again to kickstart the ring in forward direction...\n");
132 send_start_messages(ctdb
, 1);
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);
138 event_add_timed(ctdb
->ev
, ctdb
, timeval_current_ofs(1, 0), each_second
, ctdb
);
141 static void dummy_event(struct event_context
*ev
, struct timed_event
*te
,
142 struct timeval t
, void *private_data
)
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
);
149 benchmark sending messages in a ring around the nodes
151 static void bench_ring(struct ctdb_context
*ctdb
, struct event_context
*ev
)
153 int pnn
=ctdb_get_pnn(ctdb
);
156 event_add_timed(ctdb
->ev
, ctdb
, timeval_current_ofs(1, 0), each_second
, ctdb
);
158 event_add_timed(ctdb
->ev
, ctdb
, timeval_current_ofs(1, 0), dummy_event
, ctdb
);
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
);
171 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\n",
172 msg_count
/end_timer(), msg_plus
, msg_minus
);
178 int main(int argc
, const char *argv
[])
180 struct ctdb_context
*ctdb
;
181 struct ctdb_db_context
*ctdb_db
;
183 struct poptOption popt_options
[] = {
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" },
192 const char **extra_argv
;
196 struct event_context
*ev
;
198 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
200 while ((opt
= poptGetNextOpt(pc
)) != -1) {
203 fprintf(stderr
, "Invalid option %s: %s\n",
204 poptBadOption(pc
, 0), poptStrerror(opt
));
209 /* setup the remaining options for the main program to use */
210 extra_argv
= poptGetArgs(pc
);
213 while (extra_argv
[extra_argc
]) extra_argc
++;
216 if (num_nodes
== 0) {
217 printf("You must specify the number of nodes\n");
221 ev
= event_context_init(NULL
);
223 /* initialise ctdb */
224 ctdb
= ctdb_cmdline_client(ev
, timeval_current_ofs(3, 0));
229 /* attach to a specific database */
230 ctdb_db
= ctdb_attach(ctdb
, timeval_current_ofs(2, 0), "test.tdb",
233 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb
));
237 /* setup a ctdb call function */
238 ret
= ctdb_set_call(ctdb_db
, incr_func
, FUNC_INCR
);
240 DEBUG(DEBUG_DEBUG
,("ctdb_set_call() failed, ignoring return code %d\n", ret
));
242 ret
= ctdb_set_call(ctdb_db
, fetch_func
, FUNC_FETCH
);
244 DEBUG(DEBUG_DEBUG
,("ctdb_set_call() failed, ignoring return code %d\n", ret
));
247 if (ctdb_client_set_message_handler(ctdb
, 0, ring_message_handler
,&msg_count
))
250 printf("Waiting for cluster\n");
253 ctdb_ctrl_getrecmode(ctdb
, ctdb
, timeval_zero(), CTDB_CURRENT_NODE
, &recmode
);
254 if (recmode
== 0) break;
258 bench_ring(ctdb
, ev
);