Patch to remove segfault on the exiting of a service.
[openais.git] / test / evtbench.c
blob41ed099cdf961f590bd36179d8320f2a5bb8e493
1 /*
2 * Test program for event service
3 */
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/poll.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #ifndef OPENAIS_SOLARIS
11 #include <stdint.h>
12 #include <getopt.h>
13 #else
14 #include <sys/types.h>
15 #endif
16 #include <stdlib.h>
17 #include <sys/time.h>
18 #include "saAis.h"
19 #include "saEvt.h"
21 // #define EVENT_SUBSCRIBE
23 #ifdef OPENAIS_SOLARIS
24 #define timersub(a, b, result) \
25 do { \
26 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
27 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
28 if ((result)->tv_usec < 0) { \
29 --(result)->tv_sec; \
30 (result)->tv_usec += 1000000; \
31 } \
32 } while (0)
33 #endif
35 SaVersionT version = { 'B', 0x01, 0x01 };
37 void event_callback( SaEvtSubscriptionIdT subscriptionId,
38 const SaEvtEventHandleT eventHandle,
39 const SaSizeT eventDataSize);
41 SaEvtCallbacksT callbacks = {
43 event_callback
47 char channel[256] = "EVENT_TEST_CHANNEL";
48 unsigned int subscription_id = 0xfedcba98;
49 unsigned long long ret_time = 0000000000ULL; /* 0 seconds */
50 char pubname[256] = "Test Pub Name";
53 #define _patt1 "Filter pattern 1"
54 #define patt1 (SaUint8T *) _patt1
55 #define patt1_size sizeof(_patt1)
57 #define _patt2 "Filter pattern 2"
58 #define patt2 (SaUint8T *) _patt2
59 #define patt2_size sizeof(_patt2)
61 #define _patt3 "Filter pattern 3"
62 #define patt3 (SaUint8T *) _patt3
63 #define patt3_size sizeof(_patt3)
65 #define _patt4 "Filter pattern 4"
66 #define patt4 (SaUint8T *) _patt4
67 #define patt4_size sizeof(_patt4)
69 SaEvtEventFilterT filters[] = {
70 {SA_EVT_PREFIX_FILTER, {patt1_size, patt1_size, patt1}},
71 {SA_EVT_SUFFIX_FILTER, {patt2_size, patt2_size, patt2}},
72 {SA_EVT_EXACT_FILTER, {patt3_size, patt3_size, patt3}},
73 {SA_EVT_PASS_ALL_FILTER, {patt4_size, patt4_size, patt4}}
76 SaEvtEventFilterArrayT subscribe_filters = {
77 sizeof(filters)/sizeof(SaEvtEventFilterT),
78 filters
82 SaUint8T pat0[100];
83 SaUint8T pat1[100];
84 SaUint8T pat2[100];
85 SaUint8T pat3[100];
86 SaUint8T pat4[100];
87 SaEvtEventPatternT evt_patts[5] = {
88 {100, 100, pat0},
89 {100, 100, pat1},
90 {100, 100, pat2},
91 {100, 100, pat3},
92 {100, 100, pat4}};
93 SaEvtEventPatternArrayT evt_pat_get_array = { 100, 0, evt_patts };
95 SaEvtEventPatternT patterns[] = {
96 {patt1_size, patt1_size, patt1},
97 {patt2_size, patt2_size, patt2},
98 {patt3_size, patt3_size, patt3},
99 {patt4_size, patt4_size, patt4}
101 SaNameT test_pub_name;
102 #define TEST_PRIORITY 2
104 SaEvtEventPatternArrayT evt_pat_set_array = {
105 sizeof(patterns)/sizeof(SaEvtEventPatternT),
106 sizeof(patterns)/sizeof(SaEvtEventPatternT),
107 patterns
110 char user_data_file[256];
111 char user_data[100000];
112 int user_data_size = 50000;
114 uint64_t clust_time_now(void)
116 struct timeval tv;
117 uint64_t time_now;
119 if (gettimeofday(&tv, 0)) {
120 return 0ULL;
123 time_now = (uint64_t)(tv.tv_sec) * 1000000000ULL;
124 time_now += (uint64_t)(tv.tv_usec) * 1000ULL;
126 return time_now;
129 void
130 test_pub()
132 SaEvtHandleT handle;
133 SaEvtChannelHandleT channel_handle;
134 SaEvtEventHandleT event_handle;
135 SaEvtChannelOpenFlagsT flags;
136 SaNameT channel_name;
137 uint64_t test_retention;
138 SaSelectionObjectT fd;
139 int i;
140 struct timeval tv1, tv2, tv_elapsed;
141 int write_count = 10000;
142 int write_size = user_data_size;
145 SaEvtEventIdT event_id;
146 #ifdef EVENT_SUBSCRIBE
147 struct pollfd pfd;
148 int nfd;
149 int timeout = 1000;
150 #endif
154 int result;
156 flags = SA_EVT_CHANNEL_PUBLISHER |
157 #ifdef EVENT_SUBSCRIBE
158 SA_EVT_CHANNEL_SUBSCRIBER |
159 #endif
160 SA_EVT_CHANNEL_CREATE;
161 strcpy((char *)channel_name.value, channel);
162 channel_name.length = strlen(channel);
165 result = saEvtInitialize (&handle, &callbacks, &version);
166 if (result != SA_AIS_OK) {
167 printf("Event Initialize result: %d\n", result);
168 exit(1);
170 result = saEvtChannelOpen(handle, &channel_name, flags,
171 SA_TIME_MAX, &channel_handle);
172 if (result != SA_AIS_OK) {
173 printf("channel open result: %d\n", result);
174 goto evt_fin;
178 * Publish with pattens
180 printf("Publish\n");
182 #ifdef EVENT_SUBSCRIBE
183 result = saEvtEventSubscribe(channel_handle,
184 &subscribe_filters,
185 subscription_id);
187 if (result != SA_AIS_OK) {
188 printf("event subscribe result: %d\n", result);
189 result = saEvtChannelClose(channel_handle);
190 if (result != SA_AIS_OK)
191 printf("Channel close result: %d\n", result);
192 result = saEvtFinalize(handle);
193 if (result != SA_AIS_OK)
194 printf("Finalize result: %d\n", result);
195 return;
197 #endif
198 result = saEvtEventAllocate(channel_handle, &event_handle);
199 if (result != SA_AIS_OK) {
200 printf("event Allocate result: %d\n", result);
201 goto evt_free;
204 strcpy((char *)test_pub_name.value, pubname);
205 test_pub_name.length = strlen(pubname);
206 test_retention = ret_time;
207 result = saEvtEventAttributesSet(event_handle,
208 &evt_pat_set_array,
209 TEST_PRIORITY,
210 test_retention,
211 &test_pub_name);
212 if (result != SA_AIS_OK) {
213 printf("event set attr result(2): %d\n", result);
214 goto evt_free;
217 gettimeofday (&tv1, NULL);
218 for (i = 0; i < write_count; i++) {
219 result = saEvtEventPublish(event_handle, user_data,
220 write_size, &event_id);
221 if (result != SA_AIS_OK) {
222 printf("event Publish result(2): %d\n", result);
223 goto evt_close;
226 gettimeofday (&tv2, NULL);
227 timersub (&tv2, &tv1, &tv_elapsed);
229 printf ("%5d Writes ", write_count);
230 printf ("%5d bytes per write ", write_size);
231 printf ("%7.3f Seconds runtime ",
232 (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
233 printf ("%9.3f TP/s ",
234 ((float)write_count) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
235 printf ("%7.3f MB/s.\n",
236 ((float)write_count) * ((float)write_size) / ((tv_elapsed.tv_sec + (
237 tv_elapsed.tv_usec / 1000000.0)) * 1000000.0));
239 exit (1);
240 printf("Published event ID: %llx\n", (unsigned long long)event_id);
243 * See if we got the event
245 result = saEvtSelectionObjectGet(handle, &fd);
246 if (result != SA_AIS_OK) {
247 printf("saEvtSelectionObject get %d\n", result);
248 /* error */
249 return;
251 #ifdef EVENT_SUBSCRIBE
252 pfd.fd = fd;
253 pfd.events = POLLIN;
254 nfd = poll(&pfd, 1, timeout);
255 if (nfd <= 0) {
256 printf("poll fds %d\n", nfd);
257 if (nfd < 0) {
258 perror("poll error");
260 goto evt_free;
263 printf("Got poll event\n");
264 result = saEvtDispatch(handle, SA_DISPATCH_ONE);
265 if (result != SA_AIS_OK) {
266 printf("saEvtDispatch %d\n", result);
267 goto evt_fin;
269 #endif
273 * Test cleanup
275 evt_free:
276 result = saEvtEventFree(event_handle);
277 if (result != SA_AIS_OK) {
278 printf("event free result: %d\n", result);
281 evt_close:
282 result = saEvtChannelClose(channel_handle);
284 if (result != SA_AIS_OK) {
285 printf("channel close result: %d\n", result);
287 evt_fin:
288 result = saEvtFinalize(handle);
290 if (result != SA_AIS_OK) {
291 printf("Event Finalize result: %d\n", result);
293 printf("Done\n");
297 void
298 event_callback( SaEvtSubscriptionIdT subscription_id,
299 const SaEvtEventHandleT event_handle,
300 const SaSizeT event_data_size)
302 SaAisErrorT result;
303 SaUint8T priority;
304 SaTimeT retention_time;
305 SaNameT publisher_name = {0, {0}};
306 SaTimeT publish_time;
307 SaEvtEventIdT event_id;
308 int i;
310 printf("event_callback called\n");
311 printf("sub ID: %x\n", subscription_id);
312 printf("event_handle %llx\n", (unsigned long long)event_handle);
313 printf("event data size %llu\n", (unsigned long long)event_data_size);
315 evt_pat_get_array.patternsNumber = 4;
316 result = saEvtEventAttributesGet(event_handle,
317 &evt_pat_get_array, /* patterns */
318 &priority, /* priority */
319 &retention_time, /* retention time */
320 &publisher_name, /* publisher name */
321 &publish_time, /* publish time */
322 &event_id /* event_id */
324 if (result != SA_AIS_OK) {
325 printf("event get attr result(2): %d\n", result);
326 goto evt_free;
328 printf("pattern array count: %llu\n",
329 (unsigned long long)evt_pat_get_array.patternsNumber);
330 for (i = 0; i < evt_pat_get_array.patternsNumber; i++) {
331 printf( "pattern %d =\"%s\"\n", i,
332 evt_pat_get_array.patterns[i].pattern);
335 printf("priority: 0x%x\n", priority);
336 printf("retention: %llx\n", (unsigned long long)retention_time);
337 printf("publisher name content: \"%s\"\n", publisher_name.value);
338 printf("event id: %llx\n", (unsigned long long)event_id);
339 evt_free:
340 result = saEvtEventFree(event_handle);
341 printf("event free result: %d\n", result);
345 int main (int argc, char **argv)
347 static const char opts[] = "c:i:t:n:x:u:";
349 int pub_count = 1;
350 int option;
352 while (1) {
353 option = getopt(argc, argv, opts);
354 if (option == -1)
355 break;
357 switch (option) {
358 case 'u': {
359 int fd;
360 int sz;
362 strcpy(user_data_file, optarg);
363 fd = open(user_data_file, O_RDONLY);
364 if (fd < 0) {
365 printf("Can't open user data file %s\n",
366 user_data_file);
367 exit(1);
369 sz = read(fd, user_data, 65536);
370 if (sz < 0) {
371 perror("subscription\n");
372 exit(1);
374 close(fd);
375 user_data_size = sz;
376 break;
379 case 'c':
380 strcpy(channel, optarg);
381 break;
382 case 'n':
383 strcpy(pubname, optarg);
384 break;
385 case 'i':
386 subscription_id =
387 (unsigned int)strtoul(optarg, NULL, 0);
388 break;
389 case 't':
390 ret_time = strtoull(optarg, NULL, 0);
391 ret_time *= 1000000000;
392 break;
393 case 'x':
394 pub_count = strtoul(optarg, NULL, 0);
395 break;
396 default:
397 printf("invalid arg: \"%s\"\n", optarg);
398 return 1;
402 while (pub_count--) {
403 test_pub();
406 return 0;