fxt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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 <common/config.h>
  19. #include <common/utils.h>
  20. #include <starpu_util.h>
  21. #include <starpu_profiling.h>
  22. #ifdef STARPU_USE_FXT
  23. #include <common/fxt.h>
  24. #include <starpu_fxt.h>
  25. #ifdef STARPU_HAVE_WINDOWS
  26. #include <windows.h>
  27. #endif
  28. #ifdef __linux__
  29. #include <sys/syscall.h> /* for SYS_gettid */
  30. #elif defined(__FreeBSD__)
  31. #include <sys/thr.h> /* for thr_self() */
  32. #endif
  33. static char _STARPU_PROF_FILE_USER[128];
  34. static int _starpu_fxt_started = 0;
  35. static int _starpu_written = 0;
  36. static int _starpu_id;
  37. #ifdef STARPU_SIMGRID
  38. /* Give virtual time to FxT */
  39. uint64_t fut_getstamp(void)
  40. {
  41. return starpu_timing_now()*1000.;
  42. }
  43. #endif
  44. long _starpu_gettid(void)
  45. {
  46. #ifdef STARPU_SIMGRID
  47. return (uintptr_t) MSG_process_self();
  48. #else
  49. #if defined(__linux__)
  50. return syscall(SYS_gettid);
  51. #elif defined(__FreeBSD__)
  52. long tid;
  53. thr_self(&tid);
  54. return tid;
  55. #elif defined(__MINGW32__)
  56. return (long) GetCurrentThreadId();
  57. #else
  58. return (long) pthread_self();
  59. #endif
  60. #endif
  61. }
  62. static void _starpu_profile_set_tracefile(void *last, ...)
  63. {
  64. va_list vl;
  65. char *user;
  66. char *fxt_prefix = getenv("STARPU_FXT_PREFIX");
  67. if (!fxt_prefix)
  68. fxt_prefix = "/tmp/";
  69. va_start(vl, last);
  70. vsprintf(_STARPU_PROF_FILE_USER, fxt_prefix, vl);
  71. va_end(vl);
  72. user = getenv("USER");
  73. if (!user)
  74. user = "";
  75. char suffix[128];
  76. snprintf(suffix, 128, "prof_file_%s_%d", user, _starpu_id);
  77. strcat(_STARPU_PROF_FILE_USER, suffix);
  78. }
  79. void starpu_profiling_set_id(int new_id)
  80. {
  81. _STARPU_DEBUG("Set id to <%d>\n", new_id);
  82. _starpu_id = new_id;
  83. _starpu_profile_set_tracefile(NULL);
  84. #ifdef HAVE_FUT_SET_FILENAME
  85. fut_set_filename(_STARPU_PROF_FILE_USER);
  86. #endif
  87. }
  88. void starpu_fxt_start_profiling()
  89. {
  90. unsigned threadid = _starpu_gettid();
  91. fut_keychange(FUT_ENABLE, FUT_KEYMASKALL, threadid);
  92. }
  93. void starpu_fxt_stop_profiling()
  94. {
  95. unsigned threadid = _starpu_gettid();
  96. fut_keychange(FUT_DISABLE, FUT_KEYMASKALL, threadid);
  97. }
  98. void _starpu_init_fxt_profiling(unsigned trace_buffer_size)
  99. {
  100. unsigned threadid;
  101. if (!_starpu_fxt_started)
  102. {
  103. _starpu_fxt_started = 1;
  104. _starpu_written = 0;
  105. _starpu_profile_set_tracefile(NULL);
  106. }
  107. #ifdef HAVE_FUT_SET_FILENAME
  108. fut_set_filename(_STARPU_PROF_FILE_USER);
  109. #endif
  110. #ifdef HAVE_ENABLE_FUT_FLUSH
  111. // when the event buffer is full, fxt stops recording events.
  112. // The trace may thus be incomplete.
  113. // Enable the fut_flush function which is called when the
  114. // fxt event buffer is full to flush the buffer to disk,
  115. // therefore allowing to record the remaining events.
  116. enable_fut_flush();
  117. #endif
  118. threadid = _starpu_gettid();
  119. atexit(_starpu_stop_fxt_profiling);
  120. unsigned int key_mask = FUT_KEYMASKALL;
  121. if (fut_setup(trace_buffer_size / sizeof(unsigned long), key_mask, threadid) < 0)
  122. {
  123. perror("fut_setup");
  124. STARPU_ABORT();
  125. }
  126. fut_keychange(FUT_ENABLE, key_mask, threadid);
  127. return;
  128. }
  129. static void _starpu_generate_paje_trace(char *input_fxt_filename, char *output_paje_filename)
  130. {
  131. /* We take default options */
  132. struct starpu_fxt_options options;
  133. starpu_fxt_options_init(&options);
  134. /* TODO parse some STARPU_GENERATE_TRACE_OPTIONS env variable */
  135. options.ninputfiles = 1;
  136. options.filenames[0] = input_fxt_filename;
  137. options.out_paje_path = output_paje_filename;
  138. options.file_prefix = "";
  139. options.file_rank = -1;
  140. starpu_fxt_generate_trace(&options);
  141. }
  142. void _starpu_stop_fxt_profiling(void)
  143. {
  144. if (!_starpu_written)
  145. {
  146. #ifdef STARPU_VERBOSE
  147. char hostname[128];
  148. gethostname(hostname, 128);
  149. fprintf(stderr, "Writing FxT traces into file %s:%s\n", hostname, _STARPU_PROF_FILE_USER);
  150. #endif
  151. fut_endup(_STARPU_PROF_FILE_USER);
  152. /* Should we generate a Paje trace directly ? */
  153. int generate_trace = starpu_get_env_number("STARPU_GENERATE_TRACE");
  154. if (generate_trace == 1)
  155. _starpu_generate_paje_trace(_STARPU_PROF_FILE_USER, "paje.trace");
  156. int ret = fut_done();
  157. if (ret < 0)
  158. {
  159. /* Something went wrong with the FxT trace (eg. there
  160. * was too many events) */
  161. fprintf(stderr, "Warning: the FxT trace could not be generated properly\n");
  162. }
  163. _starpu_written = 1;
  164. _starpu_fxt_started = 0;
  165. }
  166. }
  167. void _starpu_fxt_register_thread(unsigned cpuid)
  168. {
  169. FUT_DO_PROBE2(FUT_NEW_LWP_CODE, cpuid, _starpu_gettid());
  170. }
  171. #else // STARPU_USE_FXT
  172. void starpu_fxt_start_profiling()
  173. {
  174. }
  175. void starpu_fxt_stop_profiling()
  176. {
  177. }
  178. #endif // STARPU_USE_FXT
  179. void starpu_trace_user_event(unsigned long code STARPU_ATTRIBUTE_UNUSED)
  180. {
  181. #ifdef STARPU_USE_FXT
  182. _STARPU_TRACE_USER_EVENT(code);
  183. #endif
  184. }