ckptbenchth.c 7.9 KB

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