[FreeBSD] Timing Services

2024. 7. 15. 19:19ComputerScience/FreeBSD

 

 

 

3.6 Timing Services

The kernel provides several different timing services to processes. These services include timers that run in real time and timers that run only while a process is executing.

 

 

 

Real Time

The system’s time offset since January 1, 1970, Universal Coordinated Time (UTC), also known as the Epoch, is returned by the system call gettimeofday. (협정 세계시) Most modern processors (including the PC processors) maintain a battery-backup time-of-day register. This clock continues to run even if the processor is turned off. When the system boots, it consults the processor’s time-of-day register to find out the current time.  

The system’s time is then maintained by the clock interrupts. At each interrupt, the system increments its global time variable by an amount equal to the number of microseconds per tick. For the PC, running at 1000 ticks per second, each tick represents 1000 microseconds.

 

 

 

External Representation

Time is always exported from the system as microseconds, rather than as clock ticks, to provide a resolution independent format. Internally, the kernel is free to select whatever tick rate best trades off clock-interrupt-handling overhead with timer resolution. As the tick rate per second increases, the resolution of the system timers improves, but the time spent dealing with hardclock interrupts increases.

As processors become faster, the tick rate can be increased to provide finer resolution without adversely affecting user applications. Systems with real-time constraints often run the clock at 5000 or 10,000 ticks per second. As explained in Section 3.4, the kernel can usually eliminate most of the interrupts associated with a high tick rate. All filesystem (and other) timestamps are maintained in UTC offsets from the Epoch. Conversion to local time, including adjustment for daylight saving time, is handled externally to the system in the C library. 

 

 

 

Adjustment of the Time

Often, it is desirable to maintain the same time on all the machines on a network. It is also possible to keep more accurate time than that available from the basic processor clock. For example, hardware is readily available that listens to the set of radio stations that broadcast UTC synchronization signals in the United States.

When processes on different machines agree on a common time, they will wish to change the clock on their host processor to agree with the networkwide time value. One possibility is to change the system time to the network time using the settimeofday system call. (시간을 조정하는 시스템 콜도 존재하는구나.!)

Unfortunately, the settimeofday system call will result in time running backward on machines whose clocks were fast. Time running backward can confuse user programs (such as make) that expect time to invariably increase. To avoid this problem, the system provides the adjtime system call [Mills, 1992]. The adjtime system call takes a time delta (either positive or negative) and changes the rate at which time advances by 10 percent, faster or slower, until the time has been corrected. The operating system does the speedup by incrementing the global time by 1100 microseconds for each tick and does the slowdown by incrementing the global time by 900 microseconds for each tick. Regardless, time increases monotonically, and user processes depending on the ordering of file-modification times are not affected. However, time changes that take tens of seconds to adjust will affect programs that are measuring time intervals by using repeated calls to gettimeofday.

 

 

 

Interval Time

The system provides each process with three interval timers.

(1) The real timer decrements in real time. An example of use for this timer is a library routine maintaining a wakeup-service queueA SIGALRM signal is delivered to the process when this timer expires. The real-time timer is run from the timeout queue maintained by the softclock() routine (see Section 3.4). 

 

(2) The profiling timer decrements both in process virtual time (when running in user mode) and when the system is running on behalf of the process. It is designed to be used by processes to profile their execution statistically. A SIGPROF signal is delivered to the process when this timer expires. Each time that profclock() runs, it checks to see whether the currently running process has requested a profiling timer; if it has, profclock() decrements the timer and sends the process a signal when zero is reached.

 

(3) The virtual timer decrements in process virtual time. It runs only when the process is executing in user mode. A SIGVTALRM signal is delivered to the process when this timer expires. The virtual timer is also implemented in profclock() as the profiling timer is, except that it decrements the timer for the current process only if it is executing in user mode and not if it is running in the kernel.

 

 

 

https://medium.com/@jalal92/the-dining-philosophers-7157cc05315

 

 

 

+ 랩미팅 준비하다가 gettimeofday가 사용된 파일을 발견했다.!! 

- 오늘 배운 내용이 갑자기 나타나서, 엄청 신기했다 😗 ㅋㅊㅌㅋㅋㅋㅋㅋ

https://github.com/SohyeonKim-dev/prim-benchmarks/blob/main/VA/support/timer.h

 

prim-benchmarks/VA/support/timer.h at main · SohyeonKim-dev/prim-benchmarks

PrIM (Processing-In-Memory benchmarks) is the first benchmark suite for a real-world processing-in-memory (PIM) architecture. PrIM is developed to evaluate, analyze, and characterize the first publ...

github.com

 

#include <sys/time.h>

typedef struct Timer{

    struct timeval startTime[4];
    struct timeval stopTime[4];
    double         time[4];

}Timer;

void start(Timer *timer, int i, int rep) {
    if(rep == 0) {
        timer->time[i] = 0.0;
    }
    gettimeofday(&timer->startTime[i], NULL);
}

void stop(Timer *timer, int i) {
    gettimeofday(&timer->stopTime[i], NULL);
    timer->time[i] += (timer->stopTime[i].tv_sec - timer->startTime[i].tv_sec) * 1000000.0 +
                      (timer->stopTime[i].tv_usec - timer->startTime[i].tv_usec);
}

void print(Timer *timer, int i, int REP) { printf("Time (ms): %f\t", timer->time[i] / (1000 * REP)); }

 

 

 

 

'ComputerScience > FreeBSD' 카테고리의 다른 글

[FreeBSD] Kernel Tracing Facilities, DTrace and KTR  (0) 2024.07.16
[FreeBSD] Resource Services  (0) 2024.07.15
[FreeBSD] Memory Management Services  (1) 2024.07.14
[FreeBSD] Clock Interrupts  (0) 2024.07.10
[FreeBSD] Traps and Interrupts  (0) 2024.07.10