Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2014, Michael Ellerman, IBM Corp.
0004  */
0005 
0006 #include <stdio.h>
0007 #include <stdlib.h>
0008 #include <stdbool.h>
0009 
0010 #include "ebb.h"
0011 
0012 
0013 /*
0014  * Test of counting cycles while using MMCR0_FC (freeze counters) to only count
0015  * parts of the code. This is complicated by the fact that FC is set by the
0016  * hardware when the event overflows. We may take the EBB after we have set FC,
0017  * so we have to be careful about whether we clear FC at the end of the EBB
0018  * handler or not.
0019  */
0020 
0021 static bool counters_frozen = false;
0022 static int ebbs_while_frozen = 0;
0023 
0024 static void ebb_callee(void)
0025 {
0026     uint64_t mask, val;
0027 
0028     mask = MMCR0_PMAO | MMCR0_FC;
0029 
0030     val = mfspr(SPRN_BESCR);
0031     if (!(val & BESCR_PMEO)) {
0032         ebb_state.stats.spurious++;
0033         goto out;
0034     }
0035 
0036     ebb_state.stats.ebb_count++;
0037     trace_log_counter(ebb_state.trace, ebb_state.stats.ebb_count);
0038 
0039     val = mfspr(SPRN_MMCR0);
0040     trace_log_reg(ebb_state.trace, SPRN_MMCR0, val);
0041 
0042     if (counters_frozen) {
0043         trace_log_string(ebb_state.trace, "frozen");
0044         ebbs_while_frozen++;
0045         mask &= ~MMCR0_FC;
0046     }
0047 
0048     count_pmc(1, sample_period);
0049 out:
0050     reset_ebb_with_clear_mask(mask);
0051 }
0052 
0053 int cycles_with_freeze(void)
0054 {
0055     struct event event;
0056     uint64_t val;
0057     bool fc_cleared;
0058 
0059     SKIP_IF(!ebb_is_supported());
0060 
0061     event_init_named(&event, 0x1001e, "cycles");
0062     event_leader_ebb_init(&event);
0063 
0064     event.attr.exclude_kernel = 1;
0065     event.attr.exclude_hv = 1;
0066     event.attr.exclude_idle = 1;
0067 
0068     FAIL_IF(event_open(&event));
0069 
0070     setup_ebb_handler(ebb_callee);
0071     ebb_global_enable();
0072     FAIL_IF(ebb_event_enable(&event));
0073 
0074     mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
0075 
0076     fc_cleared = false;
0077 
0078     /* Make sure we loop until we take at least one EBB */
0079     while ((ebb_state.stats.ebb_count < 20 && !fc_cleared) ||
0080         ebb_state.stats.ebb_count < 1)
0081     {
0082         counters_frozen = false;
0083         mb();
0084         mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
0085 
0086         FAIL_IF(core_busy_loop());
0087 
0088         counters_frozen = true;
0089         mb();
0090         mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) |  MMCR0_FC);
0091 
0092         val = mfspr(SPRN_MMCR0);
0093         if (! (val & MMCR0_FC)) {
0094             printf("Outside of loop, FC NOT set MMCR0 0x%lx\n", val);
0095             fc_cleared = true;
0096         }
0097     }
0098 
0099     ebb_global_disable();
0100     ebb_freeze_pmcs();
0101 
0102     dump_ebb_state();
0103 
0104     printf("EBBs while frozen %d\n", ebbs_while_frozen);
0105 
0106     event_close(&event);
0107 
0108     FAIL_IF(ebb_state.stats.ebb_count == 0);
0109     FAIL_IF(fc_cleared);
0110 
0111     return 0;
0112 }
0113 
0114 int main(void)
0115 {
0116     return test_harness(cycles_with_freeze, "cycles_with_freeze");
0117 }