disk_leveldb.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <stdio.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <common/config.h>
  20. #include <leveldb/db.h>
  21. #include <leveldb/options.h>
  22. #include <starpu.h>
  23. #include <core/disk.h>
  24. #include <core/perfmodel/perfmodel.h>
  25. #include <datawizard/copy_driver.h>
  26. #include <datawizard/memory_manager.h>
  27. #include <starpu_parameters.h>
  28. #define NITER _starpu_calibration_minimum
  29. /* ------------------- use leveldb to write on disk ------------------- */
  30. struct starpu_leveldb_obj
  31. {
  32. char * key;
  33. size_t size;
  34. starpu_pthread_mutex_t mutex;
  35. };
  36. struct starpu_leveldb_base
  37. {
  38. leveldb::DB* db;
  39. /* if StarPU creates the leveldb */
  40. bool created;
  41. };
  42. /* allocation memory on disk */
  43. static void *starpu_leveldb_alloc(void *base, size_t size STARPU_ATTRIBUTE_UNUSED)
  44. {
  45. struct starpu_leveldb_base *base_tmp = (struct starpu_leveldb_base *) base;
  46. struct starpu_leveldb_obj *obj = (struct starpu_leveldb_obj *)malloc(sizeof(struct starpu_leveldb_obj));
  47. STARPU_ASSERT(obj);
  48. STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
  49. size_t len = 6 + 1 + 2+sizeof(void*)*2 + 1;
  50. char *key = (char *)malloc(len*sizeof(char));
  51. STARPU_ASSERT(key);
  52. snprintf(key, len, "STARPU-%p", obj);
  53. /* create and add a key with a small memory */
  54. leveldb::Status s = base_tmp->db->Put(leveldb::WriteOptions(), key, "a");
  55. STARPU_ASSERT(s.ok());
  56. /* obj->size is the real size in the disk */
  57. obj->key = key;
  58. obj->size = sizeof(char);
  59. return (void *) obj;
  60. }
  61. /* free memory on disk */
  62. static void starpu_leveldb_free(void *base , void *obj, size_t size STARPU_ATTRIBUTE_UNUSED)
  63. {
  64. struct starpu_leveldb_obj *tmp = (struct starpu_leveldb_obj *) obj;
  65. struct starpu_leveldb_base *base_tmp = (struct starpu_leveldb_base *) base;
  66. base_tmp->db->Delete(leveldb::WriteOptions(), tmp->key);
  67. STARPU_PTHREAD_MUTEX_DESTROY(&tmp->mutex);
  68. free(tmp->key);
  69. free(tmp);
  70. }
  71. /* open an existing memory on disk */
  72. static void *starpu_leveldb_open(void *base STARPU_ATTRIBUTE_UNUSED, void *pos, size_t size)
  73. {
  74. struct starpu_leveldb_obj *obj = (struct starpu_leveldb_obj *)malloc(sizeof(struct starpu_leveldb_obj));
  75. STARPU_ASSERT(obj);
  76. STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
  77. obj->key = strdup((char*) pos);
  78. obj->size = size;
  79. return (void *) obj;
  80. }
  81. /* free memory without delete it */
  82. static void starpu_leveldb_close(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, size_t size STARPU_ATTRIBUTE_UNUSED)
  83. {
  84. struct starpu_leveldb_obj *tmp = (struct starpu_leveldb_obj *) obj;
  85. STARPU_PTHREAD_MUTEX_DESTROY(&tmp->mutex);
  86. free(tmp->key);
  87. free(tmp);
  88. }
  89. /* in the leveldb, we are obliged to read and to write the entire data
  90. * so, we have to use buffers to have offset and size options */
  91. static int starpu_leveldb_read(void *base, void *obj, void *buf, off_t offset, size_t size)
  92. {
  93. struct starpu_leveldb_obj *tmp = (struct starpu_leveldb_obj *) obj;
  94. struct starpu_leveldb_base *base_tmp = (struct starpu_leveldb_base *) base;
  95. STARPU_PTHREAD_MUTEX_LOCK(&tmp->mutex);
  96. /* leveldb need a string to store datas */
  97. std::string value;
  98. leveldb::Status s = base_tmp->db->Get(leveldb::ReadOptions(), tmp->key, &value);
  99. uintptr_t value_read = (uintptr_t)(value.c_str());
  100. /* use buffer */
  101. if(s.ok())
  102. memcpy(buf, (void *) (value_read+offset), size);
  103. else
  104. STARPU_ASSERT(s.ok());
  105. STARPU_PTHREAD_MUTEX_UNLOCK(&tmp->mutex);
  106. return 0;
  107. }
  108. static int starpu_leveldb_full_read(void *base, void *obj, void **ptr, size_t *size)
  109. {
  110. struct starpu_leveldb_obj *tmp = (struct starpu_leveldb_obj *) obj;
  111. struct starpu_leveldb_base *base_tmp = (struct starpu_leveldb_base *) base;
  112. STARPU_PTHREAD_MUTEX_LOCK(&tmp->mutex);
  113. /* leveldb need a string to store datas */
  114. std::string value;
  115. leveldb::Status s = base_tmp->db->Get(leveldb::ReadOptions(), tmp->key, &value);
  116. STARPU_ASSERT(s.ok());
  117. *size = value.length();
  118. *ptr = malloc(*size);
  119. STARPU_ASSERT(*ptr);
  120. /* use buffer */
  121. memcpy(*ptr, value.c_str(), *size);
  122. STARPU_PTHREAD_MUTEX_UNLOCK(&tmp->mutex);
  123. return 0;
  124. }
  125. /* write on the memory disk */
  126. static int starpu_leveldb_write(void *base, void *obj, const void *buf, off_t offset, size_t size)
  127. {
  128. struct starpu_leveldb_obj *tmp = (struct starpu_leveldb_obj *) obj;
  129. struct starpu_leveldb_base *base_tmp = (struct starpu_leveldb_base *) base;
  130. void *buffer;
  131. leveldb::Status s;
  132. STARPU_PTHREAD_MUTEX_LOCK(&tmp->mutex);
  133. if (offset == 0 && size >= tmp->size)
  134. {
  135. /* We overwrite everything, no need to get the old value */
  136. buffer = (void*) buf;
  137. }
  138. else
  139. {
  140. uintptr_t buf_tmp = (uintptr_t) buf;
  141. buffer = malloc((tmp->size > (offset + size)) ? tmp->size : (offset + size));
  142. STARPU_ASSERT(buffer);
  143. /* we read the data */
  144. std::string value;
  145. s = base_tmp->db->Get(leveldb::ReadOptions(), tmp->key, &value);
  146. uintptr_t value_read = (uintptr_t)(value.c_str());
  147. STARPU_ASSERT(s.ok());
  148. memcpy(buffer, (void *) value_read, tmp->size);
  149. /* put the new data on their new place */
  150. memcpy(buffer, (void *) (buf_tmp+offset), size);
  151. }
  152. /* and write them */
  153. s = base_tmp->db->Put(leveldb::WriteOptions(), tmp->key, (char *)buffer);
  154. STARPU_ASSERT(s.ok());
  155. /* if the new size is higher than the old, we update it - first write after the alloc */
  156. tmp->size = (tmp->size > size) ? tmp->size : size;
  157. if (buffer != buf)
  158. free(buffer);
  159. STARPU_PTHREAD_MUTEX_UNLOCK(&tmp->mutex);
  160. return 0;
  161. }
  162. static int starpu_leveldb_full_write(void *base, void *obj, void *ptr, size_t size)
  163. {
  164. struct starpu_leveldb_obj *tmp = (struct starpu_leveldb_obj *) obj;
  165. struct starpu_leveldb_base *base_tmp = (struct starpu_leveldb_base *) base;
  166. /* update file size to achieve correct writes */
  167. tmp->size = size;
  168. leveldb::WriteOptions write_options;
  169. write_options.sync = true;
  170. leveldb::Status s = base_tmp->db->Put(write_options, tmp->key, (char *)ptr);
  171. STARPU_ASSERT(s.ok());
  172. return 0;
  173. }
  174. /* create a new copy of parameter == base */
  175. static void *starpu_leveldb_plug(void *parameter, starpu_ssize_t size STARPU_ATTRIBUTE_UNUSED)
  176. {
  177. struct starpu_leveldb_base *tmp = (struct starpu_leveldb_base *)malloc(sizeof(struct starpu_leveldb_base));
  178. STARPU_ASSERT(tmp);
  179. leveldb::Status status;
  180. leveldb::DB *db;
  181. leveldb::Options options;
  182. options.create_if_missing = true;
  183. /* try to create the database */
  184. options.error_if_exists = true;
  185. status = leveldb::DB::Open(options, (char *) parameter, &db);
  186. tmp->created = true;
  187. /* if it has already been created before */
  188. if (!status.ok())
  189. {
  190. options.error_if_exists = false;
  191. status = leveldb::DB::Open(options, (char *) parameter, &db);
  192. STARPU_ASSERT_MSG(status.ok(), "StarPU leveldb plug failed !");
  193. tmp->created = false;
  194. }
  195. tmp->db = db;
  196. STARPU_ASSERT(status.ok());
  197. return (void *) tmp;
  198. }
  199. /* free memory allocated for the base */
  200. static void starpu_leveldb_unplug(void *base)
  201. {
  202. struct starpu_leveldb_base *base_tmp = (struct starpu_leveldb_base *) base;
  203. if(base_tmp->created)
  204. delete base_tmp->db;
  205. free(base);
  206. }
  207. static int get_leveldb_bandwidth_between_disk_and_main_ram(unsigned node)
  208. {
  209. unsigned iter;
  210. double timing_slowness, timing_latency;
  211. double start;
  212. double end;
  213. srand(time (NULL));
  214. char *buf = (char *)malloc(SIZE_DISK_MIN*sizeof(char));
  215. STARPU_ASSERT(buf);
  216. /* allocate memory */
  217. void *mem = _starpu_disk_alloc(node, SIZE_DISK_MIN);
  218. /* fail to alloc */
  219. if (mem == NULL)
  220. {
  221. free(buf);
  222. return 0;
  223. }
  224. /* Measure upload slowness */
  225. start = starpu_timing_now();
  226. for (iter = 0; iter < NITER; ++iter)
  227. {
  228. _starpu_disk_write(STARPU_MAIN_RAM, node, mem, buf, 0, SIZE_DISK_MIN, NULL);
  229. }
  230. end = starpu_timing_now();
  231. timing_slowness = end - start;
  232. /* free memory */
  233. free(buf);
  234. buf = (char *)malloc(sizeof(char));
  235. STARPU_ASSERT(buf);
  236. /* Measure latency */
  237. start = starpu_timing_now();
  238. for (iter = 0; iter < NITER; ++iter)
  239. {
  240. _starpu_disk_write(STARPU_MAIN_RAM, node, mem, buf, rand() % (SIZE_DISK_MIN -1) , 1, NULL);
  241. }
  242. end = starpu_timing_now();
  243. timing_latency = end - start;
  244. _starpu_disk_free(node, mem, SIZE_DISK_MIN);
  245. free(buf);
  246. _starpu_save_bandwidth_and_latency_disk((NITER/timing_slowness)*1000000, (NITER/timing_slowness)*1000000,
  247. timing_latency/NITER, timing_latency/NITER, node);
  248. return 1;
  249. }
  250. #if __cplusplus >= 201103L
  251. struct starpu_disk_ops starpu_disk_leveldb_ops =
  252. {
  253. .plug = starpu_leveldb_plug,
  254. .unplug = starpu_leveldb_unplug,
  255. .bandwidth = get_leveldb_bandwidth_between_disk_and_main_ram,
  256. .alloc = starpu_leveldb_alloc,
  257. .free = starpu_leveldb_free,
  258. .open = starpu_leveldb_open,
  259. .close = starpu_leveldb_close,
  260. .read = starpu_leveldb_read,
  261. .write = starpu_leveldb_write,
  262. .full_read = starpu_leveldb_full_read,
  263. .full_write = starpu_leveldb_full_write,
  264. .async_write = NULL,
  265. .async_read = NULL,
  266. .async_full_read = NULL,
  267. .async_full_write = NULL,
  268. .copy = NULL,
  269. .wait_request = NULL,
  270. .test_request = NULL,
  271. .free_request = NULL
  272. };
  273. #else
  274. struct starpu_disk_ops starpu_disk_leveldb_ops =
  275. {
  276. starpu_leveldb_plug,
  277. starpu_leveldb_unplug,
  278. get_leveldb_bandwidth_between_disk_and_main_ram,
  279. starpu_leveldb_alloc,
  280. starpu_leveldb_free,
  281. starpu_leveldb_open,
  282. starpu_leveldb_close,
  283. starpu_leveldb_read,
  284. starpu_leveldb_write,
  285. starpu_leveldb_full_read,
  286. starpu_leveldb_full_write,
  287. NULL,
  288. NULL,
  289. NULL,
  290. NULL,
  291. NULL,
  292. NULL,
  293. NULL,
  294. NULL
  295. };
  296. #endif