`On Thu, 2009-03-19 at 22:47 +0100, Thomas Gleixner wrote:> On Thu, 19 Mar 2009, Michal Simek wrote:> > And the second question is about shift and rating values.> > I wrote one message in past http://lkml.org/lkml/2009/1/11/291> > Here is the important of part of that message.> > > > ...> > > > And the second part is about shift and rating values. Rating is> > describe(linux/clocksource.h) and seems to me that should be> > corresponded with CONFIG_HZ value,right?Not sure where the idea of correspondence w/ CONFIG_HZ came from. Therating value just provides a relative ordering of preferences betweenpossible clocksources. Since different hardware may have a number ofdifferent clocksources available, we just need to have a method ofselecting a preferred clocksource, and the rating value is used forthat.The guide in linux/clocksource.h is just a guide. Most arches, whichonly have one or two clocksource options probably won't need much care,and a rating of 200 or 300 will probably suffice. Or if there reallyisn't any option about it and there is only one which is a must-useclocksource, 400.> > And I found any explanation of shift value -> max value for equation> > (2-5) * freq << shift / NSEC_PER_SEC should be for my case still 32bit> > number, where (2-5s) are because of NTP> > @John, can you explain the shift vlaue please ?The shift value is a bit more difficult to explain. The algorithm youdescribe above is used by sparc to generate shift, and I think it willwork, but may not be optimal.This question comes up over and over, so I figured I should sit down andreally solve it. Basically the constraint is you want to calculate a mult value using thehighest shift possible. However we have to be careful not to overflow64bits when we multiply ~5second worth of cycles times the mult value.So I finally put this down into code and here it is. No promises that itis 100% right, but from my simple test examples it worked ok.Let me know if it helps.thanks-johnAdd helper functions to calculate ideal clocksource shift values givenits frequency (either hz or khz). Lightly tested, use with care. Also fixes a few old-old-old timesource (well, timsource) references myfind and replace attempts failed on.Signed-off-by: John Stultz <johnstul@us.ibm.com>diff --git a/include/linux/clocksource.h b/include/linux/clocksource.hindex f88d32f..9ef062d 100644--- a/include/linux/clocksource.h+++ b/include/linux/clocksource.h@@ -116,7 +116,7 @@ extern struct clocksource *clock;	/* current clocksource */  * @khz:		Clocksource frequency in KHz  * @shift_constant:	Clocksource shift factor  *- * Helper functions that converts a khz counter frequency to a timsource+ * Helper functions that converts a khz counter frequency to a clocksource  * multiplier, given the clocksource shift value  */ static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)@@ -142,7 +142,7 @@ static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)  * @shift_constant:	Clocksource shift factor  *  * Helper functions that converts a hz counter- * frequency to a timsource multiplier, given the+ * frequency to a clocksource multiplier, given the  * clocksource shift value  */ static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)@@ -162,6 +162,10 @@ static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant) 	return (u32)tmp; } +extern u32 clocksource_khz2shift(u32 freq_khz);+extern u32 clocksource_hz2shift(u32 freq_hz);++ /**  * clocksource_read: - Access the clocksource's current cycle value  * @cs:		pointer to clocksource being readdiff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.cindex ca89e15..0a2538d 100644--- a/kernel/time/clocksource.c+++ b/kernel/time/clocksource.c@@ -212,6 +212,83 @@ static void clocksource_check_watchdog(struct clocksource *cs) static inline void clocksource_resume_watchdog(void) { } #endif ++/* Maximum seconds worth of cycles we consider valid */+#define MAX_SEC_OF_CYCLES 5ULL++/**+ * clocksource_khz2shift - calculates shift from khz+ * @freq_khz:		Clocksource frequency in KHz+ *+ * Helper functions that converts a khz counter+ * frequency to a clocksource shift.+ */+u32 clocksource_khz2shift(u32 freq_khz)+{+	s32 shift = 32;+	u32 mult;++	while (1) {+		s64 tmp;+		u64 cycles;+		mult = clocksource_khz2mult(freq_khz, shift);++		cycles = (u64)freq_khz*1000*MAX_SEC_OF_CYCLES;+		tmp = (cycles * mult) >> shift;++		tmp = (MAX_SEC_OF_CYCLES*NSEC_PER_SEC) - tmp;+		if (tmp < 0)+			tmp = -tmp;++		/* if we are witin one mult unit, we're good */+		if (tmp < cycles>>shift)+			break;++		shift--;+		if (shift < 1)+			BUG();+	}+	return shift;+}++/**+ * clocksource_hz2shift - calculates shift from hz+ * @freq_hz:		Clocksource frequency in Hz+ *+ * Helper functions that converts a hz counter+ * frequency to a clocksource shift.+ */++u32 clocksource_hz2shift(u32 freq_hz)+{+	s32 shift = 32;+	u32 mult;++	while (1) {+		s64 tmp;+		u64 cycles;+		mult = clocksource_hz2mult(freq_hz, shift);++		cycles = (u64)freq_hz*MAX_SEC_OF_CYCLES;+		tmp = (cycles * mult) >> shift;++		tmp = MAX_SEC_OF_CYCLES*NSEC_PER_SEC - tmp;+		if (tmp < 0)+			tmp = -tmp;++		/* if we are witin one mult unit, we're good */+		if (tmp < cycles>>shift)+			break;++		shift--;+		if (shift < 1)+			BUG();+	}+	return shift;+}+++ /**  * clocksource_resume - resume the clocksource(s)  */`