cycles_with_freeze_test.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2014, Michael Ellerman, IBM Corp.
  3. * Licensed under GPLv2.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include "ebb.h"
  9. /*
  10. * Test of counting cycles while using MMCR0_FC (freeze counters) to only count
  11. * parts of the code. This is complicated by the fact that FC is set by the
  12. * hardware when the event overflows. We may take the EBB after we have set FC,
  13. * so we have to be careful about whether we clear FC at the end of the EBB
  14. * handler or not.
  15. */
  16. static bool counters_frozen = false;
  17. static int ebbs_while_frozen = 0;
  18. static void ebb_callee(void)
  19. {
  20. uint64_t mask, val;
  21. mask = MMCR0_PMAO | MMCR0_FC;
  22. val = mfspr(SPRN_BESCR);
  23. if (!(val & BESCR_PMEO)) {
  24. ebb_state.stats.spurious++;
  25. goto out;
  26. }
  27. ebb_state.stats.ebb_count++;
  28. trace_log_counter(ebb_state.trace, ebb_state.stats.ebb_count);
  29. val = mfspr(SPRN_MMCR0);
  30. trace_log_reg(ebb_state.trace, SPRN_MMCR0, val);
  31. if (counters_frozen) {
  32. trace_log_string(ebb_state.trace, "frozen");
  33. ebbs_while_frozen++;
  34. mask &= ~MMCR0_FC;
  35. }
  36. count_pmc(1, sample_period);
  37. out:
  38. reset_ebb_with_clear_mask(mask);
  39. }
  40. int cycles_with_freeze(void)
  41. {
  42. struct event event;
  43. uint64_t val;
  44. bool fc_cleared;
  45. event_init_named(&event, 0x1001e, "cycles");
  46. event_leader_ebb_init(&event);
  47. event.attr.exclude_kernel = 1;
  48. event.attr.exclude_hv = 1;
  49. event.attr.exclude_idle = 1;
  50. FAIL_IF(event_open(&event));
  51. setup_ebb_handler(ebb_callee);
  52. ebb_global_enable();
  53. FAIL_IF(ebb_event_enable(&event));
  54. mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
  55. fc_cleared = false;
  56. /* Make sure we loop until we take at least one EBB */
  57. while ((ebb_state.stats.ebb_count < 20 && !fc_cleared) ||
  58. ebb_state.stats.ebb_count < 1)
  59. {
  60. counters_frozen = false;
  61. mb();
  62. mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
  63. FAIL_IF(core_busy_loop());
  64. counters_frozen = true;
  65. mb();
  66. mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) | MMCR0_FC);
  67. val = mfspr(SPRN_MMCR0);
  68. if (! (val & MMCR0_FC)) {
  69. printf("Outside of loop, FC NOT set MMCR0 0x%lx\n", val);
  70. fc_cleared = true;
  71. }
  72. }
  73. ebb_global_disable();
  74. ebb_freeze_pmcs();
  75. count_pmc(1, sample_period);
  76. dump_ebb_state();
  77. printf("EBBs while frozen %d\n", ebbs_while_frozen);
  78. event_close(&event);
  79. FAIL_IF(ebb_state.stats.ebb_count == 0);
  80. FAIL_IF(fc_cleared);
  81. return 0;
  82. }
  83. int main(void)
  84. {
  85. return test_harness(cycles_with_freeze, "cycles_with_freeze");
  86. }