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"
22 #include "system/network.h"
28 #include "lib/util/time.h"
29 #include "lib/util/debug.h"
31 #include "common/cmdline.h"
33 #include "ctdb_logging.h"
34 #include "ctdb_private.h"
35 #include "ctdb_client.h"
37 static struct timeval tp1,tp2;
39 static void start_timer(void)
41 gettimeofday(&tp1,NULL);
44 static double end_timer(void)
46 gettimeofday(&tp2,NULL);
47 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
48 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
52 static int timelimit = 10;
53 static int num_records = 10;
56 enum my_functions {FUNC_INCR=1, FUNC_FETCH=2};
59 ctdb call function to increment an integer
61 static int incr_func(struct ctdb_call_info *call)
63 if (call->record_data.dsize == 0) {
64 call->new_data = talloc(call, TDB_DATA);
65 if (call->new_data == NULL) {
66 return CTDB_ERR_NOMEM;
68 call->new_data->dptr = talloc_size(call, 4);
69 call->new_data->dsize = 4;
70 *(uint32_t *)call->new_data->dptr = 0;
72 call->new_data = &call->record_data;
74 (*(uint32_t *)call->new_data->dptr)++;
79 ctdb call function to fetch a record
81 static int fetch_func(struct ctdb_call_info *call)
83 call->reply_data = &call->record_data;
89 struct ctdb_context *ctdb;
90 struct tevent_context *ev;
92 int msg_plus, msg_minus;
96 handler for messages in bench_ring()
98 static void ring_message_handler(uint64_t srvid, TDB_DATA data,
101 struct bench_data *bdata = talloc_get_type_abort(
102 private_data, struct bench_data);
103 int incr = *(int *)data.dptr;
107 dest = (ctdb_get_pnn(bdata->ctdb) + num_nodes + incr) % num_nodes;
108 ctdb_client_send_message(bdata->ctdb, dest, srvid, data);
117 static void send_start_messages(struct ctdb_context *ctdb, int incr)
119 /* two messages are injected into the ring, moving
120 in opposite directions */
124 data.dptr = (uint8_t *)&incr;
125 data.dsize = sizeof(incr);
127 dest = (ctdb_get_pnn(ctdb) + num_nodes + incr) % num_nodes;
128 ctdb_client_send_message(ctdb, dest, 0, data);
131 static void each_second(struct tevent_context *ev, struct tevent_timer *te,
132 struct timeval t, void *private_data)
134 struct bench_data *bdata = talloc_get_type_abort(
135 private_data, struct bench_data);
137 /* we kickstart the ring into action by inserting messages from node
139 it may happen that some other node does not yet have ctdb_bench
140 running in which case the ring is broken and the messages are lost.
141 if so, once every second try again to restart the ring
143 if (bdata->msg_plus == 0) {
144 // printf("no messages recevied, try again to kickstart the ring in forward direction...\n");
145 send_start_messages(bdata->ctdb, 1);
147 if (bdata->msg_minus == 0) {
148 // printf("no messages recevied, try again to kickstart the ring in reverse direction...\n");
149 send_start_messages(bdata->ctdb, -1);
151 tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
155 static void dummy_event(struct tevent_context *ev, struct tevent_timer *te,
156 struct timeval t, void *private_data)
158 struct bench_data *bdata = talloc_get_type_abort(
159 private_data, struct bench_data);
161 tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
166 benchmark sending messages in a ring around the nodes
168 static void bench_ring(struct bench_data *bdata)
170 int pnn = ctdb_get_pnn(bdata->ctdb);
173 tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
176 tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
181 while (end_timer() < timelimit) {
182 if (pnn == 0 && bdata->msg_count % 10000 == 0 && end_timer() > 0) {
183 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\r",
184 bdata->msg_count/end_timer(),
185 bdata->msg_plus, bdata->msg_minus);
188 tevent_loop_once(bdata->ev);
191 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\n",
192 bdata->msg_count/end_timer(),
193 bdata->msg_plus, bdata->msg_minus);
199 int main(int argc, const char *argv[])
201 struct ctdb_context *ctdb;
202 struct ctdb_db_context *ctdb_db;
204 struct poptOption popt_options[] = {
207 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
208 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
209 { NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
213 const char **extra_argv;
217 struct tevent_context *ev;
218 struct bench_data *bdata;
220 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
222 while ((opt = poptGetNextOpt(pc)) != -1) {
225 fprintf(stderr, "Invalid option %s: %s\n",
226 poptBadOption(pc, 0), poptStrerror(opt));
231 /* setup the remaining options for the main program to use */
232 extra_argv = poptGetArgs(pc);
235 while (extra_argv[extra_argc]) extra_argc++;
238 if (num_nodes == 0) {
239 printf("You must specify the number of nodes\n");
243 ev = tevent_context_init(NULL);
245 /* initialise ctdb */
246 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
251 /* attach to a specific database */
252 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
255 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
259 /* setup a ctdb call function */
260 ret = ctdb_set_call(ctdb_db, incr_func, FUNC_INCR);
262 DEBUG(DEBUG_DEBUG,("ctdb_set_call() failed, ignoring return code %d\n", ret));
264 ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
266 DEBUG(DEBUG_DEBUG,("ctdb_set_call() failed, ignoring return code %d\n", ret));
269 bdata = talloc_zero(ctdb, struct bench_data);
276 if (ctdb_client_set_message_handler(ctdb, 0, ring_message_handler, bdata))
279 printf("Waiting for cluster\n");
282 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
283 if (recmode == 0) break;
284 tevent_loop_once(ev);