disk_stdio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Corentin Salingue
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <sys/stat.h>
  20. #include <sys/time.h>
  21. #include <aio.h>
  22. #include <errno.h>
  23. #include <time.h>
  24. #include <starpu.h>
  25. #include <core/disk.h>
  26. #include <core/perfmodel/perfmodel.h>
  27. #include <datawizard/copy_driver.h>
  28. #ifdef STARPU_HAVE_WINDOWS
  29. #include <io.h>
  30. #endif
  31. #define NITER 64
  32. /* ------------------- use STDIO to write on disk ------------------- */
  33. struct starpu_stdio_obj {
  34. int descriptor;
  35. FILE * file;
  36. char * path;
  37. double size;
  38. starpu_pthread_mutex_t mutex;
  39. };
  40. /* allocation memory on disk */
  41. static void *
  42. starpu_stdio_alloc (void *base, size_t size)
  43. {
  44. struct starpu_stdio_obj * obj = malloc(sizeof(struct starpu_stdio_obj));
  45. STARPU_ASSERT(obj != NULL);
  46. int id = -1;
  47. /* create template for mkstemp */
  48. unsigned int sizeBase = 16;
  49. while(sizeBase < (strlen(base)+7))
  50. sizeBase *= 2;
  51. char * baseCpy = malloc(sizeBase*sizeof(char));
  52. STARPU_ASSERT(baseCpy != NULL);
  53. char * tmp = "STARPU_XXXXXX";
  54. strcpy(baseCpy, (char *) base);
  55. strcat(baseCpy,tmp);
  56. #ifdef STARPU_HAVE_WINDOWS
  57. _mktemp(baseCpy);
  58. id = open(baseCpy, "rb+");
  59. #else
  60. id = mkstemp(baseCpy);
  61. #endif
  62. /* fail */
  63. if (id < 0)
  64. {
  65. free(obj);
  66. free(baseCpy);
  67. return NULL;
  68. }
  69. FILE * f = fdopen(id, "rb+");
  70. /* fail */
  71. if (f == NULL)
  72. {
  73. /* delete fic */
  74. free(obj);
  75. free(baseCpy);
  76. unlink(baseCpy);
  77. return NULL;
  78. }
  79. #ifdef STARPU_HAVE_WINDOWS
  80. int val = _chsize(id, size);
  81. #else
  82. int val = ftruncate(id,size);
  83. #endif
  84. /* fail */
  85. if (val < 0)
  86. {
  87. free(obj);
  88. free(baseCpy);
  89. unlink(baseCpy);
  90. return NULL;
  91. }
  92. STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
  93. obj->descriptor = id;
  94. obj->file = f;
  95. obj->path = baseCpy;
  96. obj->size = size;
  97. return (void *) obj;
  98. }
  99. /* free memory on disk */
  100. static void
  101. starpu_stdio_free (void *base STARPU_ATTRIBUTE_UNUSED, void *obj, size_t size STARPU_ATTRIBUTE_UNUSED)
  102. {
  103. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) obj;
  104. STARPU_PTHREAD_MUTEX_DESTROY(&tmp->mutex);
  105. unlink(tmp->path);
  106. fclose(tmp->file);
  107. close(tmp->descriptor);
  108. free(tmp->path);
  109. free(tmp);
  110. }
  111. /* open an existing memory on disk */
  112. static void *
  113. starpu_stdio_open (void *base, void *pos, size_t size)
  114. {
  115. struct starpu_stdio_obj * obj = malloc(sizeof(struct starpu_stdio_obj));
  116. STARPU_ASSERT(obj != NULL);
  117. /* create template */
  118. unsigned int sizeBase = 16;
  119. while(sizeBase < (strlen(base)+strlen(pos)+1))
  120. sizeBase *= 2;
  121. char * baseCpy = malloc(sizeBase*sizeof(char));
  122. STARPU_ASSERT(baseCpy != NULL);
  123. strcpy(baseCpy,(char *) base);
  124. strcat(baseCpy,(char *) pos);
  125. int id = open(baseCpy, O_RDWR);
  126. if (id < 0)
  127. {
  128. free(obj);
  129. free(baseCpy);
  130. return NULL;
  131. }
  132. FILE * f = fdopen(id,"rb+");
  133. if (f == NULL)
  134. {
  135. free(obj);
  136. free(baseCpy);
  137. return NULL;
  138. }
  139. STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
  140. obj->descriptor = id;
  141. obj->file = f;
  142. obj->path = baseCpy;
  143. obj->size = size;
  144. return (void *) obj;
  145. }
  146. /* free memory without delete it */
  147. static void
  148. starpu_stdio_close (void *base STARPU_ATTRIBUTE_UNUSED, void *obj, size_t size STARPU_ATTRIBUTE_UNUSED)
  149. {
  150. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) obj;
  151. STARPU_PTHREAD_MUTEX_DESTROY(&tmp->mutex);
  152. fclose(tmp->file);
  153. close(tmp->descriptor);
  154. free(tmp->path);
  155. free(tmp);
  156. }
  157. /* read the memory disk */
  158. static int
  159. starpu_stdio_read (void *base STARPU_ATTRIBUTE_UNUSED, void *obj, void *buf, off_t offset, size_t size, void * async_channel)
  160. {
  161. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) obj;
  162. STARPU_PTHREAD_MUTEX_LOCK(&tmp->mutex);
  163. int res = fseek(tmp->file, offset, SEEK_SET);
  164. STARPU_ASSERT_MSG(res == 0, "Stdio read failed");
  165. ssize_t nb = fread (buf, 1, size, tmp->file);
  166. STARPU_ASSERT_MSG(nb >= 0, "Stdio read failed");
  167. STARPU_PTHREAD_MUTEX_UNLOCK(&tmp->mutex);
  168. return 0;
  169. }
  170. static int
  171. starpu_stdio_async_read (void *base STARPU_ATTRIBUTE_UNUSED, void *obj, void *buf, off_t offset, size_t size, void * async_channel)
  172. {
  173. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) obj;
  174. struct _starpu_async_channel * channel = (struct _starpu_async_channel *) async_channel;
  175. struct aiocb *aiocb = &channel->event.disk_event._starpu_aiocb_disk;
  176. memset(aiocb, 0, sizeof(struct aiocb));
  177. aiocb->aio_fildes = tmp->descriptor;
  178. aiocb->aio_offset = offset;
  179. aiocb->aio_nbytes = size;
  180. aiocb->aio_buf = buf;
  181. aiocb->aio_reqprio = 0;
  182. aiocb->aio_lio_opcode = LIO_NOP;
  183. return aio_read(aiocb);
  184. }
  185. /* write on the memory disk */
  186. static int
  187. starpu_stdio_write (void *base STARPU_ATTRIBUTE_UNUSED, void *obj, const void *buf, off_t offset, size_t size, void * async_channel)
  188. {
  189. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) obj;
  190. STARPU_PTHREAD_MUTEX_LOCK(&tmp->mutex);
  191. int res = fseek(tmp->file, offset, SEEK_SET);
  192. STARPU_ASSERT_MSG(res == 0, "Stdio write failed");
  193. ssize_t nb = fwrite (buf, 1, size, tmp->file);
  194. STARPU_PTHREAD_MUTEX_UNLOCK(&tmp->mutex);
  195. return nb;
  196. }
  197. static int
  198. starpu_stdio_async_write (void *base STARPU_ATTRIBUTE_UNUSED, void *obj, void *buf, off_t offset, size_t size, void * async_channel)
  199. {
  200. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) obj;
  201. struct _starpu_async_channel * channel = (struct _starpu_async_channel *) async_channel;
  202. struct aiocb *aiocb = &channel->event.disk_event._starpu_aiocb_disk ;
  203. memset(aiocb, 0, sizeof(struct aiocb));
  204. aiocb->aio_fildes = tmp->descriptor;
  205. aiocb->aio_offset = offset;
  206. aiocb->aio_nbytes = size;
  207. aiocb->aio_buf = buf;
  208. aiocb->aio_reqprio = 0;
  209. aiocb->aio_lio_opcode = LIO_NOP;
  210. return aio_write(aiocb);
  211. }
  212. /* create a new copy of parameter == base */
  213. static void *
  214. starpu_stdio_plug (void *parameter)
  215. {
  216. char * tmp = malloc(sizeof(char)*(strlen(parameter)+1));
  217. STARPU_ASSERT(tmp != NULL);
  218. strcpy(tmp,(char *) parameter);
  219. return (void *) tmp;
  220. }
  221. /* free memory allocated for the base */
  222. static void
  223. starpu_stdio_unplug (void *base)
  224. {
  225. free(base);
  226. }
  227. static int
  228. get_stdio_bandwidth_between_disk_and_main_ram(unsigned node)
  229. {
  230. unsigned iter;
  231. double timing_slowness, timing_latency;
  232. struct timeval start;
  233. struct timeval end;
  234. srand (time (NULL));
  235. char * buf = malloc(SIZE_DISK_MIN*sizeof(char));
  236. STARPU_ASSERT(buf != NULL);
  237. /* allocate memory */
  238. void * mem = _starpu_disk_alloc(node, SIZE_DISK_MIN);
  239. /* fail to alloc */
  240. if (mem == NULL)
  241. return 0;
  242. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) mem;
  243. /* Measure upload slowness */
  244. gettimeofday(&start, NULL);
  245. for (iter = 0; iter < NITER; ++iter)
  246. {
  247. _starpu_disk_write(STARPU_MAIN_RAM, node, mem, buf, 0, SIZE_DISK_MIN, NULL);
  248. /* clean cache memory */
  249. int res = fflush (tmp->file);
  250. STARPU_ASSERT_MSG(res == 0, "Slowness computation failed \n");
  251. #ifdef STARPU_HAVE_WINDOWS
  252. res = _commit(tmp->descriptor);
  253. #else
  254. res = fsync(tmp->descriptor);
  255. #endif
  256. STARPU_ASSERT_MSG(res == 0, "Slowness computation failed \n");
  257. }
  258. gettimeofday(&end, NULL);
  259. timing_slowness = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  260. /* free memory */
  261. free(buf);
  262. buf = malloc(sizeof(char));
  263. STARPU_ASSERT(buf != NULL);
  264. /* Measure latency */
  265. gettimeofday(&start, NULL);
  266. for (iter = 0; iter < NITER; ++iter)
  267. {
  268. _starpu_disk_write(STARPU_MAIN_RAM, node, mem, buf, rand() % (SIZE_DISK_MIN -1) , 1, NULL);
  269. int res = fflush (tmp->file);
  270. STARPU_ASSERT_MSG(res == 0, "Latency computation failed");
  271. #ifdef STARPU_HAVE_WINDOWS
  272. res = _commit(tmp->descriptor);
  273. #else
  274. res = fsync(tmp->descriptor);
  275. #endif
  276. STARPU_ASSERT_MSG(res == 0, "Latency computation failed");
  277. }
  278. gettimeofday(&end, NULL);
  279. timing_latency = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  280. _starpu_disk_free(node, mem, SIZE_DISK_MIN);
  281. free(buf);
  282. _starpu_save_bandwidth_and_latency_disk((NITER/timing_slowness)*1000000, (NITER/timing_slowness)*1000000,
  283. timing_latency/NITER, timing_latency/NITER, node);
  284. return 1;
  285. }
  286. static void
  287. starpu_stdio_wait_request(void * async_channel)
  288. {
  289. struct _starpu_async_channel * channel = (struct _starpu_async_channel *) async_channel;
  290. const struct aiocb * aiocb = &channel->event.disk_event._starpu_aiocb_disk;
  291. const struct aiocb * list[1];
  292. list[0] = aiocb;
  293. int values = -1;
  294. int error_disk = EAGAIN;
  295. while(values < 0 || error_disk == EAGAIN)
  296. {
  297. /* Wait the answer of the request TIMESTAMP IS NULL */
  298. values = aio_suspend(list, 1, NULL);
  299. error_disk = errno;
  300. }
  301. }
  302. static int
  303. starpu_stdio_test_request(void * async_channel)
  304. {
  305. struct timespec time_wait_request;
  306. time_wait_request.tv_sec = 0;
  307. time_wait_request.tv_nsec = 0;
  308. struct _starpu_async_channel * channel = (struct _starpu_async_channel *) async_channel;
  309. const struct aiocb * aiocb = &channel->event.disk_event._starpu_aiocb_disk;
  310. const struct aiocb * list[1];
  311. list[0] = aiocb;
  312. int values = -1;
  313. int error_disk = EAGAIN;
  314. /* Wait the answer of the request */
  315. values = aio_suspend(list, 1, &time_wait_request);
  316. error_disk = errno;
  317. /* request is finished */
  318. if (values == 0)
  319. return 1;
  320. /* values == -1 */
  321. if (error_disk == EAGAIN)
  322. return 0;
  323. /* an error occured */
  324. STARPU_ABORT();
  325. }
  326. struct starpu_disk_ops starpu_disk_stdio_ops = {
  327. .alloc = starpu_stdio_alloc,
  328. .free = starpu_stdio_free,
  329. .open = starpu_stdio_open,
  330. .close = starpu_stdio_close,
  331. .read = starpu_stdio_read,
  332. .async_read = starpu_stdio_async_read,
  333. .write = starpu_stdio_write,
  334. .async_write = starpu_stdio_async_write,
  335. .plug = starpu_stdio_plug,
  336. .unplug = starpu_stdio_unplug,
  337. .copy = NULL,
  338. .bandwidth = get_stdio_bandwidth_between_disk_and_main_ram,
  339. .wait_request = starpu_stdio_wait_request,
  340. .test_request = starpu_stdio_test_request
  341. };