disk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Corentin Salingue
  4. * Copyright (C) 2015, 2016 CNRS
  5. * Copyright (C) 2017 Inria
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <fcntl.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <time.h>
  25. #include <common/config.h>
  26. #include <core/debug.h>
  27. #include <core/disk.h>
  28. #include <core/workers.h>
  29. #include <core/perfmodel/perfmodel.h>
  30. #include <core/topology.h>
  31. #include <datawizard/memory_nodes.h>
  32. #include <datawizard/memory_manager.h>
  33. #include <datawizard/memalloc.h>
  34. #include <drivers/cuda/driver_cuda.h>
  35. #include <drivers/opencl/driver_opencl.h>
  36. #include <profiling/profiling.h>
  37. #include <common/uthash.h>
  38. struct disk_register
  39. {
  40. unsigned node;
  41. void *base;
  42. struct starpu_disk_ops *functions;
  43. /* disk condition (1 = all authorizations, */
  44. int flag;
  45. };
  46. static void add_disk_in_list(unsigned node, struct starpu_disk_ops *func, void *base);
  47. static int get_location_with_node(unsigned node);
  48. static struct disk_register **disk_register_list = NULL;
  49. static int disk_number = -1;
  50. static int size_register_list = 2;
  51. int starpu_disk_swap_node = -1;
  52. int starpu_disk_register(struct starpu_disk_ops *func, void *parameter, starpu_ssize_t size)
  53. {
  54. STARPU_ASSERT_MSG(size < 0 || size >= SIZE_DISK_MIN,"Minimum disk size is %u Bytes ! (Here %u) \n", (int) SIZE_DISK_MIN, (int) size);
  55. /* register disk */
  56. unsigned disk_memnode = _starpu_memory_node_register(STARPU_DISK_RAM, 0);
  57. /* Connect the disk memory node to all numa memory nodes */
  58. int nb_numa_nodes = starpu_memory_nodes_get_numa_count();
  59. int numa_node;
  60. for (numa_node = 0; numa_node < nb_numa_nodes; numa_node++)
  61. {
  62. _starpu_register_bus(disk_memnode, numa_node);
  63. _starpu_register_bus(numa_node, disk_memnode);
  64. }
  65. /* connect disk */
  66. void *base = func->plug(parameter, size);
  67. /* remember it */
  68. add_disk_in_list(disk_memnode,func,base);
  69. int ret = func->bandwidth(disk_memnode);
  70. /* have a problem with the disk */
  71. if (ret == 0)
  72. return -ENOENT;
  73. if (size >= 0)
  74. _starpu_memory_manager_set_global_memory_size(disk_memnode, size);
  75. return disk_memnode;
  76. }
  77. void _starpu_disk_unregister(void)
  78. {
  79. int i;
  80. /* search disk and delete it */
  81. for (i = 0; i <= disk_number; ++i)
  82. {
  83. _starpu_set_disk_flag(disk_register_list[i]->node, STARPU_DISK_NO_RECLAIM);
  84. _starpu_free_all_automatically_allocated_buffers(disk_register_list[i]->node);
  85. /* don't forget to unplug */
  86. disk_register_list[i]->functions->unplug(disk_register_list[i]->base);
  87. free(disk_register_list[i]);
  88. }
  89. /* no disk in the list -> delete the list */
  90. disk_number--;
  91. if (disk_register_list != NULL && disk_number == -1)
  92. {
  93. free(disk_register_list);
  94. disk_register_list = NULL;
  95. }
  96. }
  97. /* interface between user and disk memory */
  98. void *_starpu_disk_alloc(unsigned node, size_t size)
  99. {
  100. int pos = get_location_with_node(node);
  101. return disk_register_list[pos]->functions->alloc(disk_register_list[pos]->base, size);
  102. }
  103. void _starpu_disk_free(unsigned node, void *obj, size_t size)
  104. {
  105. int pos = get_location_with_node(node);
  106. disk_register_list[pos]->functions->free(disk_register_list[pos]->base, obj, size);
  107. }
  108. /* src_node == disk node and dst_node == STARPU_MAIN_RAM */
  109. int _starpu_disk_read(unsigned src_node, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, void *obj, void *buf, off_t offset, size_t size, struct _starpu_async_channel *channel)
  110. {
  111. int pos = get_location_with_node(src_node);
  112. if (channel != NULL)
  113. {
  114. if (disk_register_list[pos]->functions->async_read == NULL)
  115. channel = NULL;
  116. else
  117. {
  118. channel->type = STARPU_DISK_RAM;
  119. channel->event.disk_event.memory_node = src_node;
  120. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  121. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_read(disk_register_list[pos]->base, obj, buf, offset, size);
  122. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  123. }
  124. }
  125. /* asynchronous request failed or synchronous request is asked */
  126. if (channel == NULL || !channel->event.disk_event.backend_event)
  127. {
  128. disk_register_list[pos]->functions->read(disk_register_list[pos]->base, obj, buf, offset, size);
  129. return 0;
  130. }
  131. return -EAGAIN;
  132. }
  133. /* src_node == STARPU_MAIN_RAM and dst_node == disk node */
  134. int _starpu_disk_write(unsigned src_node STARPU_ATTRIBUTE_UNUSED, unsigned dst_node, void *obj, void *buf, off_t offset, size_t size, struct _starpu_async_channel *channel)
  135. {
  136. int pos = get_location_with_node(dst_node);
  137. if (channel != NULL)
  138. {
  139. if (disk_register_list[pos]->functions->async_write == NULL)
  140. channel = NULL;
  141. else
  142. {
  143. channel->type = STARPU_DISK_RAM;
  144. channel->event.disk_event.memory_node = dst_node;
  145. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  146. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_write(disk_register_list[pos]->base, obj, buf, offset, size);
  147. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  148. }
  149. }
  150. /* asynchronous request failed or synchronous request is asked */
  151. if (channel == NULL || !channel->event.disk_event.backend_event)
  152. {
  153. disk_register_list[pos]->functions->write(disk_register_list[pos]->base, obj, buf, offset, size);
  154. return 0;
  155. }
  156. return -EAGAIN;
  157. }
  158. int _starpu_disk_copy(unsigned node_src, void *obj_src, off_t offset_src, unsigned node_dst, void *obj_dst, off_t offset_dst, size_t size, struct _starpu_async_channel *channel)
  159. {
  160. int pos_src = get_location_with_node(node_src);
  161. int pos_dst = get_location_with_node(node_dst);
  162. /* both nodes have same copy function */
  163. channel->event.disk_event.memory_node = node_src;
  164. channel->event.disk_event.backend_event = disk_register_list[pos_src]->functions->copy(disk_register_list[pos_src]->base, obj_src, offset_src,
  165. disk_register_list[pos_dst]->base, obj_dst, offset_dst,
  166. size);
  167. STARPU_ASSERT(channel->event.disk_event.backend_event);
  168. return -EAGAIN;
  169. }
  170. int _starpu_disk_full_read(unsigned src_node, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, void *obj, void **ptr, size_t *size, struct _starpu_async_channel *channel)
  171. {
  172. int pos = get_location_with_node(src_node);
  173. if (channel != NULL)
  174. {
  175. if (disk_register_list[pos]->functions->async_full_read == NULL)
  176. channel = NULL;
  177. else
  178. {
  179. channel->type = STARPU_DISK_RAM;
  180. channel->event.disk_event.memory_node = src_node;
  181. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  182. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_full_read(disk_register_list[pos]->base, obj, ptr, size);
  183. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  184. }
  185. }
  186. /* asynchronous request failed or synchronous request is asked */
  187. if (channel == NULL || !channel->event.disk_event.backend_event)
  188. {
  189. disk_register_list[pos]->functions->full_read(disk_register_list[pos]->base, obj, ptr, size);
  190. return 0;
  191. }
  192. return -EAGAIN;
  193. }
  194. int _starpu_disk_full_write(unsigned src_node STARPU_ATTRIBUTE_UNUSED, unsigned dst_node, void *obj, void *ptr, size_t size, struct _starpu_async_channel *channel)
  195. {
  196. int pos = get_location_with_node(dst_node);
  197. if (channel != NULL)
  198. {
  199. if (disk_register_list[pos]->functions->async_full_write == NULL)
  200. channel = NULL;
  201. else
  202. {
  203. channel->type = STARPU_DISK_RAM;
  204. channel->event.disk_event.memory_node = dst_node;
  205. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  206. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_full_write(disk_register_list[pos]->base, obj, ptr, size);
  207. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  208. }
  209. }
  210. /* asynchronous request failed or synchronous request is asked */
  211. if (channel == NULL || !channel->event.disk_event.backend_event)
  212. {
  213. disk_register_list[pos]->functions->full_write(disk_register_list[pos]->base, obj, ptr, size);
  214. return 0;
  215. }
  216. return -EAGAIN;
  217. }
  218. void *starpu_disk_open(unsigned node, void *pos, size_t size)
  219. {
  220. int position = get_location_with_node(node);
  221. return disk_register_list[position]->functions->open(disk_register_list[position]->base, pos, size);
  222. }
  223. void starpu_disk_close(unsigned node, void *obj, size_t size)
  224. {
  225. int position = get_location_with_node(node);
  226. disk_register_list[position]->functions->close(disk_register_list[position]->base, obj, size);
  227. }
  228. void starpu_disk_wait_request(struct _starpu_async_channel *async_channel)
  229. {
  230. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  231. disk_register_list[position]->functions->wait_request(async_channel->event.disk_event.backend_event);
  232. }
  233. int starpu_disk_test_request(struct _starpu_async_channel *async_channel)
  234. {
  235. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  236. return disk_register_list[position]->functions->test_request(async_channel->event.disk_event.backend_event);
  237. }
  238. void starpu_disk_free_request(struct _starpu_async_channel *async_channel)
  239. {
  240. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  241. if (async_channel->event.disk_event.backend_event)
  242. disk_register_list[position]->functions->free_request(async_channel->event.disk_event.backend_event);
  243. }
  244. static void add_disk_in_list(unsigned node, struct starpu_disk_ops *func, void *base)
  245. {
  246. /* initialization */
  247. if (disk_register_list == NULL)
  248. {
  249. _STARPU_MALLOC(disk_register_list, size_register_list*sizeof(struct disk_register *));
  250. }
  251. /* small size -> new size */
  252. if ((disk_number+1) > size_register_list)
  253. {
  254. size_register_list *= 2;
  255. _STARPU_REALLOC(disk_register_list, size_register_list*sizeof(struct disk_register *));
  256. }
  257. struct disk_register *dr;
  258. _STARPU_MALLOC(dr, sizeof(struct disk_register));
  259. dr->node = node;
  260. dr->base = base;
  261. dr->flag = STARPU_DISK_ALL;
  262. dr->functions = func;
  263. disk_register_list[++disk_number] = dr;
  264. }
  265. static int get_location_with_node(unsigned node)
  266. {
  267. #ifdef STARPU_DEVEL
  268. #warning optimize with a MAXNODE array
  269. #endif
  270. int i;
  271. for (i = 0; i <= disk_number; ++i)
  272. if (disk_register_list[i]->node == node)
  273. return i;
  274. STARPU_ASSERT_MSG(false, "Disk node not found !(%u) ", node);
  275. return -1;
  276. }
  277. int _starpu_is_same_kind_disk(unsigned node1, unsigned node2)
  278. {
  279. if (starpu_node_get_kind(node1) == STARPU_DISK_RAM && starpu_node_get_kind(node2) == STARPU_DISK_RAM)
  280. {
  281. int pos1 = get_location_with_node(node1);
  282. int pos2 = get_location_with_node(node2);
  283. if (disk_register_list[pos1]->functions == disk_register_list[pos2]->functions)
  284. /* they must have a copy function */
  285. if (disk_register_list[pos1]->functions->copy != NULL)
  286. return 1;
  287. }
  288. return 0;
  289. }
  290. void _starpu_set_disk_flag(unsigned node, int flag)
  291. {
  292. int pos = get_location_with_node(node);
  293. disk_register_list[pos]->flag = flag;
  294. }
  295. int _starpu_get_disk_flag(unsigned node)
  296. {
  297. int pos = get_location_with_node(node);
  298. return disk_register_list[pos]->flag;
  299. }
  300. void _starpu_swap_init(void)
  301. {
  302. char *backend;
  303. char *path;
  304. starpu_ssize_t size;
  305. struct starpu_disk_ops *ops;
  306. path = starpu_getenv("STARPU_DISK_SWAP");
  307. if (!path)
  308. return;
  309. backend = starpu_getenv("STARPU_DISK_SWAP_BACKEND");
  310. if (!backend)
  311. {
  312. _starpu_mkpath(path, S_IRWXU);
  313. ops = &starpu_disk_unistd_ops;
  314. }
  315. else if (!strcmp(backend, "stdio"))
  316. {
  317. _starpu_mkpath(path, S_IRWXU);
  318. ops = &starpu_disk_stdio_ops;
  319. }
  320. else if (!strcmp(backend, "unistd"))
  321. {
  322. _starpu_mkpath(path, S_IRWXU);
  323. ops = &starpu_disk_unistd_ops;
  324. }
  325. else if (!strcmp(backend, "unistd_o_direct"))
  326. {
  327. #ifdef STARPU_LINUX_SYS
  328. _starpu_mkpath(path, S_IRWXU);
  329. ops = &starpu_disk_unistd_o_direct_ops;
  330. #else
  331. _STARPU_DISP("Warning: o_direct support is not compiled in, could not enable disk swap");
  332. return;
  333. #endif
  334. }
  335. else if (!strcmp(backend, "leveldb"))
  336. {
  337. #ifdef STARPU_HAVE_LEVELDB
  338. ops = &starpu_disk_leveldb_ops;
  339. #else
  340. _STARPU_DISP("Warning: leveldb support is not compiled in, could not enable disk swap");
  341. return;
  342. #endif
  343. }
  344. else
  345. {
  346. _STARPU_DISP("Warning: unknown disk swap backend %s, could not enable disk swap", backend);
  347. return;
  348. }
  349. size = starpu_get_env_number_default("STARPU_DISK_SWAP_SIZE", -1);
  350. starpu_disk_swap_node = starpu_disk_register(ops, path, ((size_t) size) << 20);
  351. if (starpu_disk_swap_node < 0)
  352. {
  353. _STARPU_DISP("Warning: could not enable disk swap %s on %s with size %ld, could not enable disk swap", backend, path, (long) size);
  354. return;
  355. }
  356. }