oacc-profiling.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /* OpenACC Profiling Interface
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. Contributed by Mentor, a Siemens Business.
  4. This file is part of the GNU Offloading and Multi Processing Library
  5. (libgomp).
  6. Libgomp is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #define _GNU_SOURCE
  22. #include "libgomp.h"
  23. #include "oacc-int.h"
  24. #include "secure_getenv.h"
  25. #include "acc_prof.h"
  26. #include <assert.h>
  27. #ifdef HAVE_STRING_H
  28. # include <string.h>
  29. #endif
  30. #ifdef PLUGIN_SUPPORT
  31. # include <dlfcn.h>
  32. #endif
  33. #define STATIC_ASSERT(expr) _Static_assert (expr, "!(" #expr ")")
  34. /* Statically assert that the layout of the common fields in the
  35. 'acc_event_info' variants matches. */
  36. /* 'event_type' */
  37. STATIC_ASSERT (offsetof (acc_event_info, event_type)
  38. == offsetof (acc_event_info, data_event.event_type));
  39. STATIC_ASSERT (offsetof (acc_event_info, data_event.event_type)
  40. == offsetof (acc_event_info, launch_event.event_type));
  41. STATIC_ASSERT (offsetof (acc_event_info, data_event.event_type)
  42. == offsetof (acc_event_info, other_event.event_type));
  43. /* 'valid_bytes' */
  44. STATIC_ASSERT (offsetof (acc_event_info, data_event.valid_bytes)
  45. == offsetof (acc_event_info, launch_event.valid_bytes));
  46. STATIC_ASSERT (offsetof (acc_event_info, data_event.valid_bytes)
  47. == offsetof (acc_event_info, other_event.valid_bytes));
  48. /* 'parent_construct' */
  49. STATIC_ASSERT (offsetof (acc_event_info, data_event.parent_construct)
  50. == offsetof (acc_event_info, launch_event.parent_construct));
  51. STATIC_ASSERT (offsetof (acc_event_info, data_event.parent_construct)
  52. == offsetof (acc_event_info, other_event.parent_construct));
  53. /* 'implicit' */
  54. STATIC_ASSERT (offsetof (acc_event_info, data_event.implicit)
  55. == offsetof (acc_event_info, launch_event.implicit));
  56. STATIC_ASSERT (offsetof (acc_event_info, data_event.implicit)
  57. == offsetof (acc_event_info, other_event.implicit));
  58. /* 'tool_info' */
  59. STATIC_ASSERT (offsetof (acc_event_info, data_event.tool_info)
  60. == offsetof (acc_event_info, launch_event.tool_info));
  61. STATIC_ASSERT (offsetof (acc_event_info, data_event.tool_info)
  62. == offsetof (acc_event_info, other_event.tool_info));
  63. struct goacc_prof_callback_entry
  64. {
  65. acc_prof_callback cb;
  66. int ref;
  67. bool enabled;
  68. struct goacc_prof_callback_entry *next;
  69. };
  70. /* Use a separate flag to minimize run-time performance impact for the (very
  71. common) case that profiling is not enabled.
  72. Once enabled, we're not going to disable this anymore, anywhere. We
  73. probably could, by adding appropriate logic to 'acc_prof_register',
  74. 'acc_prof_unregister'. */
  75. bool goacc_prof_enabled = false;
  76. /* Global state for registered callbacks.
  77. 'goacc_prof_callbacks_enabled[acc_ev_none]' acts as a global toggle. */
  78. static bool goacc_prof_callbacks_enabled[acc_ev_last];
  79. static struct goacc_prof_callback_entry *goacc_prof_callback_entries[acc_ev_last];
  80. /* Lock used to protect access to 'goacc_prof_callbacks_enabled', and
  81. 'goacc_prof_callback_entries'. */
  82. static gomp_mutex_t goacc_prof_lock;
  83. void
  84. goacc_profiling_initialize (void)
  85. {
  86. gomp_mutex_init (&goacc_prof_lock);
  87. /* Initially, all callbacks for all events are enabled. */
  88. for (int i = 0; i < acc_ev_last; ++i)
  89. goacc_prof_callbacks_enabled[i] = true;
  90. #ifdef PLUGIN_SUPPORT
  91. char *acc_proflibs = secure_getenv ("ACC_PROFLIB");
  92. while (acc_proflibs != NULL && acc_proflibs[0] != '\0')
  93. {
  94. char *acc_proflibs_sep = strchr (acc_proflibs, ';');
  95. char *acc_proflib;
  96. if (acc_proflibs_sep == acc_proflibs)
  97. {
  98. /* Stray ';' separator: make sure we don't 'dlopen' the main
  99. program. */
  100. acc_proflib = NULL;
  101. }
  102. else
  103. {
  104. if (acc_proflibs_sep != NULL)
  105. {
  106. /* Single out the first library. */
  107. acc_proflib = gomp_malloc (acc_proflibs_sep - acc_proflibs + 1);
  108. memcpy (acc_proflib, acc_proflibs,
  109. acc_proflibs_sep - acc_proflibs);
  110. acc_proflib[acc_proflibs_sep - acc_proflibs] = '\0';
  111. }
  112. else
  113. {
  114. /* No ';' separator, so only one library. */
  115. acc_proflib = acc_proflibs;
  116. }
  117. gomp_debug (0, "%s: dlopen (\"%s\")\n", __FUNCTION__, acc_proflib);
  118. void *dl_handle = dlopen (acc_proflib, RTLD_LAZY);
  119. if (dl_handle != NULL)
  120. {
  121. typeof (&acc_register_library) a_r_l
  122. = dlsym (dl_handle, "acc_register_library");
  123. if (a_r_l == NULL)
  124. goto dl_fail;
  125. gomp_debug (0, " %s: calling %s:acc_register_library\n",
  126. __FUNCTION__, acc_proflib);
  127. a_r_l (acc_prof_register, acc_prof_unregister,
  128. acc_prof_lookup);
  129. }
  130. else
  131. {
  132. dl_fail:
  133. gomp_error ("while loading ACC_PROFLIB \"%s\": %s",
  134. acc_proflib, dlerror ());
  135. if (dl_handle != NULL)
  136. {
  137. int err = dlclose (dl_handle);
  138. dl_handle = NULL;
  139. if (err != 0)
  140. goto dl_fail;
  141. }
  142. }
  143. }
  144. if (acc_proflib != acc_proflibs)
  145. {
  146. free (acc_proflib);
  147. acc_proflibs = acc_proflibs_sep + 1;
  148. }
  149. else
  150. acc_proflibs = NULL;
  151. }
  152. #endif /* PLUGIN_SUPPORT */
  153. }
  154. void
  155. acc_prof_register (acc_event_t ev, acc_prof_callback cb, acc_register_t reg)
  156. {
  157. gomp_debug (0, "%s: ev=%d, cb=%p, reg=%d\n",
  158. __FUNCTION__, (int) ev, (void *) cb, (int) reg);
  159. /* For any events to be dispatched, the user first has to register a
  160. callback, which makes this here a good place for enabling the whole
  161. machinery. */
  162. if (!GOACC_PROF_ENABLED)
  163. __atomic_store_n (&goacc_prof_enabled, true, MEMMODEL_RELEASE);
  164. enum
  165. {
  166. EVENT_KIND_BOGUS,
  167. EVENT_KIND_NORMAL,
  168. /* As end events invoke callbacks in the reverse order, we register these
  169. in the reverse order here. */
  170. EVENT_KIND_END,
  171. } event_kind = EVENT_KIND_BOGUS;
  172. switch (ev)
  173. {
  174. case acc_ev_none:
  175. case acc_ev_device_init_start:
  176. case acc_ev_device_shutdown_start:
  177. case acc_ev_runtime_shutdown:
  178. case acc_ev_create:
  179. case acc_ev_delete:
  180. case acc_ev_alloc:
  181. case acc_ev_free:
  182. case acc_ev_enter_data_start:
  183. case acc_ev_exit_data_start:
  184. case acc_ev_update_start:
  185. case acc_ev_compute_construct_start:
  186. case acc_ev_enqueue_launch_start:
  187. case acc_ev_enqueue_upload_start:
  188. case acc_ev_enqueue_download_start:
  189. case acc_ev_wait_start:
  190. event_kind = EVENT_KIND_NORMAL;
  191. break;
  192. case acc_ev_device_init_end:
  193. case acc_ev_device_shutdown_end:
  194. case acc_ev_enter_data_end:
  195. case acc_ev_exit_data_end:
  196. case acc_ev_update_end:
  197. case acc_ev_compute_construct_end:
  198. case acc_ev_enqueue_launch_end:
  199. case acc_ev_enqueue_upload_end:
  200. case acc_ev_enqueue_download_end:
  201. case acc_ev_wait_end:
  202. event_kind = EVENT_KIND_END;
  203. break;
  204. case acc_ev_last:
  205. break;
  206. }
  207. if (event_kind == EVENT_KIND_BOGUS)
  208. {
  209. /* Silently ignore. */
  210. gomp_debug (0, " ignoring request for bogus 'acc_event_t'\n");
  211. return;
  212. }
  213. bool bogus = true;
  214. switch (reg)
  215. {
  216. case acc_reg:
  217. case acc_toggle:
  218. case acc_toggle_per_thread:
  219. bogus = false;
  220. break;
  221. }
  222. if (bogus)
  223. {
  224. /* Silently ignore. */
  225. gomp_debug (0, " ignoring request with bogus 'acc_register_t'\n");
  226. return;
  227. }
  228. /* Special cases. */
  229. if (reg == acc_toggle)
  230. {
  231. if (cb == NULL)
  232. {
  233. gomp_debug (0, " globally enabling callbacks\n");
  234. gomp_mutex_lock (&goacc_prof_lock);
  235. /* For 'acc_ev_none', this acts as a global toggle. */
  236. goacc_prof_callbacks_enabled[ev] = true;
  237. gomp_mutex_unlock (&goacc_prof_lock);
  238. return;
  239. }
  240. else if (ev == acc_ev_none && cb != NULL)
  241. {
  242. gomp_debug (0, " ignoring request\n");
  243. return;
  244. }
  245. }
  246. else if (reg == acc_toggle_per_thread)
  247. {
  248. if (ev == acc_ev_none && cb == NULL)
  249. {
  250. gomp_debug (0, " thread: enabling callbacks\n");
  251. goacc_lazy_initialize ();
  252. struct goacc_thread *thr = goacc_thread ();
  253. thr->prof_callbacks_enabled = true;
  254. return;
  255. }
  256. /* Silently ignore. */
  257. gomp_debug (0, " ignoring bogus request\n");
  258. return;
  259. }
  260. gomp_mutex_lock (&goacc_prof_lock);
  261. struct goacc_prof_callback_entry *it, *it_p;
  262. it = goacc_prof_callback_entries[ev];
  263. it_p = NULL;
  264. while (it)
  265. {
  266. if (it->cb == cb)
  267. break;
  268. it_p = it;
  269. it = it->next;
  270. }
  271. switch (reg)
  272. {
  273. case acc_reg:
  274. /* If we already have this callback registered, just increment its
  275. reference count. */
  276. if (it != NULL)
  277. {
  278. it->ref++;
  279. gomp_debug (0, " already registered;"
  280. " incrementing reference count to: %d\n", it->ref);
  281. }
  282. else
  283. {
  284. struct goacc_prof_callback_entry *e
  285. = gomp_malloc (sizeof (struct goacc_prof_callback_entry));
  286. e->cb = cb;
  287. e->ref = 1;
  288. e->enabled = true;
  289. bool prepend = (event_kind == EVENT_KIND_END);
  290. /* If we don't have any callback registered yet, also use the
  291. 'prepend' code path. */
  292. if (it_p == NULL)
  293. prepend = true;
  294. if (prepend)
  295. {
  296. gomp_debug (0, " prepending\n");
  297. e->next = goacc_prof_callback_entries[ev];
  298. goacc_prof_callback_entries[ev] = e;
  299. }
  300. else
  301. {
  302. gomp_debug (0, " appending\n");
  303. e->next = NULL;
  304. it_p->next = e;
  305. }
  306. }
  307. break;
  308. case acc_toggle:
  309. if (it == NULL)
  310. {
  311. gomp_debug (0, " ignoring request: is not registered\n");
  312. break;
  313. }
  314. else
  315. {
  316. gomp_debug (0, " enabling\n");
  317. it->enabled = true;
  318. }
  319. break;
  320. case acc_toggle_per_thread:
  321. __builtin_unreachable ();
  322. }
  323. gomp_mutex_unlock (&goacc_prof_lock);
  324. }
  325. void
  326. acc_prof_unregister (acc_event_t ev, acc_prof_callback cb, acc_register_t reg)
  327. {
  328. gomp_debug (0, "%s: ev=%d, cb=%p, reg=%d\n",
  329. __FUNCTION__, (int) ev, (void *) cb, (int) reg);
  330. /* If profiling is not enabled, there cannot be anything to unregister. */
  331. if (!GOACC_PROF_ENABLED)
  332. return;
  333. if (ev < acc_ev_none
  334. || ev >= acc_ev_last)
  335. {
  336. /* Silently ignore. */
  337. gomp_debug (0, " ignoring request for bogus 'acc_event_t'\n");
  338. return;
  339. }
  340. bool bogus = true;
  341. switch (reg)
  342. {
  343. case acc_reg:
  344. case acc_toggle:
  345. case acc_toggle_per_thread:
  346. bogus = false;
  347. break;
  348. }
  349. if (bogus)
  350. {
  351. /* Silently ignore. */
  352. gomp_debug (0, " ignoring request with bogus 'acc_register_t'\n");
  353. return;
  354. }
  355. /* Special cases. */
  356. if (reg == acc_toggle)
  357. {
  358. if (cb == NULL)
  359. {
  360. gomp_debug (0, " globally disabling callbacks\n");
  361. gomp_mutex_lock (&goacc_prof_lock);
  362. /* For 'acc_ev_none', this acts as a global toggle. */
  363. goacc_prof_callbacks_enabled[ev] = false;
  364. gomp_mutex_unlock (&goacc_prof_lock);
  365. return;
  366. }
  367. else if (ev == acc_ev_none && cb != NULL)
  368. {
  369. gomp_debug (0, " ignoring request\n");
  370. return;
  371. }
  372. }
  373. else if (reg == acc_toggle_per_thread)
  374. {
  375. if (ev == acc_ev_none && cb == NULL)
  376. {
  377. gomp_debug (0, " thread: disabling callbacks\n");
  378. goacc_lazy_initialize ();
  379. struct goacc_thread *thr = goacc_thread ();
  380. thr->prof_callbacks_enabled = false;
  381. return;
  382. }
  383. /* Silently ignore. */
  384. gomp_debug (0, " ignoring bogus request\n");
  385. return;
  386. }
  387. gomp_mutex_lock (&goacc_prof_lock);
  388. struct goacc_prof_callback_entry *it, *it_p;
  389. it = goacc_prof_callback_entries[ev];
  390. it_p = NULL;
  391. while (it)
  392. {
  393. if (it->cb == cb)
  394. break;
  395. it_p = it;
  396. it = it->next;
  397. }
  398. switch (reg)
  399. {
  400. case acc_reg:
  401. if (it == NULL)
  402. {
  403. /* Silently ignore. */
  404. gomp_debug (0, " ignoring bogus request: is not registered\n");
  405. break;
  406. }
  407. it->ref--;
  408. gomp_debug (0, " decrementing reference count to: %d\n", it->ref);
  409. if (it->ref == 0)
  410. {
  411. if (it_p == NULL)
  412. goacc_prof_callback_entries[ev] = it->next;
  413. else
  414. it_p->next = it->next;
  415. free (it);
  416. }
  417. break;
  418. case acc_toggle:
  419. if (it == NULL)
  420. {
  421. gomp_debug (0, " ignoring request: is not registered\n");
  422. break;
  423. }
  424. else
  425. {
  426. gomp_debug (0, " disabling\n");
  427. it->enabled = false;
  428. }
  429. break;
  430. case acc_toggle_per_thread:
  431. __builtin_unreachable ();
  432. }
  433. gomp_mutex_unlock (&goacc_prof_lock);
  434. }
  435. acc_query_fn
  436. acc_prof_lookup (const char *name)
  437. {
  438. gomp_debug (0, "%s (%s)\n",
  439. __FUNCTION__, name ?: "NULL");
  440. return NULL;
  441. }
  442. void
  443. acc_register_library (acc_prof_reg reg, acc_prof_reg unreg,
  444. acc_prof_lookup_func lookup)
  445. {
  446. gomp_fatal ("TODO");
  447. }
  448. /* Prepare to dispatch events? */
  449. bool
  450. _goacc_profiling_dispatch_p (bool check_not_nested_p)
  451. {
  452. gomp_debug (0, "%s\n", __FUNCTION__);
  453. bool ret;
  454. struct goacc_thread *thr = goacc_thread ();
  455. if (__builtin_expect (thr == NULL, false))
  456. {
  457. /* If we don't have any per-thread state yet, that means that per-thread
  458. callback dispatch has not been explicitly disabled (which only a call
  459. to 'acc_prof_unregister' with 'acc_toggle_per_thread' would do, and
  460. that would have allocated per-thread state via
  461. 'goacc_lazy_initialize'); initially, all callbacks for all events are
  462. enabled. */
  463. gomp_debug (0, " %s: don't have any per-thread state yet\n", __FUNCTION__);
  464. }
  465. else
  466. {
  467. if (check_not_nested_p)
  468. {
  469. /* No nesting. */
  470. assert (thr->prof_info == NULL);
  471. assert (thr->api_info == NULL);
  472. }
  473. if (__builtin_expect (!thr->prof_callbacks_enabled, true))
  474. {
  475. gomp_debug (0, " %s: disabled for this thread\n", __FUNCTION__);
  476. ret = false;
  477. goto out;
  478. }
  479. }
  480. gomp_mutex_lock (&goacc_prof_lock);
  481. /* 'goacc_prof_callbacks_enabled[acc_ev_none]' acts as a global toggle. */
  482. if (__builtin_expect (!goacc_prof_callbacks_enabled[acc_ev_none], true))
  483. {
  484. gomp_debug (0, " %s: disabled globally\n", __FUNCTION__);
  485. ret = false;
  486. goto out_unlock;
  487. }
  488. else
  489. ret = true;
  490. out_unlock:
  491. gomp_mutex_unlock (&goacc_prof_lock);
  492. out:
  493. return ret;
  494. }
  495. /* Set up to dispatch events? */
  496. bool
  497. _goacc_profiling_setup_p (struct goacc_thread *thr,
  498. acc_prof_info *prof_info, acc_api_info *api_info)
  499. {
  500. gomp_debug (0, "%s (%p)\n", __FUNCTION__, thr);
  501. /* If we don't have any per-thread state yet, we can't register 'prof_info'
  502. and 'api_info'. */
  503. if (__builtin_expect (thr == NULL, false))
  504. {
  505. gomp_debug (0, "Can't dispatch OpenACC Profiling Interface events for"
  506. " the current call, construct, or directive\n");
  507. return false;
  508. }
  509. if (thr->prof_info != NULL)
  510. {
  511. /* Profiling has already been set up for an outer construct. In this
  512. case, we continue to use the existing information, and thus return
  513. 'false' here.
  514. This can happen, for example, for an 'enter data' directive, which
  515. sets up profiling, then calls into 'acc_copyin', which should not
  516. again set up profiling, should not overwrite the existing
  517. information. */
  518. return false;
  519. }
  520. thr->prof_info = prof_info;
  521. thr->api_info = api_info;
  522. /* Fill in some defaults. */
  523. prof_info->event_type = -1; /* Must be set later. */
  524. prof_info->valid_bytes = _ACC_PROF_INFO_VALID_BYTES;
  525. prof_info->version = _ACC_PROF_INFO_VERSION;
  526. if (thr->dev)
  527. {
  528. prof_info->device_type = acc_device_type (thr->dev->type);
  529. prof_info->device_number = thr->dev->target_id;
  530. }
  531. else
  532. {
  533. prof_info->device_type = -1;
  534. prof_info->device_number = -1;
  535. }
  536. prof_info->thread_id = -1;
  537. prof_info->async = acc_async_sync;
  538. prof_info->async_queue = prof_info->async;
  539. prof_info->src_file = NULL;
  540. prof_info->func_name = NULL;
  541. prof_info->line_no = -1;
  542. prof_info->end_line_no = -1;
  543. prof_info->func_line_no = -1;
  544. prof_info->func_end_line_no = -1;
  545. api_info->device_api = acc_device_api_none;
  546. api_info->valid_bytes = _ACC_API_INFO_VALID_BYTES;
  547. api_info->device_type = prof_info->device_type;
  548. api_info->vendor = -1;
  549. api_info->device_handle = NULL;
  550. api_info->context_handle = NULL;
  551. api_info->async_handle = NULL;
  552. return true;
  553. }
  554. /* Dispatch events.
  555. This must only be called if 'GOACC_PROFILING_DISPATCH_P' or
  556. 'GOACC_PROFILING_SETUP_P' returned a true result. */
  557. void
  558. goacc_profiling_dispatch (acc_prof_info *prof_info, acc_event_info *event_info,
  559. acc_api_info *apt_info)
  560. {
  561. acc_event_t event_type = event_info->event_type;
  562. gomp_debug (0, "%s: event_type=%d\n", __FUNCTION__, (int) event_type);
  563. assert (event_type > acc_ev_none
  564. && event_type < acc_ev_last);
  565. gomp_mutex_lock (&goacc_prof_lock);
  566. if (!goacc_prof_callbacks_enabled[event_type])
  567. {
  568. gomp_debug (0, " disabled for this event type\n");
  569. goto out_unlock;
  570. }
  571. for (struct goacc_prof_callback_entry *e
  572. = goacc_prof_callback_entries[event_type];
  573. e != NULL;
  574. e = e->next)
  575. {
  576. if (!e->enabled)
  577. {
  578. gomp_debug (0, " disabled for callback %p\n", e->cb);
  579. continue;
  580. }
  581. gomp_debug (0, " calling callback %p\n", e->cb);
  582. e->cb (prof_info, event_info, apt_info);
  583. }
  584. out_unlock:
  585. gomp_mutex_unlock (&goacc_prof_lock);
  586. }