gpt4 book ai didi

c - Jiffies - 如何计算经过的秒数?

转载 作者:太空狗 更新时间:2023-10-29 15:44:38 25 4
gpt4 key购买 nike

我有一段代码,我想以秒为单位计算时间。虽然我得到的时间是以 jiffies 为单位的,但我如何将它转换为秒?

这是我的内核代码:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/timer.h>
unsigned long js, je, tet;
int netblock_init_module(void){
js = jiffies;
printk("\n[Jiffies start Time : %lu]\nModule Started.\n", js);
return 0;
}

void netblock_cleanup_module(void) {
je = jiffies;
printk("\n[Jiffies End Time : %lu]\nModule Removed.\n", je);
tet = je - js;
printk("\nEnd Time [%lu] - Start Time [%lu]: \nTotlal elapsed Time [%lu]\n",js,je, tet);

}

module_init(netblock_init_module);
module_exit(netblock_cleanup_module);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("JIFFIES EXAMPLE");
MODULE_AUTHOR("RAHEEL");

我得到的输出是这样的:

$insmod jiffexample.ko

[Jiffies start Time : 13363583]

Module Started

$rmmod jiffexample.ko

[Jiffies End Time : 13361588]

Module Removed.

End Time 13361588 - Start Time 1336358

Total Elapsed time [1605]

现在我想以秒为单位转换时间..如何将耗时 1605 转换为秒?或者你能告诉我一秒钟有多少个 jiffies 吗?

最佳答案

对于您的用例,您可以使用:

jiffies_to_msecs 或jiffies_to_usecs


以下所有转换例程:

来自include/linux/jiffies.h

...
/*
* Convert various time units to each other:
*/
extern unsigned int jiffies_to_msecs(const unsigned long j);
extern unsigned int jiffies_to_usecs(const unsigned long j);
extern unsigned long msecs_to_jiffies(const unsigned int m);
extern unsigned long usecs_to_jiffies(const unsigned int u);
extern unsigned long timespec_to_jiffies(const struct timespec *value);
extern void jiffies_to_timespec(const unsigned long jiffies,
struct timespec *value);
extern unsigned long timeval_to_jiffies(const struct timeval *value);
extern void jiffies_to_timeval(const unsigned long jiffies,
struct timeval *value);
extern clock_t jiffies_to_clock_t(unsigned long x);
extern unsigned long clock_t_to_jiffies(unsigned long x);
extern u64 jiffies_64_to_clock_t(u64 x);
extern u64 nsec_to_clock_t(u64 x);
extern u64 nsecs_to_jiffies64(u64 n);
extern unsigned long nsecs_to_jiffies(u64 n);
...

关于c - Jiffies - 如何计算经过的秒数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10885685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com