From 252315eb68be42b6bd4a5a4483605b3e4e3c2418 Mon Sep 17 00:00:00 2001 From: Erwei Deng Date: Thu, 27 Aug 2020 16:47:42 +0800 Subject: [PATCH 2064/2944] alinux: sched: get_sched_lat_count_idx optimization to #30165513 Optimize the get_sched_lat_count_idx function. The raw function use too many if-else branches which could consume more time and bring a little performance loss. I use another method that use less if-else branches and do some test for the performance improvement. I generate 10000 random numbers in each different ranges and run these two methods recording the total running time(us). See the result table below: --------------------------------------- range | raw | own | perf --------------------------------------- [0, 10) | 163 | 57 | +65.03% [0, 50) | 209 | 81 | +61.24% [0, 100) | 174 | 131 | +24.71% [0, 1000) | 192 | 73 | +61.98% [0, 10000) | 203 | 79 | +61.08% [0, 100000) | 141 | 69 | +51.06% We can see that our own method displays the better result. Fixes: fa418988c52e (alinux: sched: Finer grain of sched latency) Signed-off-by: Erwei Deng Acked-by: Shanpei Chen --- kernel/sched/cpuacct.c | 46 ++++++++++++++-------------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/kernel/sched/cpuacct.c b/kernel/sched/cpuacct.c index b037fa9..4936863 100644 --- a/kernel/sched/cpuacct.c +++ b/kernel/sched/cpuacct.c @@ -81,38 +81,20 @@ struct sched_cgroup_lat_stat_cpu { static inline enum sched_lat_count_t get_sched_lat_count_idx(u64 msecs) { - enum sched_lat_count_t idx; - - if (msecs < 1) - idx = SCHED_LAT_0_1; - else if (msecs < 4) - idx = SCHED_LAT_1_4; - else if (msecs < 7) - idx = SCHED_LAT_4_7; - else if (msecs < 10) - idx = SCHED_LAT_7_10; - else if (msecs < 20) - idx = SCHED_LAT_10_20; - else if (msecs < 30) - idx = SCHED_LAT_20_30; - else if (msecs < 40) - idx = SCHED_LAT_30_40; - else if (msecs < 50) - idx = SCHED_LAT_40_50; - else if (msecs < 100) - idx = SCHED_LAT_50_100; - else if (msecs < 500) - idx = SCHED_LAT_100_500; - else if (msecs < 1000) - idx = SCHED_LAT_500_1000; - else if (msecs < 5000) - idx = SCHED_LAT_1000_5000; - else if (msecs < 10000) - idx = SCHED_LAT_5000_10000; - else - idx = SCHED_LAT_10000_INF; - - return idx; + if (msecs < 1) + return SCHED_LAT_0_1; + if (msecs < 10) + return SCHED_LAT_0_1 + (msecs + 2) / 3; + if (msecs < 50) + return SCHED_LAT_7_10 + msecs / 10; + if (msecs < 100) + return SCHED_LAT_50_100; + if (msecs < 1000) + return SCHED_LAT_100_500 + (msecs / 500); + if (msecs < 10000) + return SCHED_LAT_1000_5000 + (msecs / 5000); + + return SCHED_LAT_10000_INF; } /* track CPU usage of a group of tasks and its child groups */ -- 1.8.3.1