fxt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013,2015 Inria
  4. * Copyright (C) 2008-2017 Université de Bordeaux
  5. * Copyright (C) 2010-2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <common/config.h>
  20. #include <common/utils.h>
  21. #include <starpu_util.h>
  22. #include <starpu_profiling.h>
  23. /* we need to identify each task to generate the DAG. */
  24. unsigned long _starpu_job_cnt = 0;
  25. #ifdef STARPU_USE_FXT
  26. #include <common/fxt.h>
  27. #include <starpu_fxt.h>
  28. #ifdef STARPU_HAVE_WINDOWS
  29. #include <windows.h>
  30. #endif
  31. #ifdef __linux__
  32. #include <sys/syscall.h> /* for SYS_gettid */
  33. #elif defined(__FreeBSD__)
  34. #include <sys/thr.h> /* for thr_self() */
  35. #endif
  36. static char _STARPU_PROF_FILE_USER[128];
  37. int _starpu_fxt_started = 0;
  38. int _starpu_fxt_willstart = 1;
  39. starpu_pthread_mutex_t _starpu_fxt_started_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  40. starpu_pthread_cond_t _starpu_fxt_started_cond = STARPU_PTHREAD_COND_INITIALIZER;
  41. /* and their submission order. */
  42. unsigned long _starpu_submit_order = 0;
  43. static int _starpu_written = 0;
  44. static int _starpu_id;
  45. static unsigned int initial_key_mask = FUT_KEYMASKALL;
  46. #ifdef STARPU_SIMGRID
  47. /* Give virtual time to FxT */
  48. uint64_t fut_getstamp(void)
  49. {
  50. return starpu_timing_now()*1000.;
  51. }
  52. #endif
  53. long _starpu_gettid(void)
  54. {
  55. /* TODO: test at configure whether __thread is available, and use that
  56. * to cache the value.
  57. * Don't use the TSD, this is getting called before we would have the
  58. * time to allocate it. */
  59. #ifdef STARPU_SIMGRID
  60. return (uintptr_t) MSG_process_self();
  61. #else
  62. #if defined(__linux__)
  63. return syscall(SYS_gettid);
  64. #elif defined(__FreeBSD__)
  65. long tid;
  66. thr_self(&tid);
  67. return tid;
  68. #elif defined(_WIN32) && !defined(__CYGWIN__)
  69. return (long) GetCurrentThreadId();
  70. #else
  71. return (long) starpu_pthread_self();
  72. #endif
  73. #endif
  74. }
  75. static void _starpu_profile_set_tracefile(void)
  76. {
  77. char *user;
  78. char *fxt_prefix = starpu_getenv("STARPU_FXT_PREFIX");
  79. if (!fxt_prefix)
  80. fxt_prefix = "/tmp/";
  81. user = starpu_getenv("USER");
  82. if (!user)
  83. user = "";
  84. char suffix[128];
  85. snprintf(suffix, sizeof(suffix), "prof_file_%s_%d", user, _starpu_id);
  86. snprintf(_STARPU_PROF_FILE_USER, sizeof(_STARPU_PROF_FILE_USER), "%s%s", fxt_prefix, suffix);
  87. }
  88. void starpu_profiling_set_id(int new_id)
  89. {
  90. _STARPU_DEBUG("Set id to <%d>\n", new_id);
  91. _starpu_id = new_id;
  92. _starpu_profile_set_tracefile();
  93. #ifdef HAVE_FUT_SET_FILENAME
  94. fut_set_filename(_STARPU_PROF_FILE_USER);
  95. #endif
  96. }
  97. void starpu_fxt_autostart_profiling(int autostart)
  98. {
  99. if (autostart)
  100. initial_key_mask = FUT_KEYMASKALL;
  101. else
  102. initial_key_mask = FUT_KEYMASK0;
  103. }
  104. void starpu_fxt_start_profiling()
  105. {
  106. unsigned threadid = _starpu_gettid();
  107. fut_keychange(FUT_ENABLE, FUT_KEYMASKALL, threadid);
  108. _STARPU_TRACE_EVENT("start_profiling");
  109. }
  110. void starpu_fxt_stop_profiling()
  111. {
  112. unsigned threadid = _starpu_gettid();
  113. _STARPU_TRACE_EVENT("stop_profiling");
  114. fut_keychange(FUT_DISABLE, FUT_KEYMASKALL, threadid);
  115. }
  116. void _starpu_fxt_init_profiling(unsigned trace_buffer_size)
  117. {
  118. unsigned threadid;
  119. STARPU_PTHREAD_MUTEX_LOCK(&_starpu_fxt_started_mutex);
  120. if (!(_starpu_fxt_willstart = starpu_get_env_number_default("STARPU_FXT_TRACE", 1)))
  121. {
  122. STARPU_PTHREAD_COND_BROADCAST(&_starpu_fxt_started_cond);
  123. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_fxt_started_mutex);
  124. return;
  125. }
  126. STARPU_ASSERT(!_starpu_fxt_started);
  127. _starpu_fxt_started = 1;
  128. _starpu_written = 0;
  129. _starpu_profile_set_tracefile();
  130. #ifdef HAVE_FUT_SET_FILENAME
  131. fut_set_filename(_STARPU_PROF_FILE_USER);
  132. #endif
  133. #ifdef HAVE_ENABLE_FUT_FLUSH
  134. // when the event buffer is full, fxt stops recording events.
  135. // The trace may thus be incomplete.
  136. // Enable the fut_flush function which is called when the
  137. // fxt event buffer is full to flush the buffer to disk,
  138. // therefore allowing to record the remaining events.
  139. enable_fut_flush();
  140. #endif
  141. threadid = _starpu_gettid();
  142. if (fut_setup(trace_buffer_size / sizeof(unsigned long), initial_key_mask, threadid) < 0)
  143. {
  144. perror("fut_setup");
  145. STARPU_ABORT();
  146. }
  147. STARPU_PTHREAD_COND_BROADCAST(&_starpu_fxt_started_cond);
  148. STARPU_PTHREAD_MUTEX_UNLOCK(&_starpu_fxt_started_mutex);
  149. return;
  150. }
  151. static void _starpu_generate_paje_trace(char *input_fxt_filename, char *output_paje_filename)
  152. {
  153. /* We take default options */
  154. struct starpu_fxt_options options;
  155. starpu_fxt_options_init(&options);
  156. /* TODO parse some STARPU_GENERATE_TRACE_OPTIONS env variable */
  157. options.ninputfiles = 1;
  158. options.filenames[0] = input_fxt_filename;
  159. options.out_paje_path = output_paje_filename;
  160. options.file_prefix = "";
  161. options.file_rank = -1;
  162. starpu_fxt_generate_trace(&options);
  163. }
  164. void _starpu_fxt_dump_file(void)
  165. {
  166. if (!_starpu_fxt_started)
  167. return;
  168. #ifdef STARPU_VERBOSE
  169. _STARPU_MSG("Writing FxT traces into file %s\n", _STARPU_PROF_FILE_USER);
  170. #endif
  171. fut_endup(_STARPU_PROF_FILE_USER);
  172. }
  173. void _starpu_stop_fxt_profiling(void)
  174. {
  175. if (!_starpu_fxt_started)
  176. return;
  177. if (!_starpu_written)
  178. {
  179. #ifdef STARPU_VERBOSE
  180. char hostname[128];
  181. gethostname(hostname, 128);
  182. _STARPU_MSG("Writing FxT traces into file %s:%s\n", hostname, _STARPU_PROF_FILE_USER);
  183. #endif
  184. fut_endup(_STARPU_PROF_FILE_USER);
  185. /* Should we generate a Paje trace directly ? */
  186. int generate_trace = starpu_get_env_number("STARPU_GENERATE_TRACE");
  187. if (generate_trace == 1)
  188. _starpu_generate_paje_trace(_STARPU_PROF_FILE_USER, "paje.trace");
  189. int ret = fut_done();
  190. if (ret < 0)
  191. {
  192. /* Something went wrong with the FxT trace (eg. there
  193. * was too many events) */
  194. _STARPU_MSG("Warning: the FxT trace could not be generated properly\n");
  195. }
  196. _starpu_written = 1;
  197. _starpu_fxt_started = 0;
  198. }
  199. }
  200. void _starpu_fxt_register_thread(unsigned cpuid)
  201. {
  202. FUT_DO_PROBE2(FUT_NEW_LWP_CODE, cpuid, _starpu_gettid());
  203. }
  204. #else // STARPU_USE_FXT
  205. void starpu_fxt_autostart_profiling(int autostart STARPU_ATTRIBUTE_UNUSED)
  206. {
  207. }
  208. void starpu_fxt_start_profiling()
  209. {
  210. }
  211. void starpu_fxt_stop_profiling()
  212. {
  213. }
  214. #endif // STARPU_USE_FXT
  215. void starpu_fxt_trace_user_event(unsigned long code STARPU_ATTRIBUTE_UNUSED)
  216. {
  217. #ifdef STARPU_USE_FXT
  218. _STARPU_TRACE_USER_EVENT(code);
  219. #endif
  220. }
  221. void starpu_fxt_trace_user_event_string(const char *s STARPU_ATTRIBUTE_UNUSED)
  222. {
  223. #ifdef STARPU_USE_FXT
  224. _STARPU_TRACE_EVENT(s);
  225. #endif
  226. }