jiffies 的使用
jiffies 的使用
每一个技术点都是要靠自己对着书来一步步实践
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/uaccess.h> /* copy_*_user */
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/jiffies.h>static void __exit hello_exit(void)
{printk("%d\n", jiffies);return;
}static int __init hello_init(void)
{dev_t dev = 0;return 0;}module_init(hello_init);
module_exit(hello_exit);
调用dmesg查看结果,注意这里获取都是32位的
[ 1724.354757] 355881
[ 1751.993783] 362791
[ 1753.189108] 363090
[ 1754.108915] 363320
下面我继续看64位的获取方法
get_jiffies_64();
输出的结果是:
[ 591.756554] timer:4295039905
防止溢出的回环处理函数
#include <linux/jiffies.h>
int time_after(unsigned long a, unsigned long b);
int time_before(unsigned long a, unsigned long b);
int time_after_eq(unsigned long a, unsigned long b);
int time_before_eq(unsigned long a, unsigned long b);
jiffies 和 timeval 以及 timespace 的转化,timeval使用的是秒和毫秒,timespace使用的是秒和纳秒,内核提供了4个辅助函数
struct timespec {__kernel_time_t tv_sec; /* seconds */long tv_nsec; /* nanoseconds */
};
#endifstruct timeval {__kernel_time_t tv_sec; /* seconds */__kernel_suseconds_t tv_usec; /* microseconds */
};
书上写的 jiffiestotimeval 好像不能用了,然后我看到编译的细节是头文件的目录在/usr/src/linux-headers-5.8.0-44-generic/include/linux/time.h ,linux 内核5.8以后没有这个了,所以我看了下头文件内容
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _LINUX_TIME_H
#define _LINUX_TIME_H# include <linux/cache.h>
# include <linux/seqlock.h>
# include <linux/math64.h>
# include <linux/time64.h>extern struct timezone sys_tz;int get_timespec64(struct timespec64 *ts,const struct __kernel_timespec __user *uts);
int put_timespec64(const struct timespec64 *ts,struct __kernel_timespec __user *uts);
int get_itimerspec64(struct itimerspec64 *it,const struct __kernel_itimerspec __user *uit);
int put_itimerspec64(const struct itimerspec64 *it,struct __kernel_itimerspec __user *uit);extern time64_t mktime64(const unsigned int year, const unsigned int mon,const unsigned int day, const unsigned int hour,const unsigned int min, const unsigned int sec);/* Some architectures do not supply their own clocksource.* This is mainly the case in architectures that get their* inter-tick times by reading the counter on their interval* timer. Since these timers wrap every tick, they're not really* useful as clocksources. Wrapping them to act like one is possible* but not very efficient. So we provide a callout these arches* can implement for use with the jiffies clocksource to provide* finer then tick granular time.*/
#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
extern u32 (*arch_gettimeoffset)(void);
#endif#ifdef CONFIG_POSIX_TIMERS
extern void clear_itimer(void);
#else
static inline void clear_itimer(void) {}
#endifextern long do_utimes(int dfd, const char __user *filename, struct timespec64 *times, int flags);/** Similar to the struct tm in userspace <time.h>, but it needs to be here so* that the kernel source is self contained.*/
struct tm {/** the number of seconds after the minute, normally in the range* 0 to 59, but can be up to 60 to allow for leap seconds*/int tm_sec;/* the number of minutes after the hour, in the range 0 to 59*/int tm_min;/* the number of hours past midnight, in the range 0 to 23 */int tm_hour;/* the day of the month, in the range 1 to 31 */int tm_mday;/* the number of months since January, in the range 0 to 11 */int tm_mon;/* the number of years since 1900 */long tm_year;/* the number of days since Sunday, in the range 0 to 6 */int tm_wday;/* the number of days since January 1, in the range 0 to 365 */int tm_yday;
};void time64_to_tm(time64_t totalsecs, int offset, struct tm *result);# include <linux/time32.h>static inline bool itimerspec64_valid(const struct itimerspec64 *its)
{if (!timespec64_valid(&(its->it_interval)) ||!timespec64_valid(&(its->it_value)))return false;return true;
}/*** time_after32 - compare two 32-bit relative times* @a: the time which may be after @b* @b: the time which may be before @a** time_after32(a, b) returns true if the time @a is after time @b.* time_before32(b, a) returns true if the time @b is before time @a.** Similar to time_after(), compare two 32-bit timestamps for relative* times. This is useful for comparing 32-bit seconds values that can't* be converted to 64-bit values (e.g. due to disk format or wire protocol* issues) when it is known that the times are less than 68 years apart.*/
#define time_after32(a, b) ((s32)((u32)(b) - (u32)(a)) < 0)
#define time_before32(b, a) time_after32(a, b)/*** time_between32 - check if a 32-bit timestamp is within a given time range* @t: the time which may be within [l,h]* @l: the lower bound of the range* @h: the higher bound of the range** time_before32(t, l, h) returns true if @l <= @t <= @h. All operands are* treated as 32-bit integers.** Equivalent to !(time_before32(@t, @l) || time_after32(@t, @h)).*/
#define time_between32(t, l, h) ((u32)(h) - (u32)(l) >= (u32)(t) - (u32)(l))# include <vdso/time.h>#endif
处理特定寄存器
很多场景我们需要很高的精度,而又忽略可移植性的不可预测。
为了解决这一个问题cpu通过计算时钟周期而度量时间差的简单可靠做法。绝大多数都包含一个随着时钟周期而不断递增的计数寄存器,这个时钟计数器是完成分辨率计时任务的唯一可靠途径。
无论这个计数寄存器是否为0我们都不应该去重置他
最有名气的寄存器是时间戳计数器(TSC)。
包含头文件
两个重要的函数
rdtsc(low32, high32)
rdtscl(low32);
rdtscll(var64);
在内核头文件中还有一个与体系结构无关的函数可以替代rdtsc 多数时候用低32就够了,但是1-GHZ的处理器,每4.2秒就会溢出,因此所有平台都平台提供了
#include <linux/timex.h>
cycles_t get_cycloes(void);static int __init hello_init(void)
{printk("timer:%lld\n",get_cycles());return 0;}
结果:
[ 591.756554] timer:4295039905
[11211.385908] timer:26952505672285
获取当前执行时间
jiffies 的使用相关推荐
- linux上的定时器上的jiffies,linux定时器和Jiffies汇.doc
linux定时器和Jiffies汇 1.linux HZ Linux核心几个重要跟时间有关的名词或变数,将介绍HZ.tick与jiffies. HZ Linux核心每隔固定周期会发出timer int ...
- 在linux内核中获得比jiffies精度更高的时间值【转】
转自:http://blog.chinaunix.net/uid-20672257-id-2831219.html 内核一般通过jiffies值来获取当前时间.尽管该数值表示的是自上次系统启动到当前的 ...
- 对 jiffies 溢出、回绕及 time_after 宏的理解
原文如下: 关于jiffies变量: 全局变量jiffies用来记录自启动以来产生的节拍的总数.系统启动时会将该变量初始化为0,此后,每当时钟中断产生时就会增加该变量的值.jiffies和另 ...
- 内核变量——Jiffies
全局变量jiffies表示自系统启动以来系统产生的嘀嗒数.当启动时,内核初始化该变量为0.每次时钟中断就会增1,所以系统运行时候可以计算为:jiffies/HZ秒. jiffies变量定义如下: ex ...
- linux HZ Tick Jiffies
1.linux HZ Linux核心几个重要跟时间有关的名词或变数,底下将介绍HZ.tick与jiffies. HZ Linux核心每隔固定周期会发出timer interrupt (IRQ 0),H ...
- linux 内核获取时间,Linux内核中的jiffies 以及时间的获取time
硬件给内核提供一个系统定时器用以计算和管理时间,内核通过编程预设系统定时器的频率,即节拍率(tick rate),每一个周期称作一个tick(节拍).Linux内核从2.5版内核开始把频率从100调高 ...
- socket read time out解决方法_time_after方法对jiffies回绕问题的解决
前言: 最近在啃< Linux内核设计与实现>,看到第四章CFS时候,读了几遍没太理清这一小节到思路,看到40页这么一句话:"如果这里所讨论的定时器节拍对你来说很陌生,快先去看看 ...
- 度量时间差和jiffies计数器
HZ 1.内核通过定时器中断来跟踪时间流 2.时钟中断由系统定时硬件以周期性的间隔产生,这个间隔由内核根据HZ的值设定,HZ是一个与体系结构有关的常数,定义在<linux/param.h> ...
- linux上的定时器上的jiffies,Linux kernel -- 定时器/jiffies
0. 测试环境 Linux 2.6.39 AT91SAM9G45 1. 定时器简单的测试例子 #include #include MODULE_LICENSE("GPL"); st ...
- jiffies回绕问题
jiffies变量 全局变量jiffies用来记录自启动以来产生的节拍的总数.系统启动时会将该变量初始化为0,此后,每当时钟中断产生时就会增加该变量的值.jiffies和另外一个变量息息相关:HZ.H ...
最新文章
- 【pandas学习笔记】DataFrame
- 量子纠缠buff加持,雷达精度提高500倍,论文已登物理顶刊
- 四、Go语言复合数据类型(上)
- 栈的应用实例——计算后缀表达式
- 信息时代把数据当成了信息,互联网让数据真正发挥出价值,让人们相信人眼看不见的数据世界。...
- 仓库无证如何处罚_“非现场执法”查处无证网约车,罚款15万!滴哥:怎们罚的都不知道!...
- 解决idea启动项目报错:Unable to open debugger port(127.0.0.1:60157):java.net.SocketExceptionsocket closed
- 前端学习(3066):vue+element今日头条管理-频道筛选
- Twitter Storm安装配置(Ubuntu系统)单机版
- Echo:新生好看的一言网站源码
- Filter使用详解
- Java之序列化和反序列化
- mysql 协议还原_mysql备份还原方案xtrabackup
- 【HDU - 4342】History repeat itself(数学)
- 某处发现百分百恢复覆盖的分区数据恢复方法(掌握)
- 为什么恢复后的文件打不开?U盘数据恢复常见问题
- windows 下杀手tomcat 进程
- 【Linux 4】定时任务调度与进程服务管理
- hdu 3966 树链剖分
- 金山Wps珠海实习杂记(一)
热门文章
- 数据弹性的隐形的翅膀
- ReportEvent的用法
- 电信大型服务器机房_中国电信西二旗机房
- linux sssd加入AD域 Key table file ‘/etc/krb5.keytab‘ not found
- windows目录下文件详解
- Nacos 安装部署
- TechSmith Camtasia Studio 9录屏软件提示video codec open failed 错误的解决方案
- C++小病毒(通用加强版)
- HTML+CSS好看的小黄人网页制作(人物介绍部分,附全部代码)
- Linux中的存储设备管理(设备识别,挂载,分区,磁盘配额)