1 --- a/util-linux/hwclock.c
2 +++ b/util-linux/hwclock.c
3 @@ -109,10 +109,53 @@ static void to_sys_clock(const char **pp
5 static void from_sys_clock(const char **pp_rtcname, int utc)
7 -#define TWEAK_USEC 200
14 + rtc = rtc_xopen(pp_rtcname, O_WRONLY);
15 + gettimeofday(&tv, NULL);
16 + /* Prepare tm_time */
17 + if (sizeof(time_t) == sizeof(tv.tv_sec)) {
19 + gmtime_r((time_t*)&tv.tv_sec, &tm_time);
21 + localtime_r((time_t*)&tv.tv_sec, &tm_time);
23 + time_t t = tv.tv_sec;
25 + gmtime_r(&t, &tm_time);
27 + localtime_r(&t, &tm_time);
30 +/* Bloated code which tries to set hw clock with better precision.
31 + * On x86, even though code does set hw clock within <1ms of exact
32 + * whole seconds, apparently hw clock (at least on some machines)
33 + * doesn't reset internal fractional seconds to 0,
34 + * making all this a pointless excercise.
36 + /* If we see that we are N usec away from whole second,
37 + * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
38 + * that CPU is not infinitely fast.
39 + * On infinitely fast CPU, next wakeup would be
40 + * on (exactly_next_whole_second - ADJ). On real CPUs,
41 + * this difference between current time and whole second
42 + * is less than ADJ (assuming system isn't heavily loaded).
44 + /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
45 + * Slower CPUs will fail to sync and will go to bigger
46 + * ADJ values. qemu-emulated armv4tl with ~100 MHz
47 + * performance ends up using ADJ ~= 4*1024 and it takes
48 + * 2+ secs (2 tries with successively larger ADJ)
49 + * to sync. Even straced one on the same qemu (very slow)
50 + * takes only 4 tries.
52 +#define TWEAK_USEC 256
53 unsigned adj = TWEAK_USEC;
56 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
58 /* Try to catch the moment when whole second is close */
59 @@ -124,55 +167,64 @@ static void from_sys_clock(const char **
62 rem_usec = 1000000 - tv.tv_usec;
63 - if (rem_usec < 1024) {
64 - /* Less than 1ms to next second. Good enough */
65 + if (rem_usec < adj) {
72 + /* Prepare tm_time from t */
74 gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
76 localtime_r(&t, &tm_time); /* same */
77 - tm_time.tm_isdst = 0;
79 + if (adj >= 32*1024) {
80 + break; /* 32 ms diff and still no luck?? give up trying to sync */
83 /* gmtime/localtime took some time, re-get cur time */
84 gettimeofday(&tv, NULL);
86 - if (tv.tv_sec < t /* may happen if rem_usec was < 1024 */
87 - || (tv.tv_sec == t && tv.tv_usec < 1024)
88 + if (tv.tv_sec < t /* we are still in old second */
89 + || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
91 - /* We are not too far into next second. Good. */
94 - adj += 32; /* 2^(10-5) = 2^5 = 32 iterations max */
96 - /* Give up trying to sync */
98 + break; /* good, we are in sync! */
101 - /* Try to sync up by sleeping */
102 rem_usec = 1000000 - tv.tv_usec;
103 - if (rem_usec < 1024) {
104 - goto small_rem; /* already close, don't sleep */
105 + if (rem_usec < adj) {
107 + goto small_rem; /* already close to next sec, don't sleep */
110 - * Note that small adj on slow processors can make us
111 - * to always overshoot tv.tv_usec < 1024 check on next
112 - * iteration. That's why adj is increased on each iteration.
113 - * This also allows it to be reused as a loop limiter.
115 - usleep(rem_usec - adj);
118 - xioctl(rtc, RTC_SET_TIME, &tm_time);
119 + /* Try to sync up by sleeping */
120 + usleep(rem_usec - adj);
122 - /* Debug aid to find "good" TWEAK_USEC.
123 + /* Jump to 1ms diff, then increase fast (x2): EVERY loop
124 + * takes ~1 sec, people won't like slowly converging code here!
126 + //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
129 + /* ... and if last "overshoot" does not look insanely big,
130 + * just use it as adj increment. This makes convergence faster.
132 + if (tv.tv_usec < adj * 8) {
138 + /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
139 * Look for a value which makes tv_usec close to 999999 or 0.
140 - * for 2.20GHz Intel Core 2: TWEAK_USEC ~= 200
141 + * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
143 - //bb_error_msg("tv.tv_usec:%d adj:%d", (int)tv.tv_usec, adj);
144 + //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
147 + tm_time.tm_isdst = 0;
148 + xioctl(rtc, RTC_SET_TIME, &tm_time);
150 if (ENABLE_FEATURE_CLEAN_UP)