ckptbenchth.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #define _BSD_SOURCE
  2. /*
  3. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <errno.h>
  40. #include <unistd.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/un.h>
  47. #include <pthread.h>
  48. #include <assert.h>
  49. #include <signal.h>
  50. #include "saAis.h"
  51. #include "saCkpt.h"
  52. #include "sa_error.h"
  53. #ifdef OPENAIS_SOLARIS
  54. #define timersub(a, b, result) \
  55. do { \
  56. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  57. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  58. if ((result)->tv_usec < 0) { \
  59. --(result)->tv_sec; \
  60. (result)->tv_usec += 1000000; \
  61. } \
  62. } while (0)
  63. #endif
  64. int alarm_notice = 0;
  65. void fail_on_error(SaAisErrorT error, char* opName) {
  66. if (error != SA_AIS_OK) {
  67. printf ("%s: result %s\n", opName, get_sa_error_b(error));
  68. exit (1);
  69. }
  70. }
  71. void printSaNameT (SaNameT *name)
  72. {
  73. int i;
  74. for (i = 0; i < name->length; i++) {
  75. printf ("%c", name->value[i]);
  76. }
  77. }
  78. SaVersionT version = { 'B', 1, 1 };
  79. SaCkptCallbacksT callbacks = {
  80. 0,
  81. 0
  82. };
  83. SaCkptCheckpointCreationAttributesT checkpointCreationAttributes = {
  84. .creationFlags = SA_CKPT_WR_ALL_REPLICAS,
  85. .checkpointSize = 100000,
  86. .retentionDuration = 0,
  87. .maxSections = 5,
  88. .maxSectionSize = 150000,
  89. .maxSectionIdSize = 15
  90. };
  91. SaCkptSectionIdT sectionId1 = {
  92. 14,
  93. (SaUint8T *) "section ID #1"
  94. };
  95. SaCkptSectionIdT sectionId2 = {
  96. 14,
  97. (SaUint8T *) "section ID #2"
  98. };
  99. SaCkptSectionCreationAttributesT sectionCreationAttributes1 = {
  100. &sectionId1,
  101. 0xFFFFFFFF
  102. };
  103. SaCkptSectionCreationAttributesT sectionCreationAttributes2 = {
  104. &sectionId2,
  105. 0xFFFFFFFF
  106. };
  107. char readBuffer1[1025];
  108. char readBuffer2[1025];
  109. SaCkptIOVectorElementT ReadVectorElements[] = {
  110. {
  111. {
  112. 14,
  113. (SaUint8T *) "section ID #1"
  114. },
  115. readBuffer1,
  116. sizeof (readBuffer1),
  117. 0,
  118. 0
  119. },
  120. {
  121. {
  122. 14,
  123. (SaUint8T *) "section ID #2"
  124. },
  125. readBuffer2,
  126. sizeof (readBuffer2),
  127. 0,
  128. 0
  129. }
  130. };
  131. #define DATASIZE 1000
  132. #define LOOPS 5000
  133. char data[500000];
  134. SaCkptIOVectorElementT WriteVectorElements[] = {
  135. {
  136. {
  137. 14,
  138. (SaUint8T *) "section ID #1"
  139. },
  140. data, /*"written data #1, this should extend past end of old section data", */
  141. DATASIZE, /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  142. 0, //5,
  143. 0
  144. }
  145. #ifdef COMPILE_OUT
  146. {
  147. {
  148. 14,
  149. (SaUint8T *) "section ID #2"
  150. },
  151. data, /*"written data #2, this should extend past end of old section data" */
  152. DATASIZE, /*sizeof ("written data #2, this should extend past end of old section data") + 1, */
  153. 0, //3,
  154. 0
  155. }
  156. #endif
  157. };
  158. int runs = 0;
  159. struct threaddata {
  160. SaCkptHandleT ckpt_handle;
  161. SaCkptCheckpointHandleT checkpoint_handle;
  162. int write_size;
  163. int thread;
  164. pthread_attr_t thread_attr;
  165. pthread_t thread_id;
  166. int written;
  167. };
  168. extern void pthread_exit(void *) __attribute__((noreturn));
  169. void *benchmark_thread (void *arg)
  170. {
  171. SaCkptCheckpointHandleT checkpoint_handle;
  172. SaCkptHandleT ckpt_handle;
  173. int write_size;
  174. SaAisErrorT error;
  175. SaUint32T erroroneousVectorIndex = 0;
  176. struct threaddata *td = (struct threaddata *)arg;
  177. checkpoint_handle = td->checkpoint_handle;
  178. ckpt_handle = td->ckpt_handle;
  179. write_size = td->write_size;
  180. WriteVectorElements[0].dataSize = write_size;
  181. do {
  182. /*
  183. * Test checkpoint write
  184. */
  185. do {
  186. error = saCkptCheckpointWrite (checkpoint_handle,
  187. WriteVectorElements,
  188. 1,
  189. &erroroneousVectorIndex);
  190. } while (error == SA_AIS_ERR_TRY_AGAIN);
  191. fail_on_error(error, "saCkptCheckpointWrite");
  192. td->written += 1;
  193. } while (alarm_notice == 0);
  194. pthread_exit (0);
  195. }
  196. void threaded_bench (
  197. SaCkptHandleT *ckpt_handles,
  198. SaCkptCheckpointHandleT *checkpoint_handles,
  199. int threads,
  200. int write_size)
  201. {
  202. struct timeval tv1, tv2, tv_elapsed;
  203. struct threaddata td[100];
  204. int i;
  205. int res;
  206. int written = 0;
  207. runs = threads;
  208. gettimeofday (&tv1, NULL);
  209. for (i = 0; i < threads; i++) {
  210. td[i].ckpt_handle = ckpt_handles[i];
  211. td[i].checkpoint_handle = checkpoint_handles[i];
  212. td[i].write_size = write_size;
  213. td[i].thread = i;
  214. td[i].written = 0;
  215. pthread_attr_init (&td[i].thread_attr);
  216. pthread_attr_setstacksize (&td[i].thread_attr, 16384);
  217. pthread_attr_setdetachstate (&td[i].thread_attr, PTHREAD_CREATE_JOINABLE);
  218. res = pthread_create (&td[i].thread_id, &td[i].thread_attr,
  219. benchmark_thread, (void *)&td[i]);
  220. }
  221. for (i = 0; i < threads; i++) {
  222. pthread_join (td[i].thread_id, NULL);
  223. written += td[i].written;
  224. }
  225. alarm_notice = 0;
  226. gettimeofday (&tv2, NULL);
  227. timersub (&tv2, &tv1, &tv_elapsed);
  228. printf ("%5d Writes ", written);
  229. printf ("%5d bytes per write ", write_size);
  230. printf ("%7.3f Seconds runtime ",
  231. (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  232. printf ("%9.3f TP/s ",
  233. ((float)written) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  234. printf ("%7.3f MB/s.\n",
  235. ((float)written) * ((float)write_size) / ((tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)) * 1000000.0));
  236. }
  237. SaNameT checkpointName;
  238. #define CHECKPOINT_THREADS_START 25
  239. #define CHECKPOINT_THREADS_MAX 500
  240. void sigalrm_handler (int num)
  241. {
  242. alarm_notice = 1;
  243. }
  244. int main (void) {
  245. SaCkptHandleT ckpt_handles[CHECKPOINT_THREADS_MAX];
  246. SaCkptCheckpointHandleT checkpoint_handles[CHECKPOINT_THREADS_MAX];
  247. SaAisErrorT error;
  248. int size;
  249. int i, j;
  250. signal (SIGALRM, sigalrm_handler);
  251. printf ("Creating (%d) checkpoints.\n", CHECKPOINT_THREADS_MAX);
  252. /*
  253. * Create CHECPOINT_THREADS_MAX checkpoints
  254. */
  255. for (i = 0; i < CHECKPOINT_THREADS_MAX; i++) {
  256. sprintf ((char *)checkpointName.value, "checkpoint (%d)", i);
  257. checkpointName.length = strlen ((char *)checkpointName.value);
  258. do {
  259. error = saCkptInitialize (&ckpt_handles[i], &callbacks, &version);
  260. } while (error == SA_AIS_ERR_TRY_AGAIN);
  261. assert (error == SA_AIS_OK);
  262. do {
  263. error = saCkptCheckpointOpen (ckpt_handles[i],
  264. &checkpointName,
  265. &checkpointCreationAttributes,
  266. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  267. SA_TIME_END,
  268. &checkpoint_handles[i]);
  269. } while (error == SA_AIS_ERR_TRY_AGAIN);
  270. assert (error == SA_AIS_OK);
  271. do {
  272. error = saCkptSectionCreate (checkpoint_handles[i],
  273. &sectionCreationAttributes1,
  274. "Initial Data #0",
  275. strlen ("Initial Data #0") + 1);
  276. } while (error == SA_AIS_ERR_TRY_AGAIN);
  277. assert (error == SA_AIS_OK);
  278. do {
  279. error = saCkptSectionCreate (checkpoint_handles[i],
  280. &sectionCreationAttributes2,
  281. "Initial Data #0",
  282. strlen ("Initial Data #0") + 1);
  283. } while (error == SA_AIS_ERR_TRY_AGAIN);
  284. assert (error == SA_AIS_OK);
  285. }
  286. for (i = CHECKPOINT_THREADS_START; i < CHECKPOINT_THREADS_MAX; i++) { /* i threads */
  287. printf ("Starting benchmark with (%d) threads.\n", i);
  288. size = 10000; /* initial size */
  289. for (j = 0; j < 5; j++) { /* number of runs with i threads */
  290. alarm (10);
  291. threaded_bench (ckpt_handles, checkpoint_handles, i, size);
  292. size += 1000;
  293. }
  294. }
  295. return (0);
  296. }