Skip to content

Commit e08c43e

Browse files
visitorckwmpe
authored andcommitted
powerpc/perf: Optimize find_alternatives_list() using binary search
This patch improves the performance of event alternative lookup by replacing the previous linear search with a more efficient binary search. This change reduces the time complexity for the search process from O(n) to O(log(n)). A pre-sorted table of event values and their corresponding indices has been introduced to expedite the search process. Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> [mpe: Call the array "presort*ed*_event_table", minor formatting] Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://msgid.link/20231013175714.2142775-1-visitorckw@gmail.com
1 parent d45c4b4 commit e08c43e

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

arch/powerpc/perf/power6-pmu.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -335,26 +335,38 @@ static const unsigned int event_alternatives[][MAX_ALT] = {
335335
{ 0x3000fe, 0x400056 }, /* PM_DATA_FROM_L3MISS */
336336
};
337337

338-
/*
339-
* This could be made more efficient with a binary search on
340-
* a presorted list, if necessary
341-
*/
342338
static int find_alternatives_list(u64 event)
343339
{
344-
int i, j;
345-
unsigned int alt;
346-
347-
for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
348-
if (event < event_alternatives[i][0])
349-
return -1;
350-
for (j = 0; j < MAX_ALT; ++j) {
351-
alt = event_alternatives[i][j];
352-
if (!alt || event < alt)
353-
break;
354-
if (event == alt)
355-
return i;
356-
}
340+
const unsigned int presorted_event_table[] = {
341+
0x0130e8, 0x080080, 0x080088, 0x10000a, 0x10000b, 0x10000d, 0x10000e,
342+
0x100010, 0x10001a, 0x100026, 0x100054, 0x100056, 0x1000f0, 0x1000f8,
343+
0x1000fc, 0x200008, 0x20000e, 0x200010, 0x200012, 0x200054, 0x2000f0,
344+
0x2000f2, 0x2000f4, 0x2000f5, 0x2000f6, 0x2000f8, 0x2000fc, 0x2000fe,
345+
0x2d0030, 0x30000a, 0x30000c, 0x300010, 0x300012, 0x30001a, 0x300056,
346+
0x3000f0, 0x3000f2, 0x3000f6, 0x3000f8, 0x3000fc, 0x3000fe, 0x400006,
347+
0x400007, 0x40000a, 0x40000e, 0x400010, 0x400018, 0x400056, 0x4000f0,
348+
0x4000f8, 0x600005
349+
};
350+
const unsigned int event_index_table[] = {
351+
0, 1, 2, 3, 4, 1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 12, 14,
352+
7, 15, 2, 9, 16, 3, 4, 0, 17, 10, 18, 19, 20, 1, 17, 15, 19,
353+
18, 2, 16, 21, 8, 0, 22, 13, 14, 11, 21, 5, 20, 22, 1, 6, 3
354+
};
355+
int hi = ARRAY_SIZE(presorted_event_table) - 1;
356+
int lo = 0;
357+
358+
while (lo <= hi) {
359+
int mid = lo + (hi - lo) / 2;
360+
unsigned int alt = presorted_event_table[mid];
361+
362+
if (alt < event)
363+
lo = mid + 1;
364+
else if (alt > event)
365+
hi = mid - 1;
366+
else
367+
return event_index_table[mid];
357368
}
369+
358370
return -1;
359371
}
360372

0 commit comments

Comments
 (0)