PKUOS - Pintos
Pintos source browser for PKU Operating System course
Functions | Variables
timer.c File Reference
#include "devices/timer.h"
#include <debug.h>
#include <inttypes.h>
#include <round.h>
#include <stdio.h>
#include "devices/pit.h"
#include "threads/interrupt.h"
#include "threads/synch.h"
#include "threads/thread.h"
Include dependency graph for timer.c:

Go to the source code of this file.

Functions

static bool too_many_loops (unsigned loops)
 Returns true if LOOPS iterations waits for more than one timer tick, otherwise false. More...
 
static void busy_wait (int64_t loops)
 Iterates through a simple loop LOOPS times, for implementing brief delays. More...
 
static void real_time_sleep (int64_t num, int32_t denom)
 Sleep for approximately NUM/DENOM seconds. More...
 
static void real_time_delay (int64_t num, int32_t denom)
 Busy-wait for approximately NUM/DENOM seconds. More...
 
void timer_init (void)
 Sets up the timer to interrupt TIMER_FREQ times per second, and registers the corresponding interrupt. More...
 
void timer_calibrate (void)
 Calibrates loops_per_tick, used to implement brief delays. More...
 
int64_t timer_ticks (void)
 Returns the number of timer ticks since the OS booted. More...
 
int64_t timer_elapsed (int64_t then)
 Returns the number of timer ticks elapsed since THEN, which should be a value once returned by timer_ticks(). More...
 
void timer_sleep (int64_t ticks)
 Sleeps for approximately TICKS timer ticks. More...
 
void timer_msleep (int64_t ms)
 Sleeps for approximately MS milliseconds. More...
 
void timer_usleep (int64_t us)
 Sleeps for approximately US microseconds. More...
 
void timer_nsleep (int64_t ns)
 Sleeps for approximately NS nanoseconds. More...
 
void timer_mdelay (int64_t ms)
 Busy-waits for approximately MS milliseconds. More...
 
void timer_udelay (int64_t us)
 Sleeps for approximately US microseconds. More...
 
void timer_ndelay (int64_t ns)
 Sleeps execution for approximately NS nanoseconds. More...
 
void timer_print_stats (void)
 Prints timer statistics. More...
 
static void timer_interrupt (struct intr_frame *args UNUSED)
 Timer interrupt handler. More...
 

Variables

static int64_t ticks
 See [8254] for hardware details of the 8254 timer chip. More...
 
static unsigned loops_per_tick
 Number of loops per timer tick. More...
 
static intr_handler_func timer_interrupt
 

Function Documentation

◆ busy_wait()

static void NO_INLINE busy_wait ( int64_t  loops)
static

Iterates through a simple loop LOOPS times, for implementing brief delays.

Marked NO_INLINE because code alignment can significantly affect timings, so that if this function was inlined differently in different places the results would be difficult to predict.

Definition at line 204 of file timer.c.

References barrier.

Referenced by real_time_delay(), and too_many_loops().

Here is the caller graph for this function:

◆ real_time_delay()

static void real_time_delay ( int64_t  num,
int32_t  denom 
)
static

Busy-wait for approximately NUM/DENOM seconds.

Definition at line 240 of file timer.c.

References ASSERT, busy_wait(), loops_per_tick, and TIMER_FREQ.

Referenced by real_time_sleep(), timer_mdelay(), timer_ndelay(), and timer_udelay().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ real_time_sleep()

static void real_time_sleep ( int64_t  num,
int32_t  denom 
)
static

Sleep for approximately NUM/DENOM seconds.

Definition at line 212 of file timer.c.

References ASSERT, intr_get_level(), INTR_ON, real_time_delay(), ticks, TIMER_FREQ, and timer_sleep().

Referenced by timer_msleep(), timer_nsleep(), and timer_usleep().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_calibrate()

void timer_calibrate ( void  )

Calibrates loops_per_tick, used to implement brief delays.

Definition at line 44 of file timer.c.

References ASSERT, intr_get_level(), INTR_ON, loops_per_tick, printf(), PRIu64, TIMER_FREQ, and too_many_loops().

Referenced by pintos_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_elapsed()

int64_t timer_elapsed ( int64_t  then)

Returns the number of timer ticks elapsed since THEN, which should be a value once returned by timer_ticks().

Definition at line 82 of file timer.c.

References timer_ticks().

Referenced by alarm_priority_thread(), block_thread(), load_thread(), test_mlfqs_fair(), and timer_sleep().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_init()

void timer_init ( void  )

Sets up the timer to interrupt TIMER_FREQ times per second, and registers the corresponding interrupt.

Definition at line 36 of file timer.c.

References intr_register_ext(), pit_configure_channel(), TIMER_FREQ, and timer_interrupt.

Referenced by pintos_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_interrupt()

static void timer_interrupt ( struct intr_frame *args  UNUSED)
static

Timer interrupt handler.

Definition at line 171 of file timer.c.

References thread_tick(), and ticks.

Here is the call graph for this function:

◆ timer_mdelay()

void timer_mdelay ( int64_t  ms)

Busy-waits for approximately MS milliseconds.

Busy waits.

Interrupts need not be turned on.

Busy waiting wastes CPU cycles, and busy waiting with interrupts off for the interval between timer ticks or longer will cause timer ticks to be lost. Thus, use timer_msleep() instead if interrupts are enabled.

Definition at line 131 of file timer.c.

References real_time_delay().

Here is the call graph for this function:

◆ timer_msleep()

void timer_msleep ( int64_t  ms)

Sleeps for approximately MS milliseconds.

Interrupts must be turned on.

Definition at line 102 of file timer.c.

References real_time_sleep().

Referenced by reset_channel(), speaker_beep(), and wait_while_busy().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_ndelay()

void timer_ndelay ( int64_t  ns)

Sleeps execution for approximately NS nanoseconds.

Interrupts need not be turned on.

Busy waiting wastes CPU cycles, and busy waiting with interrupts off for the interval between timer ticks or longer will cause timer ticks to be lost. Thus, use timer_nsleep() instead if interrupts are enabled.

Definition at line 157 of file timer.c.

References real_time_delay().

Here is the call graph for this function:

◆ timer_nsleep()

void timer_nsleep ( int64_t  ns)

Sleeps for approximately NS nanoseconds.

Interrupts must be turned on.

Definition at line 118 of file timer.c.

References real_time_sleep().

Referenced by select_device().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_print_stats()

void timer_print_stats ( void  )

Prints timer statistics.

devices/timer.h

Definition at line 164 of file timer.c.

References PRId64, printf(), and timer_ticks().

Referenced by print_stats().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_sleep()

void timer_sleep ( int64_t  ticks)

Sleeps for approximately TICKS timer ticks.

Sleep and yield the CPU to other threads.

Interrupts must be turned on.

Definition at line 90 of file timer.c.

References ASSERT, intr_get_level(), INTR_ON, start, thread_yield(), ticks, timer_elapsed(), and timer_ticks().

Referenced by alarm_priority_thread(), load_thread(), real_time_sleep(), sleeper(), test_mlfqs_fair(), and test_sleep().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_ticks()

int64_t timer_ticks ( void  )

Returns the number of timer ticks since the OS booted.

Definition at line 71 of file timer.c.

References intr_disable(), intr_set_level(), and ticks.

Referenced by alarm_priority_thread(), block_thread(), load_thread(), sleeper(), test_mlfqs_fair(), test_sleep(), timer_elapsed(), timer_print_stats(), and timer_sleep().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_udelay()

void timer_udelay ( int64_t  us)

Sleeps for approximately US microseconds.

Interrupts need not be turned on.

Busy waiting wastes CPU cycles, and busy waiting with interrupts off for the interval between timer ticks or longer will cause timer ticks to be lost. Thus, use timer_usleep() instead if interrupts are enabled.

Definition at line 144 of file timer.c.

References real_time_delay().

Referenced by shutdown_reboot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_usleep()

void timer_usleep ( int64_t  us)

Sleeps for approximately US microseconds.

Interrupts must be turned on.

Definition at line 110 of file timer.c.

References real_time_sleep().

Referenced by reset_channel(), and wait_until_idle().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ too_many_loops()

static bool too_many_loops ( unsigned  loops)
static

Returns true if LOOPS iterations waits for more than one timer tick, otherwise false.

Definition at line 180 of file timer.c.

References barrier, busy_wait(), start, and ticks.

Referenced by timer_calibrate().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ loops_per_tick

unsigned loops_per_tick
static

Number of loops per timer tick.

Initialized by timer_calibrate().

Definition at line 25 of file timer.c.

Referenced by real_time_delay(), and timer_calibrate().

◆ ticks

int64_t ticks
static

See [8254] for hardware details of the 8254 timer chip.

Number of timer ticks since OS booted.

Definition at line 21 of file timer.c.

Referenced by real_time_sleep(), timer_interrupt(), timer_sleep(), timer_ticks(), and too_many_loops().

◆ timer_interrupt

intr_handler_func timer_interrupt
static

Definition at line 27 of file timer.c.

Referenced by timer_init().