LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   clocksource, clockevent, jiffies (https://www.linuxquestions.org/questions/linux-newbie-8/clocksource-clockevent-jiffies-4175559343/)

david_8274 11-19-2015 05:01 PM

clocksource, clockevent, jiffies
 
Hi all,

What are the relations between clocksourcem clockevent and jiffies? I looked at the kernel source code but it's hard to understand. Can someone gives a brief to top level explanation?

Thanks,
Wei Xu

jpollard 11-19-2015 05:09 PM

Quote:

Originally Posted by david_8274 (Post 5452303)
Hi all,

What are the relations between clocksourcem clockevent and jiffies? I looked at the kernel source code but it's hard to understand. Can someone gives a brief to top level explanation?

Thanks,
Wei Xu

Jiffies are the easiest - they are the shortest measurable time given a particular clock. The jiffy counter is used when no other scheduling clock is available.

The following should be included with the kernel source, but it provides an overview of how timekeeping is used.

https://www.kernel.org/doc/Documenta...imekeeping.txt

david_8274 11-19-2015 06:44 PM

Hi,

So is jiffies based on clocksource or clockevent?

Thanks,
Wei Xu


Quote:

Originally Posted by jpollard (Post 5452310)
Jiffies are the easiest - they are the shortest measurable time given a particular clock. The jiffy counter is used when no other scheduling clock is available.

The following should be included with the kernel source, but it provides an overview of how timekeeping is used.

https://www.kernel.org/doc/Documenta...imekeeping.txt


jpollard 11-19-2015 07:01 PM

Quote:

Originally Posted by david_8274 (Post 5452357)
Hi,

So is jiffies based on clocksource or clockevent?

Thanks,
Wei Xu

It all depends on the local clock. Some clocks don't have an interrupt - they just count. A jiffy is just a conversion unit that allows for a common notation. I've worked on systems where a jiffy is 1/60 second - but not rigidly as the clock was based on the local 60hz power line... which varied +- 5 percent. On a 50Hz power line, that would be 1/50 second instead.

Since it depends on the environment, the "bogomips" values got used to create a fictitious speed that could be used for very low level spinwaits. A request for a millisecond (or less) would go into a loop for a fixed count - computed from the bogomips speed for the given CPU. Not perfect, but it allows things to work where there are no interrupts.

david_8274 11-19-2015 08:15 PM

Hi,

When I look at how jiffies is implemented, i saw the following code:

# define jiffies raid6_jiffies()

static inline uint32_t raid6_jiffies(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec*1000 + tv.tv_usec/1000;
}

Looks like it call gettimeofday() to get the elapsed time then return the second *1000 and micor_second/1000.
My question is, why multiply the second by 1000 is hard coded? doesn't jiffies depends on
#define HZ xxx

in other words, if I redefine HZ to other value, could than not make the factor 1000 invalid?

Thanks,
Wei Xu





Quote:

Originally Posted by jpollard (Post 5452367)
It all depends on the local clock. Some clocks don't have an interrupt - they just count. A jiffy is just a conversion unit that allows for a common notation. I've worked on systems where a jiffy is 1/60 second - but not rigidly as the clock was based on the local 60hz power line... which varied +- 5 percent. On a 50Hz power line, that would be 1/50 second instead.

Since it depends on the environment, the "bogomips" values got used to create a fictitious speed that could be used for very low level spinwaits. A request for a millisecond (or less) would go into a loop for a fixed count - computed from the bogomips speed for the given CPU. Not perfect, but it allows things to work where there are no interrupts.


jpollard 11-20-2015 07:51 AM

Quote:

Originally Posted by david_8274 (Post 5452397)
Hi,

When I look at how jiffies is implemented, i saw the following code:
Quote:

# define jiffies raid6_jiffies()

static inline uint32_t raid6_jiffies(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec*1000 + tv.tv_usec/1000;
}
Looks like it call gettimeofday() to get the elapsed time then return the second *1000 and micor_second/1000.
My question is, why multiply the second by 1000 is hard coded? doesn't jiffies depends on
#define HZ xxx

in other words, if I redefine HZ to other value, could than not make the factor 1000 invalid?

Thanks,
Wei Xu

No. If you notice, this is only defined if things are NOT the kernel.

Here, a jiffy is being defined from the timeval structure (manpage gettimeofday):
Code:

struct timeval {
      time_t      tv_sec;    /* seconds */
      suseconds_t tv_usec;    /* microseconds */
};

So divide the microseconds by 1000, and multiply the seconds by 1000 and add. This is done to get a 32 bit number. (1 second = 1e6 microseconds... so scale one up and one down to keep the units straight).

The result is a monotonically increasing count. It just happens that the 1000 is the same value as the Hz value - but is not related. The units being computed are milliseconds, so changing the values by 1000.

I believe in this case the count is used to set generations of the raid 6 data structures to identify out-of-date data blocks that are in error, and a millisecond is judged to be short enough to make that determination.

The "jiffy" in this case is the minimum time between generations of data blocks before an error has occured, which happens to be units of 1 millisecond.

As before, a "jiffy" is just a minimum amount of time a given clock can detect. In this case, 1 millisecond.


All times are GMT -5. The time now is 04:49 PM.