Skip to contentSkip to conversation

U300 sched_clock implementation

PATCH In review Linus Walleij 4 participants 10 messages May 23, 2009 – Jul 8, 2009 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 U300 sched_clock implementation

This patch was submitted to the ARM Linux maillist, but we need some scheduler person to have a look at this sched_clock() thing.

The U300 timekeeping code is in the -next tree at:
arch/arm/mach-u300/time.c

The OMAP2 solution is in:
arch/arm/plat-omap/common.c

When I look at it I get the feeling, that if this works at all times, then anything that has a continous clock source should actually implement it this way if they have nothing else so schedule with. I wonder how hard that would be to do in a generic way.

PATCH FOLLOWS
This overrides the global sched_clock() symbol in the Linux scheduler with a local implementation which takes advantage of the timesource in U300 giving a scheduling resolution of 1us. The solution is the same as found in the OMAP2 core code.

---
 arch/arm/mach-u300/timer.c |   16 ++++++++++++++++

 1 files changed, 16 insertions(+), 0 deletions(-)
arch/arm/mach-u300/timer.c +0 -0
index 4bf82f8..222de18 100644
@@ -346,6 +346,22 @@ static struct clocksource clocksource_u300_1mhz = {

       .flags          = CLOCK_SOURCE_IS_CONTINUOUS, };

+/*
+ * Override the global weak sched_clock symbol with this
+ * local implementation which uses the clocksource to get some
+ * better resolution when scheduling the kernel. We accept that
+ * this wraps around for now, since it is just a relative time
+ * stamp. (Inspired by OMAP implementation.)
+ */
+unsigned long long sched_clock(void)
+{
+       unsigned long long ret;
+
+       ret = (unsigned long long) u300_get_cycles();
+       ret = (ret * clocksource_u300_1mhz.mult_orig) >>
+               clocksource_u300_1mhz.shift;
+       return ret;
+}

 /*
 * This sets up the system timers, clock source and clock event. --
1.6.2.1

#2 lore raw
Quoted 4 lines · Linus Walleij

This overrides the global sched_clock() symbol in the Linux scheduler with a local implementation which takes advantage of the timesource in U300 giving a scheduling resolution of 1us. The solution is the same as found in the OMAP2 core code.

We assume sched_clock() to return time in ns (e-9) resolution.

Quoted 34 lines
---
 arch/arm/mach-u300/timer.c |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
arch/arm/mach-u300/timer.c +16 -1
index 4bf82f8..222de18 100644
@@ -346,6 +346,22 @@ static struct clocksource clocksource_u300_1mhz = {
       .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
 };
 
+/*
+ * Override the global weak sched_clock symbol with this
+ * local implementation which uses the clocksource to get some
+ * better resolution when scheduling the kernel. We accept that
+ * this wraps around for now, since it is just a relative time
+ * stamp. (Inspired by OMAP implementation.)
+ */
+unsigned long long sched_clock(void)
+{
+       unsigned long long ret;
+
+       ret = (unsigned long long) u300_get_cycles();
+       ret = (ret * clocksource_u300_1mhz.mult_orig) >>
+               clocksource_u300_1mhz.shift;
+       return ret;
+}
 
 /*
 * This sets up the system timers, clock source and clock event.
--

1.6.2.1

#3 lore raw

2009/5/24 Peter Zijlstra <[email protected]>:

Quoted 8 lines · Linus Walleij
Quoted 5 lines

This overrides the global sched_clock() symbol in the Linux scheduler with a local implementation which takes advantage of the timesource in U300 giving a scheduling resolution of 1us. The solution is the same as found in the OMAP2 core code.

We assume sched_clock() to return time in ns (e-9) resolution.

Yep okay and in this case:

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       ret = (unsigned long long) u300_get_cycles();
+       ret = (ret * clocksource_u300_1mhz.mult_orig) >>
+               clocksource_u300_1mhz.shift;
+       return ret;

(mult_orig >> shift) == 1000

So for each cycle in cyclecount register we return 1000 * cycles i.e 1000ns.

If it looks nicer we can of course simply:
return (unsigned long long) u300_get_cycles * 1000;

But the question here is whether this resolution is enough for sched_clock() or if it is irrelevant to override sched_clock() if it cannot schedule with better precision than 1000 ns.

Yours,
Linus Walleij

#4 lore raw
Quoted 11 lines · Linus Walleij

2009/5/24 Peter Zijlstra <[email protected]>:

Quoted 8 lines · Linus Walleij
Quoted 5 lines

This overrides the global sched_clock() symbol in the Linux scheduler with a local implementation which takes advantage of the timesource in U300 giving a scheduling resolution of 1us. The solution is the same as found in the OMAP2 core code.

We assume sched_clock() to return time in ns (e-9) resolution.

Quoted 8 lines

Yep okay and in this case:

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       ret = (unsigned long long) u300_get_cycles();
+       ret = (ret * clocksource_u300_1mhz.mult_orig) >>
+               clocksource_u300_1mhz.shift;
+       return ret;

(mult_orig >> shift) == 1000

Ah, ok -- missed that little detail ;-)

Quoted 9 lines

So for each cycle in cyclecount register we return 1000 * cycles i.e 1000ns.

If it looks nicer we can of course simply:
return (unsigned long long) u300_get_cycles * 1000;

But the question here is whether this resolution is enough for sched_clock() or if it is irrelevant to override sched_clock() if it cannot schedule with better precision than 1000 ns.

No anything better than jiffies is good, 1us certainly is worth the trouble.

#5 lore raw
Quoted 29 lines · Peter Zijlstra
Quoted 11 lines · Linus Walleij

2009/5/24 Peter Zijlstra <[email protected]>:

Quoted 8 lines · Linus Walleij
Quoted 5 lines

This overrides the global sched_clock() symbol in the Linux scheduler with a local implementation which takes advantage of the timesource in U300 giving a scheduling resolution of 1us. The solution is the same as found in the OMAP2 core code.

We assume sched_clock() to return time in ns (e-9) resolution.

Quoted 8 lines

Yep okay and in this case:

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       ret = (unsigned long long) u300_get_cycles();
+       ret = (ret * clocksource_u300_1mhz.mult_orig) >>
+               clocksource_u300_1mhz.shift;
+       return ret;

(mult_orig >> shift) == 1000

Ah, ok -- missed that little detail ;-)

Quoted 6 lines

So for each cycle in cyclecount register we return 1000 * cycles i.e 1000ns.

If it looks nicer we can of course simply:
return (unsigned long long) u300_get_cycles * 1000;

Quoted 6 lines
Quoted 3 lines

But the question here is whether this resolution is enough for sched_clock() or if it is irrelevant to override sched_clock() if it cannot schedule with better precision than 1000 ns.

No anything better than jiffies is good, 1us certainly is worth the trouble.

One note, sched_clock() is assumed to be _cheap_. Now I assume you knew that and chose a suitable clocksource.

But that is the reason this isn't generic, non of the 'stable' clocksources on x86 are fast enough to use as sched_clock.

Of course, x86 isn't the only arch and if enough architectures do show this pattern, we could indeed think about doing this in generic code.

#6 lore raw
In reply to Peter Zijlstra

2009/5/25 Peter Zijlstra <[email protected]>:

Quoted 29 lines · Linus Walleij
Quoted 11 lines

2009/5/24 Peter Zijlstra <[email protected]>:

Quoted 8 lines · Linus Walleij
Quoted 5 lines

This overrides the global sched_clock() symbol in the Linux scheduler with a local implementation which takes advantage of the timesource in U300 giving a scheduling resolution of 1us. The solution is the same as found in the OMAP2 core code.

We assume sched_clock() to return time in ns (e-9) resolution.

Quoted 8 lines

Yep okay and in this case:

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       ret = (unsigned long long) u300_get_cycles();
+       ret = (ret * clocksource_u300_1mhz.mult_orig) >>
+               clocksource_u300_1mhz.shift;
+       return ret;

(mult_orig >> shift) == 1000

Ah, ok -- missed that little detail ;-)

Quoted 6 lines

So for each cycle in cyclecount register we return 1000 * cycles i.e 1000ns.

If it looks nicer we can of course simply:
return (unsigned long long) u300_get_cycles * 1000;

Quoted 6 lines
Quoted 3 lines

But the question here is whether this resolution is enough for sched_clock() or if it is irrelevant to override sched_clock() if it cannot schedule with better precision than 1000 ns.

No anything better than jiffies is good, 1us certainly is worth the trouble.

Can I interpret this as Acked-by: Peter Zijlstra <[email protected]> ?

Russell wanted an indication from the scheduler people that it looked sane...

Yours,
Linus Walleij

#7 lore raw
Quoted 30 lines · Linus Walleij

2009/5/25 Peter Zijlstra <[email protected]>:

Quoted 29 lines · Linus Walleij
Quoted 11 lines

2009/5/24 Peter Zijlstra <[email protected]>:

Quoted 8 lines · Linus Walleij
Quoted 5 lines

This overrides the global sched_clock() symbol in the Linux scheduler with a local implementation which takes advantage of the timesource in U300 giving a scheduling resolution of 1us. The solution is the same as found in the OMAP2 core code.

We assume sched_clock() to return time in ns (e-9) resolution.

Quoted 8 lines

Yep okay and in this case:

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       ret = (unsigned long long) u300_get_cycles();
+       ret = (ret * clocksource_u300_1mhz.mult_orig) >>
+               clocksource_u300_1mhz.shift;
+       return ret;

(mult_orig >> shift) == 1000

Ah, ok -- missed that little detail ;-)

Quoted 6 lines

So for each cycle in cyclecount register we return 1000 * cycles i.e 1000ns.

If it looks nicer we can of course simply:
return (unsigned long long) u300_get_cycles * 1000;

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

But the question here is whether this resolution is enough for sched_clock() or if it is irrelevant to override sched_clock() if it cannot schedule with better precision than 1000 ns.

No anything better than jiffies is good, 1us certainly is worth the trouble.

Can I interpret this as Acked-by: Peter Zijlstra <[email protected]> ?

I think its best if we continue with the patch Paul Mundt has been proposing.

#8 lore raw
Quoted 35 lines · Peter Zijlstra
Quoted 34 lines · Linus Walleij

2009/5/25 Peter Zijlstra <[email protected]>:

Quoted 33 lines · Linus Walleij
Quoted 11 lines

2009/5/24 Peter Zijlstra <[email protected]>:

Quoted 8 lines · Linus Walleij
Quoted 5 lines

This overrides the global sched_clock() symbol in the Linux scheduler with a local implementation which takes advantage of the timesource in U300 giving a scheduling resolution of 1us. The solution is the same as found in the OMAP2 core code.

We assume sched_clock() to return time in ns (e-9) resolution.

Quoted 8 lines

Yep okay and in this case:

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       ret = (unsigned long long) u300_get_cycles();
+       ret = (ret * clocksource_u300_1mhz.mult_orig) >>
+               clocksource_u300_1mhz.shift;
+       return ret;

(mult_orig >> shift) == 1000

Ah, ok -- missed that little detail ;-)

Quoted 9 lines

So for each cycle in cyclecount register we return 1000 * cycles i.e 1000ns.

If it looks nicer we can of course simply:
return (unsigned long long) u300_get_cycles * 1000;

But the question here is whether this resolution is enough for sched_clock() or if it is irrelevant to override sched_clock() if it cannot schedule with better precision than 1000 ns.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

No anything better than jiffies is good, 1us certainly is worth the trouble.

Can I interpret this as Acked-by: Peter Zijlstra <[email protected]> ?

I think its best if we continue with the patch Paul Mundt has been proposing.

[added Tim Bird to CC]

So what do we do? There's apparantly been zero movement on this for over a month, and Tim Bird is reposting his patch adding __notrace to ARMs existing sched_clock implementations.

Given that it seems the generic approach has died a death, I suggest we merge Linus' U300 patch, and get Tim to redo his patch to take account of that, and apply both.

Then, if the generic approach eventually happens, everything can then be fixed up.

Alternatively, if there is movement on the generic approach...

Discuss.

#9 lore raw

Adding in Paul M since it was his patch that was supposed to fix up a generic solution...

2009/7/7 Russell King - ARM Linux <[email protected]>:

Quoted 54 lines · Peter Zijlstra
Quoted 35 lines · Linus Walleij
Quoted 34 lines

2009/5/25 Peter Zijlstra <[email protected]>:

Quoted 33 lines · Linus Walleij
Quoted 11 lines

2009/5/24 Peter Zijlstra <[email protected]>:

Quoted 8 lines · Linus Walleij
Quoted 5 lines

This overrides the global sched_clock() symbol in the Linux scheduler with a local implementation which takes advantage of the timesource in U300 giving a scheduling resolution of 1us. The solution is the same as found in the OMAP2 core code.

We assume sched_clock() to return time in ns (e-9) resolution.

Quoted 8 lines

Yep okay and in this case:

Quoted 4 lines
Quoted 4 lines

+       ret = (unsigned long long) u300_get_cycles();
+       ret = (ret * clocksource_u300_1mhz.mult_orig) >>
+               clocksource_u300_1mhz.shift;
+       return ret;

(mult_orig >> shift) == 1000

Ah, ok -- missed that little detail ;-)

Quoted 9 lines

So for each cycle in cyclecount register we return 1000 * cycles i.e 1000ns.

If it looks nicer we can of course simply:
return (unsigned long long) u300_get_cycles * 1000;

But the question here is whether this resolution is enough for sched_clock() or if it is irrelevant to override sched_clock() if it cannot schedule with better precision than 1000 ns.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

No anything better than jiffies is good, 1us certainly is worth the trouble.

Can I interpret this as Acked-by: Peter Zijlstra <[email protected]> ?

I think its best if we continue with the patch Paul Mundt has been proposing.

[added Tim Bird to CC]

So what do we do?  There's apparantly been zero movement on this for over a month, and Tim Bird is reposting his patch adding __notrace to ARMs existing sched_clock implementations.

Given that it seems the generic approach has died a death, I suggest we merge Linus' U300 patch, and get Tim to redo his patch to take account of that, and apply both.

Quoted 7 lines

Then, if the generic approach eventually happens, everything can then be fixed up.

Alternatively, if there is movement on the generic approach...

Discuss.

I would really like to see Pauls work finalized, it looked very promising, and I think there was actually a rough consensus about his last patch. But I guess that will be in the 2.6.32 merge window earliest?

Yours,
Linus Walleij

#10 lore raw
Quoted 21 lines · Linus Walleij

Adding in Paul M since it was his patch that was supposed to fix up a generic solution...

2009/7/7 Russell King - ARM Linux <[email protected]>:

Quoted 17 lines · Peter Zijlstra
Quoted 2 lines

I think its best if we continue with the patch Paul Mundt has been proposing.

[added Tim Bird to CC]

So what do we do? ?There's apparantly been zero movement on this for over a month, and Tim Bird is reposting his patch adding __notrace to ARMs existing sched_clock implementations.

Given that it seems the generic approach has died a death, I suggest we merge Linus' U300 patch, and get Tim to redo his patch to take account of that, and apply both.

Then, if the generic approach eventually happens, everything can then be fixed up.

Quoted 9 lines
Quoted 4 lines

Alternatively, if there is movement on the generic approach...

Discuss.

I would really like to see Pauls work finalized, it looked very promising, and I think there was actually a rough consensus about his last patch. But I guess that will be in the 2.6.32 merge window earliest?

There was a consensus up until the point John noted that sched_clock() can't wrap, so the generic approach is going to need a bit more work to take the cycle shift and so on in to account. I've gotten momentarily sidetracked with other things, but I'll post an updated version shortly.

Having said that, I don't see any reason why this should block progress on the ARM side, once folks are happy with the generic version then most of the implementations can be killed off, so a few more isn't going to make much of a difference one way or the other.

The only reason I haven't done an SH-specific sched_clock() is because none of our clocksources are architecture specific, and they all reside in drivers/.