Skip to content

Commit 8508cf3

Browse files
hnaztorvalds
authored andcommitted
sched: loadavg: consolidate LOAD_INT, LOAD_FRAC, CALC_LOAD
There are several definitions of those functions/macros in places that mess with fixed-point load averages. Provide an official version. [akpm@linux-foundation.org: fix missed conversion in block/blk-iolatency.c] Link: http://lkml.kernel.org/r/20180828172258.3185-5-hannes@cmpxchg.org Signed-off-by: Johannes Weiner <hannes@cmpxchg.org> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Tested-by: Suren Baghdasaryan <surenb@google.com> Tested-by: Daniel Drake <drake@endlessm.com> Cc: Christopher Lameter <cl@linux.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Johannes Weiner <jweiner@fb.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Enderborg <peter.enderborg@sony.com> Cc: Randy Dunlap <rdunlap@infradead.org> Cc: Shakeel Butt <shakeelb@google.com> Cc: Tejun Heo <tj@kernel.org> Cc: Vinayak Menon <vinmenon@codeaurora.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b1d29ba commit 8508cf3

File tree

9 files changed

+27
-46
lines changed

9 files changed

+27
-46
lines changed

arch/powerpc/platforms/cell/cpufreq_spudemand.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static int calc_freq(struct spu_gov_info_struct *info)
4949
cpu = info->policy->cpu;
5050
busy_spus = atomic_read(&cbe_spu_info[cpu_to_node(cpu)].busy_spus);
5151

52-
CALC_LOAD(info->busy_spus, EXP, busy_spus * FIXED_1);
52+
info->busy_spus = calc_load(info->busy_spus, EXP, busy_spus * FIXED_1);
5353
pr_debug("cpu %d: busy_spus=%d, info->busy_spus=%ld\n",
5454
cpu, busy_spus, info->busy_spus);
5555

arch/powerpc/platforms/cell/spufs/sched.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -987,9 +987,9 @@ static void spu_calc_load(void)
987987
unsigned long active_tasks; /* fixed-point */
988988

989989
active_tasks = count_active_contexts() * FIXED_1;
990-
CALC_LOAD(spu_avenrun[0], EXP_1, active_tasks);
991-
CALC_LOAD(spu_avenrun[1], EXP_5, active_tasks);
992-
CALC_LOAD(spu_avenrun[2], EXP_15, active_tasks);
990+
spu_avenrun[0] = calc_load(spu_avenrun[0], EXP_1, active_tasks);
991+
spu_avenrun[1] = calc_load(spu_avenrun[1], EXP_5, active_tasks);
992+
spu_avenrun[2] = calc_load(spu_avenrun[2], EXP_15, active_tasks);
993993
}
994994

995995
static void spusched_wake(struct timer_list *unused)
@@ -1071,9 +1071,6 @@ void spuctx_switch_state(struct spu_context *ctx,
10711071
}
10721072
}
10731073

1074-
#define LOAD_INT(x) ((x) >> FSHIFT)
1075-
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
1076-
10771074
static int show_spu_loadavg(struct seq_file *s, void *private)
10781075
{
10791076
int a, b, c;

arch/s390/appldata/appldata_os.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525

2626
#include "appldata.h"
2727

28-
29-
#define LOAD_INT(x) ((x) >> FSHIFT)
30-
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
31-
3228
/*
3329
* OS data
3430
*

block/blk-iolatency.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ struct iolatency_grp {
153153
#define BLKIOLATENCY_MAX_WIN_SIZE NSEC_PER_SEC
154154
/*
155155
* These are the constants used to fake the fixed-point moving average
156-
* calculation just like load average. The call to CALC_LOAD folds
156+
* calculation just like load average. The call to calc_load() folds
157157
* (FIXED_1 (2048) - exp_factor) * new_sample into lat_avg. The sampling
158158
* window size is bucketed to try to approximately calculate average
159159
* latency such that 1/exp (decay rate) is [1 min, 2.5 min) when windows
@@ -248,7 +248,7 @@ static inline void iolat_update_total_lat_avg(struct iolatency_grp *iolat,
248248
return;
249249

250250
/*
251-
* CALC_LOAD takes in a number stored in fixed point representation.
251+
* calc_load() takes in a number stored in fixed point representation.
252252
* Because we are using this for IO time in ns, the values stored
253253
* are significantly larger than the FIXED_1 denominator (2048).
254254
* Therefore, rounding errors in the calculation are negligible and
@@ -257,7 +257,9 @@ static inline void iolat_update_total_lat_avg(struct iolatency_grp *iolat,
257257
exp_idx = min_t(int, BLKIOLATENCY_NR_EXP_FACTORS - 1,
258258
div64_u64(iolat->cur_win_nsec,
259259
BLKIOLATENCY_EXP_BUCKET_SIZE));
260-
CALC_LOAD(iolat->lat_avg, iolatency_exp_factors[exp_idx], stat->rqs.mean);
260+
iolat->lat_avg = calc_load(iolat->lat_avg,
261+
iolatency_exp_factors[exp_idx],
262+
stat->rqs.mean);
261263
}
262264

263265
static inline bool iolatency_may_queue(struct iolatency_grp *iolat,

drivers/cpuidle/governors/menu.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ struct menu_device {
130130
int interval_ptr;
131131
};
132132

133-
134-
#define LOAD_INT(x) ((x) >> FSHIFT)
135-
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
136-
137133
static inline int get_loadavg(unsigned long load)
138134
{
139135
return LOAD_INT(load) * 10 + LOAD_FRAC(load) / 10;

fs/proc/loadavg.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#include <linux/seqlock.h>
1111
#include <linux/time.h>
1212

13-
#define LOAD_INT(x) ((x) >> FSHIFT)
14-
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
15-
1613
static int loadavg_proc_show(struct seq_file *m, void *v)
1714
{
1815
unsigned long avnrun[3];

include/linux/sched/loadavg.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,23 @@ extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift);
2222
#define EXP_5 2014 /* 1/exp(5sec/5min) */
2323
#define EXP_15 2037 /* 1/exp(5sec/15min) */
2424

25-
#define CALC_LOAD(load,exp,n) \
26-
load *= exp; \
27-
load += n*(FIXED_1-exp); \
28-
load >>= FSHIFT;
25+
/*
26+
* a1 = a0 * e + a * (1 - e)
27+
*/
28+
static inline unsigned long
29+
calc_load(unsigned long load, unsigned long exp, unsigned long active)
30+
{
31+
unsigned long newload;
32+
33+
newload = load * exp + active * (FIXED_1 - exp);
34+
if (active >= load)
35+
newload += FIXED_1-1;
36+
37+
return newload / FIXED_1;
38+
}
39+
40+
#define LOAD_INT(x) ((x) >> FSHIFT)
41+
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
2942

3043
extern void calc_global_load(unsigned long ticks);
3144

kernel/debug/kdb/kdb_main.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,16 +2556,11 @@ static int kdb_summary(int argc, const char **argv)
25562556
}
25572557
kdb_printf("%02ld:%02ld\n", val.uptime/(60*60), (val.uptime/60)%60);
25582558

2559-
/* lifted from fs/proc/proc_misc.c::loadavg_read_proc() */
2560-
2561-
#define LOAD_INT(x) ((x) >> FSHIFT)
2562-
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
25632559
kdb_printf("load avg %ld.%02ld %ld.%02ld %ld.%02ld\n",
25642560
LOAD_INT(val.loads[0]), LOAD_FRAC(val.loads[0]),
25652561
LOAD_INT(val.loads[1]), LOAD_FRAC(val.loads[1]),
25662562
LOAD_INT(val.loads[2]), LOAD_FRAC(val.loads[2]));
2567-
#undef LOAD_INT
2568-
#undef LOAD_FRAC
2563+
25692564
/* Display in kilobytes */
25702565
#define K(x) ((x) << (PAGE_SHIFT - 10))
25712566
kdb_printf("\nMemTotal: %8lu kB\nMemFree: %8lu kB\n"

kernel/sched/loadavg.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,6 @@ long calc_load_fold_active(struct rq *this_rq, long adjust)
9191
return delta;
9292
}
9393

94-
/*
95-
* a1 = a0 * e + a * (1 - e)
96-
*/
97-
static unsigned long
98-
calc_load(unsigned long load, unsigned long exp, unsigned long active)
99-
{
100-
unsigned long newload;
101-
102-
newload = load * exp + active * (FIXED_1 - exp);
103-
if (active >= load)
104-
newload += FIXED_1-1;
105-
106-
return newload / FIXED_1;
107-
}
108-
10994
#ifdef CONFIG_NO_HZ_COMMON
11095
/*
11196
* Handle NO_HZ for the global load-average.

0 commit comments

Comments
 (0)