cpgbench.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2006, 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <signal.h>
  39. #include <unistd.h>
  40. #include <errno.h>
  41. #include <time.h>
  42. #include <sys/time.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <sys/select.h>
  46. #include <sys/uio.h>
  47. #include <sys/un.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <pthread.h>
  51. #include <qb/qblog.h>
  52. #include <qb/qbutil.h>
  53. #include <corosync/corotypes.h>
  54. #include <corosync/cpg.h>
  55. static cpg_handle_t handle;
  56. static pthread_t thread;
  57. #ifndef timersub
  58. #define timersub(a, b, result) \
  59. do { \
  60. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  61. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  62. if ((result)->tv_usec < 0) { \
  63. --(result)->tv_sec; \
  64. (result)->tv_usec += 1000000; \
  65. } \
  66. } while (0)
  67. #endif /* timersub */
  68. static int alarm_notice;
  69. static void cpg_bm_confchg_fn (
  70. cpg_handle_t handle_in,
  71. const struct cpg_name *group_name,
  72. const struct cpg_address *member_list, size_t member_list_entries,
  73. const struct cpg_address *left_list, size_t left_list_entries,
  74. const struct cpg_address *joined_list, size_t joined_list_entries)
  75. {
  76. }
  77. static unsigned int write_count;
  78. static void cpg_bm_deliver_fn (
  79. cpg_handle_t handle_in,
  80. const struct cpg_name *group_name,
  81. uint32_t nodeid,
  82. uint32_t pid,
  83. void *msg,
  84. size_t msg_len)
  85. {
  86. write_count++;
  87. }
  88. static cpg_callbacks_t callbacks = {
  89. .cpg_deliver_fn = cpg_bm_deliver_fn,
  90. .cpg_confchg_fn = cpg_bm_confchg_fn
  91. };
  92. #define ONE_MEG 1048576
  93. static char data[ONE_MEG];
  94. static void cpg_benchmark (
  95. cpg_handle_t handle_in,
  96. int write_size)
  97. {
  98. struct timeval tv1, tv2, tv_elapsed;
  99. struct iovec iov;
  100. unsigned int res;
  101. alarm_notice = 0;
  102. iov.iov_base = data;
  103. iov.iov_len = write_size;
  104. write_count = 0;
  105. alarm (10);
  106. gettimeofday (&tv1, NULL);
  107. do {
  108. res = cpg_mcast_joined (handle_in, CPG_TYPE_AGREED, &iov, 1);
  109. } while (alarm_notice == 0 && (res == CS_OK || res == CS_ERR_TRY_AGAIN));
  110. gettimeofday (&tv2, NULL);
  111. timersub (&tv2, &tv1, &tv_elapsed);
  112. printf ("%5d messages received ", write_count);
  113. printf ("%5d bytes per write ", write_size);
  114. printf ("%7.3f Seconds runtime ",
  115. (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  116. printf ("%9.3f TP/s ",
  117. ((float)write_count) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  118. printf ("%7.3f MB/s.\n",
  119. ((float)write_count) * ((float)write_size) / ((tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)) * 1000000.0));
  120. }
  121. static void sigalrm_handler (int num)
  122. {
  123. alarm_notice = 1;
  124. }
  125. static struct cpg_name group_name = {
  126. .value = "cpg_bm",
  127. .length = 6
  128. };
  129. static void* dispatch_thread (void *arg)
  130. {
  131. cpg_dispatch (handle, CS_DISPATCH_BLOCKING);
  132. return NULL;
  133. }
  134. int main (void) {
  135. unsigned int size;
  136. int i;
  137. unsigned int res;
  138. qb_log_init("cpgbench", LOG_USER, LOG_EMERG);
  139. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
  140. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  141. QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
  142. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  143. size = 64;
  144. signal (SIGALRM, sigalrm_handler);
  145. res = cpg_initialize (&handle, &callbacks);
  146. if (res != CS_OK) {
  147. printf ("cpg_initialize failed with result %d\n", res);
  148. exit (1);
  149. }
  150. pthread_create (&thread, NULL, dispatch_thread, NULL);
  151. res = cpg_join (handle, &group_name);
  152. if (res != CS_OK) {
  153. printf ("cpg_join failed with result %d\n", res);
  154. exit (1);
  155. }
  156. for (i = 0; i < 10; i++) { /* number of repetitions - up to 50k */
  157. cpg_benchmark (handle, size);
  158. signal (SIGALRM, sigalrm_handler);
  159. size *= 5;
  160. if (size >= (ONE_MEG - 100)) {
  161. break;
  162. }
  163. }
  164. res = cpg_finalize (handle);
  165. if (res != CS_OK) {
  166. printf ("cpg_finalize failed with result %d\n", res);
  167. exit (1);
  168. }
  169. return (0);
  170. }