第7章-时间管理模板.ppt_第1页
第7章-时间管理模板.ppt_第2页
第7章-时间管理模板.ppt_第3页
第7章-时间管理模板.ppt_第4页
第7章-时间管理模板.ppt_第5页
已阅读5页,还剩37页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 3 2 第7章时间管理 度量时间差 延迟执行 内核定时器 度量时间差 度量时间差系统定时硬件周期性地产生中断信号 每秒产生中断的次数称为HZ HZ是一个依赖体系结构的值 定义在中或者该文件包含的某一个子平台文件中 在发布的内核源码中的缺省值在真实硬件上为50 1200 大部分平台为100或者1000 流行的x86PC缺省是1000 以前版本是100 增大HZ值可提高时间分辨率 但会增加系统负担每次当时钟中断发生时 内核内部计数器的值就增加一 这个计数器在系统启动时初始化为0 因此它代表从最近一次启动以来的时钟中断 滴答的数目 这个计数器是一个64位变量 即便在32位的体系上 称为jiffies 64 驱动开发者通常使用jiffies变量 一个unsignedlong 或者和jiffies 64是相同或者是它的最低32位 使用jiffies常常是首选 因为它更快 并且在32位架构上对jiffies 64的访问不是原子的 度量时间差 使用jiffies计数器 includeunsignedlongj stamp 1 stamp half stamp n j jiffies stamp 1 j HZ stamp half j HZ 2 stamp n j n HZ 1000 nmilliseconds 32位平台上当HZ是1000时 计数器约50天溢出一次 驱动程序应当注意这个事件 使用下面的宏定义进行时间比较是安全的 度量时间差 includeinttime after unsignedlonga unsignedlongb a比b靠后返回真inttime before unsignedlonga unsignedlongb a比b靠前返回真inttime after eq unsignedlonga unsignedlongb a比b靠后或相等返回真inttime before eq unsignedlonga unsignedlongb a比b靠前或相等返回真 度量时间差 计算时间差diff long t2 long t1 将差转化为毫秒msec diff 1000 HZ 保存当前时间 墙上时间 的结构体structtimespecxtime structtimespec kernel time ttv sec seconds longtv nsec nanoseconds xtime tv sec存放着自1970年1月1日 UTC 以来的时间 1970年1月1日称为纪元 度量时间差 当前时间与内核jiffies变量的转换 includeunsignedlongtimespec to jiffies structtimespec value voidjiffies to timespec unsignedlongjiffies structtimespec value unsignedlongtimeval to jiffies structtimeval value voidjiffies to timeval unsignedlongjiffies structtimeval value voiddo gettimeofday structtimeval tv 返回自Epoch以来的秒和微秒数 structtimespeccurrent kernel time void 返回当前时间 精度1个jiffies structtimespecxtime structtimespec kernel time ttv sec 秒 longtv nsec 纳秒 structtimevalxtime structtimeval kernel time ttv sec 秒 kernel suseconds ttv usec 微秒 1 3 2 第7章时间管理 度量时间差 延迟执行 内核定时器 延迟执行 1 长延时延迟长于一个时钟滴答忙等待while time before jiffies j1 cpu relax J1为延迟终止时间 cpu relax 与架构相关 不执行大量处理器代码 存在问题 如内核配置为不可抢战 循环在延迟期间将锁住处理器如内核配置为可抢战 处理器不会被锁住 但忙等待浪费若循环前禁止了中断 jiffies不会更新 将永远while 延迟执行 让出处理器忙等待占着处理器 增加了系统负担 下面的延迟让出处理器 while time before jiffies j1 schedule 当前进程虽然释放了CPU而不做任何事情 但它仍然在运行队列中 如果系统中只有一个可运行的进程 则又会立即运行 空闲任务得不到运行 耗电 延迟执行 超时 includelongwait event timeout wait queue head tq condition longtimeout longwait event interruptible timeout wait queue head tq condition longtimeout 调用上述函数的进程会在给定的等待队列上休眠 但是会在超时到期时返回 如果超时到期 函数返回0 而如果进程由其他事件唤醒 则会返回剩余的延迟时间 并用jiffies表示上述函数的使用需要声明等待队列头 使用另一个函数可免除此麻烦 延迟执行 includesignedlongschedule timeout signedlongtimeout timeout是用jiffies表示的延迟时间 正常时返回0schedule timeout要求调用者首先设置当前进程的状态 例如 set current state TASK INTERRUPTIBLE schedule timeout delay 状态也可设置为TASK UNINTERRUPTIBLE 延迟执行 2 短延迟有时驱动程序不但需要很短的延迟 比时钟节拍短 如1ms 而且要求延迟精度高 此时前面基于jiffies的长延迟就不能用了 如100Hz时钟 节拍间隔至少为10ms 1000Hz时钟 其节拍间隔也只能到1ms includevoidudelay unsignedlongusecs voidndelay unsignedlongnsecs voidmdelay unsignedlongmsecs udelay以忙等待方式将任务延迟指定的微秒 其他函基于此函数实现udelay的实现使用软件循环 根据引导期间计算出处理器速度及loops per jiffy变量确定循环次数 延迟执行 上述延迟基于忙等等函数 延迟过程中无法运行其他任务下列基于schedule timeout 的短延迟不涉及忙等待voidmsleep unsignedintmillisecs unsignedlongmsleep interruptible unsignedintmillisecs voidssleep unsignedintseconds 上述函数将调用进程延迟指定的时间延迟到期时返回值为0提前唤醒时返回值为原所求休眠时间的剩余毫秒数 1 3 2 第7章时间管理 度量时间差 延迟执行 内核定时器 内核定时器 内核定时器是将任务推迟到将来的某个时间执行的一种机制定时器的分类低分辨率定时器 基于周期性事件 也称经典定时器高分辨率定时器 基于时钟事件 2 6开始引进 可选配置 编译进内核前面以及下面的讨论均指低分辨率定时器 内核定时器 内核定时器的实现基于TIMER SOFTIRQ软中断 内核定时器 软中断的实现需要经过三个步骤 分配索引号 软中断号注册 安装软中断处理程序触发软中断内核定时器基于软中断TIMER SOFTIRQ 其实现也需要上述步骤第1步 分配索引号 已完成第2步 注册 安装软中断TIMER SOFTIRQ的处理程序 在何处安装 内核定时器 Linux系统初始化期间调用init timers 为软中断TIMER SOFTIRQ安装中断处理程序run timer softirq void initinit timers void interr timer cpu notify 内核定时器 软中断TIMER SOFTIRQ安装中断处理程序run timer softirq后需要触发才能运行 在何处触发 在定时器中断处理程序中触发定时器软中断TIMER SOFTIRQ 定时器中断处理程的核心函数是do timer 与update process times 定时器软中断在update process times 中触发 内核定时器 do timer do timer 负责全系统范围的 全局性的任务 更新jiffies值 处理进程统计 在多处理器系统上 会选择一个特定的CPU来执行这两个任务 而不涉及其他CPU voiddo timer unsignedlongticks jiffies 64 ticks update wall time calc global load 内核定时器 update process times 函数 Calledfromthetimerinterrupthandlertochargeoneticktothecurrent process user tickis1ifthetickisusertime 0forsystem voidupdate process times intuser tick structtask struct p current intcpu smp processor id account process tick p user tick run local timers rcu check callbacks cpu user tick printk tick perf event do pending scheduler tick run posix cpu timers p 内核定时器 account process tick使用account user time或account sys time来更新进程在用户态或核心态消耗的CPU时间 即taskstructure中的utime或stime成员 如果进程超出了Rlimit指定的CPU份额限制 那么还会每隔1秒发送SIGXCPU信号 内核定时器 run local timers触发定时器软中断TIMER SOFTIRQ 或对其进行到期操作 scheduler tick是一个用于CPU调度器的辅助函数run posix cpu timers使当前注册的POSIX定时器开始运行 内核定时器 软中断TIMER SOFTIRQ被触发后 其处理程序run timer softirq稍后即可执行 处理程序做什么事 下面先介绍定时器数据结构与定时器管理再介绍处理程序 内核定时器 定时器数据结构structtimer list structlist headentry unsignedlongexpires void function unsignedlong unsignedlongdata structtvec t base s base entry 定时器链表的入口expires 定时器到期时间 单位是jiffiesfunction 指向定时器处理函数 超时时调用base 指向一个基元素 其中的定时器按到期时间排序 内核定时器 kernel timer cstructtvec t base s unsignedlongtimer jiffies tvec root ttv1 tvec ttv2 tvec ttv3 tvec ttv4 tvec ttv5 cacheline aligned in smp typedefstructtvec s structlist headvec TVN SIZE tvec t typedefstructtvec root s structlist headvec TVR SIZE tvec root t 64 256 内核定时器 第1组256项 到期时间0 255第2组64项 第1项到期时间256 256 2 1 第2项到期时间512 256 3 1 第3项到期时间以此类推第3 4 5组与第2组类似 base 存储到期时间为0的定时器的structtime list 到期时间1 到期时间255 到期时间256 511 到期时间512 767 内核定时器 第1组中的索引项指向的数组元素 保存了稍后即将执行的各定时器的time list实例 每当遇到一个时钟中断时 内核都扫描该链表 执行所有定时器函数 并将索引位置加1 刚执行过的定时器则从数据结构中移除 下一次发生时钟中断时 将执行新的数组位置上的的定时器 并将其从数据结构中移除 同样将索引加1 依次类推 在所有项都处理后 索引值为255 将索引值回0 将第2组的第1项移至第1组的256项中 其余项依次前移 第3 4 5组亦如此 重复1的操作 内核定时器 定时器处理程序run timer softirq staticvoidrun timer softirq structsoftirq action h structtvec base base get cpu var tvec bases hrtimer run pending if time after eq jiffies base timer jiffies run timers base 内核定时器 staticinlinevoid run timers structtvec base base structtimer list timer spin lock irq Spin unlock irq base lock run timers 内核定时器 2 使用定时器定时器的使用很简单 只需创建一个定时器结构体structtime list 编写超时时执行的处理函数 然后激活即可 共3步 1 创建定时器结构体DEFINE TIMER name function expires data 静态创建一个timer list结构实例name timer list结构名称function 处理函数expires 到期时间data 传给处理函数的参数 structtimer list structlist headentry unsignedlongexpires void function unsignedlong unsignedlongdata structtvec t base s base 内核定时器 也可动态创建一个timer list结构实例structtimer listmy timer init timer structtimer list structlist headentry unsignedlongexpires void function unsignedlong unsignedlongdata structtvec t base s base 内核定时器 2 编写定时器处理函数voidmy timer function unsignedlongdata 3 激活定时器add timer 内核定时器 例1jitimer1 jitimer声明及初始化structjit data structtimer listtimer structtasklet structtlet inthi wait queue head twait unsignedlongprevjiffies unsignedchar buf intloops 内核定时器 intjit timer char buf char start off toffset intlen int eof void unused data structjit data data char buf2 buf unsignedlongj jiffies data kmalloc sizeof data GFP KERNEL if data return ENOMEM init timer fillthedataforourtimerfunction 内核定时器 data prevjiffies j data buf buf2 data loops JIT ASYNC LOOPS registerthetimer data timer data unsignedlong data data timer function jit timer fn data timer expires j tdelay parameter add timer 内核定时器 2 编写jitimer处理函数voidjit timer fn unsignedlongarg structjit data data structjit data arg unsignedlongj jiffies data buf sprintf data buf 9li 3li i 6i i s n j j data prevjiffies in interrupt 1 0 current pid smp processor id current comm if data loops data timer expires tdelay data prevjiffies j add timer 内核定时器 int initjit init void create proc read entry currentime 0 NULL jit currentime NULL create proc read entry jitbusy 0 NULL jit fn void JIT BUSY create proc read entry jitsched 0 NULL jit fn void JIT SCHED create proc read entry jitqueue 0 NUL

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论