初识linux内核中的jiffies计数器

84 篇文章 0 订阅
本文探讨了赫兹作为频率单位在电磁波领域的应用,Linux系统中jiffies计数器的作用与溢出问题,以及proc目录下中断信息和uptime的使用。重点讲解了如何避免jiffies溢出和time_after宏防止回绕溢出的原理。
摘要由CSDN通过智能技术生成

1.度量时间差

 


赫兹是什么意思

赫兹是国际的频率单位,是电磁波的性质,是德国物理学家‘海因里希·鲁道夫·赫兹’,是他首先证实了电磁波的存在。

已经发布的linux系统一般是50~1200,常见的HZ值默认是1000,软件仿真器默认是24

 

jiff是瞬间的意思

使用jiffies计数器获取当前时间

#include <linux/jiffies.h>

unsigned long j, stamp_1, stamp_half, stamp_n;

 

j = jiffies; /* read the current value */ 
stamp_1 = j + HZ; /* 1 秒 */ 

stamp_half = j + HZ/2; /* 半秒 */

stamp_n = j + n * HZ / 1000; /* n 毫秒 */

jiffies 在linux源码中的位置

vim /usr/include/linux/param.h
 

/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef _LINUX_PARAM_H
#define _LINUX_PARAM_H

#include <asm/param.h>

#endif

vim /usr/include/i386-linux-gnu/asm/param.h
 

#include <asm-generic/param.h>

vim /usr/include/asm-generic/param.h

 

/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef __ASM_GENERIC_PARAM_H
#define __ASM_GENERIC_PARAM_H

#ifndef HZ
#define HZ 100
#endif

#ifndef EXEC_PAGESIZE
#define EXEC_PAGESIZE   4096
#endif

#ifndef NOGROUP
#define NOGROUP         (-1)
#endif

#define MAXHOSTNAMELEN  64      /* max length of hostname */


#endif /* __ASM_GENERIC_PARAM_H */

2.jiffies溢出

 


内核时钟中断发生的时候,内核内部的计数器就会加一,这个值会在系统引导时候初始化为0,这个计数器是一个64位的变量jiffies_64,我们一般会访问jiffies要不他是和jiffies_64相同,
要不他是jiffies_64的低32位,因此它的访问很快,所以我们不需要考虑它的原子性,从而jiffies_64并不是在所有平台都是原子的。

回到一个问题,如何避免jiffies溢出?
 

jiffies溢出造成程序逻辑出错,出自百度

unsigned long timeout = jiffies + HZ/2; /* timeout in 0.5s */
 
/* do some work ... */
do_somework();
 
/* then see whether we took too long */
if (timeout > jiffies) {
/* we did not time out, call no_timeout_handler() ... */
no_timeout_handler();
 
} else {
/* we timed out, call timeout_handler() ... */
timeout_handler();
}

为什么jiffies的time_after宏定义可以防止jiffies溢出?

 

time_after防止回绕原理

注意一个细节

time_after(jiffies, timeout) ((long)(timeout) - (long)(jiffies) < 0)

这可能出现两种情况,比如说用一个char字节看

第一种情况是正常情况都在char内,最大是127

timeout 是 124 但是jiffies 是 125 这种情况没有溢出,可以很容易看出是否超时

第二种情况

jiffies 溢出了 比如是127 + 1变成-128了,这时候你要+3 后超时,其实是 -128 + 3 + 128 这时候是没有超时的

所以说time_after可以防止回绕溢出

 

3.proc下面的特殊文件

 

cat /proc/interrupts 

读取到的内容从左到右,分别为:1、逻辑中断号,2、中断在各CPU发生的次数,3、中断所属设备类名称,4、硬件中断号,5、中断处理函数。

 

zhanglei@zhanglei-Latitude-5400:~$ cat /proc/interrupts 
            CPU0       CPU1       CPU2       CPU3       CPU4       CPU5       CPU6       CPU7       
   0:          8          0          0          0          0          0          0          0  IR-IO-APIC    2-edge      timer
   1:          0          0          0          0          0      13701          0          0  IR-IO-APIC    1-edge      i8042
   8:          0          0          0          0          0          0          0          0  IR-IO-APIC    8-edge      rtc0
   9:          0      46418          0          0          0          0          0          0  IR-IO-APIC    9-fasteoi   acpi
  12:          0          0          0          0        139          0          0          0  IR-IO-APIC   12-edge      i8042
  14:          0          0          0          0          0          0          0          0  IR-IO-APIC   14-fasteoi   INT34BB:00
  16:          0          0          3          0          0          0          0          0  IR-IO-APIC   16-fasteoi   i801_smbus, idma64.0, i2c_designware.0
  17:          0          0          0          0          0     517956          0          0  IR-IO-APIC   17-fasteoi   idma64.1, i2c_designware.1
  32:          0          0          0          0          0          0          0          0  IR-IO-APIC   32-fasteoi   i2c_designware.2
  51:          0      27858          0          0          0          0          0          0  IR-IO-APIC   51-fasteoi   DELL08B8:00
  67:          0          0          0          0          0          0          0          0  IR-IO-APIC   67-edge      smo8800
 120:          0          0          0          0          0          0          0          0  DMAR-MSI    0-edge      dmar0
 121:          0          0          0          0          0          0          0          0  DMAR-MSI    1-edge      dmar1
 122:          0          0          0          0          0          0          0          0  IR-PCI-MSI 458752-edge      PCIe PME, aerdrv, pcie-dpc
 123:          0          0          0          0          0          0          0          0  IR-PCI-MSI 475136-edge      PCIe PME, aerdrv, pcie-dpc
 124:          0          8          0          0          0          0          0          0  IR-PCI-MSI 524288-edge      rtsx_pci
 125:          0          0          0          0          0          0          0          0  IR-PCI-MSI 376832-edge      ahci[0000:00:17.0]
 126:          0         45         95        691         11         55        780        425  IR-PCI-MSI 520192-edge      eno2
 127:          0          0          0          0         21          0       5572          0  IR-PCI-MSI 327680-edge      xhci_hcd
 128:          0          0          0          0          0          0          0         22  IR-PCI-MSI 1048576-edge      nvme0q0
 129:      13960          0          0          0          0          0          0          0  IR-PCI-MSI 1048577-edge      nvme0q1
 130:          0      12471          0          0          0          0          0          0  IR-PCI-MSI 1048578-edge      nvme0q2
 131:          0          0      13084          0          0          0          0          0  IR-PCI-MSI 1048579-edge      nvme0q3
 132:          0          0          0      10950          0          0          0          0  IR-PCI-MSI 1048580-edge      nvme0q4
 133:          0          0          0          0      11695          0          0          0  IR-PCI-MSI 1048581-edge      nvme0q5
 134:          0          0          0          0          0      11573          0          0  IR-PCI-MSI 1048582-edge      nvme0q6
 135:          0          0          0          0          0          0      10825          0  IR-PCI-MSI 1048583-edge      nvme0q7
 136:          0          0          0          0          0          0          0      12227  IR-PCI-MSI 1048584-edge      nvme0q8
 137:          0          0          0          0         42          0          0          0  IR-PCI-MSI 360448-edge      mei_me
 138:          0        909          0      20773          0       3576       1664       5158  IR-PCI-MSI 333824-edge      iwlwifi: default queue
 139:          0          0        676       2446          0        161       1035        242  IR-PCI-MSI 333825-edge      iwlwifi: queue 1
 140:          0        636         59        627          0        803        702       1554  IR-PCI-MSI 333826-edge      iwlwifi: queue 2
 141:          2         17        330        434         33        127       1018      11794  IR-PCI-MSI 333827-edge      iwlwifi: queue 3
 142:          0        263       2200       1242          0          1         70        455  IR-PCI-MSI 333828-edge      iwlwifi: queue 4
 143:          0          0         12          0         23          0          0          0  IR-PCI-MSI 333829-edge      iwlwifi: exception
 144:     700127          0          0          0          0          0        606          0  IR-PCI-MSI 32768-edge      i915
 145:          0          0          0          0          0          0          0       2288  IR-PCI-MSI 514048-edge      snd_hda_intel:card0
 NMI:         26         27         26         26         28         26         26         26   Non-maskable interrupts
 LOC:    1502282    1393407    1336895    1322739    1294190    1292473    1239581    1225163   Local timer interrupts
 SPU:          0          0          0          0          0          0          0          0   Spurious interrupts
 PMI:         26         27         26         26         28         26         26         26   Performance monitoring interrupts
 IWI:     294865          2          0          2          1          0        108          1   IRQ work interrupts
 RTR:          0          0          0          0          0          0          0          0   APIC ICR read retries
 RES:      12893      15266      10011      10953      11209      11661       9480       9732   Rescheduling interrupts
 CAL:     122780     136066     123312      99824     147351     128111     102245     120520   Function call interrupts
 TLB:     137752     173940     160424     119748     185417     152165     147724     157154   TLB shootdowns
 TRM:          0          0          0          0          0          0          0          0   Thermal event interrupts
 THR:          0          0          0          0          0          0          0          0   Threshold APIC interrupts
 DFR:          0          0          0          0          0          0          0          0   Deferred Error APIC interrupts
 MCE:          0          0          0          0          0          0          0          0   Machine check exceptions
 MCP:         14         15         15         15         15         15         15         15   Machine check polls
 ERR:          0
 MIS:          0
 PIN:          0          0          0          0          0          0          0          0   Posted-interrupt notification event
 NPI:          0          0          0          0          0          0          0          0   Nested posted-interrupt event
 PIW:          0          0          0          0          0          0          0          0   Posted-interrupt wakeup event
zhanglei@zhanglei-Latitude-5400:~$ 


cat /proc/uptime可以查看系统运行时间

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值