2010年3月5日 星期五

ping time is always 0ms...

Linux-2.6.31.1
It is due to HZ is 100, and the clocksource is jiffies, such that the time resolution is 10ms, so anything below 10ms, is 0.

A hardware timer is used to generate interrupt at HZ per second, and a clockevent is implemented.
So I added a clocksource for the same timer. However, the timer isn't a free-run timer, it is configured to count-down with a reload value, ie, the timer count down from reload value to 0, then generate an interrupt, and repeat, so it's somehow tricky to return value for clocksource::read():

  1. directly return the counter value isn't make sense, since it is the value "not counted", while we need the value "counted"
  2. so I return the value (reload - counter), but for reason unknown, the date/sleep just stop functioning....
  3. so I have to add jiffies to the value returned. Return ns2cyc(jiffies * NSEC_PER_JIFFY) + (reload-counter). Kernel doesn't provde ns2cyc, but there is reference in include/linux/clocksource.h:
    static inline void clocksource_calculate_interval(struct clocksource *c,
                                                      unsigned long length_nsec)
    {
            u64 tmp;

            /* Do the ns -> cycle conversion first, using original mult */
            tmp = length_nsec;
            tmp <<= c->shift;
            tmp += c->mult_orig/2;
            do_div(tmp, c->mult_orig);



busybox-1.12.2/networking/ping.c: sendping4
busybox-1.12.2/libb/time.c: monotonic_us
sysctl(__NR_clock_gettime,1)
kernel/posix-timers.c: sys_clock_gettime = SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, struct timespec __user *,tp)
kernel/posix-timers.c: posix_ktime_get_ts
kernel/hrtimer.c: ktime_get_ts
kernel/time/timekeeping.c: getnstimeofday
include/linux/clocksource.h: clocksource_read


./kernel/time.c: SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv, struct timezone __user *, tz)
kernel/time/timekeeping.c: do_gettimeofday
kernel/time/timekeeping.c: getnstimeofday



kernel/hrtimer.c: SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp, struct timespec __user *, rmtp)
kernel/hrtimer.c: hrtimer_nanosleep



sysfs_override_clocksource

沒有留言: