aot_perf_map.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_perf_map.h"
  6. #include "bh_log.h"
  7. #include "bh_platform.h"
  8. struct func_info {
  9. uint32 idx;
  10. void *ptr;
  11. };
  12. static uint32
  13. get_func_size(const AOTModule *module, struct func_info *sorted_func_ptrs,
  14. uint32 idx)
  15. {
  16. uint32 func_sz;
  17. if (idx == module->func_count - 1)
  18. func_sz = (uintptr_t)module->code + module->code_size
  19. - (uintptr_t)(sorted_func_ptrs[idx].ptr);
  20. else
  21. func_sz = (uintptr_t)(sorted_func_ptrs[idx + 1].ptr)
  22. - (uintptr_t)(sorted_func_ptrs[idx].ptr);
  23. return func_sz;
  24. }
  25. static int
  26. compare_func_ptrs(const void *f1, const void *f2)
  27. {
  28. return (intptr_t)((struct func_info *)f1)->ptr
  29. - (intptr_t)((struct func_info *)f2)->ptr;
  30. }
  31. static struct func_info *
  32. sort_func_ptrs(const AOTModule *module, char *error_buf, uint32 error_buf_size)
  33. {
  34. uint64 content_len;
  35. struct func_info *sorted_func_ptrs;
  36. unsigned i;
  37. content_len = (uint64)sizeof(struct func_info) * module->func_count;
  38. sorted_func_ptrs = wasm_runtime_malloc(content_len);
  39. if (!sorted_func_ptrs) {
  40. snprintf(error_buf, error_buf_size,
  41. "allocate memory failed when creating perf map");
  42. return NULL;
  43. }
  44. for (i = 0; i < module->func_count; i++) {
  45. sorted_func_ptrs[i].idx = i;
  46. sorted_func_ptrs[i].ptr = module->func_ptrs[i];
  47. }
  48. qsort(sorted_func_ptrs, module->func_count, sizeof(struct func_info),
  49. compare_func_ptrs);
  50. return sorted_func_ptrs;
  51. }
  52. bool
  53. aot_create_perf_map(const AOTModule *module, char *error_buf,
  54. uint32 error_buf_size)
  55. {
  56. struct func_info *sorted_func_ptrs = NULL;
  57. char perf_map_path[64] = { 0 };
  58. char perf_map_info[128] = { 0 };
  59. FILE *perf_map = NULL;
  60. uint32 i;
  61. pid_t pid = getpid();
  62. bool ret = false;
  63. sorted_func_ptrs = sort_func_ptrs(module, error_buf, error_buf_size);
  64. if (!sorted_func_ptrs)
  65. goto quit;
  66. snprintf(perf_map_path, sizeof(perf_map_path) - 1, "/tmp/perf-%d.map", pid);
  67. perf_map = fopen(perf_map_path, "a");
  68. if (!perf_map) {
  69. LOG_WARNING("warning: can't create /tmp/perf-%d.map, because %s", pid,
  70. strerror(errno));
  71. goto quit;
  72. }
  73. const char *module_name = aot_get_module_name((AOTModule *)module);
  74. for (i = 0; i < module->func_count; i++) {
  75. memset(perf_map_info, 0, 128);
  76. if (strlen(module_name) > 0)
  77. snprintf(perf_map_info, 128, "%lx %x [%s]#aot_func#%u\n",
  78. (uintptr_t)sorted_func_ptrs[i].ptr,
  79. get_func_size(module, sorted_func_ptrs, i), module_name,
  80. sorted_func_ptrs[i].idx);
  81. else
  82. snprintf(perf_map_info, 128, "%lx %x aot_func#%u\n",
  83. (uintptr_t)sorted_func_ptrs[i].ptr,
  84. get_func_size(module, sorted_func_ptrs, i),
  85. sorted_func_ptrs[i].idx);
  86. /* fwrite() is thread safe */
  87. fwrite(perf_map_info, 1, strlen(perf_map_info), perf_map);
  88. }
  89. LOG_VERBOSE("write map information from %s into /tmp/perf-%d.map",
  90. module_name, pid);
  91. ret = true;
  92. quit:
  93. if (sorted_func_ptrs)
  94. wasm_runtime_free(sorted_func_ptrs);
  95. if (perf_map)
  96. fclose(perf_map);
  97. return ret;
  98. }