Skip to contentSkip to conversation

sched: Improve readability in update_cpu_load() code

PATCH In review Gautham R Shenoy 3 participants 6 messages May 15, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched: Improve readability in update_cpu_load() code

Author: Gautham R Shenoy <[email protected]>
Date:   Thu May 15 17:55:49 2008 +0530

sched: Improve readability in update_cpu_load() code

Currently the cpu_load[i] is calculated as:
this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;

However, since scale = 2^i, this can be simplified as:
this_rq->cpu_load[i] = old_load + ((new_load - old_load) >> i);

Makes it easier to read.

kernel/sched.c +1 -1
index 2d7d8f1..e1a6985 100644
@@ -2921,7 +2921,7 @@ static void update_cpu_load(struct rq *this_rq)
 		 */
 		if (new_load > old_load)
 			new_load += scale-1;
-		this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
+		this_rq->cpu_load[i] = old_load + ((new_load - old_load) >> i);
 	}
 }
 
Signature · 2 lines
Thanks and Regards
gautham
#1 lore raw
Quoted 19 lines · Gautham R Shenoy
Author: Gautham R Shenoy <[email protected]>
Date:   Thu May 15 17:55:49 2008 +0530

sched: Improve readability in update_cpu_load() code

Currently the cpu_load[i] is calculated as:
this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;

However, since scale = 2^i, this can be simplified as:
this_rq->cpu_load[i] = old_load + ((new_load - old_load) >> i);

Makes it easier to read.

kernel/sched.c +0 -0
index 2d7d8f1..e1a6985 100644

kernel/sched.c static void update_cpu_load(struct rq *this_rq) · line 2924

Quoted 5 lines
kernel/sched.c +1 -1
@@ -2921,7 +2921,7 @@ static void update_cpu_load(struct rq *this_rq)
 		 */
 		if (new_load > old_load)
 			new_load += scale-1;
-		this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
+		this_rq->cpu_load[i] = old_load + ((new_load - old_load) >> i);

This wont work when new_load < old_load ..

For ex: I tried this prog:

#include <stdio.h>

main()

{
	unsigned long old_load = 100, new_load = 90, this_load, this_load1;
	int i = 1, scale = 2 << i;

	this_load = (old_load*(scale-1) + new_load) >> i;
	this_load1 = old_load + ((new_load - old_load) >> i);

	printf ("this_load = %u, this_load1 = %u \n", this_load, this_load1);
}

This is what I get:

$ ./a.out
this_load = 195, this_load1 = 2147483743
$
Signature · 2 lines
Regards,
vatsa
#2 lore raw
Quoted 10 lines · Srivatsa Vaddagiri

This wont work when new_load < old_load ..

For ex: I tried this prog:

#include <stdio.h>

main()

{
	unsigned long old_load = 100, new_load = 90, this_load, this_load1;
	int i = 1, scale = 2 << i;

s/scale = 2<< i/scale = 2 << (i-1)

Quoted 7 lines
	this_load = (old_load*(scale-1) + new_load) >> i;
	this_load1 = old_load + ((new_load - old_load) >> i);

	printf ("this_load = %u, this_load1 = %u \n", this_load, this_load1);
}
Quoted 5 lines

This is what I get:

$ ./a.out
this_load = 195, this_load1 = 2147483743
$

[with above modification:]

this_load = 95, this_load1 = 2147483743

Signature · 2 lines
Regards,
vatsa
#3 lore raw
In reply to Srivatsa Vaddagiri

2008/5/15 Srivatsa Vaddagiri <[email protected]>:

Quoted 32 lines · Gautham R Shenoy
Quoted 19 lines
Author: Gautham R Shenoy <[email protected]>
Date:   Thu May 15 17:55:49 2008 +0530

sched: Improve readability in update_cpu_load() code

Currently the cpu_load[i] is calculated as:
this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;

However, since scale = 2^i, this can be simplified as:
this_rq->cpu_load[i] = old_load + ((new_load - old_load) >> i);

Makes it easier to read.

kernel/sched.c +0 -0
index 2d7d8f1..e1a6985 100644

kernel/sched.c static void update_cpu_load(struct rq *this_rq) · line 2924

Quoted 5 lines
kernel/sched.c +1 -1
@@ -2921,7 +2921,7 @@ static void update_cpu_load(struct rq *this_rq)
               */
              if (new_load > old_load)
                      new_load += scale-1;
-             this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
+             this_rq->cpu_load[i] = old_load + ((new_load - old_load) >> i);

This wont work when new_load < old_load ..

For ex: I tried this prog:

#include <stdio.h>

Quoted 7 lines

main()

{
       unsigned long old_load = 100, new_load = 90, this_load, this_load1;
       int i = 1, scale = 2 << i;

       this_load = (old_load*(scale-1) + new_load) >> i;
       this_load1 = old_load + ((new_load - old_load) >> i);

it should be

this_load2 = 2 * old_load + (new_load >> i) - (old_load >> i);

scale == 2 << i == 1 << (i + 1), so scale >> i = 2.

and it may result in (probably) little difefrences due to different rounding. I think, for me the currently existing version is more easy-to-read.

Quoted 3 lines

--
Regards,
vatsa

Signature · 2 lines
Best regards,
Dmitry Adamushko
#4 lore raw

2008/5/15 Dmitry Adamushko <[email protected]>:

Quoted 38 lines

2008/5/15 Srivatsa Vaddagiri <[email protected]>:

Quoted 37 lines · Gautham R Shenoy
Quoted 19 lines
Author: Gautham R Shenoy <[email protected]>
Date:   Thu May 15 17:55:49 2008 +0530

sched: Improve readability in update_cpu_load() code

Currently the cpu_load[i] is calculated as:
this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;

However, since scale = 2^i, this can be simplified as:
this_rq->cpu_load[i] = old_load + ((new_load - old_load) >> i);

Makes it easier to read.

kernel/sched.c +0 -0
index 2d7d8f1..e1a6985 100644

kernel/sched.c static void update_cpu_load(struct rq *this_rq) · line 2924

Quoted 5 lines
kernel/sched.c +1 -1
@@ -2921,7 +2921,7 @@ static void update_cpu_load(struct rq *this_rq)
               */
              if (new_load > old_load)
                      new_load += scale-1;
-             this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
+             this_rq->cpu_load[i] = old_load + ((new_load - old_load) >> i);

This wont work when new_load < old_load ..

For ex: I tried this prog:

#include <stdio.h>

main()

{
       unsigned long old_load = 100, new_load = 90, this_load, this_load1;
       int i = 1, scale = 2 << i;
Quoted 8 lines
Quoted 2 lines
       this_load = (old_load*(scale-1) + new_load) >> i;
       this_load1 = old_load + ((new_load - old_load) >> i);

it should be

this_load2 = 2 * old_load + (new_load >> i) - (old_load >> i);

scale == 2 << i == 1 << (i + 1), so scale >> i = 2.

argh.. sorry, it's (scale = 2 << i) in your test-program which is wrong. 2^i == (1 << i), so the (2 *) part is redundant, of course.

this_load2 = old_load + (new_load >> i) - (old_load >> i);

Signature · 2 lines
Best regards,
Dmitry Adamushko
#5 lore raw
In reply to Srivatsa Vaddagiri
Quoted 15 lines · Srivatsa Vaddagiri
Quoted 14 lines · Gautham R Shenoy
Author: Gautham R Shenoy <[email protected]>
Date:   Thu May 15 17:55:49 2008 +0530

sched: Improve readability in update_cpu_load() code

Currently the cpu_load[i] is calculated as:
this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;

However, since scale = 2^i, this can be simplified as:
this_rq->cpu_load[i] = old_load + ((new_load - old_load) >> i);

Makes it easier to read.

Quoted 13 lines

kernel/sched.c static void update_cpu_load(struct rq *this_rq) · line 2924

Quoted 10 lines
kernel/sched.c +1 -1
index 2d7d8f1..e1a6985 100644
@@ -2921,7 +2921,7 @@ static void update_cpu_load(struct rq *this_rq)
 		 */
 		if (new_load > old_load)
 			new_load += scale-1;
-		this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
+		this_rq->cpu_load[i] = old_load + ((new_load - old_load) >> i);

This wont work when new_load < old_load ..

Sorry for the noise. I must have read the previous if() condition incorrectly.

Quoted 26 lines

For ex: I tried this prog:

#include <stdio.h>

main()

{
	unsigned long old_load = 100, new_load = 90, this_load, this_load1;
	int i = 1, scale = 2 << i;

	this_load = (old_load*(scale-1) + new_load) >> i;
	this_load1 = old_load + ((new_load - old_load) >> i);

	printf ("this_load = %u, this_load1 = %u \n", this_load, this_load1);
}

This is what I get:

$ ./a.out
this_load = 195, this_load1 = 2147483743
$
Signature · 2 lines
Regards,
vatsa
Signature · 2 lines
Thanks and Regards
gautham