Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch6. Flow of Time Ch7. Getting Hold of Memory 2003. 1. 24 홍원의.

Similar presentations


Presentation on theme: "Ch6. Flow of Time Ch7. Getting Hold of Memory 2003. 1. 24 홍원의."— Presentation transcript:

1 Ch6. Flow of Time Ch7. Getting Hold of Memory 2003. 1. 24 홍원의

2 Contents Flow of Time Time Intervals in the Kernel Knowing the Current Time Delaying Execution Task Queue Kernel Timers Getting Hold of Memory The Real Story of kmalloc get_free_page and Friends vmalloc and Friends Boot-Time Allocation

3 Time Intervals in the Kernel timer interrupt the kernel uses to keep track of time intervals HZ : an architecture-dependent value jiffies initialized to 0 when the system boots timer interrupt  jiffies increase change the HZ value  different clock interupt frequency to use Linux for hard real-time tasks : known to raise the value of HZ to get better response times

4 Knowing the Current Time measure time interval : looking at the jiffies measure short time lapse sharply : processor-specific register wall-clock time void do_gettimeofday(struct timeval *tv) : near ㎲ resolution for many architecture xtime value : can not automatically access both fields void get_fast_time(struct timeval *tv) : quick but less precise

5 Delaying Execution to allow the hardware to accomplish some task Long Delays (longer than on clock tick) : use the system clock Short Delays : implemented with software loops

6 Delaying Execution Long Delays busy waiting unsigned long j = jiffies + jit_delay * HZ; while (jiffies < j) / * nothing */; locks the processor If interrupts is disabled,jiffies won’t be updated should be avoided

7 Delaying Execution a better solution while (jiffies < j) schedule(); allows other processes to run If it is the only process, idle task will never run If system is very busy, could ends up waiting rather longer

8 Delaying Execution asking the kernel to do waiting for some events within a certain period of time (bounded sleep) sleep_on_timeout (wait_queue_head_t *q, unsigned long timeout); interruptible_sleep_on_timeout (wait_queue_head_t *q, unsigned long timeout); no other events expected schedule_timeout (unsigned long timeout);

9 Delaying Execution Short Delays often used to synchronize with h/w kernel function: void udelay (unsigned long usec); void mdelay (unsigned long msec); udelay is based on the value loops_per_second, which is the result of BogoMips at boot-time software loop, busy waiting

10 Task Queue provides a flexible utility for scheduling execution at a “later” time examples to manage hardware that cannot generate interrupts but still allows block read (periodically waking a process requires two context switching each time) giving timely input to a simple hardware device (example: stepper motor)

11 Task Queue a list of tasks, each task being represented by a function pointer and an argument struct tq_struct { struct tq_struct *next; /* linked list of active bh’s */ int sync; /* must be initialized to zero */ void (*routine)(void *); /* function to call */ void *data; /* argument to function */ } task_queue : pointer to struct tq_struct operations DECLARE_TASK_QUEUE(name); int queue_task(struct tq_struct *task, task_queue *list); void run_task_queue(task_queue *list);

12 Task Queue How it works Each of queued tasks do its task independently They are always run when the kernel has no other pressing work to do Different queues are run at different times They are not run when the process that queued the task is executing

13 Task Queue predefined queue the scheduler queue the timer queue the immediate queue custom queue use DECLARE_TASK_QUEUE(), queue_task(), run_task_queue(); Tasklet may be run in parallel with other tasklets on SMP systems

14 Kernel Timer can specify “when” in the future your function will be called register a function once, and the kernel calls it one when the timer expires struct timer_list { struct timer_list *next; struct timer_list *prev; unsigned long expires; unsigned long data; void (*function)(unsigned long); volatile int running; }

15 Kernel Timer functions void init_timer(struct timer_list *timer); void add_timer(struct timer_list *timer); int mod_timer(struct timer_list *timer, unsigned long expires); int del_timer(struct timer_list *timer);

16 The Real Story of kmalloc powerful and easily learned prototype void *kmalloc(size_t size, int flags); void kfree(void *obj); most-used flags GFP_KERNEL, GFP_ATOMIC additional flags GFP_BUFFER, GFP_USER, GFP_HIGHUSER, __GFP_DMA, __GFP_HIGHMEM

17 The Real Story of kmalloc the size argument kernel allocate only certain predefined fixed-size of byte array minimum size depends on the page size of the architecture data size : power of 2 asking 2000 bytes  allocate 2048 bytes 2048 bytes  allocate 4096 bytes (due to the control flags; worst possible case)

18 get_free_page and Friends allocate big chunks of memory unsigned long get_zeroed_page (int flags); unsigned long __get_free_page (int flags); unsigned long __get_free_pages (int flags, unsigned long order); unsigned long __get_dma_pages (int flag, unsigned long order); void free_page (unsigned long addr); void free_pages (unsigned long addr, unsigned long order); arguments flags : same in kmalloc() order : page size of log 2 N better memory usage, speed

19 vmalloc and Friends allocate a contiguous memory region in the virtual address space void vmalloc (unsigned long size); void vfree (void *addr); void * ioremap (unsigned long offset, unsigned long size); void iounmap (void *addr); vmalloc must both retreive the memory and build the page table  has more overhead than __get_free_pages ioremap does not actually allocate any memory

20 Boot-Time Allocation allocate a huge buffer of physically contiguous memory at boot time bypassing the limit of get_free_pages void *alloc_bootmem (unsigned long size); void *alloc_bootmem_low (unsigned long size); void *alloc_bootmem_pages (unsigned long size); void *alloc_bootmem_low_page (unsigned long size); a device driver can only be installed or replaced by “rebuilding the kernel and rebooting the computer”


Download ppt "Ch6. Flow of Time Ch7. Getting Hold of Memory 2003. 1. 24 홍원의."

Similar presentations


Ads by Google