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 "ctdb_logging.h"
32 #include "ctdb_private.h"
33 #include "ctdb_client.h"
35 #include "common/cmdline.h"
36 #include "common/common.h"
38 static struct timeval tp1,tp2;
40 static void start_timer(void)
42 gettimeofday(&tp1,NULL);
45 static double end_timer(void)
47 gettimeofday(&tp2,NULL);
48 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
49 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
53 static int timelimit = 10;
54 static int num_records = 10;
57 enum my_functions {FUNC_INCR=1, FUNC_FETCH=2};
60 ctdb call function to increment an integer
62 static int incr_func(struct ctdb_call_info *call)
64 if (call->record_data.dsize == 0) {
65 call->new_data = talloc(call, TDB_DATA);
66 if (call->new_data == NULL) {
67 return CTDB_ERR_NOMEM;
69 call->new_data->dptr = talloc_size(call, 4);
70 call->new_data->dsize = 4;
71 *(uint32_t *)call->new_data->dptr = 0;
73 call->new_data = &call->record_data;
75 (*(uint32_t *)call->new_data->dptr)++;
80 ctdb call function to fetch a record
82 static int fetch_func(struct ctdb_call_info *call)
84 call->reply_data = &call->record_data;
90 struct ctdb_context *ctdb;
91 struct tevent_context *ev;
93 int msg_plus, msg_minus;
97 handler for messages in bench_ring()
99 static void ring_message_handler(uint64_t srvid, TDB_DATA data,
102 struct bench_data *bdata = talloc_get_type_abort(
103 private_data, struct bench_data);
104 int incr = *(int *)data.dptr;
108 dest = (ctdb_get_pnn(bdata->ctdb) + num_nodes + incr) % num_nodes;
109 ctdb_client_send_message(bdata->ctdb, dest, srvid, data);
118 static void send_start_messages(struct ctdb_context *ctdb, int incr)
120 /* two messages are injected into the ring, moving
121 in opposite directions */
125 data.dptr = (uint8_t *)&incr;
126 data.dsize = sizeof(incr);
128 dest = (ctdb_get_pnn(ctdb) + num_nodes + incr) % num_nodes;
129 ctdb_client_send_message(ctdb, dest, 0, data);
132 static void each_second(struct tevent_context *ev, struct tevent_timer *te,
133 struct timeval t, void *private_data)
135 struct bench_data *bdata = talloc_get_type_abort(
136 private_data, struct bench_data);
138 /* we kickstart the ring into action by inserting messages from node
140 it may happen that some other node does not yet have ctdb_bench
141 running in which case the ring is broken and the messages are lost.
142 if so, once every second try again to restart the ring
144 if (bdata->msg_plus == 0) {
145 // printf("no messages recevied, try again to kickstart the ring in forward direction...\n");
146 send_start_messages(bdata->ctdb, 1);
148 if (bdata->msg_minus == 0) {
149 // printf("no messages recevied, try again to kickstart the ring in reverse direction...\n");
150 send_start_messages(bdata->ctdb, -1);
152 tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
156 static void dummy_event(struct tevent_context *ev, struct tevent_timer *te,
157 struct timeval t, void *private_data)
159 struct bench_data *bdata = talloc_get_type_abort(
160 private_data, struct bench_data);
162 tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
167 benchmark sending messages in a ring around the nodes
169 static void bench_ring(struct bench_data *bdata)
171 int pnn = ctdb_get_pnn(bdata->ctdb);
174 tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
177 tevent_add_timer(bdata->ev, bdata, timeval_current_ofs(1, 0),
182 while (end_timer() < timelimit) {
183 if (pnn == 0 && bdata->msg_count % 10000 == 0 && end_timer() > 0) {
184 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\r",
185 bdata->msg_count/end_timer(),
186 bdata->msg_plus, bdata->msg_minus);
189 tevent_loop_once(bdata->ev);
192 printf("Ring: %.2f msgs/sec (+ve=%d -ve=%d)\n",
193 bdata->msg_count/end_timer(),
194 bdata->msg_plus, bdata->msg_minus);
200 int main(int argc, const char *argv[])
202 struct ctdb_context *ctdb;
203 struct ctdb_db_context *ctdb_db;
205 struct poptOption popt_options[] = {
208 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "integer" },
209 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
210 { NULL, 'n', POPT_ARG_INT, &num_nodes, 0, "num_nodes", "integer" },
214 const char **extra_argv;
218 struct tevent_context *ev;
219 struct bench_data *bdata;
221 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
223 while ((opt = poptGetNextOpt(pc)) != -1) {
226 fprintf(stderr, "Invalid option %s: %s\n",
227 poptBadOption(pc, 0), poptStrerror(opt));
232 /* setup the remaining options for the main program to use */
233 extra_argv = poptGetArgs(pc);
236 while (extra_argv[extra_argc]) extra_argc++;
239 if (num_nodes == 0) {
240 printf("You must specify the number of nodes\n");
244 ev = tevent_context_init(NULL);
246 /* initialise ctdb */
247 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
252 /* attach to a specific database */
253 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
256 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
260 /* setup a ctdb call function */
261 ret = ctdb_set_call(ctdb_db, incr_func, FUNC_INCR);
263 DEBUG(DEBUG_DEBUG,("ctdb_set_call() failed, ignoring return code %d\n", ret));
265 ret = ctdb_set_call(ctdb_db, fetch_func, FUNC_FETCH);
267 DEBUG(DEBUG_DEBUG,("ctdb_set_call() failed, ignoring return code %d\n", ret));
270 bdata = talloc_zero(ctdb, struct bench_data);
277 if (ctdb_client_set_message_handler(ctdb, 0, ring_message_handler, bdata))
280 printf("Waiting for cluster\n");
283 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
284 if (recmode == 0) break;
285 tevent_loop_once(ev);