123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- #include <linux/export.h>
- #include "sched.h"
- atomic_long_t calc_load_tasks;
- unsigned long calc_load_update;
- unsigned long avenrun[3];
- EXPORT_SYMBOL(avenrun);
- void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
- {
- loads[0] = (avenrun[0] + offset) << shift;
- loads[1] = (avenrun[1] + offset) << shift;
- loads[2] = (avenrun[2] + offset) << shift;
- }
- long calc_load_fold_active(struct rq *this_rq, long adjust)
- {
- long nr_active, delta = 0;
- nr_active = this_rq->nr_running - adjust;
- nr_active += (long)this_rq->nr_uninterruptible;
- if (nr_active != this_rq->calc_load_active) {
- delta = nr_active - this_rq->calc_load_active;
- this_rq->calc_load_active = nr_active;
- }
- return delta;
- }
- static unsigned long
- calc_load(unsigned long load, unsigned long exp, unsigned long active)
- {
- unsigned long newload;
- newload = load * exp + active * (FIXED_1 - exp);
- if (active >= load)
- newload += FIXED_1-1;
- return newload / FIXED_1;
- }
- #ifdef CONFIG_NO_HZ_COMMON
- static atomic_long_t calc_load_idle[2];
- static int calc_load_idx;
- static inline int calc_load_write_idx(void)
- {
- int idx = calc_load_idx;
-
- smp_rmb();
-
- if (!time_before(jiffies, calc_load_update))
- idx++;
- return idx & 1;
- }
- static inline int calc_load_read_idx(void)
- {
- return calc_load_idx & 1;
- }
- void calc_load_enter_idle(void)
- {
- struct rq *this_rq = this_rq();
- long delta;
-
- delta = calc_load_fold_active(this_rq, 0);
- if (delta) {
- int idx = calc_load_write_idx();
- atomic_long_add(delta, &calc_load_idle[idx]);
- }
- }
- void calc_load_exit_idle(void)
- {
- struct rq *this_rq = this_rq();
-
- this_rq->calc_load_update = calc_load_update;
- if (time_before(jiffies, this_rq->calc_load_update))
- return;
-
- if (time_before(jiffies, this_rq->calc_load_update + 10))
- this_rq->calc_load_update += LOAD_FREQ;
- }
- static long calc_load_fold_idle(void)
- {
- int idx = calc_load_read_idx();
- long delta = 0;
- if (atomic_long_read(&calc_load_idle[idx]))
- delta = atomic_long_xchg(&calc_load_idle[idx], 0);
- return delta;
- }
- static unsigned long
- fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n)
- {
- unsigned long result = 1UL << frac_bits;
- if (n) {
- for (;;) {
- if (n & 1) {
- result *= x;
- result += 1UL << (frac_bits - 1);
- result >>= frac_bits;
- }
- n >>= 1;
- if (!n)
- break;
- x *= x;
- x += 1UL << (frac_bits - 1);
- x >>= frac_bits;
- }
- }
- return result;
- }
- static unsigned long
- calc_load_n(unsigned long load, unsigned long exp,
- unsigned long active, unsigned int n)
- {
- return calc_load(load, fixed_power_int(exp, FSHIFT, n), active);
- }
- static void calc_global_nohz(void)
- {
- long delta, active, n;
- if (!time_before(jiffies, calc_load_update + 10)) {
-
- delta = jiffies - calc_load_update - 10;
- n = 1 + (delta / LOAD_FREQ);
- active = atomic_long_read(&calc_load_tasks);
- active = active > 0 ? active * FIXED_1 : 0;
- avenrun[0] = calc_load_n(avenrun[0], EXP_1, active, n);
- avenrun[1] = calc_load_n(avenrun[1], EXP_5, active, n);
- avenrun[2] = calc_load_n(avenrun[2], EXP_15, active, n);
- calc_load_update += n * LOAD_FREQ;
- }
-
- smp_wmb();
- calc_load_idx++;
- }
- #else
- static inline long calc_load_fold_idle(void) { return 0; }
- static inline void calc_global_nohz(void) { }
- #endif
- void calc_global_load(unsigned long ticks)
- {
- long active, delta;
- if (time_before(jiffies, calc_load_update + 10))
- return;
-
- delta = calc_load_fold_idle();
- if (delta)
- atomic_long_add(delta, &calc_load_tasks);
- active = atomic_long_read(&calc_load_tasks);
- active = active > 0 ? active * FIXED_1 : 0;
- avenrun[0] = calc_load(avenrun[0], EXP_1, active);
- avenrun[1] = calc_load(avenrun[1], EXP_5, active);
- avenrun[2] = calc_load(avenrun[2], EXP_15, active);
- calc_load_update += LOAD_FREQ;
-
- calc_global_nohz();
- }
- void calc_global_load_tick(struct rq *this_rq)
- {
- long delta;
- if (time_before(jiffies, this_rq->calc_load_update))
- return;
- delta = calc_load_fold_active(this_rq, 0);
- if (delta)
- atomic_long_add(delta, &calc_load_tasks);
- this_rq->calc_load_update += LOAD_FREQ;
- }
|