profiling.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013, 2016-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2016 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <starpu_profiling.h>
  19. #include <profiling/profiling.h>
  20. #include <core/workers.h>
  21. #include <common/config.h>
  22. #include <common/utils.h>
  23. #include <common/timing.h>
  24. #include <common/fxt.h>
  25. #include <errno.h>
  26. static struct starpu_profiling_worker_info worker_info[STARPU_NMAXWORKERS];
  27. /* TODO: rather use rwlock */
  28. static starpu_pthread_mutex_t worker_info_mutex[STARPU_NMAXWORKERS];
  29. /* In case the worker is still sleeping when the user request profiling info,
  30. * we need to account for the time elasped while sleeping. */
  31. static unsigned worker_registered_sleeping_start[STARPU_NMAXWORKERS];
  32. static struct timespec sleeping_start_date[STARPU_NMAXWORKERS];
  33. static unsigned worker_registered_executing_start[STARPU_NMAXWORKERS];
  34. static struct timespec executing_start_date[STARPU_NMAXWORKERS];
  35. /* Store the busid of the different (src, dst) pairs. busid_matrix[src][dst]
  36. * contains the busid of (src, dst) or -1 if the bus was not registered. */
  37. struct node_pair
  38. {
  39. int src;
  40. int dst;
  41. struct starpu_profiling_bus_info *bus_info;
  42. };
  43. static int busid_matrix[STARPU_MAXNODES][STARPU_MAXNODES];
  44. static struct starpu_profiling_bus_info bus_profiling_info[STARPU_MAXNODES][STARPU_MAXNODES];
  45. static struct node_pair busid_to_node_pair[STARPU_MAXNODES*STARPU_MAXNODES];
  46. static char bus_direct[STARPU_MAXNODES*STARPU_MAXNODES];
  47. static int bus_ngpus[STARPU_MAXNODES*STARPU_MAXNODES];
  48. static unsigned busid_cnt = 0;
  49. static void _starpu_bus_reset_profiling_info(struct starpu_profiling_bus_info *bus_info);
  50. /*
  51. * Global control of profiling
  52. */
  53. /* Disabled by default, unless simulating */
  54. int _starpu_profiling =
  55. #ifdef STARPU_SIMGRID
  56. 1
  57. #else
  58. 0
  59. #endif
  60. ;
  61. void starpu_profiling_init()
  62. {
  63. _starpu_profiling_init();
  64. }
  65. void _starpu_profiling_reset_counters()
  66. {
  67. int worker;
  68. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  69. {
  70. _starpu_worker_reset_profiling_info(worker);
  71. }
  72. int busid;
  73. int bus_cnt = starpu_bus_get_count();
  74. for (busid = 0; busid < bus_cnt; busid++)
  75. {
  76. struct starpu_profiling_bus_info *bus_info;
  77. bus_info = busid_to_node_pair[busid].bus_info;
  78. _starpu_bus_reset_profiling_info(bus_info);
  79. }
  80. }
  81. int starpu_profiling_status_set(int status)
  82. {
  83. ANNOTATE_HAPPENS_AFTER(&_starpu_profiling);
  84. int prev_value = _starpu_profiling;
  85. _starpu_profiling = status;
  86. ANNOTATE_HAPPENS_BEFORE(&_starpu_profiling);
  87. _STARPU_TRACE_SET_PROFILING(status);
  88. /* If we enable profiling, we reset the counters. */
  89. if (status == STARPU_PROFILING_ENABLE)
  90. {
  91. _starpu_profiling_reset_counters();
  92. }
  93. return prev_value;
  94. }
  95. void _starpu_profiling_init(void)
  96. {
  97. int worker;
  98. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  99. {
  100. STARPU_PTHREAD_MUTEX_INIT(&worker_info_mutex[worker], NULL);
  101. }
  102. }
  103. void _starpu_profiling_start(void)
  104. {
  105. const char *env;
  106. if ((env = starpu_getenv("STARPU_PROFILING")) && atoi(env))
  107. {
  108. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  109. }
  110. }
  111. void _starpu_profiling_terminate(void)
  112. {
  113. int worker;
  114. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  115. {
  116. STARPU_PTHREAD_MUTEX_DESTROY(&worker_info_mutex[worker]);
  117. }
  118. }
  119. /*
  120. * Task profiling
  121. */
  122. struct starpu_profiling_task_info *_starpu_allocate_profiling_info_if_needed(struct starpu_task *task)
  123. {
  124. struct starpu_profiling_task_info *info = NULL;
  125. /* If we are benchmarking, we need room for the energy */
  126. if (starpu_profiling_status_get() || (task->cl && task->cl->energy_model && (task->cl->energy_model->benchmarking || _starpu_get_calibrate_flag())))
  127. {
  128. _STARPU_CALLOC(info, 1, sizeof(struct starpu_profiling_task_info));
  129. }
  130. return info;
  131. }
  132. /*
  133. * Worker profiling
  134. */
  135. static void _starpu_worker_reset_profiling_info_with_lock(int workerid)
  136. {
  137. _starpu_clock_gettime(&worker_info[workerid].start_time);
  138. /* This is computed in a lazy fashion when the application queries
  139. * profiling info. */
  140. starpu_timespec_clear(&worker_info[workerid].total_time);
  141. starpu_timespec_clear(&worker_info[workerid].executing_time);
  142. starpu_timespec_clear(&worker_info[workerid].sleeping_time);
  143. worker_info[workerid].executed_tasks = 0;
  144. worker_info[workerid].used_cycles = 0;
  145. worker_info[workerid].stall_cycles = 0;
  146. worker_info[workerid].energy_consumed = 0;
  147. worker_info[workerid].flops = 0;
  148. /* We detect if the worker is already sleeping or doing some
  149. * computation */
  150. enum _starpu_worker_status status = _starpu_worker_get_status(workerid);
  151. if (status == STATUS_SLEEPING)
  152. {
  153. worker_registered_sleeping_start[workerid] = 1;
  154. _starpu_clock_gettime(&sleeping_start_date[workerid]);
  155. }
  156. else
  157. {
  158. worker_registered_sleeping_start[workerid] = 0;
  159. }
  160. if (status == STATUS_EXECUTING)
  161. {
  162. worker_registered_executing_start[workerid] = 1;
  163. _starpu_clock_gettime(&executing_start_date[workerid]);
  164. }
  165. else
  166. {
  167. worker_registered_executing_start[workerid] = 0;
  168. }
  169. }
  170. void _starpu_worker_reset_profiling_info(int workerid)
  171. {
  172. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  173. _starpu_worker_reset_profiling_info_with_lock(workerid);
  174. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  175. }
  176. void _starpu_worker_restart_sleeping(int workerid)
  177. {
  178. if (starpu_profiling_status_get())
  179. {
  180. struct timespec sleep_start_time;
  181. _starpu_clock_gettime(&sleep_start_time);
  182. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  183. worker_registered_sleeping_start[workerid] = 1;
  184. memcpy(&sleeping_start_date[workerid], &sleep_start_time, sizeof(struct timespec));
  185. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  186. }
  187. }
  188. void _starpu_worker_stop_sleeping(int workerid)
  189. {
  190. if (starpu_profiling_status_get())
  191. {
  192. struct timespec *sleeping_start, sleep_end_time;
  193. _starpu_clock_gettime(&sleep_end_time);
  194. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  195. sleeping_start = &sleeping_start_date[workerid];
  196. /* Perhaps that profiling was enabled while the worker was
  197. * already blocked, so we don't measure (end - start), but
  198. * (end - max(start,worker_start)) where worker_start is the
  199. * date of the previous profiling info reset on the worker */
  200. struct timespec *worker_start = &worker_info[workerid].start_time;
  201. if (starpu_timespec_cmp(sleeping_start, worker_start, <))
  202. {
  203. /* sleeping_start < worker_start */
  204. sleeping_start = worker_start;
  205. }
  206. struct timespec sleeping_time;
  207. starpu_timespec_sub(&sleep_end_time, sleeping_start, &sleeping_time);
  208. starpu_timespec_accumulate(&worker_info[workerid].sleeping_time, &sleeping_time);
  209. worker_registered_sleeping_start[workerid] = 0;
  210. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  211. }
  212. }
  213. void _starpu_worker_register_executing_start_date(int workerid, struct timespec *executing_start)
  214. {
  215. if (starpu_profiling_status_get())
  216. {
  217. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  218. worker_registered_executing_start[workerid] = 1;
  219. memcpy(&executing_start_date[workerid], executing_start, sizeof(struct timespec));
  220. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  221. }
  222. }
  223. void _starpu_worker_register_executing_end(int workerid)
  224. {
  225. if (starpu_profiling_status_get())
  226. {
  227. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  228. worker_registered_executing_start[workerid] = 0;
  229. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  230. }
  231. }
  232. void _starpu_worker_update_profiling_info_executing(int workerid, struct timespec *executing_time, int executed_tasks, uint64_t used_cycles, uint64_t stall_cycles, double energy_consumed, double flops)
  233. {
  234. if (starpu_profiling_status_get())
  235. {
  236. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  237. if (executing_time)
  238. starpu_timespec_accumulate(&worker_info[workerid].executing_time, executing_time);
  239. worker_info[workerid].used_cycles += used_cycles;
  240. worker_info[workerid].stall_cycles += stall_cycles;
  241. worker_info[workerid].energy_consumed += energy_consumed;
  242. worker_info[workerid].executed_tasks += executed_tasks;
  243. worker_info[workerid].flops += flops;
  244. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  245. }
  246. else /* Not thread safe, shouldn't be too much a problem */
  247. worker_info[workerid].executed_tasks += executed_tasks;
  248. }
  249. int starpu_profiling_worker_get_info(int workerid, struct starpu_profiling_worker_info *info)
  250. {
  251. if (!starpu_profiling_status_get())
  252. {
  253. /* Not thread safe, shouldn't be too much a problem */
  254. info->executed_tasks = worker_info[workerid].executed_tasks;
  255. }
  256. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  257. if (info)
  258. {
  259. /* The total time is computed in a lazy fashion */
  260. struct timespec now;
  261. _starpu_clock_gettime(&now);
  262. /* In case some worker is currently sleeping, we take into
  263. * account the time spent since it registered. */
  264. if (worker_registered_sleeping_start[workerid])
  265. {
  266. struct timespec sleeping_time;
  267. starpu_timespec_sub(&now, &sleeping_start_date[workerid], &sleeping_time);
  268. starpu_timespec_accumulate(&worker_info[workerid].sleeping_time, &sleeping_time);
  269. }
  270. if (worker_registered_executing_start[workerid])
  271. {
  272. struct timespec executing_time;
  273. starpu_timespec_sub(&now, &executing_start_date[workerid], &executing_time);
  274. starpu_timespec_accumulate(&worker_info[workerid].executing_time, &executing_time);
  275. }
  276. /* total_time = now - start_time */
  277. starpu_timespec_sub(&now, &worker_info[workerid].start_time,
  278. &worker_info[workerid].total_time);
  279. memcpy(info, &worker_info[workerid], sizeof(struct starpu_profiling_worker_info));
  280. }
  281. _starpu_worker_reset_profiling_info_with_lock(workerid);
  282. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  283. return 0;
  284. }
  285. /* When did the task reach the scheduler ? */
  286. void _starpu_profiling_set_task_push_start_time(struct starpu_task *task)
  287. {
  288. if (!starpu_profiling_status_get())
  289. return;
  290. struct starpu_profiling_task_info *profiling_info;
  291. profiling_info = task->profiling_info;
  292. if (profiling_info)
  293. _starpu_clock_gettime(&profiling_info->push_start_time);
  294. }
  295. void _starpu_profiling_set_task_push_end_time(struct starpu_task *task)
  296. {
  297. if (!starpu_profiling_status_get())
  298. return;
  299. struct starpu_profiling_task_info *profiling_info;
  300. profiling_info = task->profiling_info;
  301. if (profiling_info)
  302. _starpu_clock_gettime(&profiling_info->push_end_time);
  303. }
  304. /*
  305. * Bus profiling
  306. */
  307. void _starpu_initialize_busid_matrix(void)
  308. {
  309. int i, j;
  310. for (j = 0; j < STARPU_MAXNODES; j++)
  311. for (i = 0; i < STARPU_MAXNODES; i++)
  312. busid_matrix[i][j] = -1;
  313. busid_cnt = 0;
  314. }
  315. static void _starpu_bus_reset_profiling_info(struct starpu_profiling_bus_info *bus_info)
  316. {
  317. _starpu_clock_gettime(&bus_info->start_time);
  318. bus_info->transferred_bytes = 0;
  319. bus_info->transfer_count = 0;
  320. }
  321. int _starpu_register_bus(int src_node, int dst_node)
  322. {
  323. if (starpu_bus_get_id(src_node, dst_node) != -1)
  324. return -EBUSY;
  325. int busid = STARPU_ATOMIC_ADD(&busid_cnt, 1) - 1;
  326. busid_matrix[src_node][dst_node] = busid;
  327. busid_to_node_pair[busid].src = src_node;
  328. busid_to_node_pair[busid].dst = dst_node;
  329. busid_to_node_pair[busid].bus_info = &bus_profiling_info[src_node][dst_node];
  330. _starpu_bus_reset_profiling_info(&bus_profiling_info[src_node][dst_node]);
  331. return busid;
  332. }
  333. int starpu_bus_get_count(void)
  334. {
  335. return busid_cnt;
  336. }
  337. int starpu_bus_get_id(int src, int dst)
  338. {
  339. return busid_matrix[src][dst];
  340. }
  341. int starpu_bus_get_src(int busid)
  342. {
  343. return busid_to_node_pair[busid].src;
  344. }
  345. int starpu_bus_get_dst(int busid)
  346. {
  347. return busid_to_node_pair[busid].dst;
  348. }
  349. void starpu_bus_set_direct(int busid, int direct)
  350. {
  351. bus_direct[busid] = direct;
  352. }
  353. int starpu_bus_get_direct(int busid)
  354. {
  355. return bus_direct[busid];
  356. }
  357. void starpu_bus_set_ngpus(int busid, int ngpus)
  358. {
  359. bus_ngpus[busid] = ngpus;
  360. }
  361. int starpu_bus_get_ngpus(int busid)
  362. {
  363. struct _starpu_machine_topology *topology = &_starpu_get_machine_config()->topology;
  364. int ngpus = bus_ngpus[busid];
  365. if (!ngpus)
  366. /* Unknown number of GPUs, assume it's shared by all GPUs */
  367. ngpus = topology->ncudagpus+topology->nopenclgpus;
  368. return ngpus;
  369. }
  370. int starpu_bus_get_profiling_info(int busid, struct starpu_profiling_bus_info *bus_info)
  371. {
  372. int src_node = starpu_bus_get_src(busid);
  373. int dst_node = starpu_bus_get_dst(busid);
  374. /* XXX protect all this method with a mutex */
  375. if (bus_info)
  376. {
  377. struct timespec now;
  378. _starpu_clock_gettime(&now);
  379. /* total_time = now - start_time */
  380. starpu_timespec_sub(&now, &bus_profiling_info[src_node][dst_node].start_time,
  381. &bus_profiling_info[src_node][dst_node].total_time);
  382. memcpy(bus_info, &bus_profiling_info[src_node][dst_node], sizeof(struct starpu_profiling_bus_info));
  383. }
  384. _starpu_bus_reset_profiling_info(&bus_profiling_info[src_node][dst_node]);
  385. return 0;
  386. }
  387. void _starpu_bus_update_profiling_info(int src_node, int dst_node, size_t size)
  388. {
  389. bus_profiling_info[src_node][dst_node].transferred_bytes += size;
  390. bus_profiling_info[src_node][dst_node].transfer_count++;
  391. // fprintf(stderr, "PROFILE %d -> %d : %d (cnt %d)\n", src_node, dst_node, size, bus_profiling_info[src_node][dst_node].transfer_count);
  392. }
  393. #undef starpu_profiling_status_get
  394. int starpu_profiling_status_get(void)
  395. {
  396. int ret;
  397. ANNOTATE_HAPPENS_AFTER(&_starpu_profiling);
  398. ret = _starpu_profiling;
  399. ANNOTATE_HAPPENS_BEFORE(&_starpu_profiling);
  400. return ret;
  401. }