首页 > 解决方案 > /proc/stat、/proc/uptime 和 /proc/[pid]/stat 中的时间是以滴答声还是秒数表示的时间是准确的还是近似的

问题描述

man proc:"/proc/stat:idle 空闲任务花费的时间。这个值应该是 USER_HZ 乘以 /proc/uptime 伪文件中的第二个条目。"

但我发现两者之间存在细微的差异。此外,按照我的想法,/proc/stat 的第一行 CPU ticks 的总和应该等于 uptime 乘以 USER_HZ 乘以内核数,并且等于 /proc/self/ 中的 start_time 字段stat 乘以进程启动时的核心数。

有一个简单的代码:

import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;

public class TestTick{
    public static void main(String[] args){
        System.out.println(calTotTicks()/12.0/calSelfStartTime());
        System.out.println(12.0*uptime()*100/calTotTicks());
        System.out.println(calIdleTicks()/100/idleTime());
        System.out.println(calSelfStartTime()/100/uptime());
    }

    private static long calTotTicks(){
        try{
            BufferedReader reader = new BufferedReader(new FileReader("/proc/stat"));
            String stat=reader.readLine();
            String[] stats = stat.split("\\s+");
            long tot = 0;
            for(int i=1;i<stats.length;++i)
                tot += Long.parseLong(stats[i]);
            return tot;
        }catch(IOException e){
            System.out.println(e);
            System.exit(1);
        }
        return 0;
    }

    private static long calIdleTicks(){
        try{
            BufferedReader reader = new BufferedReader(new FileReader("/proc/stat"));
            String[] stats = reader.readLine().split("\\s+");
            return Long.parseLong(stats[4]);
        }catch(IOException e){
            System.out.println(e);
            System.exit(1);
        }
        return 0;
    }

    private static long calSelfStartTime(){
        try{
            BufferedReader reader = new BufferedReader(new FileReader("/proc/self/stat"));
            String[] stats = reader.readLine().split("\\s+");
            return Long.parseLong(stats[21]);
        }catch(IOException e){
            System.out.println(e);
            System.exit(1);
        }
        return 0;
    }

    private static double uptime(){
        try{
            BufferedReader reader = new BufferedReader(new FileReader("/proc/uptime"));
            String stat = reader.readLine();
            String[] stats = stat.split("\\s+");
            return Double.parseDouble(stats[0]);
        }catch(IOException e){
            System.out.println(e);
            System.exit(1);
        }
        return 0.0;
    }

    private static double idleTime(){
        try{
            BufferedReader reader = new BufferedReader(new FileReader("/proc/uptime"));
            String[] stats = reader.readLine().split("\\s+");
            return Double.parseDouble(stats[1]);
        }catch(IOException e){
            System.out.println(e);
            System.exit(1);
        }
        return 0.0;
    }
}

我的电脑是Linux版本4.4.0-18362-Microsoft 12核,USER_HZ是100,运行一次的结果是

1.0018526913814474
0.9981444139842264
1.0077164731297508
0.9999877709392094

多次运行的结果各不相同,它们似乎都接近 1,但绝对数字的差异很大。这些文件中的刻度数也不准确,或者我错了。

谢谢大家的帮助!

标签: javalinux

解决方案


推荐阅读