将jiffies转换为秒

时间:2010-10-06 18:38:47

标签: linux linux-kernel

我有一段用户空间代码正在解析/ proc / PID / task / TID / stat以获取cpu使用率。我可以使用HZ来获取每秒的jiffies,但是这段代码可以移动到具有不同配置值的另一台机器。有没有办法在运行时从用户空间获取HZ的值?

4 个答案:

答案 0 :(得分:6)

除以从sysconf(_SC_CLK_TCK)获得的数字。

但是,我认为无论实际的时钟滴答如何,在Linux下这可能总是100,它总是以100的形式呈现给用户空间。

见man proc(5)。

答案 1 :(得分:2)

澄清MarkR's回答背后的数学:

sysconf(_SC_CLK_TCK)会得到jiffies per second。将jiffies除以sysconf(_SC_CLK_TCK)获得的数字,即可获得总秒数。

      jiffies                      jiffies              seconds
--------------------    =     -----------------    =    -------    =    seconds
sysconf(_SC_CLK_TCK)          (jiffies/second)             1

答案 2 :(得分:0)

“ps”命令的源包含文件<linux/param.h>以获取HZ的值。

他们还会查找编号为17的“ELF音符”来查找HZ(sysinfo.c)的值:

 //extern char** environ;

 /* for ELF executables, notes are pushed before environment and args */
 static unsigned long find_elf_note(unsigned long findme){
   unsigned long *ep = (unsigned long *)environ;
   while(*ep++);
   while(*ep){
     if(ep[0]==findme) return ep[1];
     ep+=2;
   }
   return NOTE_NOT_FOUND;
 }
 [...]
 hz = find_elf_note(17);

我必须承认这对我来说很奇怪,因为ELF笔记是在编译期间定义的一个部分。

答案 3 :(得分:0)

对于shell脚本等,请使用命令行中的getconf CLK_TCK。 Use可以使用它将该参数作为环境变量或命令行传递。

main(int argc, char **argv) { 
    unsigned long clk_tck = atol(
        getenv("CLK_TCK") || "0"
    ) || sysconf(_SC_CLK_TCK) ;
    ... /* your code */

这使用上面的sysconf,但允许您使用环境变量覆盖它,可以使用上面的命令设置它。

相关问题