History

perf/x86/intel: Use switch in intel_pmu_disable/enable_event

Currently, the if-else is used in the intel_pmu_disable/enable_event to check the type of an event. It works well, but with more and more types added later, e.g., perf metrics, compared to the switch statement, the if-else may impair the readability of the code. There is no harm to use the switch statement to replace the if-else here. Also, some optimizing compilers may compile a switch statement into a jump-table which is more efficient than if-else for a large number of cases. The performance gain may not be observed for now, because the number of cases is only 5, but the benefits may be observed with more and more types added in the future. Use switch to replace the if-else in the intel_pmu_disable/enable_event. If the idx is invalid, print a warning. For the case INTEL_PMC_IDX_FIXED_BTS in intel_pmu_disable_event, don't need to check the event->attr.precise_ip. Use return for the case.

Kan Liang committed 6 years ago

Commit 58da7db1 parent bbdbde21 file · +28 −8Browse files

Unified · Split

arch/x86/events/intel/core.c+28−8
@@ -2180,17 +2180,28 @@ static void intel_pmu_disable_event(struct perf_event *event) · 2179 unmodified lines
21802180 struct hw_perf_event *hwc = &event->hw;
21812181 int idx = hwc->idx;
21822182
2183− if (idx < INTEL_PMC_IDX_FIXED) {
2183+ switch (idx) {
2184+ case 0 ... INTEL_PMC_IDX_FIXED - 1:
21842185 intel_clear_masks(event, idx);
21852186 x86_pmu_disable_event(event);
2186− } else if (idx < INTEL_PMC_IDX_FIXED_BTS) {
2187+ break;
2188+ case INTEL_PMC_IDX_FIXED ... INTEL_PMC_IDX_FIXED_BTS - 1:
21872189 intel_clear_masks(event, idx);
21882190 intel_pmu_disable_fixed(event);
2189− } else if (idx == INTEL_PMC_IDX_FIXED_BTS) {
2191+ break;
2192+ case INTEL_PMC_IDX_FIXED_BTS:
21902193 intel_pmu_disable_bts();
21912194 intel_pmu_drain_bts_buffer();
2192− } else if (idx == INTEL_PMC_IDX_FIXED_VLBR)
2195+ return;
2196+ case INTEL_PMC_IDX_FIXED_VLBR:
21932197 intel_clear_masks(event, idx);
2198+ break;
2199+ default:
2200+ intel_clear_masks(event, idx);
2201+ pr_warn("Failed to disable the event with invalid index %d\n",
2202+ idx);
2203+ return;
2204+ }
21942205
21952206 /*
21962207 * Needs to be called after x86_pmu_disable_event,
@@ -2262,18 +2273,27 @@ static void intel_pmu_enable_event(struct perf_event *event) · 65 unmodified lines
22622273 if (unlikely(event->attr.precise_ip))
22632274 intel_pmu_pebs_enable(event);
22642275
2265− if (idx < INTEL_PMC_IDX_FIXED) {
2276+ switch (idx) {
2277+ case 0 ... INTEL_PMC_IDX_FIXED - 1:
22662278 intel_set_masks(event, idx);
22672279 __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
2268− } else if (idx < INTEL_PMC_IDX_FIXED_BTS) {
2280+ break;
2281+ case INTEL_PMC_IDX_FIXED ... INTEL_PMC_IDX_FIXED_BTS - 1:
22692282 intel_set_masks(event, idx);
22702283 intel_pmu_enable_fixed(event);
2271− } else if (idx == INTEL_PMC_IDX_FIXED_BTS) {
2284+ break;
2285+ case INTEL_PMC_IDX_FIXED_BTS:
22722286 if (!__this_cpu_read(cpu_hw_events.enabled))
22732287 return;
22742288 intel_pmu_enable_bts(hwc->config);
2275− } else if (idx == INTEL_PMC_IDX_FIXED_VLBR)
2289+ break;
2290+ case INTEL_PMC_IDX_FIXED_VLBR:
22762291 intel_set_masks(event, idx);
2292+ break;
2293+ default:
2294+ pr_warn("Failed to enable the event with invalid index %d\n",
2295+ idx);
2296+ }
22772297 }
22782298
22792299 static void intel_pmu_add_event(struct perf_event *event)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t