Expose confdb write to the library.
[openais.git] / test / cpgbench.c
blobbdc99289bddb1047ff6df5976ec2e1e29e9f435b
1 #define _BSD_SOURCE
2 /*
3 * Copyright (c) 2006 Red Hat, Inc.
5 * All rights reserved.
7 * Author: Steven Dake (sdake@mvista.com)
9 * This software licensed under BSD license, the text of which follows:
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
14 * - Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * - Neither the name of the MontaVista Software, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived from this
21 * software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <time.h>
44 #include <sys/time.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/select.h>
48 #include <sys/un.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
53 #include "saAis.h"
54 #include "cpg.h"
56 #ifdef OPENAIS_SOLARIS
57 #define timersub(a, b, result) \
58 do { \
59 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
60 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
61 if ((result)->tv_usec < 0) { \
62 --(result)->tv_sec; \
63 (result)->tv_usec += 1000000; \
64 } \
65 } while (0)
66 #endif
68 int alarm_notice;
70 void cpg_bm_confchg_fn (
71 cpg_handle_t handle,
72 struct cpg_name *group_name,
73 struct cpg_address *member_list, int member_list_entries,
74 struct cpg_address *left_list, int left_list_entries,
75 struct cpg_address *joined_list, int joined_list_entries)
79 unsigned int write_count;
81 void cpg_bm_deliver_fn (
82 cpg_handle_t handle,
83 struct cpg_name *group_name,
84 uint32_t nodeid,
85 uint32_t pid,
86 void *msg,
87 int msg_len)
89 write_count++;
92 cpg_callbacks_t callbacks = {
93 .cpg_deliver_fn = cpg_bm_deliver_fn,
94 .cpg_confchg_fn = cpg_bm_confchg_fn
97 char data[500000];
99 void cpg_benchmark (
100 cpg_handle_t handle,
101 int write_size)
103 struct timeval tv1, tv2, tv_elapsed;
104 struct iovec iov;
105 unsigned int res;
106 cpg_flow_control_state_t flow_control_state;
108 alarm_notice = 0;
109 iov.iov_base = data;
110 iov.iov_len = write_size;
112 write_count = 0;
113 alarm (10);
115 gettimeofday (&tv1, NULL);
116 do {
118 * Test checkpoint write
120 cpg_flow_control_state_get (handle, &flow_control_state);
121 if (flow_control_state == CPG_FLOW_CONTROL_DISABLED) {
122 retry:
123 res = cpg_mcast_joined (handle, CPG_TYPE_AGREED, &iov, 1);
124 if (res == CPG_ERR_TRY_AGAIN) {
125 goto retry;
128 res = cpg_dispatch (handle, CPG_DISPATCH_ALL);
129 if (res != CPG_OK) {
130 printf ("cpg dispatch returned error %d\n", res);
131 exit (1);
133 } while (alarm_notice == 0);
134 gettimeofday (&tv2, NULL);
135 timersub (&tv2, &tv1, &tv_elapsed);
137 printf ("%5d messages received ", write_count);
138 printf ("%5d bytes per write ", write_size);
139 printf ("%7.3f Seconds runtime ",
140 (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
141 printf ("%9.3f TP/s ",
142 ((float)write_count) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
143 printf ("%7.3f MB/s.\n",
144 ((float)write_count) * ((float)write_size) / ((tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)) * 1000000.0));
147 void sigalrm_handler (int num)
149 alarm_notice = 1;
152 static struct cpg_name group_name = {
153 .value = "cpg_bm",
154 .length = 6
157 int main (void) {
158 cpg_handle_t handle;
159 unsigned int size = 1;
160 int i;
161 unsigned int res;
163 signal (SIGALRM, sigalrm_handler);
164 res = cpg_initialize (&handle, &callbacks);
165 if (res != CPG_OK) {
166 printf ("cpg_initialize failed with result %d\n", res);
167 exit (1);
170 res = cpg_join (handle, &group_name);
171 if (res != CPG_OK) {
172 printf ("cpg_join failed with result %d\n", res);
173 exit (1);
176 for (i = 0; i < 50; i++) { /* number of repetitions - up to 50k */
177 cpg_benchmark (handle, size);
178 size += 1000;
181 res = cpg_finalize (handle);
182 if (res != CPG_OK) {
183 printf ("cpg_join failed with result %d\n", res);
184 exit (1);
186 return (0);