본문 바로가기
프로그램.../프로....Kernel

[Timer] hrtimer

by 크크다스 2015. 4. 13.
반응형

= 참고

. http://decdream.tistory.com/409

. http://makingdream.tistory.com/20

. https://gist.github.com/maggocnx/5946907


= Kernel 기본 단위

. HZ를 기본 단위로 하는 Jiffies처럼 HZ가 기본 Resolution임

. HZ 단위 예

10msec=> 1초 == 100HZ

Linux Kernel 2.4

ARM Linux Kernel 2.6

4msec=> 1초 == 250HZ

Linux Kernel 2.6.13

1msec=> 1초 == 1000HZ

Linux Kernel 2.6

사용자 설정(CONFIG_NO_HZ)

Linux Kernel 2.6.21 ~


= HZ이하 Resolution Timer => hrtimer

. 확인 방법 : cat /proc/timer_list
Timer List Version: v0.5
HRTIMER_MAX_CLOCK_BASES: 2
now at 4852326844575716 nsecs

cpu: 0
 clock 0:
  .base:       00000000
  .index:      0
  .resolution: 1 nsecs
  .get_time:   ktime_get_real
  .offset:     1424078155474551175 nsecs
active timers:
 clock 1:
  .base:       00000000
  .index:      1
  .resolution: 1 nsecs
  .get_time:   ktime_get
  .offset:     0 nsecs

= timertest.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
 
unsigned long timer_interval_ns = 1e6;
static struct hrtimer hr_timer;
 
enum hrtimer_restart timer_callback( struct hrtimer *timer_for_restart )
{
ktime_t currtime , interval;
currtime = ktime_get();
interval = ktime_set(0,timer_interval_ns);
hrtimer_forward(timer_for_restart, currtime , interval);
// set_pin_value(PIO_G,9,(cnt++ & 1)); //Toggle LED
return HRTIMER_RESTART;
}
 
static int __init timer_init(void) {
ktime = ktime_set( 0, timer_interval_ns );
hrtimer_init( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
hr_timer.function = &timer_callback;
hrtimer_start( &hr_timer, ktime, HRTIMER_MODE_REL );
return 0;
}
 
static void __exit timer_exit(void) {
int ret;
ret = hrtimer_cancel( &hr_timer );
if (ret) printk("The timer was still in use...\n");
printk("HR Timer module uninstalling\n");
}
 
module_init(timer_init);
module_exit(timer_exit);
반응형