MentOS  0.8.0
The Mentoring Operating System
Classes | Macros | Typedefs | Functions
timer.h File Reference

Programmable Interval Timer (PIT) definitions. More...

Go to the source code of this file.

Classes

struct  tvec_base_s
 Contains all the timers of a single CPU. More...
 
struct  timer_list
 Represents the request to execute a function in the future, also known as timer. More...
 

Macros

#define ENABLE_REAL_TIMER_SYSTEM
 
#define ITIMER_REAL   0
 Counts down in real (i.e., wall clock) time. More...
 
#define ITIMER_VIRTUAL   1
 Counts down against the user-mode CPU time consumed by the process.
 
#define ITIMER_PROF   2
 
#define TICKS_PER_SECOND   1193
 Number of ticks per seconds.
 
#define TVN_COUNT   4
 Number of normal timer vectors.
 
#define TVN_BITS   6
 Number of bits for the normal timer vector.
 
#define TVR_BITS   8
 Number of bits for the root timer vector.
 
#define TVN_SIZE   (1 << TVN_BITS)
 Number of headers in a normal timer vector.
 
#define TVR_SIZE   (1 << TVR_BITS)
 Number of headers in a root timer vector.
 
#define TVN_MASK   (TVN_SIZE - 1)
 A mask with all 1s for the normal timer vector (0b00111111)
 
#define TVR_MASK   (TVR_SIZE - 1)
 A mask with all 1s for the root timer vector (0b11111111)
 
#define TIMER_TICKS_BITS(tv)   (TVR_BITS + TVN_BITS * (tv))
 A shift used to calculate a timer position inside the tvec_base structure.
 
#define TIMER_TICKS(tv)   (1 << TIMER_TICKS_BITS(tv))
 Expiration ticks of timer based on position inside tvec_base structure.
 

Typedefs

typedef struct tvec_base_s tvec_base_t
 Contains all the timers of a single CPU.
 

Functions

void timer_handler (pt_regs *f)
 Handles the timer. More...
 
void timer_install (void)
 Sets up the system clock by installing the timer handler into IRQ0.
 
uint64_t timer_get_seconds (void)
 Returns the number of seconds since the system started its execution. More...
 
unsigned long timer_get_ticks (void)
 Returns the number of ticks since the system started its execution. More...
 
void timer_phase (const uint32_t hz)
 Allows to set the timer phase to the given frequency. More...
 
void dynamic_timers_install (void)
 Initialize dynamic timer system.
 
void init_timer (struct timer_list *timer)
 Initializes a new timer struct. More...
 
void run_timer_softirq (void)
 Updates the timer data structures.
 
void add_timer (struct timer_list *timer)
 Add a new timer to the current CPU. More...
 
void remove_timer (struct timer_list *timer)
 Removes a timer from the current CPU. More...
 
int sys_nanosleep (const struct timespec *req, struct timespec *rem)
 Suspends the execution of the calling thread. More...
 
unsigned sys_alarm (int seconds)
 Send signal to calling thread after desired seconds. More...
 
int sys_getitimer (int which, struct itimerval *curr_value)
 Fills the structure pointed to by curr_value with the current setting for the timer specified by which. More...
 
int sys_setitimer (int which, const struct itimerval *new_value, struct itimerval *old_value)
 Arms or disarms the timer specified by which, by setting the timer to the value specified by new_value. More...
 
void update_process_profiling_timer (task_struct *proc)
 Update the profiling timer and generate SIGPROF if it has expired. More...
 

Detailed Description

Programmable Interval Timer (PIT) definitions.

Macro Definition Documentation

◆ ENABLE_REAL_TIMER_SYSTEM

#define ENABLE_REAL_TIMER_SYSTEM

This enables the dynamic timer system use an hierarchical timing wheel, an optimized data structure that allows O(1) insertion, O(1) deletion and ammortized O(1) tick update.

◆ ITIMER_PROF

#define ITIMER_PROF   2

This timer counts down against the total (i.e., both user and system) CPU time consumed by the process.

◆ ITIMER_REAL

#define ITIMER_REAL   0

Counts down in real (i.e., wall clock) time.

This enables the system dump tvec_base timer vectors content on the console.

Function Documentation

◆ add_timer()

void add_timer ( struct timer_list timer)

Add a new timer to the current CPU.

Parameters
timerThe timer to add.

◆ init_timer()

void init_timer ( struct timer_list timer)

Initializes a new timer struct.

Parameters
timerThe timer to initialize.

◆ remove_timer()

void remove_timer ( struct timer_list timer)

Removes a timer from the current CPU.

Parameters
timerThe timer to remove.

◆ sys_alarm()

unsigned sys_alarm ( int  seconds)

Send signal to calling thread after desired seconds.

Parameters
secondsThe number of seconds in the interval
Returns
the number of seconds remaining until any previously scheduled alarm was due to be delivered, or zero if there was no previously scheduled alarm.

◆ sys_getitimer()

int sys_getitimer ( int  which,
struct itimerval curr_value 
)

Fills the structure pointed to by curr_value with the current setting for the timer specified by which.

Parameters
whichThe time domain (i.e., ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF)
curr_valueThere the values must be stored.
Returns
Zero on success, or a negative value indicating the error.

◆ sys_nanosleep()

int sys_nanosleep ( const struct timespec req,
struct timespec rem 
)

Suspends the execution of the calling thread.

Parameters
reqThe amount of time we want to sleep.
remThe remaining time we did not sleep.
Returns
If the call is interrupted by a signal handler, nanosleep() returns -1, sets errno to EINTR, and writes the remaining time into the structure pointed to by rem unless rem is NULL.

The execution is suspended until either at least the time specified in *req has elapsed, or the delivery of a signal that triggers the invocation of a handler in the calling thread or that terminates the process.

◆ sys_setitimer()

int sys_setitimer ( int  which,
const struct itimerval new_value,
struct itimerval old_value 
)

Arms or disarms the timer specified by which, by setting the timer to the value specified by new_value.

The system provides each process with three interval timers, each decrementing in a distinct time domain. When any timer expires, a signal is sent to the process, and the timer (potentially) restarts.

Parameters
whichThe time domain (i.e., ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF)
new_valueThe new value to set.
old_valueWhere the old value must be stored.
Returns
Zero on success, or a negative value indicating the error.

◆ timer_get_seconds()

uint64_t timer_get_seconds ( void  )

Returns the number of seconds since the system started its execution.

Returns
Value in seconds.

◆ timer_get_ticks()

unsigned long timer_get_ticks ( void  )

Returns the number of ticks since the system started its execution.

Returns
Value in ticks.

◆ timer_handler()

void timer_handler ( pt_regs f)

Handles the timer.

Parameters
fThe interrupt stack frame.

In this case, it's very simple: We increment the 'timer_ticks' variable every time the timer fires. By default, the timer fires 18.222 times per second. Why 18.222Hz? Some engineer at IBM must've been smoking something funky.

◆ timer_phase()

void timer_phase ( const uint32_t  hz)

Allows to set the timer phase to the given frequency.

Parameters
hzThe frequency to set.

◆ update_process_profiling_timer()

void update_process_profiling_timer ( task_struct proc)

Update the profiling timer and generate SIGPROF if it has expired.

Parameters
procThe process for which we must update the profiling.