2 * Test program for event service
10 #ifndef OPENAIS_SOLARIS
14 #include <sys/types.h>
21 // #define EVENT_SUBSCRIBE
23 #ifdef OPENAIS_SOLARIS
24 #define timersub(a, b, result) \
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) { \
30 (result)->tv_usec += 1000000; \
35 SaVersionT version
= { 'B', 0x01, 0x01 };
37 void event_callback( SaEvtSubscriptionIdT subscriptionId
,
38 const SaEvtEventHandleT eventHandle
,
39 const SaSizeT eventDataSize
);
41 SaEvtCallbacksT callbacks
= {
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
),
87 SaEvtEventPatternT evt_patts
[5] = {
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
),
110 char user_data_file
[256];
111 char user_data
[100000];
112 int user_data_size
= 50000;
114 uint64_t clust_time_now(void)
119 if (gettimeofday(&tv
, 0)) {
123 time_now
= (uint64_t)(tv
.tv_sec
) * 1000000000ULL;
124 time_now
+= (uint64_t)(tv
.tv_usec
) * 1000ULL;
133 SaEvtChannelHandleT channel_handle
;
134 SaEvtEventHandleT event_handle
;
135 SaEvtChannelOpenFlagsT flags
;
136 SaNameT channel_name
;
137 uint64_t test_retention
;
138 SaSelectionObjectT fd
;
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
156 flags
= SA_EVT_CHANNEL_PUBLISHER
|
157 #ifdef EVENT_SUBSCRIBE
158 SA_EVT_CHANNEL_SUBSCRIBER
|
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
);
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
);
178 * Publish with pattens
182 #ifdef EVENT_SUBSCRIBE
183 result
= saEvtEventSubscribe(channel_handle
,
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
);
198 result
= saEvtEventAllocate(channel_handle
, &event_handle
);
199 if (result
!= SA_AIS_OK
) {
200 printf("event Allocate result: %d\n", result
);
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
,
212 if (result
!= SA_AIS_OK
) {
213 printf("event set attr result(2): %d\n", result
);
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
);
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));
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
);
251 #ifdef EVENT_SUBSCRIBE
254 nfd
= poll(&pfd
, 1, timeout
);
256 printf("poll fds %d\n", nfd
);
258 perror("poll error");
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
);
276 result
= saEvtEventFree(event_handle
);
277 if (result
!= SA_AIS_OK
) {
278 printf("event free result: %d\n", result
);
282 result
= saEvtChannelClose(channel_handle
);
284 if (result
!= SA_AIS_OK
) {
285 printf("channel close result: %d\n", result
);
288 result
= saEvtFinalize(handle
);
290 if (result
!= SA_AIS_OK
) {
291 printf("Event Finalize result: %d\n", result
);
298 event_callback( SaEvtSubscriptionIdT subscription_id
,
299 const SaEvtEventHandleT event_handle
,
300 const SaSizeT event_data_size
)
304 SaTimeT retention_time
;
305 SaNameT publisher_name
= {0, {0}};
306 SaTimeT publish_time
;
307 SaEvtEventIdT event_id
;
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
);
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
);
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:";
353 option
= getopt(argc
, argv
, opts
);
362 strcpy(user_data_file
, optarg
);
363 fd
= open(user_data_file
, O_RDONLY
);
365 printf("Can't open user data file %s\n",
369 sz
= read(fd
, user_data
, 65536);
371 perror("subscription\n");
380 strcpy(channel
, optarg
);
383 strcpy(pubname
, optarg
);
387 (unsigned int)strtoul(optarg
, NULL
, 0);
390 ret_time
= strtoull(optarg
, NULL
, 0);
391 ret_time
*= 1000000000;
394 pub_count
= strtoul(optarg
, NULL
, 0);
397 printf("invalid arg: \"%s\"\n", optarg
);
402 while (pub_count
--) {