ckptbenchth.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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@mvista.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. #ifdef OPENAIS_SOLARIS
  53. #define timersub(a, b, result) \
  54. do { \
  55. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  56. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  57. if ((result)->tv_usec < 0) { \
  58. --(result)->tv_sec; \
  59. (result)->tv_usec += 1000000; \
  60. } \
  61. } while (0)
  62. #endif
  63. int alarm_notice = 0;
  64. void printSaNameT (SaNameT *name)
  65. {
  66. int i;
  67. for (i = 0; i < name->length; i++) {
  68. printf ("%c", name->value[i]);
  69. }
  70. }
  71. SaVersionT version = { 'B', 1, 1 };
  72. SaCkptCallbacksT callbacks = {
  73. 0,
  74. 0
  75. };
  76. SaCkptCheckpointCreationAttributesT checkpointCreationAttributes = {
  77. .creationFlags = SA_CKPT_WR_ALL_REPLICAS,
  78. .checkpointSize = 100000,
  79. .retentionDuration = 0,
  80. .maxSections = 5,
  81. .maxSectionSize = 150000,
  82. .maxSectionIdSize = 10
  83. };
  84. SaCkptSectionIdT sectionId1 = {
  85. 14,
  86. (SaUint8T *) "section ID #1"
  87. };
  88. SaCkptSectionIdT sectionId2 = {
  89. 14,
  90. (SaUint8T *) "section ID #2"
  91. };
  92. SaCkptSectionCreationAttributesT sectionCreationAttributes1 = {
  93. &sectionId1,
  94. 0xFFFFFFFF
  95. };
  96. SaCkptSectionCreationAttributesT sectionCreationAttributes2 = {
  97. &sectionId2,
  98. 0xFFFFFFFF
  99. };
  100. char readBuffer1[1025];
  101. char readBuffer2[1025];
  102. SaCkptIOVectorElementT ReadVectorElements[] = {
  103. {
  104. {
  105. 14,
  106. (SaUint8T *) "section ID #1"
  107. },
  108. readBuffer1,
  109. sizeof (readBuffer1),
  110. 0,
  111. 0
  112. },
  113. {
  114. {
  115. 14,
  116. (SaUint8T *) "section ID #2"
  117. },
  118. readBuffer2,
  119. sizeof (readBuffer2),
  120. 0,
  121. 0
  122. }
  123. };
  124. #define DATASIZE 1000
  125. #define LOOPS 5000
  126. char data[500000];
  127. SaCkptIOVectorElementT WriteVectorElements[] = {
  128. {
  129. {
  130. 14,
  131. (SaUint8T *) "section ID #1"
  132. },
  133. data, /*"written data #1, this should extend past end of old section data", */
  134. DATASIZE, /*sizeof ("data #1, this should extend past end of old section data") + 1, */
  135. 0, //5,
  136. 0
  137. }
  138. #ifdef COMPILE_OUT
  139. {
  140. {
  141. 14,
  142. (SaUint8T *) "section ID #2"
  143. },
  144. data, /*"written data #2, this should extend past end of old section data" */
  145. DATASIZE, /*sizeof ("written data #2, this should extend past end of old section data") + 1, */
  146. 0, //3,
  147. 0
  148. }
  149. #endif
  150. };
  151. int runs = 0;
  152. struct threaddata {
  153. SaCkptHandleT ckpt_handle;
  154. SaCkptCheckpointHandleT checkpoint_handle;
  155. int write_size;
  156. int thread;
  157. pthread_attr_t thread_attr;
  158. pthread_t thread_id;
  159. int written;
  160. };
  161. extern void pthread_exit(void *) __attribute__((noreturn));
  162. void *benchmark_thread (void *arg)
  163. {
  164. SaCkptCheckpointHandleT checkpoint_handle;
  165. SaCkptHandleT ckpt_handle;
  166. int write_size;
  167. SaAisErrorT error;
  168. SaUint32T erroroneousVectorIndex = 0;
  169. struct threaddata *td = (struct threaddata *)arg;
  170. checkpoint_handle = td->checkpoint_handle;
  171. ckpt_handle = td->ckpt_handle;
  172. write_size = td->write_size;
  173. WriteVectorElements[0].dataSize = write_size;
  174. do {
  175. /*
  176. * Test checkpoint write
  177. */
  178. do {
  179. error = saCkptCheckpointWrite (checkpoint_handle,
  180. WriteVectorElements,
  181. 1,
  182. &erroroneousVectorIndex);
  183. } while (error == SA_AIS_ERR_TRY_AGAIN);
  184. if (error != SA_AIS_OK) {
  185. printf ("saCkptCheckpointWrite result %d handle (should be 1)\n", error);
  186. exit (1);
  187. }
  188. td->written += 1;
  189. } while (alarm_notice == 0);
  190. pthread_exit (0);
  191. }
  192. void threaded_bench (
  193. SaCkptHandleT *ckpt_handles,
  194. SaCkptCheckpointHandleT *checkpoint_handles,
  195. int threads,
  196. int write_size)
  197. {
  198. struct timeval tv1, tv2, tv_elapsed;
  199. struct threaddata td[100];
  200. int i;
  201. int res;
  202. int written = 0;
  203. runs = threads;
  204. gettimeofday (&tv1, NULL);
  205. for (i = 0; i < threads; i++) {
  206. td[i].ckpt_handle = ckpt_handles[i];
  207. td[i].checkpoint_handle = checkpoint_handles[i];
  208. td[i].write_size = write_size;
  209. td[i].thread = i;
  210. td[i].written = 0;
  211. pthread_attr_init (&td[i].thread_attr);
  212. pthread_attr_setstacksize (&td[i].thread_attr, 16384);
  213. pthread_attr_setdetachstate (&td[i].thread_attr, PTHREAD_CREATE_JOINABLE);
  214. res = pthread_create (&td[i].thread_id, &td[i].thread_attr,
  215. benchmark_thread, (void *)&td[i]);
  216. }
  217. for (i = 0; i < threads; i++) {
  218. pthread_join (td[i].thread_id, NULL);
  219. written += td[i].written;
  220. }
  221. alarm_notice = 0;
  222. gettimeofday (&tv2, NULL);
  223. timersub (&tv2, &tv1, &tv_elapsed);
  224. printf ("%5d Writes ", written);
  225. printf ("%5d bytes per write ", write_size);
  226. printf ("%7.3f Seconds runtime ",
  227. (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  228. printf ("%9.3f TP/s ",
  229. ((float)written) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  230. printf ("%7.3f MB/s.\n",
  231. ((float)written) * ((float)write_size) / ((tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)) * 1000000.0));
  232. }
  233. SaNameT checkpointName;
  234. #define CHECKPOINT_THREADS_START 25
  235. #define CHECKPOINT_THREADS_MAX 500
  236. void sigalrm_handler (int num)
  237. {
  238. alarm_notice = 1;
  239. }
  240. int main (void) {
  241. SaCkptHandleT ckpt_handles[CHECKPOINT_THREADS_MAX];
  242. SaCkptCheckpointHandleT checkpoint_handles[CHECKPOINT_THREADS_MAX];
  243. SaAisErrorT error;
  244. int size;
  245. int i, j;
  246. signal (SIGALRM, sigalrm_handler);
  247. printf ("Creating (%d) checkpoints.\n", CHECKPOINT_THREADS_MAX);
  248. /*
  249. * Create CHECPOINT_THREADS_MAX checkpoints
  250. */
  251. for (i = 0; i < CHECKPOINT_THREADS_MAX; i++) {
  252. sprintf ((char *)checkpointName.value, "checkpoint (%d)", i);
  253. checkpointName.length = strlen ((char *)checkpointName.value);
  254. do {
  255. error = saCkptInitialize (&ckpt_handles[i], &callbacks, &version);
  256. } while (error == SA_AIS_ERR_TRY_AGAIN);
  257. assert (error == SA_AIS_OK);
  258. do {
  259. error = saCkptCheckpointOpen (ckpt_handles[i],
  260. &checkpointName,
  261. &checkpointCreationAttributes,
  262. SA_CKPT_CHECKPOINT_CREATE|SA_CKPT_CHECKPOINT_READ|SA_CKPT_CHECKPOINT_WRITE,
  263. SA_TIME_END,
  264. &checkpoint_handles[i]);
  265. } while (error == SA_AIS_ERR_TRY_AGAIN);
  266. assert (error == SA_AIS_OK);
  267. do {
  268. error = saCkptSectionCreate (checkpoint_handles[i],
  269. &sectionCreationAttributes1,
  270. "Initial Data #0",
  271. strlen ("Initial Data #0") + 1);
  272. } while (error == SA_AIS_ERR_TRY_AGAIN);
  273. assert (error == SA_AIS_OK);
  274. do {
  275. error = saCkptSectionCreate (checkpoint_handles[i],
  276. &sectionCreationAttributes2,
  277. "Initial Data #0",
  278. strlen ("Initial Data #0") + 1);
  279. } while (error == SA_AIS_ERR_TRY_AGAIN);
  280. assert (error == SA_AIS_OK);
  281. }
  282. for (i = CHECKPOINT_THREADS_START; i < CHECKPOINT_THREADS_MAX; i++) { /* i threads */
  283. printf ("Starting benchmark with (%d) threads.\n", i);
  284. size = 10000; /* initial size */
  285. for (j = 0; j < 5; j++) { /* number of runs with i threads */
  286. alarm (10);
  287. threaded_bench (ckpt_handles, checkpoint_handles, i, size);
  288. size += 1000;
  289. }
  290. }
  291. return (0);
  292. }