instruction_count_test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2014, Michael Ellerman, IBM Corp.
  3. * Licensed under GPLv2.
  4. */
  5. #define _GNU_SOURCE
  6. #include <stdio.h>
  7. #include <stdbool.h>
  8. #include <string.h>
  9. #include <sys/prctl.h>
  10. #include "ebb.h"
  11. /*
  12. * Run a calibrated instruction loop and count instructions executed using
  13. * EBBs. Make sure the counts look right.
  14. */
  15. extern void thirty_two_instruction_loop(uint64_t loops);
  16. static bool counters_frozen = true;
  17. static int do_count_loop(struct event *event, uint64_t instructions,
  18. uint64_t overhead, bool report)
  19. {
  20. int64_t difference, expected;
  21. double percentage;
  22. clear_ebb_stats();
  23. counters_frozen = false;
  24. mb();
  25. mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
  26. thirty_two_instruction_loop(instructions >> 5);
  27. counters_frozen = true;
  28. mb();
  29. mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) | MMCR0_FC);
  30. count_pmc(4, sample_period);
  31. event->result.value = ebb_state.stats.pmc_count[4-1];
  32. expected = instructions + overhead;
  33. difference = event->result.value - expected;
  34. percentage = (double)difference / event->result.value * 100;
  35. if (report) {
  36. printf("Looped for %lu instructions, overhead %lu\n", instructions, overhead);
  37. printf("Expected %lu\n", expected);
  38. printf("Actual %llu\n", event->result.value);
  39. printf("Delta %ld, %f%%\n", difference, percentage);
  40. printf("Took %d EBBs\n", ebb_state.stats.ebb_count);
  41. }
  42. if (difference < 0)
  43. difference = -difference;
  44. /* Tolerate a difference of up to 0.0001 % */
  45. difference *= 10000 * 100;
  46. if (difference / event->result.value)
  47. return -1;
  48. return 0;
  49. }
  50. /* Count how many instructions it takes to do a null loop */
  51. static uint64_t determine_overhead(struct event *event)
  52. {
  53. uint64_t current, overhead;
  54. int i;
  55. do_count_loop(event, 0, 0, false);
  56. overhead = event->result.value;
  57. for (i = 0; i < 100; i++) {
  58. do_count_loop(event, 0, 0, false);
  59. current = event->result.value;
  60. if (current < overhead) {
  61. printf("Replacing overhead %lu with %lu\n", overhead, current);
  62. overhead = current;
  63. }
  64. }
  65. return overhead;
  66. }
  67. static void pmc4_ebb_callee(void)
  68. {
  69. uint64_t val;
  70. val = mfspr(SPRN_BESCR);
  71. if (!(val & BESCR_PMEO)) {
  72. ebb_state.stats.spurious++;
  73. goto out;
  74. }
  75. ebb_state.stats.ebb_count++;
  76. count_pmc(4, sample_period);
  77. out:
  78. if (counters_frozen)
  79. reset_ebb_with_clear_mask(MMCR0_PMAO);
  80. else
  81. reset_ebb();
  82. }
  83. int instruction_count(void)
  84. {
  85. struct event event;
  86. uint64_t overhead;
  87. SKIP_IF(!ebb_is_supported());
  88. event_init_named(&event, 0x400FA, "PM_RUN_INST_CMPL");
  89. event_leader_ebb_init(&event);
  90. event.attr.exclude_kernel = 1;
  91. event.attr.exclude_hv = 1;
  92. event.attr.exclude_idle = 1;
  93. FAIL_IF(event_open(&event));
  94. FAIL_IF(ebb_event_enable(&event));
  95. sample_period = COUNTER_OVERFLOW;
  96. setup_ebb_handler(pmc4_ebb_callee);
  97. mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
  98. ebb_global_enable();
  99. overhead = determine_overhead(&event);
  100. printf("Overhead of null loop: %lu instructions\n", overhead);
  101. /* Run for 1M instructions */
  102. FAIL_IF(do_count_loop(&event, 0x100000, overhead, true));
  103. /* Run for 10M instructions */
  104. FAIL_IF(do_count_loop(&event, 0xa00000, overhead, true));
  105. /* Run for 100M instructions */
  106. FAIL_IF(do_count_loop(&event, 0x6400000, overhead, true));
  107. /* Run for 1G instructions */
  108. FAIL_IF(do_count_loop(&event, 0x40000000, overhead, true));
  109. /* Run for 16G instructions */
  110. FAIL_IF(do_count_loop(&event, 0x400000000, overhead, true));
  111. /* Run for 64G instructions */
  112. FAIL_IF(do_count_loop(&event, 0x1000000000, overhead, true));
  113. /* Run for 128G instructions */
  114. FAIL_IF(do_count_loop(&event, 0x2000000000, overhead, true));
  115. ebb_global_disable();
  116. event_close(&event);
  117. printf("Finished OK\n");
  118. return 0;
  119. }
  120. int main(void)
  121. {
  122. return test_harness(instruction_count, "instruction_count");
  123. }