profiling.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2020 Federal University of Rio Grande do Sul (UFRGS)
  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. #ifdef STARPU_PAPI
  27. #include <papi.h>
  28. #endif
  29. /* TODO: move to worker structure */
  30. static struct starpu_profiling_worker_info worker_info[STARPU_NMAXWORKERS];
  31. /* TODO: rather use rwlock */
  32. static starpu_pthread_mutex_t worker_info_mutex[STARPU_NMAXWORKERS];
  33. /* In case the worker is still sleeping when the user request profiling info,
  34. * we need to account for the time elasped while sleeping. */
  35. static unsigned worker_registered_sleeping_start[STARPU_NMAXWORKERS];
  36. static struct timespec sleeping_start_date[STARPU_NMAXWORKERS];
  37. static unsigned worker_registered_executing_start[STARPU_NMAXWORKERS];
  38. static struct timespec executing_start_date[STARPU_NMAXWORKERS];
  39. #ifdef STARPU_PAPI
  40. static int papi_events[PAPI_MAX_HWCTRS];
  41. static int papi_nevents = 0;
  42. static int warned_component_unavailable = 0;
  43. #endif
  44. /* Store the busid of the different (src, dst) pairs. busid_matrix[src][dst]
  45. * contains the busid of (src, dst) or -1 if the bus was not registered. */
  46. struct node_pair
  47. {
  48. int src;
  49. int dst;
  50. struct starpu_profiling_bus_info *bus_info;
  51. };
  52. static int busid_matrix[STARPU_MAXNODES][STARPU_MAXNODES];
  53. static struct starpu_profiling_bus_info bus_profiling_info[STARPU_MAXNODES][STARPU_MAXNODES];
  54. static struct node_pair busid_to_node_pair[STARPU_MAXNODES*STARPU_MAXNODES];
  55. static char bus_direct[STARPU_MAXNODES*STARPU_MAXNODES];
  56. static int bus_ngpus[STARPU_MAXNODES*STARPU_MAXNODES];
  57. static unsigned busid_cnt = 0;
  58. static void _starpu_bus_reset_profiling_info(struct starpu_profiling_bus_info *bus_info);
  59. /* Clear all the profiling info related to the worker. */
  60. static void _starpu_worker_reset_profiling_info_with_lock(int workerid);
  61. /*
  62. * Global control of profiling
  63. */
  64. /* Disabled by default, unless simulating */
  65. int _starpu_profiling =
  66. #ifdef STARPU_SIMGRID
  67. 1
  68. #else
  69. 0
  70. #endif
  71. ;
  72. void starpu_profiling_init()
  73. {
  74. _starpu_profiling_init();
  75. }
  76. static void _starpu_profiling_reset_counters()
  77. {
  78. int worker;
  79. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  80. {
  81. _starpu_worker_reset_profiling_info_with_lock(worker);
  82. }
  83. int busid;
  84. int bus_cnt = starpu_bus_get_count();
  85. for (busid = 0; busid < bus_cnt; busid++)
  86. {
  87. struct starpu_profiling_bus_info *bus_info;
  88. bus_info = busid_to_node_pair[busid].bus_info;
  89. _starpu_bus_reset_profiling_info(bus_info);
  90. }
  91. }
  92. int starpu_profiling_status_set(int status)
  93. {
  94. int worker;
  95. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  96. {
  97. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[worker]);
  98. }
  99. ANNOTATE_HAPPENS_AFTER(&_starpu_profiling);
  100. int prev_value = _starpu_profiling;
  101. _starpu_profiling = status;
  102. ANNOTATE_HAPPENS_BEFORE(&_starpu_profiling);
  103. _STARPU_TRACE_SET_PROFILING(status);
  104. /* If we enable profiling, we reset the counters. */
  105. if (status == STARPU_PROFILING_ENABLE)
  106. {
  107. _starpu_profiling_reset_counters();
  108. }
  109. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  110. {
  111. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[worker]);
  112. }
  113. return prev_value;
  114. }
  115. void _starpu_profiling_init(void)
  116. {
  117. int worker;
  118. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  119. {
  120. STARPU_PTHREAD_MUTEX_INIT(&worker_info_mutex[worker], NULL);
  121. }
  122. #ifdef STARPU_PAPI
  123. int retval = PAPI_library_init(PAPI_VER_CURRENT);
  124. if (retval != PAPI_VER_CURRENT)
  125. {
  126. _STARPU_MSG("Failed init PAPI, error: %s.\n", PAPI_strerror(retval));
  127. }
  128. retval = PAPI_thread_init(pthread_self);
  129. if (retval != PAPI_OK)
  130. {
  131. _STARPU_MSG("Failed init PAPI thread, error: %s.\n", PAPI_strerror(retval));
  132. }
  133. char *conf_papi_events;
  134. char *papi_event_name;
  135. conf_papi_events = starpu_getenv("STARPU_PROF_PAPI_EVENTS");
  136. if (conf_papi_events != NULL)
  137. {
  138. while ((papi_event_name = strtok_r(conf_papi_events, " ,", &conf_papi_events)))
  139. {
  140. _STARPU_DEBUG("Loading PAPI Event:%s\n", papi_event_name);
  141. retval = PAPI_event_name_to_code ((char*)papi_event_name, &papi_events[papi_nevents]);
  142. if (retval != PAPI_OK)
  143. _STARPU_MSG("Failed to codify papi event [%s], error: %s.\n", papi_event_name, PAPI_strerror(retval));
  144. else
  145. papi_nevents++;
  146. }
  147. }
  148. #endif
  149. }
  150. #ifdef STARPU_PAPI
  151. void _starpu_profiling_papi_task_start_counters(struct starpu_task *task)
  152. {
  153. if (!starpu_profiling_status_get())
  154. return;
  155. struct starpu_profiling_task_info *profiling_info;
  156. profiling_info = task->profiling_info;
  157. if (profiling_info)
  158. {
  159. profiling_info->papi_event_set = PAPI_NULL;
  160. PAPI_create_eventset(&profiling_info->papi_event_set);
  161. for(int i=0; i<papi_nevents; i++)
  162. {
  163. int ret = PAPI_add_event(profiling_info->papi_event_set, papi_events[i]);
  164. if (ret == PAPI_ECMP_DISABLED && !warned_component_unavailable)
  165. {
  166. _STARPU_MSG("Error while registering Papi event: Component containing event is disabled. Try running `papi_component_avail` to get more information.\n");
  167. warned_component_unavailable = 1;
  168. }
  169. profiling_info->papi_values[i]=0;
  170. }
  171. PAPI_reset(profiling_info->papi_event_set);
  172. PAPI_start(profiling_info->papi_event_set);
  173. }
  174. }
  175. void _starpu_profiling_papi_task_stop_counters(struct starpu_task *task)
  176. {
  177. if (!starpu_profiling_status_get())
  178. return;
  179. struct starpu_profiling_task_info *profiling_info;
  180. profiling_info = task->profiling_info;
  181. if (profiling_info)
  182. {
  183. PAPI_stop(profiling_info->papi_event_set, profiling_info->papi_values);
  184. for(int i=0; i<papi_nevents; i++)
  185. {
  186. _STARPU_TRACE_PAPI_TASK_EVENT(papi_events[i], task, profiling_info->papi_values[i]);
  187. }
  188. PAPI_cleanup_eventset(profiling_info->papi_event_set);
  189. PAPI_destroy_eventset(&profiling_info->papi_event_set);
  190. }
  191. }
  192. #endif
  193. void _starpu_profiling_start(void)
  194. {
  195. const char *env;
  196. if ((env = starpu_getenv("STARPU_PROFILING")) && atoi(env))
  197. {
  198. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  199. }
  200. }
  201. void _starpu_profiling_terminate(void)
  202. {
  203. int worker;
  204. for (worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  205. {
  206. STARPU_PTHREAD_MUTEX_DESTROY(&worker_info_mutex[worker]);
  207. }
  208. #ifdef STARPU_PAPI
  209. /* free the resources used by PAPI */
  210. PAPI_shutdown();
  211. #endif
  212. }
  213. /*
  214. * Task profiling
  215. */
  216. struct starpu_profiling_task_info *_starpu_allocate_profiling_info_if_needed(struct starpu_task *task)
  217. {
  218. struct starpu_profiling_task_info *info = NULL;
  219. /* If we are benchmarking, we need room for the energy */
  220. if (starpu_profiling_status_get() || (task->cl && task->cl->energy_model && (task->cl->energy_model->benchmarking || _starpu_get_calibrate_flag())))
  221. {
  222. _STARPU_CALLOC(info, 1, sizeof(struct starpu_profiling_task_info));
  223. }
  224. return info;
  225. }
  226. /*
  227. * Worker profiling
  228. */
  229. static void _starpu_worker_reset_profiling_info_with_lock(int workerid)
  230. {
  231. _starpu_clock_gettime(&worker_info[workerid].start_time);
  232. /* This is computed in a lazy fashion when the application queries
  233. * profiling info. */
  234. starpu_timespec_clear(&worker_info[workerid].total_time);
  235. starpu_timespec_clear(&worker_info[workerid].executing_time);
  236. starpu_timespec_clear(&worker_info[workerid].sleeping_time);
  237. worker_info[workerid].executed_tasks = 0;
  238. worker_info[workerid].used_cycles = 0;
  239. worker_info[workerid].stall_cycles = 0;
  240. worker_info[workerid].energy_consumed = 0;
  241. worker_info[workerid].flops = 0;
  242. /* We detect if the worker is already sleeping or doing some
  243. * computation */
  244. enum _starpu_worker_status status = _starpu_worker_get_status(workerid);
  245. if (status == STATUS_SLEEPING || status == STATUS_SLEEPING_SCHEDULING)
  246. {
  247. worker_registered_sleeping_start[workerid] = 1;
  248. _starpu_clock_gettime(&sleeping_start_date[workerid]);
  249. }
  250. else
  251. {
  252. worker_registered_sleeping_start[workerid] = 0;
  253. }
  254. if (status == STATUS_EXECUTING)
  255. {
  256. worker_registered_executing_start[workerid] = 1;
  257. _starpu_clock_gettime(&executing_start_date[workerid]);
  258. }
  259. else
  260. {
  261. worker_registered_executing_start[workerid] = 0;
  262. }
  263. }
  264. void _starpu_worker_restart_sleeping(int workerid)
  265. {
  266. if (starpu_profiling_status_get())
  267. {
  268. struct timespec sleep_start_time;
  269. _starpu_clock_gettime(&sleep_start_time);
  270. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  271. if (worker_registered_sleeping_start[workerid] == 0)
  272. {
  273. worker_registered_sleeping_start[workerid] = 1;
  274. memcpy(&sleeping_start_date[workerid], &sleep_start_time, sizeof(struct timespec));
  275. }
  276. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  277. }
  278. }
  279. void _starpu_worker_stop_sleeping(int workerid)
  280. {
  281. if (starpu_profiling_status_get())
  282. {
  283. struct timespec *sleeping_start, sleep_end_time;
  284. _starpu_clock_gettime(&sleep_end_time);
  285. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  286. if (worker_registered_sleeping_start[workerid] == 1)
  287. {
  288. sleeping_start = &sleeping_start_date[workerid];
  289. /* Perhaps that profiling was enabled while the worker was
  290. * already blocked, so we don't measure (end - start), but
  291. * (end - max(start,worker_start)) where worker_start is the
  292. * date of the previous profiling info reset on the worker */
  293. struct timespec *worker_start = &worker_info[workerid].start_time;
  294. if (starpu_timespec_cmp(sleeping_start, worker_start, <))
  295. {
  296. /* sleeping_start < worker_start */
  297. sleeping_start = worker_start;
  298. }
  299. struct timespec sleeping_time;
  300. starpu_timespec_sub(&sleep_end_time, sleeping_start, &sleeping_time);
  301. starpu_timespec_accumulate(&worker_info[workerid].sleeping_time, &sleeping_time);
  302. worker_registered_sleeping_start[workerid] = 0;
  303. }
  304. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  305. }
  306. }
  307. void _starpu_worker_register_executing_start_date(int workerid, struct timespec *executing_start)
  308. {
  309. if (starpu_profiling_status_get())
  310. {
  311. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  312. worker_registered_executing_start[workerid] = 1;
  313. memcpy(&executing_start_date[workerid], executing_start, sizeof(struct timespec));
  314. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  315. }
  316. }
  317. void _starpu_worker_register_executing_end(int workerid)
  318. {
  319. if (starpu_profiling_status_get())
  320. {
  321. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  322. worker_registered_executing_start[workerid] = 0;
  323. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  324. }
  325. }
  326. 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)
  327. {
  328. if (starpu_profiling_status_get())
  329. {
  330. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  331. if (executing_time)
  332. starpu_timespec_accumulate(&worker_info[workerid].executing_time, executing_time);
  333. worker_info[workerid].used_cycles += used_cycles;
  334. worker_info[workerid].stall_cycles += stall_cycles;
  335. worker_info[workerid].energy_consumed += energy_consumed;
  336. worker_info[workerid].executed_tasks += executed_tasks;
  337. worker_info[workerid].flops += flops;
  338. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  339. }
  340. else /* Not thread safe, shouldn't be too much a problem */
  341. worker_info[workerid].executed_tasks += executed_tasks;
  342. }
  343. int starpu_profiling_worker_get_info(int workerid, struct starpu_profiling_worker_info *info)
  344. {
  345. if (!starpu_profiling_status_get())
  346. {
  347. /* Not thread safe, shouldn't be too much a problem */
  348. info->executed_tasks = worker_info[workerid].executed_tasks;
  349. }
  350. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_get_worker_struct(workerid)->sched_mutex);
  351. STARPU_PTHREAD_MUTEX_LOCK(&worker_info_mutex[workerid]);
  352. if (info)
  353. {
  354. /* The total time is computed in a lazy fashion */
  355. struct timespec now;
  356. _starpu_clock_gettime(&now);
  357. /* In case some worker is currently sleeping, we take into
  358. * account the time spent since it registered. */
  359. if (worker_registered_sleeping_start[workerid])
  360. {
  361. struct timespec sleeping_time;
  362. starpu_timespec_sub(&now, &sleeping_start_date[workerid], &sleeping_time);
  363. starpu_timespec_accumulate(&worker_info[workerid].sleeping_time, &sleeping_time);
  364. }
  365. if (worker_registered_executing_start[workerid])
  366. {
  367. struct timespec executing_time;
  368. starpu_timespec_sub(&now, &executing_start_date[workerid], &executing_time);
  369. starpu_timespec_accumulate(&worker_info[workerid].executing_time, &executing_time);
  370. }
  371. /* total_time = now - start_time */
  372. starpu_timespec_sub(&now, &worker_info[workerid].start_time,
  373. &worker_info[workerid].total_time);
  374. memcpy(info, &worker_info[workerid], sizeof(struct starpu_profiling_worker_info));
  375. }
  376. _starpu_worker_reset_profiling_info_with_lock(workerid);
  377. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_info_mutex[workerid]);
  378. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_get_worker_struct(workerid)->sched_mutex);
  379. return 0;
  380. }
  381. /* When did the task reach the scheduler ? */
  382. void _starpu_profiling_set_task_push_start_time(struct starpu_task *task)
  383. {
  384. if (!starpu_profiling_status_get())
  385. return;
  386. struct starpu_profiling_task_info *profiling_info;
  387. profiling_info = task->profiling_info;
  388. if (profiling_info)
  389. _starpu_clock_gettime(&profiling_info->push_start_time);
  390. }
  391. void _starpu_profiling_set_task_push_end_time(struct starpu_task *task)
  392. {
  393. if (!starpu_profiling_status_get())
  394. return;
  395. struct starpu_profiling_task_info *profiling_info;
  396. profiling_info = task->profiling_info;
  397. if (profiling_info)
  398. _starpu_clock_gettime(&profiling_info->push_end_time);
  399. }
  400. /*
  401. * Bus profiling
  402. */
  403. void _starpu_initialize_busid_matrix(void)
  404. {
  405. int i, j;
  406. for (j = 0; j < STARPU_MAXNODES; j++)
  407. for (i = 0; i < STARPU_MAXNODES; i++)
  408. busid_matrix[i][j] = -1;
  409. busid_cnt = 0;
  410. }
  411. static void _starpu_bus_reset_profiling_info(struct starpu_profiling_bus_info *bus_info)
  412. {
  413. _starpu_clock_gettime(&bus_info->start_time);
  414. bus_info->transferred_bytes = 0;
  415. bus_info->transfer_count = 0;
  416. }
  417. int _starpu_register_bus(int src_node, int dst_node)
  418. {
  419. if (starpu_bus_get_id(src_node, dst_node) != -1)
  420. return -EBUSY;
  421. int busid = STARPU_ATOMIC_ADD(&busid_cnt, 1) - 1;
  422. busid_matrix[src_node][dst_node] = busid;
  423. busid_to_node_pair[busid].src = src_node;
  424. busid_to_node_pair[busid].dst = dst_node;
  425. busid_to_node_pair[busid].bus_info = &bus_profiling_info[src_node][dst_node];
  426. _starpu_bus_reset_profiling_info(&bus_profiling_info[src_node][dst_node]);
  427. return busid;
  428. }
  429. int starpu_bus_get_count(void)
  430. {
  431. return busid_cnt;
  432. }
  433. int starpu_bus_get_id(int src, int dst)
  434. {
  435. return busid_matrix[src][dst];
  436. }
  437. int starpu_bus_get_src(int busid)
  438. {
  439. return busid_to_node_pair[busid].src;
  440. }
  441. int starpu_bus_get_dst(int busid)
  442. {
  443. return busid_to_node_pair[busid].dst;
  444. }
  445. void starpu_bus_set_direct(int busid, int direct)
  446. {
  447. bus_direct[busid] = direct;
  448. }
  449. int starpu_bus_get_direct(int busid)
  450. {
  451. return bus_direct[busid];
  452. }
  453. void starpu_bus_set_ngpus(int busid, int ngpus)
  454. {
  455. bus_ngpus[busid] = ngpus;
  456. }
  457. int starpu_bus_get_ngpus(int busid)
  458. {
  459. struct _starpu_machine_topology *topology = &_starpu_get_machine_config()->topology;
  460. int ngpus = bus_ngpus[busid];
  461. if (!ngpus)
  462. /* Unknown number of GPUs, assume it's shared by all GPUs */
  463. ngpus = topology->ncudagpus+topology->nopenclgpus;
  464. return ngpus;
  465. }
  466. int starpu_bus_get_profiling_info(int busid, struct starpu_profiling_bus_info *bus_info)
  467. {
  468. int src_node = starpu_bus_get_src(busid);
  469. int dst_node = starpu_bus_get_dst(busid);
  470. /* XXX protect all this method with a mutex */
  471. if (bus_info)
  472. {
  473. struct timespec now;
  474. _starpu_clock_gettime(&now);
  475. /* total_time = now - start_time */
  476. starpu_timespec_sub(&now, &bus_profiling_info[src_node][dst_node].start_time,
  477. &bus_profiling_info[src_node][dst_node].total_time);
  478. memcpy(bus_info, &bus_profiling_info[src_node][dst_node], sizeof(struct starpu_profiling_bus_info));
  479. }
  480. _starpu_bus_reset_profiling_info(&bus_profiling_info[src_node][dst_node]);
  481. return 0;
  482. }
  483. void _starpu_bus_update_profiling_info(int src_node, int dst_node, size_t size)
  484. {
  485. bus_profiling_info[src_node][dst_node].transferred_bytes += size;
  486. bus_profiling_info[src_node][dst_node].transfer_count++;
  487. // fprintf(stderr, "PROFILE %d -> %d : %d (cnt %d)\n", src_node, dst_node, size, bus_profiling_info[src_node][dst_node].transfer_count);
  488. }
  489. #undef starpu_profiling_status_get
  490. int starpu_profiling_status_get(void)
  491. {
  492. int ret;
  493. ANNOTATE_HAPPENS_AFTER(&_starpu_profiling);
  494. ret = _starpu_profiling;
  495. ANNOTATE_HAPPENS_BEFORE(&_starpu_profiling);
  496. return ret;
  497. }