1f50a7f3dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23752e453SMichael Ellerman /*
33752e453SMichael Ellerman * Copyright 2014, Michael Ellerman, IBM Corp.
43752e453SMichael Ellerman */
53752e453SMichael Ellerman
63752e453SMichael Ellerman #define _GNU_SOURCE
73752e453SMichael Ellerman
83752e453SMichael Ellerman #include <stdio.h>
93752e453SMichael Ellerman #include <stdbool.h>
103752e453SMichael Ellerman #include <string.h>
113752e453SMichael Ellerman #include <sys/prctl.h>
123752e453SMichael Ellerman
133752e453SMichael Ellerman #include "ebb.h"
143752e453SMichael Ellerman
153752e453SMichael Ellerman
163752e453SMichael Ellerman /*
173752e453SMichael Ellerman * Run a calibrated instruction loop and count instructions executed using
183752e453SMichael Ellerman * EBBs. Make sure the counts look right.
193752e453SMichael Ellerman */
203752e453SMichael Ellerman
213752e453SMichael Ellerman extern void thirty_two_instruction_loop(uint64_t loops);
223752e453SMichael Ellerman
233752e453SMichael Ellerman static bool counters_frozen = true;
243752e453SMichael Ellerman
do_count_loop(struct event * event,uint64_t instructions,uint64_t overhead,bool report)253752e453SMichael Ellerman static int do_count_loop(struct event *event, uint64_t instructions,
263752e453SMichael Ellerman uint64_t overhead, bool report)
273752e453SMichael Ellerman {
283752e453SMichael Ellerman int64_t difference, expected;
293752e453SMichael Ellerman double percentage;
303752e453SMichael Ellerman
313752e453SMichael Ellerman clear_ebb_stats();
323752e453SMichael Ellerman
333752e453SMichael Ellerman counters_frozen = false;
343752e453SMichael Ellerman mb();
353752e453SMichael Ellerman mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
363752e453SMichael Ellerman
373752e453SMichael Ellerman thirty_two_instruction_loop(instructions >> 5);
383752e453SMichael Ellerman
393752e453SMichael Ellerman counters_frozen = true;
403752e453SMichael Ellerman mb();
413752e453SMichael Ellerman mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) | MMCR0_FC);
423752e453SMichael Ellerman
433752e453SMichael Ellerman count_pmc(4, sample_period);
443752e453SMichael Ellerman
453752e453SMichael Ellerman event->result.value = ebb_state.stats.pmc_count[4-1];
463752e453SMichael Ellerman expected = instructions + overhead;
473752e453SMichael Ellerman difference = event->result.value - expected;
483752e453SMichael Ellerman percentage = (double)difference / event->result.value * 100;
493752e453SMichael Ellerman
503752e453SMichael Ellerman if (report) {
513752e453SMichael Ellerman printf("Looped for %lu instructions, overhead %lu\n", instructions, overhead);
523752e453SMichael Ellerman printf("Expected %lu\n", expected);
533752e453SMichael Ellerman printf("Actual %llu\n", event->result.value);
54bc5c0a0dSMichael Ellerman printf("Delta %ld, %f%%\n", difference, percentage);
553752e453SMichael Ellerman printf("Took %d EBBs\n", ebb_state.stats.ebb_count);
563752e453SMichael Ellerman }
573752e453SMichael Ellerman
583752e453SMichael Ellerman if (difference < 0)
593752e453SMichael Ellerman difference = -difference;
603752e453SMichael Ellerman
613752e453SMichael Ellerman /* Tolerate a difference of up to 0.0001 % */
623752e453SMichael Ellerman difference *= 10000 * 100;
633752e453SMichael Ellerman if (difference / event->result.value)
643752e453SMichael Ellerman return -1;
653752e453SMichael Ellerman
663752e453SMichael Ellerman return 0;
673752e453SMichael Ellerman }
683752e453SMichael Ellerman
693752e453SMichael Ellerman /* Count how many instructions it takes to do a null loop */
determine_overhead(struct event * event)703752e453SMichael Ellerman static uint64_t determine_overhead(struct event *event)
713752e453SMichael Ellerman {
723752e453SMichael Ellerman uint64_t current, overhead;
733752e453SMichael Ellerman int i;
743752e453SMichael Ellerman
753752e453SMichael Ellerman do_count_loop(event, 0, 0, false);
763752e453SMichael Ellerman overhead = event->result.value;
773752e453SMichael Ellerman
783752e453SMichael Ellerman for (i = 0; i < 100; i++) {
793752e453SMichael Ellerman do_count_loop(event, 0, 0, false);
803752e453SMichael Ellerman current = event->result.value;
813752e453SMichael Ellerman if (current < overhead) {
823752e453SMichael Ellerman printf("Replacing overhead %lu with %lu\n", overhead, current);
833752e453SMichael Ellerman overhead = current;
843752e453SMichael Ellerman }
853752e453SMichael Ellerman }
863752e453SMichael Ellerman
873752e453SMichael Ellerman return overhead;
883752e453SMichael Ellerman }
893752e453SMichael Ellerman
pmc4_ebb_callee(void)903752e453SMichael Ellerman static void pmc4_ebb_callee(void)
913752e453SMichael Ellerman {
923752e453SMichael Ellerman uint64_t val;
933752e453SMichael Ellerman
943752e453SMichael Ellerman val = mfspr(SPRN_BESCR);
953752e453SMichael Ellerman if (!(val & BESCR_PMEO)) {
963752e453SMichael Ellerman ebb_state.stats.spurious++;
973752e453SMichael Ellerman goto out;
983752e453SMichael Ellerman }
993752e453SMichael Ellerman
1003752e453SMichael Ellerman ebb_state.stats.ebb_count++;
1013752e453SMichael Ellerman count_pmc(4, sample_period);
1023752e453SMichael Ellerman out:
1033752e453SMichael Ellerman if (counters_frozen)
1043752e453SMichael Ellerman reset_ebb_with_clear_mask(MMCR0_PMAO);
1053752e453SMichael Ellerman else
1063752e453SMichael Ellerman reset_ebb();
1073752e453SMichael Ellerman }
1083752e453SMichael Ellerman
instruction_count(void)1093752e453SMichael Ellerman int instruction_count(void)
1103752e453SMichael Ellerman {
1113752e453SMichael Ellerman struct event event;
1123752e453SMichael Ellerman uint64_t overhead;
1133752e453SMichael Ellerman
11439fcfb91SDenis Kirjanov SKIP_IF(!ebb_is_supported());
11539fcfb91SDenis Kirjanov
1163752e453SMichael Ellerman event_init_named(&event, 0x400FA, "PM_RUN_INST_CMPL");
1173752e453SMichael Ellerman event_leader_ebb_init(&event);
1183752e453SMichael Ellerman event.attr.exclude_kernel = 1;
1193752e453SMichael Ellerman event.attr.exclude_hv = 1;
1203752e453SMichael Ellerman event.attr.exclude_idle = 1;
1213752e453SMichael Ellerman
1223752e453SMichael Ellerman FAIL_IF(event_open(&event));
1233752e453SMichael Ellerman FAIL_IF(ebb_event_enable(&event));
1243752e453SMichael Ellerman
1253752e453SMichael Ellerman sample_period = COUNTER_OVERFLOW;
1263752e453SMichael Ellerman
1273752e453SMichael Ellerman setup_ebb_handler(pmc4_ebb_callee);
1283752e453SMichael Ellerman mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
1293752e453SMichael Ellerman ebb_global_enable();
1303752e453SMichael Ellerman
1313752e453SMichael Ellerman overhead = determine_overhead(&event);
1323752e453SMichael Ellerman printf("Overhead of null loop: %lu instructions\n", overhead);
1333752e453SMichael Ellerman
1343752e453SMichael Ellerman /* Run for 1M instructions */
1353752e453SMichael Ellerman FAIL_IF(do_count_loop(&event, 0x100000, overhead, true));
1363752e453SMichael Ellerman
1373752e453SMichael Ellerman /* Run for 10M instructions */
1383752e453SMichael Ellerman FAIL_IF(do_count_loop(&event, 0xa00000, overhead, true));
1393752e453SMichael Ellerman
1403752e453SMichael Ellerman /* Run for 100M instructions */
1413752e453SMichael Ellerman FAIL_IF(do_count_loop(&event, 0x6400000, overhead, true));
1423752e453SMichael Ellerman
1433752e453SMichael Ellerman /* Run for 1G instructions */
1443752e453SMichael Ellerman FAIL_IF(do_count_loop(&event, 0x40000000, overhead, true));
1453752e453SMichael Ellerman
1463752e453SMichael Ellerman /* Run for 16G instructions */
1473752e453SMichael Ellerman FAIL_IF(do_count_loop(&event, 0x400000000, overhead, true));
1483752e453SMichael Ellerman
1493752e453SMichael Ellerman /* Run for 64G instructions */
1503752e453SMichael Ellerman FAIL_IF(do_count_loop(&event, 0x1000000000, overhead, true));
1513752e453SMichael Ellerman
1523752e453SMichael Ellerman /* Run for 128G instructions */
1533752e453SMichael Ellerman FAIL_IF(do_count_loop(&event, 0x2000000000, overhead, true));
1543752e453SMichael Ellerman
1553752e453SMichael Ellerman ebb_global_disable();
1563752e453SMichael Ellerman event_close(&event);
1573752e453SMichael Ellerman
1583752e453SMichael Ellerman printf("Finished OK\n");
1593752e453SMichael Ellerman
1603752e453SMichael Ellerman return 0;
1613752e453SMichael Ellerman }
1623752e453SMichael Ellerman
main(void)1633752e453SMichael Ellerman int main(void)
1643752e453SMichael Ellerman {
165d97e7f19SMichael Ellerman test_harness_set_timeout(300);
1663752e453SMichael Ellerman return test_harness(instruction_count, "instruction_count");
1673752e453SMichael Ellerman }
168