profiling.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <starpu.h>
  17. #include <starpu_profiling.h>
  18. #include <profiling/profiling.h>
  19. #include <core/workers.h>
  20. #include <common/config.h>
  21. #include <common/utils.h>
  22. #include <common/timing.h>
  23. #include <common/fxt.h>
  24. #include <errno.h>
  25. static struct starpu_worker_profiling_info worker_info[STARPU_NMAXWORKERS];
  26. static pthread_mutex_t worker_info_mutex[STARPU_NMAXWORKERS];
  27. /* In case the worker is still sleeping when the user request profiling info,
  28. * we need to account for the time elasped while sleeping. */
  29. static unsigned worker_registered_sleeping_start[STARPU_NMAXWORKERS];
  30. static struct timespec sleeping_start_date[STARPU_NMAXWORKERS];
  31. static unsigned worker_registered_executing_start[STARPU_NMAXWORKERS];
  32. static struct timespec executing_start_date[STARPU_NMAXWORKERS];
  33. /* Store the busid of the different (src, dst) pairs. busid_matrix[src][dst]
  34. * contains the busid of (src, dst) or -1 if the bus was not registered. */
  35. struct node_pair {
  36. int src;
  37. int dst;
  38. struct starpu_bus_profiling_info *bus_info;
  39. };
  40. static int busid_matrix[STARPU_MAXNODES][STARPU_MAXNODES];
  41. static struct starpu_bus_profiling_info bus_profiling_info[STARPU_MAXNODES][STARPU_MAXNODES];
  42. static struct node_pair busid_to_node_pair[STARPU_MAXNODES*STARPU_MAXNODES];
  43. static int busid_cnt = 0;
  44. static void _do_starpu_bus_reset_profiling_info(struct starpu_bus_profiling_info *bus_info);
  45. /*
  46. * Global control of profiling
  47. */
  48. /* Disabled by default */
  49. static int profiling = 0;
  50. int starpu_profiling_status_set(int status)
  51. {
  52. int prev_value = profiling;
  53. profiling = status;
  54. STARPU_TRACE_SET_PROFILING(status);
  55. /* If we enable profiling, we reset the counters. */
  56. if (status == STARPU_PROFILING_ENABLE)
  57. {
  58. int worker;
  59. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  60. _starpu_worker_reset_profiling_info(worker);
  61. int busid;
  62. int bus_cnt = starpu_bus_get_count();
  63. for (busid = 0; busid < bus_cnt; busid++)
  64. {
  65. struct starpu_bus_profiling_info *bus_info;
  66. bus_info = busid_to_node_pair[busid].bus_info;
  67. _do_starpu_bus_reset_profiling_info(bus_info);;
  68. }
  69. }
  70. return prev_value;
  71. }
  72. int starpu_profiling_status_get(void)
  73. {
  74. return profiling;
  75. }
  76. void starpu_profiling_init(void)
  77. {
  78. int worker;
  79. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  80. {
  81. PTHREAD_MUTEX_INIT(&worker_info_mutex[worker], NULL);
  82. _starpu_worker_reset_profiling_info(worker);
  83. }
  84. }
  85. void starpu_profiling_terminate(void)
  86. {
  87. }
  88. /*
  89. * Task profiling
  90. */
  91. struct starpu_task_profiling_info *_starpu_allocate_profiling_info_if_needed(void)
  92. {
  93. struct starpu_task_profiling_info *info = NULL;
  94. if (profiling)
  95. {
  96. info = calloc(1, sizeof(struct starpu_task_profiling_info));
  97. STARPU_ASSERT(info);
  98. starpu_timespec_clear(&info->submit_time);
  99. starpu_timespec_clear(&info->start_time);
  100. starpu_timespec_clear(&info->end_time);
  101. }
  102. return info;
  103. }
  104. /*
  105. * Worker profiling
  106. */
  107. static void _do_starpu_worker_reset_profiling_info(int workerid)
  108. {
  109. starpu_clock_gettime(&worker_info[workerid].start_time);
  110. /* This is computed in a lazy fashion when the application queries
  111. * profiling info. */
  112. starpu_timespec_clear(&worker_info[workerid].total_time);
  113. starpu_timespec_clear(&worker_info[workerid].executing_time);
  114. starpu_timespec_clear(&worker_info[workerid].sleeping_time);
  115. worker_info[workerid].executed_tasks = 0;
  116. /* We detect if the worker is already sleeping or doing some
  117. * computation */
  118. starpu_worker_status status = _starpu_worker_get_status(workerid);
  119. if (status == STATUS_SLEEPING)
  120. {
  121. worker_registered_sleeping_start[workerid] = 1;
  122. starpu_clock_gettime(&sleeping_start_date[workerid]);
  123. }
  124. else {
  125. worker_registered_sleeping_start[workerid] = 0;
  126. }
  127. if (status == STATUS_EXECUTING)
  128. {
  129. worker_registered_executing_start[workerid] = 1;
  130. starpu_clock_gettime(&executing_start_date[workerid]);
  131. }
  132. else {
  133. worker_registered_executing_start[workerid] = 0;
  134. }
  135. }
  136. void _starpu_worker_reset_profiling_info(int workerid)
  137. {
  138. PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  139. _do_starpu_worker_reset_profiling_info(workerid);
  140. PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  141. }
  142. void _starpu_worker_register_sleeping_start_date(int workerid, struct timespec *sleeping_start)
  143. {
  144. if (profiling)
  145. {
  146. PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  147. worker_registered_sleeping_start[workerid] = 1;
  148. memcpy(&sleeping_start_date[workerid], sleeping_start, sizeof(struct timespec));
  149. PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  150. }
  151. }
  152. void _starpu_worker_register_executing_start_date(int workerid, struct timespec *executing_start)
  153. {
  154. if (profiling)
  155. {
  156. PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  157. worker_registered_executing_start[workerid] = 1;
  158. memcpy(&executing_start_date[workerid], executing_start, sizeof(struct timespec));
  159. PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  160. }
  161. }
  162. void _starpu_worker_update_profiling_info_sleeping(int workerid, struct timespec *sleeping_start, struct timespec *sleeping_end)
  163. {
  164. if (profiling)
  165. {
  166. PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  167. /* Perhaps that profiling was enabled while the worker was
  168. * already blocked, so we don't measure (end - start), but
  169. * (end - max(start,worker_start)) where worker_start is the
  170. * date of the previous profiling info reset on the worker */
  171. struct timespec *worker_start = &worker_info[workerid].start_time;
  172. if (starpu_timespec_cmp(sleeping_start, worker_start, <))
  173. {
  174. /* sleeping_start < worker_start */
  175. sleeping_start = worker_start;
  176. }
  177. struct timespec sleeping_time;
  178. starpu_timespec_sub(sleeping_end, sleeping_start, &sleeping_time);
  179. starpu_timespec_accumulate(&worker_info[workerid].sleeping_time, &sleeping_time);
  180. worker_registered_sleeping_start[workerid] = 0;
  181. PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  182. }
  183. }
  184. void _starpu_worker_update_profiling_info_executing(int workerid, struct timespec *executing_time, int executed_tasks)
  185. {
  186. if (profiling)
  187. {
  188. PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  189. starpu_timespec_accumulate(&worker_info[workerid].executing_time, executing_time);
  190. worker_info[workerid].executed_tasks += executed_tasks;
  191. PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  192. }
  193. }
  194. int starpu_worker_get_profiling_info(int workerid, struct starpu_worker_profiling_info *info)
  195. {
  196. if (!profiling)
  197. return -EINVAL;
  198. PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  199. if (info)
  200. {
  201. /* The total time is computed in a lazy fashion */
  202. struct timespec now;
  203. starpu_clock_gettime(&now);
  204. /* In case some worker is currently sleeping, we take into
  205. * account the time spent since it registered. */
  206. if (worker_registered_sleeping_start[workerid])
  207. {
  208. struct timespec sleeping_time;
  209. starpu_timespec_sub(&now, &sleeping_start_date[workerid], &sleeping_time);
  210. starpu_timespec_accumulate(&worker_info[workerid].sleeping_time, &sleeping_time);
  211. }
  212. if (worker_registered_executing_start[workerid])
  213. {
  214. struct timespec executing_time;
  215. starpu_timespec_sub(&now, &executing_start_date[workerid], &executing_time);
  216. starpu_timespec_accumulate(&worker_info[workerid].executing_time, &executing_time);
  217. }
  218. /* total_time = now - start_time */
  219. starpu_timespec_sub(&now, &worker_info[workerid].start_time,
  220. &worker_info[workerid].total_time);
  221. memcpy(info, &worker_info[workerid], sizeof(struct starpu_worker_profiling_info));
  222. }
  223. _do_starpu_worker_reset_profiling_info(workerid);
  224. PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  225. return 0;
  226. }
  227. /*
  228. * Bus profiling
  229. */
  230. void _starpu_initialize_busid_matrix(void)
  231. {
  232. int i, j;
  233. for (j = 0; j < STARPU_MAXNODES; j++)
  234. for (i = 0; i < STARPU_MAXNODES; i++)
  235. busid_matrix[i][j] = -1;
  236. busid_cnt = 0;
  237. }
  238. static void _do_starpu_bus_reset_profiling_info(struct starpu_bus_profiling_info *bus_info)
  239. {
  240. starpu_clock_gettime(&bus_info->start_time);
  241. bus_info->transferred_bytes = 0;
  242. bus_info->transfer_count = 0;
  243. }
  244. int _starpu_register_bus(int src_node, int dst_node)
  245. {
  246. if (busid_matrix[src_node][dst_node] != -1)
  247. return -EBUSY;
  248. int busid = STARPU_ATOMIC_ADD(&busid_cnt, 1) - 1;
  249. busid_matrix[src_node][dst_node] = busid;
  250. busid_to_node_pair[busid].src = src_node;
  251. busid_to_node_pair[busid].dst = dst_node;
  252. busid_to_node_pair[busid].bus_info = &bus_profiling_info[src_node][dst_node];
  253. _do_starpu_bus_reset_profiling_info(&bus_profiling_info[src_node][dst_node]);
  254. return busid;
  255. }
  256. int starpu_bus_get_count(void)
  257. {
  258. return busid_cnt;
  259. }
  260. int starpu_bus_get_id(int src, int dst)
  261. {
  262. return busid_matrix[src][dst];
  263. }
  264. int starpu_bus_get_src(int busid)
  265. {
  266. return busid_to_node_pair[busid].src;
  267. }
  268. int starpu_bus_get_dst(int busid)
  269. {
  270. return busid_to_node_pair[busid].dst;
  271. }
  272. int starpu_bus_get_profiling_info(int busid, struct starpu_bus_profiling_info *bus_info)
  273. {
  274. int src_node = busid_to_node_pair[busid].src;
  275. int dst_node = busid_to_node_pair[busid].dst;
  276. /* XXX protect all this method with a mutex */
  277. if (bus_info)
  278. {
  279. struct timespec now;
  280. starpu_clock_gettime(&now);
  281. /* total_time = now - start_time */
  282. starpu_timespec_sub(&now, &bus_profiling_info[src_node][dst_node].start_time,
  283. &bus_profiling_info[src_node][dst_node].total_time);
  284. memcpy(bus_info, &bus_profiling_info[src_node][dst_node], sizeof(struct starpu_bus_profiling_info));
  285. }
  286. _do_starpu_bus_reset_profiling_info(&bus_profiling_info[src_node][dst_node]);
  287. return 0;
  288. }
  289. void _starpu_bus_update_profiling_info(int src_node, int dst_node, size_t size)
  290. {
  291. bus_profiling_info[src_node][dst_node].transferred_bytes += size;
  292. bus_profiling_info[src_node][dst_node].transfer_count++;
  293. // fprintf(stderr, "PROFILE %d -> %d : %d (cnt %d)\n", src_node, dst_node, size, bus_profiling_info[src_node][dst_node].transfer_count);
  294. }