< Linux Kernel > Measuring Time

本文详细介绍了内核中时间测量的原理,包括使用ticks作为时间单位,以及如何通过jiffies变量记录时间。此外,文章还阐述了如何在内核代码中比较时间,以及时间单位之间的相互转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The kernel often needs to measure how much time has passed since a given moment. For example, a routine
that carries on a CPU-intensive task often releases the CPU after a given amount of time. It will continue its
job when it is rescheduled for execution. This is especially important in kernel code, even though the kernel
supports kernel preemption. A common example in the networking code is given by the routines that

implement garbage collection.


The passing of time in kernel space is measured in ticks . A tick is the time between two consecutive
expirations of the timer interrupt. The timer takes care of different tasks (we are not interested in them here)
and regularly expires HZ times per second. HZ is a variable initialized by architecture-dependent code. For
example, it is initialized to 1,000 on i386 machines. This means that the timer interrupt expires 1,000 times
per second when Linux runs on an i386 system, and that there is one millisecond between two consecutive
expirations.


Every time the timer expires it increments the global variable called jiffies. This means that at any time,

jiffies represents the number of ticks since the system booted, and the generic value n*HZ represents n
seconds of time.


If all a function needs is to measure the passing of time, it can save the value of jiffies into a local

variable and later compare the difference between jiffies and that timestamp against a time interval
(expressed in number of ticks) to see how much time has passed since measurement started.
The following example shows a function that needs to do some kind of work but does not want to hold the
CPU for more than one tick. When do_something says the work is completed by setting job_done to a
nonzero value, the function can return:


unsigned long start_time = jiffies;

int job_done = 0;
do {
   do_something(&job_done);
   If (job_done)
      return;
while (jiffies - start_time < 1);


Comparing Times


To compare the temporal relation of events, the kernel provides several auxiliary functions that prevent
off-by-one errors if they are used instead of a home-grown comparisons (a, b, and c denote jiffie time
values for some events):

❑ timer_after(a,b) returns true if time a is after time b. time_before(a,b) will be true if time a
   is before time b, as you will have guessed.
❑ time_after_eq(a,b) works like time_after, but also returns true if both times are identical.
   time_before_eq(a,b) is the inverse variant.
❑ time_in_range(a,b,c) checks if time a is contained in the time interval denoted by [b, c]. The
   boundaries are included in the range, so a may be identical to b or c.

Using these functions ensures that wraparounds of the jiffies counter are handled correctly. As a general
rule, kernel code should therefore never compare time values directly, but always use these functions.


Time Conversion

When it comes to time intervals, jiffies might not be the unit of choice in the minds of most programmers.
It is more conventional to think in milliseconds or microseconds for short time intervals. The kernel thus
provides some auxiliary functions to convert back and forth between these units and jiffies:

<jiffies.h>
unsigned int jiffies_to_msecs(const unsigned long j);
unsigned int jiffies_to_usecs(const unsigned long j);
unsigned long msecs_to_jiffies(const unsigned int m);
unsigned long usecs_to_jiffies(const unsigned int u);

The functions are self-explanatory. However, Section 15.2.3 shows that conversion functions between
jiffies and struct timeval and struct timespec, respectively, are also available.



资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化和响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”和“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)主要负责样式设计。它首先清除了一些默认的边距和填充,对 html 和 body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
<table><tbody><tr><td style="width:35%;" rowspan="4">Temperature accuracy</td><td rowspan="2"><strong>Above -100°C</strong></td><td>J, K, T, E, and N-type: ±[0.05% + 0.3°C]<sup>1</sup></td></tr><tr><td class="nobold">R and S-type: ±[0.05% + 0.4°C]<sup>1</sup></td></tr><tr><td rowspan="2"><strong>Below -100°C</strong></td><td>J, K, E, and N-types: ±[0.20% + 0.3°C]<sup>1</sup></td></tr><tr><td class="nobold">T-type: ±[0.50% + 0.3°C]</td></tr><tr><td rowspan="6">Temperature</td><td><strong>J-type</strong></td><td>-210°C to 1200°C</td></tr><tr><td>K-type</td><td>-200°C to 1372°C</td></tr><tr><td>T-type</td><td>-250°C to 400°C</td></tr><tr><td>E-type</td><td>-150°C to 1000°C</td></tr><tr><td>N-type</td><td>-200°C to 1300°C<sup>1</sup></td></tr><tr><td>R and S-type</td><td>0°C to 1767°C<sup>1</sup></td></tr><tr><td>Temperature scale</td><td colspan="2">ITS-90</td></tr><tr><td>Applicable standards</td><td colspan="2">NIST-175</td></tr><tr><td rowspan="2">Display resolution</td><td colspan="2">0.1°C, 0.1 K < 1000</td></tr><tr><td class="nobold" colspan="2">1°C, 1 K ≥ 1000</td></tr><tr><td class="nobold small-font" colspan="3">1. Only the Fluke Models 53 II B and 54 II B thermometers are capable of measuring N, R, and S-type thermocouples.</td></tr><tr><td>Operating temperature</td><td colspan="2">-10°C to 50°C</td></tr><tr><td>Storage temperature</td><td colspan="2">-40°C to 60°C</td></tr><tr><td rowspan="2">Humidity (without condensation)</td><td colspan="2">0% to 90%; 0°C to 35°C</td></tr><tr><td class="nobold" colspan="2">0% to 70%; 0°C to 50°C</td></tr><tr><td>Overvoltage category</td><td colspan="2">CSA C22.2 No. 1010.1 1992; EN 61010 Amendments 1,2</td></tr><tr><td>Agency approvals</td><td colspan="2">CE, CSA, TÜV (pending)</td></tr><tr><td>Size (L x W x D)</td><td colspan="2">173 x 86 x 38 mm</td></tr><tr><td>Weight</td><td colspan="2">400 g</td></tr><tr><td>Batteries</td><td colspan="2">3 AA batteries; typical 1000-hour life</td></tr></tbody></table> 用python代码提取上面的table标签组成键值对字典
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值