profiling.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  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_worker_profiling_info worker_info[STARPU_NMAXWORKERS];
  27. static pthread_mutex_t worker_info_mutex[STARPU_NMAXWORKERS];
  28. /* In case the worker is still sleeping when the user request profiling info,
  29. * we need to account for the time elasped while sleeping. */
  30. static unsigned worker_registered_sleeping_start[STARPU_NMAXWORKERS];
  31. static struct timespec sleeping_start_date[STARPU_NMAXWORKERS];
  32. static unsigned worker_registered_executing_start[STARPU_NMAXWORKERS];
  33. static struct timespec executing_start_date[STARPU_NMAXWORKERS];
  34. /* Store the busid of the different (src, dst) pairs. busid_matrix[src][dst]
  35. * contains the busid of (src, dst) or -1 if the bus was not registered. */
  36. struct node_pair
  37. {
  38. int src;
  39. int dst;
  40. struct starpu_bus_profiling_info *bus_info;
  41. };
  42. static int busid_matrix[STARPU_MAXNODES][STARPU_MAXNODES];
  43. static struct starpu_bus_profiling_info bus_profiling_info[STARPU_MAXNODES][STARPU_MAXNODES];
  44. static struct node_pair busid_to_node_pair[STARPU_MAXNODES*STARPU_MAXNODES];
  45. static unsigned busid_cnt = 0;
  46. static void _starpu_bus_reset_profiling_info(struct starpu_bus_profiling_info *bus_info);
  47. /*
  48. * Global control of profiling
  49. */
  50. /* Disabled by default */
  51. static int profiling = 0;
  52. int starpu_profiling_status_set(int status)
  53. {
  54. int prev_value = profiling;
  55. profiling = status;
  56. _STARPU_TRACE_SET_PROFILING(status);
  57. /* If we enable profiling, we reset the counters. */
  58. if (status == STARPU_PROFILING_ENABLE)
  59. {
  60. int worker;
  61. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  62. _starpu_worker_reset_profiling_info(worker);
  63. int busid;
  64. int bus_cnt = starpu_bus_get_count();
  65. for (busid = 0; busid < bus_cnt; busid++)
  66. {
  67. struct starpu_bus_profiling_info *bus_info;
  68. bus_info = busid_to_node_pair[busid].bus_info;
  69. _starpu_bus_reset_profiling_info(bus_info);
  70. }
  71. }
  72. return prev_value;
  73. }
  74. int starpu_profiling_status_get(void)
  75. {
  76. return profiling;
  77. }
  78. void _starpu_profiling_init(void)
  79. {
  80. int worker;
  81. const char *env;
  82. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  83. {
  84. _STARPU_PTHREAD_MUTEX_INIT(&worker_info_mutex[worker], NULL);
  85. _starpu_worker_reset_profiling_info(worker);
  86. }
  87. if ((env = getenv("STARPU_PROFILING")) && atoi(env))
  88. profiling = 1;
  89. }
  90. void _starpu_profiling_terminate(void)
  91. {
  92. }
  93. /*
  94. * Task profiling
  95. */
  96. struct starpu_task_profiling_info *_starpu_allocate_profiling_info_if_needed(struct starpu_task *task)
  97. {
  98. struct starpu_task_profiling_info *info = NULL;
  99. /* If we are benchmarking, we need room for the power consumption */
  100. if (profiling || (task->cl && task->cl->power_model && (task->cl->power_model->benchmarking || _starpu_get_calibrate_flag())))
  101. {
  102. info = (struct starpu_task_profiling_info *) calloc(1, sizeof(struct starpu_task_profiling_info));
  103. STARPU_ASSERT(info);
  104. }
  105. return info;
  106. }
  107. /*
  108. * Worker profiling
  109. */
  110. static void _starpu_worker_reset_profiling_info_with_lock(int workerid)
  111. {
  112. _starpu_clock_gettime(&worker_info[workerid].start_time);
  113. /* This is computed in a lazy fashion when the application queries
  114. * profiling info. */
  115. starpu_timespec_clear(&worker_info[workerid].total_time);
  116. starpu_timespec_clear(&worker_info[workerid].executing_time);
  117. starpu_timespec_clear(&worker_info[workerid].sleeping_time);
  118. worker_info[workerid].executed_tasks = 0;
  119. worker_info[workerid].used_cycles = 0;
  120. worker_info[workerid].stall_cycles = 0;
  121. worker_info[workerid].power_consumed = 0;
  122. /* We detect if the worker is already sleeping or doing some
  123. * computation */
  124. enum _starpu_worker_status status = _starpu_worker_get_status(workerid);
  125. if (status == STATUS_SLEEPING)
  126. {
  127. worker_registered_sleeping_start[workerid] = 1;
  128. _starpu_clock_gettime(&sleeping_start_date[workerid]);
  129. }
  130. else
  131. {
  132. worker_registered_sleeping_start[workerid] = 0;
  133. }
  134. if (status == STATUS_EXECUTING)
  135. {
  136. worker_registered_executing_start[workerid] = 1;
  137. _starpu_clock_gettime(&executing_start_date[workerid]);
  138. }
  139. else
  140. {
  141. worker_registered_executing_start[workerid] = 0;
  142. }
  143. }
  144. void _starpu_worker_reset_profiling_info(int workerid)
  145. {
  146. _STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  147. _starpu_worker_reset_profiling_info_with_lock(workerid);
  148. _STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  149. }
  150. void _starpu_worker_register_sleeping_start_date(int workerid, struct timespec *sleeping_start)
  151. {
  152. if (profiling)
  153. {
  154. _STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  155. worker_registered_sleeping_start[workerid] = 1;
  156. memcpy(&sleeping_start_date[workerid], sleeping_start, sizeof(struct timespec));
  157. _STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  158. }
  159. }
  160. void _starpu_worker_register_executing_start_date(int workerid, struct timespec *executing_start)
  161. {
  162. if (profiling)
  163. {
  164. _STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  165. worker_registered_executing_start[workerid] = 1;
  166. memcpy(&executing_start_date[workerid], executing_start, sizeof(struct timespec));
  167. _STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  168. }
  169. }
  170. void _starpu_worker_update_profiling_info_sleeping(int workerid, struct timespec *sleeping_start, struct timespec *sleeping_end)
  171. {
  172. if (profiling)
  173. {
  174. _STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  175. /* Perhaps that profiling was enabled while the worker was
  176. * already blocked, so we don't measure (end - start), but
  177. * (end - max(start,worker_start)) where worker_start is the
  178. * date of the previous profiling info reset on the worker */
  179. struct timespec *worker_start = &worker_info[workerid].start_time;
  180. if (starpu_timespec_cmp(sleeping_start, worker_start, <))
  181. {
  182. /* sleeping_start < worker_start */
  183. sleeping_start = worker_start;
  184. }
  185. struct timespec sleeping_time;
  186. starpu_timespec_sub(sleeping_end, sleeping_start, &sleeping_time);
  187. starpu_timespec_accumulate(&worker_info[workerid].sleeping_time, &sleeping_time);
  188. worker_registered_sleeping_start[workerid] = 0;
  189. _STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  190. }
  191. }
  192. 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 power_consumed)
  193. {
  194. if (profiling)
  195. {
  196. _STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  197. if (executing_time)
  198. starpu_timespec_accumulate(&worker_info[workerid].executing_time, executing_time);
  199. worker_info[workerid].used_cycles += used_cycles;
  200. worker_info[workerid].stall_cycles += stall_cycles;
  201. worker_info[workerid].power_consumed += power_consumed;
  202. worker_info[workerid].executed_tasks += executed_tasks;
  203. _STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  204. }
  205. else /* Not thread safe, shouldn't be too much a problem */
  206. worker_info[workerid].executed_tasks += executed_tasks;
  207. }
  208. int starpu_worker_get_profiling_info(int workerid, struct starpu_worker_profiling_info *info)
  209. {
  210. if (!profiling)
  211. {
  212. /* Not thread safe, shouldn't be too much a problem */
  213. info->executed_tasks = worker_info[workerid].executed_tasks;
  214. }
  215. _STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  216. if (info)
  217. {
  218. /* The total time is computed in a lazy fashion */
  219. struct timespec now;
  220. _starpu_clock_gettime(&now);
  221. /* In case some worker is currently sleeping, we take into
  222. * account the time spent since it registered. */
  223. if (worker_registered_sleeping_start[workerid])
  224. {
  225. struct timespec sleeping_time;
  226. starpu_timespec_sub(&now, &sleeping_start_date[workerid], &sleeping_time);
  227. starpu_timespec_accumulate(&worker_info[workerid].sleeping_time, &sleeping_time);
  228. }
  229. if (worker_registered_executing_start[workerid])
  230. {
  231. struct timespec executing_time;
  232. starpu_timespec_sub(&now, &executing_start_date[workerid], &executing_time);
  233. starpu_timespec_accumulate(&worker_info[workerid].executing_time, &executing_time);
  234. }
  235. /* total_time = now - start_time */
  236. starpu_timespec_sub(&now, &worker_info[workerid].start_time,
  237. &worker_info[workerid].total_time);
  238. memcpy(info, &worker_info[workerid], sizeof(struct starpu_worker_profiling_info));
  239. }
  240. _starpu_worker_reset_profiling_info_with_lock(workerid);
  241. _STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  242. return 0;
  243. }
  244. /* When did the task reach the scheduler ? */
  245. void _starpu_profiling_set_task_push_start_time(struct starpu_task *task)
  246. {
  247. if (!profiling)
  248. return;
  249. struct starpu_task_profiling_info *profiling_info;
  250. profiling_info = task->profiling_info;
  251. if (profiling_info)
  252. _starpu_clock_gettime(&profiling_info->push_start_time);
  253. }
  254. void _starpu_profiling_set_task_push_end_time(struct starpu_task *task)
  255. {
  256. if (!profiling)
  257. return;
  258. struct starpu_task_profiling_info *profiling_info;
  259. profiling_info = task->profiling_info;
  260. if (profiling_info)
  261. _starpu_clock_gettime(&profiling_info->push_end_time);
  262. }
  263. /*
  264. * Bus profiling
  265. */
  266. void _starpu_initialize_busid_matrix(void)
  267. {
  268. int i, j;
  269. for (j = 0; j < STARPU_MAXNODES; j++)
  270. for (i = 0; i < STARPU_MAXNODES; i++)
  271. busid_matrix[i][j] = -1;
  272. busid_cnt = 0;
  273. }
  274. static void _starpu_bus_reset_profiling_info(struct starpu_bus_profiling_info *bus_info)
  275. {
  276. _starpu_clock_gettime(&bus_info->start_time);
  277. bus_info->transferred_bytes = 0;
  278. bus_info->transfer_count = 0;
  279. }
  280. int _starpu_register_bus(int src_node, int dst_node)
  281. {
  282. if (busid_matrix[src_node][dst_node] != -1)
  283. return -EBUSY;
  284. int busid = STARPU_ATOMIC_ADD(&busid_cnt, 1) - 1;
  285. busid_matrix[src_node][dst_node] = busid;
  286. busid_to_node_pair[busid].src = src_node;
  287. busid_to_node_pair[busid].dst = dst_node;
  288. busid_to_node_pair[busid].bus_info = &bus_profiling_info[src_node][dst_node];
  289. _starpu_bus_reset_profiling_info(&bus_profiling_info[src_node][dst_node]);
  290. return busid;
  291. }
  292. int starpu_bus_get_count(void)
  293. {
  294. return busid_cnt;
  295. }
  296. int starpu_bus_get_id(int src, int dst)
  297. {
  298. return busid_matrix[src][dst];
  299. }
  300. int starpu_bus_get_src(int busid)
  301. {
  302. return busid_to_node_pair[busid].src;
  303. }
  304. int starpu_bus_get_dst(int busid)
  305. {
  306. return busid_to_node_pair[busid].dst;
  307. }
  308. int starpu_bus_get_profiling_info(int busid, struct starpu_bus_profiling_info *bus_info)
  309. {
  310. int src_node = busid_to_node_pair[busid].src;
  311. int dst_node = busid_to_node_pair[busid].dst;
  312. /* XXX protect all this method with a mutex */
  313. if (bus_info)
  314. {
  315. struct timespec now;
  316. _starpu_clock_gettime(&now);
  317. /* total_time = now - start_time */
  318. starpu_timespec_sub(&now, &bus_profiling_info[src_node][dst_node].start_time,
  319. &bus_profiling_info[src_node][dst_node].total_time);
  320. memcpy(bus_info, &bus_profiling_info[src_node][dst_node], sizeof(struct starpu_bus_profiling_info));
  321. }
  322. _starpu_bus_reset_profiling_info(&bus_profiling_info[src_node][dst_node]);
  323. return 0;
  324. }
  325. void _starpu_bus_update_profiling_info(int src_node, int dst_node, size_t size)
  326. {
  327. bus_profiling_info[src_node][dst_node].transferred_bytes += size;
  328. bus_profiling_info[src_node][dst_node].transfer_count++;
  329. // fprintf(stderr, "PROFILE %d -> %d : %d (cnt %d)\n", src_node, dst_node, size, bus_profiling_info[src_node][dst_node].transfer_count);
  330. }