fxt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 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 <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. #ifdef STARPU_HAVE_WINDOWS
  25. #include <windows.h>
  26. #endif
  27. #define _STARPU_PROF_BUFFER_SIZE (8*1024*1024)
  28. static char _STARPU_PROF_FILE_USER[128];
  29. static int _starpu_fxt_started = 0;
  30. static int _starpu_written = 0;
  31. static int _starpu_id;
  32. static void _starpu_profile_set_tracefile(void *last, ...)
  33. {
  34. va_list vl;
  35. char *user;
  36. char *fxt_prefix = getenv("STARPU_FXT_PREFIX");
  37. if (!fxt_prefix)
  38. fxt_prefix = "/tmp/";
  39. va_start(vl, last);
  40. vsprintf(_STARPU_PROF_FILE_USER, fxt_prefix, vl);
  41. va_end(vl);
  42. user = getenv("USER");
  43. if (!user)
  44. user = "";
  45. char suffix[128];
  46. snprintf(suffix, 128, "prof_file_%s_%d", user, _starpu_id);
  47. strcat(_STARPU_PROF_FILE_USER, suffix);
  48. }
  49. void starpu_set_profiling_id(int new_id)
  50. {
  51. _STARPU_DEBUG("Set id to <%d>\n", new_id);
  52. _starpu_id = new_id;
  53. _starpu_profile_set_tracefile(NULL);
  54. }
  55. void _starpu_start_fxt_profiling(void)
  56. {
  57. unsigned threadid;
  58. if (!_starpu_fxt_started)
  59. {
  60. _starpu_fxt_started = 1;
  61. _starpu_profile_set_tracefile(NULL);
  62. }
  63. threadid = syscall(SYS_gettid);
  64. atexit(_starpu_stop_fxt_profiling);
  65. if (fut_setup(_STARPU_PROF_BUFFER_SIZE, FUT_KEYMASKALL, threadid) < 0)
  66. {
  67. perror("fut_setup");
  68. STARPU_ABORT();
  69. }
  70. fut_keychange(FUT_ENABLE, FUT_KEYMASKALL, threadid);
  71. return;
  72. }
  73. static void _starpu_generate_paje_trace(char *input_fxt_filename, char *output_paje_filename)
  74. {
  75. /* We take default options */
  76. struct starpu_fxt_options options;
  77. starpu_fxt_options_init(&options);
  78. /* TODO parse some STARPU_GENERATE_TRACE_OPTIONS env variable */
  79. options.ninputfiles = 1;
  80. options.filenames[0] = input_fxt_filename;
  81. options.out_paje_path = output_paje_filename;
  82. options.file_prefix = "";
  83. options.file_rank = -1;
  84. starpu_fxt_generate_trace(&options);
  85. }
  86. void _starpu_stop_fxt_profiling(void)
  87. {
  88. if (!_starpu_written)
  89. {
  90. #ifdef STARPU_VERBOSE
  91. char hostname[128];
  92. gethostname(hostname, 128);
  93. fprintf(stderr, "Writing FxT traces into file %s:%s\n", hostname, _STARPU_PROF_FILE_USER);
  94. #endif
  95. fut_endup(_STARPU_PROF_FILE_USER);
  96. /* Should we generate a Paje trace directly ? */
  97. int generate_trace = starpu_get_env_number("STARPU_GENERATE_TRACE");
  98. if (generate_trace == 1)
  99. _starpu_generate_paje_trace(_STARPU_PROF_FILE_USER, "paje.trace");
  100. int ret = fut_done();
  101. if (ret < 0)
  102. {
  103. /* Something went wrong with the FxT trace (eg. there
  104. * was too many events) */
  105. fprintf(stderr, "Warning: the FxT trace could not be generated properly\n");
  106. }
  107. _starpu_written = 1;
  108. }
  109. }
  110. void _starpu_fxt_register_thread(unsigned cpuid)
  111. {
  112. FUT_DO_PROBE2(FUT_NEW_LWP_CODE, cpuid, syscall(SYS_gettid));
  113. }
  114. #endif // STARPU_USE_FXT
  115. void starpu_trace_user_event(unsigned long code STARPU_ATTRIBUTE_UNUSED)
  116. {
  117. #ifdef STARPU_USE_FXT
  118. _STARPU_TRACE_USER_EVENT(code);
  119. #endif
  120. }