offload_trace.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. Copyright (c) 2014 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "offload_trace.h"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <stdint.h>
  30. #include <sstream>
  31. #include "liboffload_error_codes.h"
  32. extern const char *prefix;
  33. #if !HOST_LIBRARY
  34. extern int mic_index;
  35. #endif
  36. // The debug routines
  37. static const char * offload_stage(std::stringstream &ss,
  38. int offload_number,
  39. const char *tag,
  40. const char *text,
  41. bool print_tag)
  42. {
  43. ss << "[" << report_get_message_str(c_report_offload) << "]";
  44. #if HOST_LIBRARY
  45. ss << " [" << prefix << "]";
  46. if (print_tag) {
  47. ss << " [" << report_get_message_str(c_report_tag);
  48. ss << " " << offload_number << "]";
  49. }
  50. else {
  51. ss << " ";
  52. }
  53. ss << " [" << tag << "]";
  54. ss << " " << text;
  55. #else
  56. ss << " [" << prefix << " " << mic_index << "]";
  57. if (print_tag) {
  58. ss << " [" << report_get_message_str(c_report_tag);
  59. ss << " " << offload_number << "]";
  60. }
  61. ss << " [" << tag << "]";
  62. ss << " " << text;
  63. #endif
  64. return 0;
  65. }
  66. static const char * offload_signal(std::stringstream &ss,
  67. int offload_number,
  68. const char *tag,
  69. const char *text)
  70. {
  71. ss << "[" << report_get_message_str(c_report_offload) << "]";
  72. ss << " [" << prefix << "]";
  73. ss << " [" << report_get_message_str(c_report_tag);
  74. ss << " " << offload_number << "]";
  75. ss << " [" << tag << "]";
  76. ss << " " << text;
  77. return 0;
  78. }
  79. void offload_stage_print(int stage, int offload_number, ...)
  80. {
  81. std::string buf;
  82. std::stringstream ss;
  83. char const *str1;
  84. char const *str2;
  85. va_list va_args;
  86. va_start(va_args, offload_number);
  87. va_arg(va_args, char*);
  88. switch (stage) {
  89. case c_offload_start:
  90. str1 = report_get_message_str(c_report_state);
  91. str2 = report_get_message_str(c_report_start);
  92. offload_stage(ss, offload_number, str1, str2, true);
  93. break;
  94. case c_offload_init:
  95. str1 = report_get_message_str(c_report_state);
  96. str2 = report_get_message_str(c_report_init);
  97. offload_stage(ss, offload_number, str1, str2, false);
  98. ss << " " << report_get_message_str(c_report_logical_card);
  99. ss << " " << va_arg(va_args, int);
  100. ss << " = " << report_get_message_str(c_report_physical_card);
  101. ss << " " << va_arg(va_args, int);
  102. break;
  103. case c_offload_register:
  104. str1 = report_get_message_str(c_report_state);
  105. str2 = report_get_message_str(c_report_register);
  106. offload_stage(ss, offload_number, str1, str2, true);
  107. break;
  108. case c_offload_init_func:
  109. str1 = report_get_message_str(c_report_state);
  110. str2 = report_get_message_str(c_report_init_func);
  111. offload_stage(ss, offload_number, str1, str2, true);
  112. ss << ": " << va_arg(va_args, char*);
  113. break;
  114. case c_offload_create_buf_host:
  115. str1 = report_get_message_str(c_report_state);
  116. str2 = report_get_message_str(c_report_create_buf_host);
  117. offload_stage(ss, offload_number, str1, str2, true);
  118. ss << ": base=0x" << std::hex << va_arg(va_args, uint64_t);
  119. ss << " length=" << std::dec << va_arg(va_args, uint64_t);
  120. break;
  121. case c_offload_create_buf_mic:
  122. str1 = report_get_message_str(c_report_state);
  123. str2 = report_get_message_str(c_report_create_buf_mic);
  124. offload_stage(ss, offload_number, str1, str2, true);
  125. ss << ": size=" << va_arg(va_args, uint64_t);
  126. ss << " offset=" << va_arg(va_args, int);
  127. if (va_arg(va_args,int))
  128. ss << " (2M page)";
  129. break;
  130. case c_offload_send_pointer_data:
  131. str1 = report_get_message_str(c_report_state);
  132. str2 = report_get_message_str(c_report_send_pointer_data);
  133. offload_stage(ss, offload_number, str1, str2, true);
  134. break;
  135. case c_offload_sent_pointer_data:
  136. str1 = report_get_message_str(c_report_state);
  137. str2 = report_get_message_str(c_report_sent_pointer_data);
  138. offload_stage(ss, offload_number, str1, str2, true);
  139. ss << " " << va_arg(va_args, uint64_t);
  140. break;
  141. case c_offload_gather_copyin_data:
  142. str1 = report_get_message_str(c_report_state);
  143. str2 = report_get_message_str(c_report_gather_copyin_data);
  144. offload_stage(ss, offload_number, str1, str2, true);
  145. break;
  146. case c_offload_copyin_data:
  147. str1 = report_get_message_str(c_report_state);
  148. str2 = report_get_message_str(c_report_copyin_data);
  149. offload_stage(ss, offload_number, str1, str2, true);
  150. ss << " " << va_arg(va_args, uint64_t) << " ";
  151. break;
  152. case c_offload_compute:
  153. str1 = report_get_message_str(c_report_state);
  154. str2 = report_get_message_str(c_report_compute);
  155. offload_stage(ss, offload_number, str1, str2, true);
  156. break;
  157. case c_offload_receive_pointer_data:
  158. str1 = report_get_message_str(c_report_state);
  159. str2 = report_get_message_str(c_report_receive_pointer_data);
  160. offload_stage(ss, offload_number, str1, str2, true);
  161. break;
  162. case c_offload_received_pointer_data:
  163. str1 = report_get_message_str(c_report_state);
  164. str2 = report_get_message_str(c_report_received_pointer_data);
  165. offload_stage(ss, offload_number, str1, str2, true);
  166. ss << " " << va_arg(va_args, uint64_t);
  167. break;
  168. case c_offload_start_target_func:
  169. str1 = report_get_message_str(c_report_state);
  170. str2 = report_get_message_str(c_report_start_target_func);
  171. offload_stage(ss, offload_number, str1, str2, true);
  172. ss << ": " << va_arg(va_args, char*);
  173. break;
  174. case c_offload_var:
  175. str1 = report_get_message_str(c_report_var);
  176. offload_stage(ss, offload_number, str1, " ", true);
  177. va_arg(va_args, int);
  178. ss << va_arg(va_args, char*);
  179. ss << " " << " " << va_arg(va_args, char*);
  180. break;
  181. case c_offload_scatter_copyin_data:
  182. str1 = report_get_message_str(c_report_state);
  183. str2 = report_get_message_str(c_report_scatter_copyin_data);
  184. offload_stage(ss, offload_number, str1, str2, true);
  185. break;
  186. case c_offload_gather_copyout_data:
  187. str1 = report_get_message_str(c_report_state);
  188. str2 = report_get_message_str(c_report_gather_copyout_data);
  189. offload_stage(ss, offload_number, str1, str2, true);
  190. break;
  191. case c_offload_scatter_copyout_data:
  192. str1 = report_get_message_str(c_report_state);
  193. str2 = report_get_message_str(c_report_scatter_copyout_data);
  194. offload_stage(ss, offload_number, str1, str2, true);
  195. break;
  196. case c_offload_copyout_data:
  197. str1 = report_get_message_str(c_report_state);
  198. str2 = report_get_message_str(c_report_copyout_data);
  199. offload_stage(ss, offload_number, str1, str2, true);
  200. ss << " " << va_arg(va_args, uint64_t);
  201. break;
  202. case c_offload_signal:
  203. {
  204. uint64_t *signal;
  205. str1 = report_get_message_str(c_report_state_signal);
  206. str2 = report_get_message_str(c_report_signal);
  207. offload_signal(ss, offload_number, str1, str2);
  208. signal = va_arg(va_args, uint64_t*);
  209. if (signal)
  210. ss << " 0x" << std::hex << *signal;
  211. else
  212. ss << " none";
  213. }
  214. break;
  215. case c_offload_wait:
  216. {
  217. int count;
  218. uint64_t **signal;
  219. str1 = report_get_message_str(c_report_state_signal);
  220. str2 = report_get_message_str(c_report_wait);
  221. offload_signal(ss, offload_number, str1, str2);
  222. count = va_arg(va_args, int);
  223. signal = va_arg(va_args, uint64_t**);
  224. if (count) {
  225. while (count) {
  226. ss << " " << std::hex << signal[count-1];
  227. count--;
  228. }
  229. }
  230. else
  231. ss << " none";
  232. }
  233. break;
  234. case c_offload_unregister:
  235. str1 = report_get_message_str(c_report_state);
  236. str2 = report_get_message_str(c_report_unregister);
  237. offload_stage(ss, offload_number, str1, str2, false);
  238. break;
  239. case c_offload_destroy:
  240. str1 = report_get_message_str(c_report_state);
  241. str2 = report_get_message_str(c_report_destroy);
  242. offload_stage(ss, offload_number, str1, str2, true);
  243. break;
  244. case c_offload_myoinit:
  245. str1 = report_get_message_str(c_report_state);
  246. str2 = report_get_message_str(c_report_myoinit);
  247. offload_stage(ss, offload_number, str1, str2, false);
  248. break;
  249. case c_offload_myoregister:
  250. str1 = report_get_message_str(c_report_state);
  251. str2 = report_get_message_str(c_report_myoregister);
  252. offload_stage(ss, offload_number, str1, str2, false);
  253. break;
  254. case c_offload_myofini:
  255. str1 = report_get_message_str(c_report_state);
  256. str2 = report_get_message_str(c_report_myofini);
  257. offload_stage(ss, offload_number, str1, str2, false);
  258. break;
  259. case c_offload_mic_myo_shared:
  260. str1 = report_get_message_str(c_report_state);
  261. str2 = report_get_message_str(c_report_mic_myo_shared);
  262. offload_stage(ss, offload_number, str1, str2, false);
  263. ss << " " << va_arg(va_args, char*);
  264. break;
  265. case c_offload_mic_myo_fptr:
  266. str1 = report_get_message_str(c_report_state);
  267. str2 = report_get_message_str(c_report_mic_myo_fptr);
  268. offload_stage(ss, offload_number, str1, str2, false);
  269. ss << " " << va_arg(va_args, char*);
  270. break;
  271. case c_offload_myosharedmalloc:
  272. str1 = report_get_message_str(c_report_state);
  273. str2 = report_get_message_str(c_report_myosharedmalloc);
  274. offload_stage(ss, offload_number, str1, str2, false);
  275. va_arg(va_args, char*);
  276. ss << " " << va_arg(va_args, size_t);
  277. break;
  278. case c_offload_myosharedfree:
  279. str1 = report_get_message_str(c_report_state);
  280. str2 = report_get_message_str(c_report_myosharedfree);
  281. offload_stage(ss, offload_number, str1, str2, false);
  282. break;
  283. case c_offload_myosharedalignedmalloc:
  284. str1 = report_get_message_str(c_report_state);
  285. str2 = report_get_message_str(c_report_myosharedalignedmalloc);
  286. offload_stage(ss, offload_number, str1, str2, false);
  287. va_arg(va_args, char*);
  288. ss << " " << va_arg(va_args, size_t);
  289. ss << " " << va_arg(va_args, size_t);
  290. break;
  291. case c_offload_myosharedalignedfree:
  292. str1 = report_get_message_str(c_report_state);
  293. str2 = report_get_message_str(c_report_myosharedalignedfree);
  294. offload_stage(ss, offload_number, str1, str2, false);
  295. break;
  296. case c_offload_myoacquire:
  297. str1 = report_get_message_str(c_report_state);
  298. str2 = report_get_message_str(c_report_myoacquire);
  299. offload_stage(ss, offload_number, str1, str2, false);
  300. break;
  301. case c_offload_myorelease:
  302. str1 = report_get_message_str(c_report_state);
  303. str2 = report_get_message_str(c_report_myorelease);
  304. offload_stage(ss, offload_number, str1, str2, false);
  305. break;
  306. default:
  307. LIBOFFLOAD_ERROR(c_report_unknown_trace_node);
  308. abort();
  309. }
  310. ss << "\n";
  311. buf = ss.str();
  312. fprintf(stdout, buf.data());
  313. fflush(stdout);
  314. va_end(va_args);
  315. return;
  316. }