offload_timer_host.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_timer.h"
  27. #ifdef __INTEL_COMPILER
  28. #include <ia32intrin.h>
  29. #else // __INTEL_COMPILER
  30. #include <x86intrin.h>
  31. #endif // __INTEL_COMPILER
  32. #include "offload_host.h"
  33. #include <sstream>
  34. #include <iostream>
  35. #include <iomanip>
  36. int timer_enabled = 0;
  37. #ifdef TIMING_SUPPORT
  38. int offload_report_level = 0;
  39. int offload_report_enabled = 1;
  40. static const int host_timer_prefix_spaces[] = {
  41. /*c_offload_host_setup_buffers*/ 0,
  42. /*c_offload_host_initialize*/ 2,
  43. /*c_offload_host_target_acquire*/ 2,
  44. /*c_offload_host_wait_deps*/ 2,
  45. /*c_offload_host_setup_buffers*/ 2,
  46. /*c_offload_host_alloc_buffers*/ 4,
  47. /*c_offload_host_setup_misc_data*/ 2,
  48. /*c_offload_host_alloc_data_buffer*/ 4,
  49. /*c_offload_host_send_pointers*/ 2,
  50. /*c_offload_host_gather_inputs*/ 2,
  51. /*c_offload_host_map_in_data_buffer*/ 4,
  52. /*c_offload_host_unmap_in_data_buffer*/ 4,
  53. /*c_offload_host_start_compute*/ 2,
  54. /*c_offload_host_wait_compute*/ 2,
  55. /*c_offload_host_start_buffers_reads*/ 2,
  56. /*c_offload_host_scatter_outputs*/ 2,
  57. /*c_offload_host_map_out_data_buffer*/ 4,
  58. /*c_offload_host_unmap_out_data_buffer*/ 4,
  59. /*c_offload_host_wait_buffers_reads*/ 2,
  60. /*c_offload_host_destroy_buffers*/ 2
  61. };
  62. const static int target_timer_prefix_spaces[] = {
  63. /*c_offload_target_total_time*/ 0,
  64. /*c_offload_target_descriptor_setup*/ 2,
  65. /*c_offload_target_func_lookup*/ 2,
  66. /*c_offload_target_func_time*/ 2,
  67. /*c_offload_target_scatter_inputs*/ 4,
  68. /*c_offload_target_add_buffer_refs*/ 6,
  69. /*c_offload_target_compute*/ 4,
  70. /*c_offload_target_gather_outputs*/ 4,
  71. /*c_offload_target_release_buffer_refs*/ 6
  72. };
  73. static OffloadHostTimerData* timer_data_head;
  74. static OffloadHostTimerData* timer_data_tail;
  75. static mutex_t timer_data_mutex;
  76. static void offload_host_phase_name(std::stringstream &ss, int p_node);
  77. static void offload_target_phase_name(std::stringstream &ss, int p_node);
  78. extern void Offload_Timer_Print(void)
  79. {
  80. std::string buf;
  81. std::stringstream ss;
  82. const char *stars =
  83. "**************************************************************";
  84. ss << "\n\n" << stars << "\n";
  85. ss << " ";
  86. ss << report_get_message_str(c_report_title) << "\n";
  87. ss << stars << "\n";
  88. double frequency = cpu_frequency;
  89. for (OffloadHostTimerData *pnode = timer_data_head;
  90. pnode != 0; pnode = pnode->next) {
  91. ss << " ";
  92. ss << report_get_message_str(c_report_from_file) << " "<< pnode->file;
  93. ss << report_get_message_str(c_report_line) << " " << pnode->line;
  94. ss << "\n";
  95. for (int i = 0; i < c_offload_host_max_phase ; i++) {
  96. ss << " ";
  97. offload_host_phase_name(ss, i);
  98. ss << " " << std::fixed << std::setprecision(5);
  99. ss << (double)pnode->phases[i].total / frequency << "\n";
  100. }
  101. for (int i = 0; i < c_offload_target_max_phase ; i++) {
  102. double time = 0;
  103. if (pnode->target.frequency != 0) {
  104. time = (double) pnode->target.phases[i].total /
  105. (double) pnode->target.frequency;
  106. }
  107. ss << " ";
  108. offload_target_phase_name(ss, i);
  109. ss << " " << std::fixed << std::setprecision(5);
  110. ss << time << "\n";
  111. }
  112. }
  113. buf = ss.str();
  114. fprintf(stdout, buf.data());
  115. fflush(stdout);
  116. }
  117. extern void Offload_Report_Prolog(OffloadHostTimerData *pnode)
  118. {
  119. double frequency = cpu_frequency;
  120. std::string buf;
  121. std::stringstream ss;
  122. if (pnode) {
  123. // [Offload] [Mic 0] [File] file.c
  124. ss << "[" << report_get_message_str(c_report_offload) << "] [";
  125. ss << report_get_message_str(c_report_mic) << " ";
  126. ss << pnode->card_number << "] [";
  127. ss << report_get_message_str(c_report_file);
  128. ss << "] " << pnode->file << "\n";
  129. // [Offload] [Mic 0] [Line] 1234
  130. ss << "[" << report_get_message_str(c_report_offload) << "] [";
  131. ss << report_get_message_str(c_report_mic) << " ";
  132. ss << pnode->card_number << "] [";
  133. ss << report_get_message_str(c_report_line);
  134. ss << "] " << pnode->line << "\n";
  135. // [Offload] [Mic 0] [Tag] Tag 1
  136. ss << "[" << report_get_message_str(c_report_offload) << "] [";
  137. ss << report_get_message_str(c_report_mic) << " ";
  138. ss << pnode->card_number << "] [";
  139. ss << report_get_message_str(c_report_tag);
  140. ss << "] " << report_get_message_str(c_report_tag);
  141. ss << " " << pnode->offload_number << "\n";
  142. buf = ss.str();
  143. fprintf(stdout, buf.data());
  144. fflush(stdout);
  145. }
  146. }
  147. extern void Offload_Report_Epilog(OffloadHostTimerData * timer_data)
  148. {
  149. double frequency = cpu_frequency;
  150. std::string buf;
  151. std::stringstream ss;
  152. OffloadHostTimerData *pnode = timer_data;
  153. if (!pnode) {
  154. return;
  155. }
  156. ss << "[" << report_get_message_str(c_report_offload) << "] [";
  157. ss << report_get_message_str(c_report_host) << "] [";
  158. ss << report_get_message_str(c_report_tag) << " ";
  159. ss << pnode->offload_number << "] [";
  160. ss << report_get_message_str(c_report_cpu_time) << "] ";
  161. ss << std::fixed << std::setprecision(6);
  162. ss << (double) pnode->phases[0].total / frequency;
  163. ss << report_get_message_str(c_report_seconds) << "\n";
  164. if (offload_report_level >= OFFLOAD_REPORT_2) {
  165. ss << "[" << report_get_message_str(c_report_offload) << "] [";
  166. ss << report_get_message_str(c_report_mic);
  167. ss << " " << pnode->card_number;
  168. ss << "] [" << report_get_message_str(c_report_tag) << " ";
  169. ss << pnode->offload_number << "] [";
  170. ss << report_get_message_str(c_report_cpu_to_mic_data) << "] ";
  171. ss << pnode->sent_bytes << " ";
  172. ss << report_get_message_str(c_report_bytes) << "\n";
  173. }
  174. double time = 0;
  175. if (pnode->target.frequency != 0) {
  176. time = (double) pnode->target.phases[0].total /
  177. (double) pnode->target.frequency;
  178. }
  179. ss << "[" << report_get_message_str(c_report_offload) << "] [";
  180. ss << report_get_message_str(c_report_mic) << " ";
  181. ss << pnode->card_number<< "] [";
  182. ss << report_get_message_str(c_report_tag) << " ";
  183. ss << pnode->offload_number << "] [";
  184. ss << report_get_message_str(c_report_mic_time) << "] ";
  185. ss << std::fixed << std::setprecision(6) << time;
  186. ss << report_get_message_str(c_report_seconds) << "\n";
  187. if (offload_report_level >= OFFLOAD_REPORT_2) {
  188. ss << "[" << report_get_message_str(c_report_offload) << "] [";
  189. ss << report_get_message_str(c_report_mic);
  190. ss << " " << pnode->card_number;
  191. ss << "] [" << report_get_message_str(c_report_tag) << " ";
  192. ss << pnode->offload_number << "] [";
  193. ss << report_get_message_str(c_report_mic_to_cpu_data) << "] ";
  194. ss << pnode->received_bytes << " ";
  195. ss << report_get_message_str(c_report_bytes) << "\n";
  196. }
  197. ss << "\n";
  198. buf = ss.str();
  199. fprintf(stdout, buf.data());
  200. fflush(stdout);
  201. offload_report_free_data(timer_data);
  202. }
  203. extern void offload_report_free_data(OffloadHostTimerData * timer_data)
  204. {
  205. OffloadHostTimerData *pnode_last = NULL;
  206. for (OffloadHostTimerData *pnode = timer_data_head;
  207. pnode != 0; pnode = pnode->next) {
  208. if (timer_data == pnode) {
  209. if (pnode_last) {
  210. pnode_last->next = pnode->next;
  211. }
  212. else {
  213. timer_data_head = pnode->next;
  214. }
  215. OFFLOAD_FREE(pnode);
  216. break;
  217. }
  218. pnode_last = pnode;
  219. }
  220. }
  221. static void fill_buf_with_spaces(std::stringstream &ss, int num)
  222. {
  223. for (; num > 0; num--) {
  224. ss << " ";
  225. }
  226. }
  227. static void offload_host_phase_name(std::stringstream &ss, int p_node)
  228. {
  229. int prefix_spaces;
  230. int str_length;
  231. int tail_length;
  232. const int message_length = 40;
  233. char const *str;
  234. str = report_get_host_stage_str(p_node);
  235. prefix_spaces = host_timer_prefix_spaces[p_node];
  236. fill_buf_with_spaces(ss, prefix_spaces);
  237. str_length = strlen(str);
  238. ss << str;
  239. tail_length = message_length - prefix_spaces - str_length;
  240. tail_length = tail_length > 0? tail_length : 1;
  241. fill_buf_with_spaces(ss, tail_length);
  242. }
  243. static void offload_target_phase_name(std::stringstream &ss, int p_node)
  244. {
  245. int prefix_spaces;
  246. int str_length;
  247. const int message_length = 40;
  248. int tail_length;
  249. char const *str;
  250. str = report_get_target_stage_str(p_node);
  251. prefix_spaces = target_timer_prefix_spaces[p_node];
  252. fill_buf_with_spaces(ss, prefix_spaces);
  253. str_length = strlen(str);
  254. ss << str;
  255. tail_length = message_length - prefix_spaces - str_length;
  256. tail_length = (tail_length > 0)? tail_length : 1;
  257. fill_buf_with_spaces(ss, tail_length);
  258. }
  259. void offload_timer_start(OffloadHostTimerData * timer_data,
  260. OffloadHostPhase p_type)
  261. {
  262. timer_data->phases[p_type].start = _rdtsc();
  263. }
  264. void offload_timer_stop(OffloadHostTimerData * timer_data,
  265. OffloadHostPhase p_type)
  266. {
  267. timer_data->phases[p_type].total += _rdtsc() -
  268. timer_data->phases[p_type].start;
  269. }
  270. void offload_timer_fill_target_data(OffloadHostTimerData * timer_data,
  271. void *buf)
  272. {
  273. uint64_t *data = (uint64_t*) buf;
  274. timer_data->target.frequency = *data++;
  275. for (int i = 0; i < c_offload_target_max_phase; i++) {
  276. timer_data->target.phases[i].total = *data++;
  277. }
  278. }
  279. void offload_timer_fill_host_sdata(OffloadHostTimerData * timer_data,
  280. uint64_t sent_bytes)
  281. {
  282. if (timer_data) {
  283. timer_data->sent_bytes += sent_bytes;
  284. }
  285. }
  286. void offload_timer_fill_host_rdata(OffloadHostTimerData * timer_data,
  287. uint64_t received_bytes)
  288. {
  289. if (timer_data) {
  290. timer_data->received_bytes += received_bytes;
  291. }
  292. }
  293. void offload_timer_fill_host_mic_num(OffloadHostTimerData * timer_data,
  294. int card_number)
  295. {
  296. if (timer_data) {
  297. timer_data->card_number = card_number;
  298. }
  299. }
  300. OffloadHostTimerData* offload_timer_init(const char *file, int line)
  301. {
  302. static bool first_time = true;
  303. OffloadHostTimerData* timer_data = NULL;
  304. timer_data_mutex.lock();
  305. {
  306. if (timer_enabled ||
  307. (offload_report_level && offload_report_enabled)) {
  308. timer_data = (OffloadHostTimerData*)
  309. OFFLOAD_MALLOC(sizeof(OffloadHostTimerData), 0);
  310. memset(timer_data, 0, sizeof(OffloadHostTimerData));
  311. timer_data->offload_number = OFFLOAD_DEBUG_INCR_OFLD_NUM() - 1;
  312. if (timer_data_head == 0) {
  313. timer_data_head = timer_data;
  314. timer_data_tail = timer_data;
  315. }
  316. else {
  317. timer_data_tail->next = timer_data;
  318. timer_data_tail = timer_data;
  319. }
  320. timer_data->file = file;
  321. timer_data->line = line;
  322. }
  323. }
  324. timer_data_mutex.unlock();
  325. return timer_data;
  326. }
  327. #endif // TIMING_SUPPORT