branch.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "util/map_symbol.h"
  2. #include "util/branch.h"
  3. #include <linux/kernel.h>
  4. static bool cross_area(u64 addr1, u64 addr2, int size)
  5. {
  6. u64 align1, align2;
  7. align1 = addr1 & ~(size - 1);
  8. align2 = addr2 & ~(size - 1);
  9. return (align1 != align2) ? true : false;
  10. }
  11. #define AREA_4K 4096
  12. #define AREA_2M (2 * 1024 * 1024)
  13. void branch_type_count(struct branch_type_stat *st, struct branch_flags *flags,
  14. u64 from, u64 to)
  15. {
  16. if (flags->type == PERF_BR_UNKNOWN || from == 0)
  17. return;
  18. st->counts[flags->type]++;
  19. if (flags->type == PERF_BR_COND) {
  20. if (to > from)
  21. st->cond_fwd++;
  22. else
  23. st->cond_bwd++;
  24. }
  25. if (cross_area(from, to, AREA_2M))
  26. st->cross_2m++;
  27. else if (cross_area(from, to, AREA_4K))
  28. st->cross_4k++;
  29. }
  30. const char *branch_type_name(int type)
  31. {
  32. const char *branch_names[PERF_BR_MAX] = {
  33. "N/A",
  34. "COND",
  35. "UNCOND",
  36. "IND",
  37. "CALL",
  38. "IND_CALL",
  39. "RET",
  40. "SYSCALL",
  41. "SYSRET",
  42. "COND_CALL",
  43. "COND_RET"
  44. };
  45. if (type >= 0 && type < PERF_BR_MAX)
  46. return branch_names[type];
  47. return NULL;
  48. }
  49. void branch_type_stat_display(FILE *fp, struct branch_type_stat *st)
  50. {
  51. u64 total = 0;
  52. int i;
  53. for (i = 0; i < PERF_BR_MAX; i++)
  54. total += st->counts[i];
  55. if (total == 0)
  56. return;
  57. fprintf(fp, "\n#");
  58. fprintf(fp, "\n# Branch Statistics:");
  59. fprintf(fp, "\n#");
  60. if (st->cond_fwd > 0) {
  61. fprintf(fp, "\n%8s: %5.1f%%",
  62. "COND_FWD",
  63. 100.0 * (double)st->cond_fwd / (double)total);
  64. }
  65. if (st->cond_bwd > 0) {
  66. fprintf(fp, "\n%8s: %5.1f%%",
  67. "COND_BWD",
  68. 100.0 * (double)st->cond_bwd / (double)total);
  69. }
  70. if (st->cross_4k > 0) {
  71. fprintf(fp, "\n%8s: %5.1f%%",
  72. "CROSS_4K",
  73. 100.0 * (double)st->cross_4k / (double)total);
  74. }
  75. if (st->cross_2m > 0) {
  76. fprintf(fp, "\n%8s: %5.1f%%",
  77. "CROSS_2M",
  78. 100.0 * (double)st->cross_2m / (double)total);
  79. }
  80. for (i = 0; i < PERF_BR_MAX; i++) {
  81. if (st->counts[i] > 0)
  82. fprintf(fp, "\n%8s: %5.1f%%",
  83. branch_type_name(i),
  84. 100.0 *
  85. (double)st->counts[i] / (double)total);
  86. }
  87. }
  88. static int count_str_scnprintf(int idx, const char *str, char *bf, int size)
  89. {
  90. return scnprintf(bf, size, "%s%s", (idx) ? " " : " (", str);
  91. }
  92. int branch_type_str(struct branch_type_stat *st, char *bf, int size)
  93. {
  94. int i, j = 0, printed = 0;
  95. u64 total = 0;
  96. for (i = 0; i < PERF_BR_MAX; i++)
  97. total += st->counts[i];
  98. if (total == 0)
  99. return 0;
  100. if (st->cond_fwd > 0)
  101. printed += count_str_scnprintf(j++, "COND_FWD", bf + printed, size - printed);
  102. if (st->cond_bwd > 0)
  103. printed += count_str_scnprintf(j++, "COND_BWD", bf + printed, size - printed);
  104. for (i = 0; i < PERF_BR_MAX; i++) {
  105. if (i == PERF_BR_COND)
  106. continue;
  107. if (st->counts[i] > 0)
  108. printed += count_str_scnprintf(j++, branch_type_name(i), bf + printed, size - printed);
  109. }
  110. if (st->cross_4k > 0)
  111. printed += count_str_scnprintf(j++, "CROSS_4K", bf + printed, size - printed);
  112. if (st->cross_2m > 0)
  113. printed += count_str_scnprintf(j++, "CROSS_2M", bf + printed, size - printed);
  114. return printed;
  115. }