Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
$B;~9o$H;~4V$N4IM}(B
[go: Go Back, main page]

$B;~9o$H;~4V$N4IM}(B

					2011$BG/(B02$B7n(B22$BF|(B
$B>pJs2J3XN`(B $B%*%Z%l!<%F%#%s%0%7%9%F%`(B II

                                       $BC^GHBg3X(B $B%7%9%F%`>pJs9)3X8&5f2J(B 
                                       $B%3%s%T%e!<%?%5%$%(%s%9@l96(B, $BEE;R!&>pJs9)3X7O(B
                                       $B?7>k(B $BLw(B
                                       <yas@is.tsukuba.ac.jp>

$B$3$N%Z!<%8$O!" http://www.coins.tsukuba.ac.jp/~yas/coins/os2-2010/2011-02-22
$B$"$k$$$O!" http://www.coins.tsukuba.ac.jp/~yas/
http://www.cs.tsukuba.ac.jp/~yas/

$B"#O"Mm;v9`(B

$B;n83$K$D$$$F(B

$B"!(Bstruct timer_list

include/linux/timer.h

  12: struct timer_list {
...
  17:         struct list_head entry;
  18:         unsigned long expires;
  19:         struct tvec_base *base;
  20: 
  21:         void (*function)(unsigned long);
  22:         unsigned long data;
...
  34: };

jiffies $B$,A}2C$7$F(B expires $B$KC#$9$l$P!"(B(*function)(data) $B$r8F$V!#(B

$B

add_timer(), add_timer_on()
$B%?%$%^$NEPO?!#(B_on() $B$O!"(BCPU $B$4$H!#(B
mod_timer()
$B%?%$%^$N5/F0;~9o$NJQ99(B
del_timer(), del_timer_sync(), try_to_del_timer_sync()
$B%?%$%^$N%-%c%s%;%k!#(B del_timer_sync() $B$O!"$b$74{$K $BMxMQNc!#(B
{
    struct timer_list my_timer;           // $B9=B$BN$N@k8@(B
    init_timer(&my_timer);$B!!!!!!!!!!!!!!!!(B// $B=i4|2=(B
    my_timer.expires = jiffies + delay;   // $B$I$N$/$i$$BT$A$?$$$+(B
    my_timer.data = (unsigned long)data;  // $BEO$7$?$$%G!<%?(B
    my_timer.function = my_timer_func;    // $B4X?t(B
    add_timer(&my_timer);                 // $BEPO?(B
}

void my_timer_func(unsigned long data) {
    ...
}

$B"!(BHigh-resolution kernel timers

struct timer_list$B$N(BAPI$B$G$O!"(Btick$BC10L$@$,!"(Bstruct hrtimer $B$N(B API $B$G$O!"(B $B%J%NICC10L$G;XDj$G$-$k!#$?$@$7!"e$N@:EY$O$J$$!#(B

$B

hrtimer_init()
struct hrtimer $B9=B$BN$N=i4|2=!#(B
hrtimer_start()
$B%?%$%^$N3+;O!#%b%N%H%K%C%/;~9o(B(CLOCK_MONOTONIC)$B$+!"%+%l%s%@(B $B;~9o(B(CLOCK_REALTIME)$B$+A*$Y$k!#0z?t$O!"@dBP;XDj(B(HRTIMER_MODE_ABS)$B$+8=:_(B $B$+$i$NAjBP(B(HRTIMER_MODE_REL)$B$+;XDj$G$-$k!#(B
hrtimer_cancel()
$B%?%$%^$N%-%c%s%;%k!#(B
schedule_hrtimeout()
$B;XDj$7$?;~4V$@$18=:_ ktime_get()
$B%b%N%H%K%C%/;~9o$N ktime_get_real()
$B%+%l%s%@;~9o$N $BMxMQNc!#(B
    struct hrtimer my_timer;
    hrtimer_init(&my_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    my_timer.function = my_timer_handler;
    ...
    hrtimer_start(&my_timer,  ktime_set(0, nano), HRTIMER_MODE_REL);
    ...

enum hrtimer_restart my_timer_handler(struct hrtimer *timer)
{
        ...
        return HRTIMER_NORESTART;
}
$B%O%s%I%i$G(B HRTIMER_RESTART $B$r(B return $B$9$k$H!"%?%$%^$,:F@_Dj$5$l$k!#(B
    hrtimer_init(&my_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
    my_timer.function = func;
    ...
    hrtimer_start(&my_timer, ktime_add_ns(ktime_get(), period),
                      HRTIMER_MODE_ABS);
hrtimer $B$N4IM}$K$O!"(Bred-black tree $B$,;H$o$l$F$$$k!#(B

$B"#

$B%G%P%$%9%I%i%$%P$G$O!"CY$$%G%P%$%9$K9g$o$;$k$?$a$K$7$P$i$/BT$C$F$+$i=h(B $BM}$r$7$?$$$3$H$,B?$$!#F@$K3d$j9~$_$N8eH>It(B(bottom-half)$B$G!#(B

$BNc(B: Ethernet $B$N%I%i%$%P$G%b!<%I$rJQ99$7$F(B 2 $B%^%$%/%mIC$@$1BT$D!#(B

$BMM!9$JJ}K!$,$"$k!#(B

  • Busy loop

$B"!6u%k!<%W(B(busy loop)

$BBT$D$?$a$NC1=c$JJ}K!$O!"6u%k!<%W$r2s$k$3$H!#(B

$BNc(B1: 10 tick ($B%$%s%?!<%P%k!&%?%$%^$K$h$k3d$j9~$_(B)$B$rBT$D!#(B

unsigned long timeout = jiffies + 10; // 10 ticks
while (time_before(jiffies,timeout))
    continue;
$BNc(B1: 2$BICBT$D(B
unsigned long delay = jiffies + 2*HZ; // 2$BIC(B
while (time_before(jiffies,timeout))
    continue;

$B"!(Btime_befefore()

jiffies $B$O!"(B32 $B%S%C%H$J$N$G!"%*!<%P%U%m!<$9$k2DG=@-$,$"$k!#(B $B unsigned long timeout = jiffies + 10; // 10 ticks while (jiffies<timeout) continue; $B include/linux/jiffies.h 106: #define time_after(a,b) \ 107: (typecheck(unsigned long, a) && \ 108: typecheck(unsigned long, b) && \ 109: ((long)(b) - (long)(a) < 0)) 110: #define time_before(a,b) time_after(b,a) 111: 112: #define time_after_eq(a,b) \ 113: (typecheck(unsigned long, a) && \ 114: typecheck(unsigned long, b) && \ 115: ((long)(a) - (long)(b) >= 0)) 116: #define time_before_eq(a,b) time_after_eq(b,a)

$B"!(Bcond_resched()

$B6u%k!<%W$O!"(BCPU $B$,L5BL$K$J$k$N$GNI$/$J$$!#$=$NBe$o$j$K unsigned long delay = jiffies + 2*HZ; // 2$BIC(B while (time_before(jiffies,timeout)) cond_resched(); $BB>$Kr7o(B)$B;~$K$O!"%9%1%8%e!<%i$r8F$s(B $B$G!"$B"!>.$5$JCY1d(B tick $BC10L(B (10$B%_%jIC(B-1$B%_%jIC(B) $B$G$OBg$-$9$.$k>l9g!">.$5$JCY1d$r void ndelay(unsigned long nsecs) void udelay(unsigned long usecs) void mdelay(unsigned long msecs) udelay() $B$O!"$"$k2s?t$N%k!<%W$G

udelay() $B$G(B1$B%_%jIC0J>eBT$C$F$O$$$1$J$$!#(B $B%k!<%W$N%$%s%G%C%/%9$,%*!<%P%U%m!<$9$k2DG=@-$,$"$k!#(B

$B"!(Bschedule_timeout()

set_current_state( TASK_INTERRUPTIBLE ); // signal $B$G5/$-$k2DG=@-$,$"$k(B
schedule_timeout( s * HZ );
$Bstruct timer_list $B$,;H$o$l$F$$$k!#(B

$B"#%W%m%;%9$K$*$1$k(Btick$B$N=hM}(B

$B"!(Baccount_process_tick()

kernel/sched.c
3446:	void account_process_tick(struct task_struct *p, int user_tick)
3447:	{
3448:	        cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
3449:	        struct rq *rq = this_rq();
3450:	
3451:	        if (user_tick)
3452:	                account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
3453:	        else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
3454:	                account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy,
3455:	                                    one_jiffy_scaled);
3456:	        else
3457:	                account_idle_time(cputime_one_jiffy);
3458:	}

3320:	void account_user_time(struct task_struct *p, cputime_t cputime,
3321:	                       cputime_t cputime_scaled)
3322:	{
...
3324:	        cputime64_t tmp;
3327:	        p->utime = cputime_add(p->utime, cputime);
3328:	        p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
3329:	        account_group_user_time(p, cputime);
...
3341:	}

  • account_process_tick() $B$O!"(Btick $B$4$H$K(B tick_periodic() $B$+$i8F$P$l$k!#(B
  • $B%$%s%?!<%P%k!&%?%$%^$K$h$k3d$j9~$_$,9T$o$l$?;~(B CPU $B$,2?$r
  • $B%f!<%6!&%b!<%I$N%f!<%6!&%W%m%;%9(B
  • $B%+!<%M%k!&%b!<%I$N%f!<%6!&%W%m%;%9(B
  • $B%"%$%I%k(B
  • $B%f!<%6!&%b!<%I$N%f!<%6!&%W%m%;%9$N>l9g$O!"(Btask_struct $B$N(B utime $B%U%#!<%k%I$K(B 1 tick $BJ,$N;~4V$rB-$79~$`!#(B

    $B"#%/%$%:(B9 $B;~9o$H;~4V$N4IM}(B

    $B!zLdBj(B(901) PIT

    PIT (Programmable Interval Timer)$B$G$O!"(B $BH/?64o$N<~GH?t$O!"(B1193182Hz $B$G$"$k!#(B $B:F@_DjMQ$N%l%8%9%?$r(B 11931 $B$K@_Dj$7$?$i!"(B $B2?IC$K(B1$B2s!"3d$j9~$_$,H/@8$9$k$+!#(B

    $B!zLdBj(B(902) $B%b%N%H%K%C%/;~9o$NMxMQ(B

    $B%+!<%M%k$NCf$G!"%+%l%s%@;~9o$G$O$J$/$F%b%N%H%K%C%/;~9o$,;H$o$l$F$$$k>l(B $B=j$,$"$k!#$=$NM}M3$r4JC1$K@bL@$7$J$5$$!#$=$N>l=j$r%+%l%s%@;~9o$r;H$&$h(B $B$&$K$9$k$H!"!V$I$N$h$&$J>l9g$K!W$I$N$h$&$JITET9g$,@8$8$k$+!#(B

    $B!zLdBj(B(903) struct timer_list$B$NMxMQ(B

    $B4X?t(Bf()$B$r void h(int a,int b, int c) { .... } $B$3$l$r struct timer_list my_timer_list; int my_arg_a,my_arg_b,my_arg_c; void f(unsigned long data) { init_timer( /*$B6uMs(B(b)*/ ); my_timer.expires = /*$B6uMs(B(c)*/; my_timer.data = 0; my_timer.function = /*$B6uMs(B(d)*/; /*$B6uMs(B(e)*/; } void my_timer_func(unsigned long data) { h( my_arg_a,my_arg_b,my_arg_c ); } $B%P%0(B: 2011/02/22 my_timer $B$H(Bmy_timer_list $B$,:.:_$7$F$$$^$7$?!#$I$A$i$G(B $B$b2D$H$7$^$9!#(B
    Last updated: 2011/02/22 16:54:27
    Yasushi Shinjo / <yas@is.tsukuba.ac.jp>