Linux time Subsystem (i) Basic concepts

Source: Internet
Author: User
Tags posix

This article uses Q & A to discuss the basic concepts of time with you before

One, what is time?

The problem is so complicated, I don't know if it's a physics, cosmology, or thermodynamics or philosophical issue, I just want to look at the concept of time from a few sides. The content of this section is that I am sitting on the bus thinking, people interested in physics can point out my mistakes (a Linux kernel do not have too deep knowledge of physics), the Linux time subsystem is interested in the people still ignore this bar.

1. Time and Space and relativity

Is there a concept of absolute time? is time independent of all existence? believe that the existence of absolute time is more in line with human thinking, that is, there is an absolute time axis exists, any event can be projected at a point in this timeline, regardless of the observer in what state, we share an absolute time axis. In such a time frame, the concept is absolute, as long as it occurs at the same point in the timeline, then two events occur simultaneously. If two events do not occur at the same time, then the interval between them is absolute, and all perceived intervals are the same. The opposite is the concept of absolute space. The location of any event can correspond to a point in the absolute space, the length of the two points is absolute, and any observer will measure the same result, no matter what state the observer is in.

Of course, the students who understand the special relativity should be able to build up their own space-time view, in fact, there is no absolute time and space (time and space is relative and is related, composed of four-dimensional time and space), but also relative (relative to their own reference system), event A and event B successively occurred, Observers in different inertial reference systems can observe different observations, one may think that A and B are related for 1 seconds, while another inertial reference system considers A and B to be 1.2 seconds apart. Of course, we study the time subsystem in Linux kernel, where the reference system can not be at the speed of light at the level of movement, so the traditional Newton's space-time view is OK, it is Einstein space-time view of a special case.

2, the direction of time

Does time have a beginning and an end? Where does the arrow of time point? What is now? What is the future? Time is not reversible? Is there any possibility of crossing?

According to the second law of thermodynamics, for an isolated system, the spontaneous heat-dependent process of the interior is bound to the direction of entropy increase. If an isolated system is not affected by the outside world, and the system is finally in equilibrium, the entropy of the system is maximized when the equilibrium state is taken. From this point of view, the direction of time is the direction of entropy increase, and is irreversible.

In addition, when the time is flowing in its direction, is continuous, or there is a basic unit, a share of the flow? I think if the exchange of energy (heat exchange) is a share, then the time is also discrete.

3. Sensing time

Time is related to the change, if all is in a stationary state, there is no displacement, no energy changes, then the meaning of the existence of time? The object moves from position A to position B, and the observer perceives the time by perceiving the change in position and judging the sequence of events. When everything is stationary, there is no movement, All stars and planets are stationary, electrons no longer move around the nucleus at high speed, and all systems in the universe do not exchange energy, then time stops, or does not exist. In essence, the state of the universe and the timeline should correspond to one by one, and if the state of the universe is only one, then the timeline stops at one point and no longer flows.

Second, measurement time

1, how to define the second?

There are two criteria when defining the term of the second:

(1) Define the second with the oscillation frequency of cesium 133.

(2) Define the second according to the Earth's rotation and revolution.

There is no doubt that the second method is in line with human habits, everyone has been accustomed to 24 hours a day, 60 minutes per hour, a minute including 60 seconds. We can define it by 1/86400 of the average solar day, or we can define the second with a year of orbit around the sun, but the second, defined by the movement of the celestial body, is not accurate, because the Earth's rotation and time of revolution are not a constant value. Thus, a physical property such as the oscillation frequency of cesium 133 can be used to define the seconds in effect to give the second a constant length of time.

For Linux kernel, it takes the first defined second, and the reference point (Linux epoch) is also UTC time, which is the first way to define the time. Historically, the POSIX standard has set the time reference point to January 1, 1970 0:0 0 Seconds (GMT), GMT is the second method of defining the time, and later fixed to UTC time.

After defining the seconds, we can further subdivide into milliseconds, microseconds, nanoseconds, etc., and can also organize the concepts of component clocks, hours, days, months, and years.

2, how to deal with leap seconds (leap seconds)?

The concept of leap seconds (leap second) is precisely because UTC time differs from the calendar time we use daily. Although you use the strict definition of UTC time, using atomic clocks as a reference, the concept of the second is precisely defined, but it is still going to bow to the reality, because the masses of people like the Earth rotation and revolution related calendar time. In order to keep UTC time consistent with the people's calendar time, it is the concept of leap seconds to increase or decrease the UTC time by an appropriate point in time (when the error accumulates to 1 seconds).

Since the current rotation of the earth is slowing down, our calendar time (GMT) is getting longer, so the current leap-second adjustment is a 1-second operation. Will the future of the Earth's rotation become faster, who knows?

For Linux kernel, although the epoch is UTC time, the kernel does not consider the leap second issue. For a short period of time in astronomy (for example, 15 months) UTC and GMT are basically the same, and most of the applications are basically functional. The solution to this problem comes from the specific application scenario and the system implementation, the kernel just supports the NTP protocol, can be synchronized with the external time server, and the external time synchronization server is support UTC, let them consider leap second bar, Linux kernel only need to keep and synchronize with them OK.

3. How to deal with leap years

Science must be used to guide human production and life, otherwise it will be meaningless. After we have precisely defined the measurement of time, we can organize time in a scientific way. For example, the cycle of the Earth's orbit around the sun is 365 days, 6 hours, 9 minutes, 10 seconds (or 365.2564 days). Not integer days require special handling, for example: Consider common year 365 days, leap year 366 days, and every four years there is a leap. If this is the case, 0.2564 x 4 = 1.0256, that is to say, this processing more than 0.0256 days, how to do? The cumulative length is also a small error. According to the calculations, each 100 leap year calculation (period 400 years), more than 2.56 days, so, defined in the year can be divisible by 100 (if the calculation period is 400 years, then there are 4 such years), select 3 do not leap years, only the year can be divisible by 400 only leap. The result of this calculation is that every 400 years less than 0.44 days, such calculations can be endless calculation.

4, how to define the benchmark?

Time has a concept of absolute and relative. For example, when we were 1, we went to dinner. Here is a concept of relative time, 1 hours is relative to the current point of time. October 1, 1949, the People's Republic of China was established, the time here is absolute time. Of course, in fact, the absolute time here is also relative time, but all the people on Earth use the same datum point, this time represents a universally applicable absolute time.

For a system, it is necessary to define an epoch, all of which are based on this datum point, and for Linux, the same point in time as the Unix epoch: January 1, 1970 0:0 0 seconds (UTC). The base point used by the NTP protocol is: January 1, 1900 0:0 0 seconds (UTC). The base point used by the GPS system is: January 6, 1980 0:0 0 seconds (UTC). Each system can define its own epoch according to its own logic, such as the base point of the Unix epoch because the UNIX operating system was formed around 1970 years.

Third, Linux kernel and time-related basic concepts

1. System clock

As we said in our previous article, time is like a line with no starting point, no end point, except to give the unit of measure (for example: seconds), and give a reference point. For Linux kernel, this reference point is the Linux Epoch. The so-called Linux epoch is actually the point in time of January 1, 1970 0:0 0 seconds (UTC). Although humans like the concepts of years, months, days, hours, minutes, seconds, for computers, they prefer to use the current time point to the number of seconds in the Linux epoch. A POSIX-compliant system must provide a system clock that records the time values of the Linux epoch at a precision of no less than seconds.

The system clock supported by the kernel is defined as follows:

#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1

......

Clock_realtime is a clock describing the real world (here the English realtime has two meanings, one means real time, the other is real-time meaning, in fact, more than Chinese has ambiguous, English is the same, need to judge according to the context, Our scene, of course, is not real-time, just like a clock hanging on a wall, telling the human current time. Of course, the clocks in your home can of course be adjusted forward or backward as you wish. Clock_monotonic is a real-world clock that is forbidden to be artificially set, and you have no way to set it up, but you can adjust it through the NTP protocol. More system clock IDs are detailed in subsequent articles

2. broken-down POSIX Time

Using Linux Epoch as a reference point on time line, it is of course convenient for a computer to use the number of seconds of a reference point to represent a point on a timeline, but for humans using computers, we are more accustomed to broken-down. That is, the cold to the reference point of the second value decomposition into a month and a day seconds. The following data structures are represented in the kernel (comments are very detailed and are not discussed here):

struct TM {
/*
* The number of seconds after the minute, normally in the range
* 0 to $, but can is up to-allow for leap seconds
*/
int tm_sec;
/* The number of minutes after the hour, and the range 0 to 59*/
int tm_min;
/* The number of hours past midnight, in the range 0 to 23 */
int tm_hour;
/* The day of the month, in the range 1 to 31 */
int tm_mday;
/* The number of months since January, in the range 0 to 11 */
int Tm_mon;
/* The number of years since 1900 * *--with the NTP epoch as the reference point
Long tm_year;
/* The number of days since Sunday, in the range 0 to 6 */
int tm_wday;
/* The number of days since January 1, in the range 0 to 365 */
int tm_yday;
};

3. Time representation of different precision

Traditional UNIX uses a time-based definition of seconds, and the associated data structure is time_t:

typedef long time_t;

time_t is a time value in seconds that is defined by the POSIX standard. For example, when you are familiar with the time function, you can get a second value from the Linux epoch to the current point, and the return value type of the function is time_t. If time_t is implemented as a signed 32-bit integer, there is actually an overflow problem in 2038.

With the development of the application, the second precision can not meet the requirements, so there is a microsecond precision time representation:

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

The concept of struct timeval is the same as the time_t, except for a microsecond member that pushes the precision of time to the microsecond level. The formula can be used when calculating the microsecond value of the current moment to the epoch point of Time: Tv_sec x 10^6 + tv_usec.

Due to the needs of real-time applications, the POSIX standard eventually advances the time precision to nanoseconds, and the time for nanosecond precision is expressed as follows:

struct Timespec {
time_t tv_sec; /* seconds */
Long tv_nsec; /* nanoseconds */
};

According to the POSIX standard, the value of the previous time point of timeline is represented by a struct timespec, which should include at least:

Member Data type Name of the Member Describe
time_t Tv_sec The second value at that point in time is only valid if it is greater than or equal to 0.
Long Tv_nsec The NS value at that point in time is only valid if it is greater than or equal to 0 and less than 10^9 nanoseconds.

struct Timespec and struct timeval concepts are similar and are not mentioned here.

Linux time Subsystem (i) Basic concepts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.