java - /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,但绝对数字的差异很大。这些文件中的刻度数也不准确,或者我错了。
谢谢大家的帮助!
解决方案
推荐阅读
- c - 我是否正确实现了 Square 功能?
- powerbi - Power BI - 如何将一个数据集中具有确切时间的数据连接到另一个数据集中的时间范围
- ruby - Easy Redmine SMTP 配置错误:发送邮件时出错(SSL_connect returned=1 errno=0 state=error: certificate verify failed)
- laravel - Laravel,我收到语法错误,意外的 '$this' (T_VARIABLE)
- javascript - 可搜索输入的 SAPUI5 命名模型绑定
- javascript - 如何转换数组中已经在javascript中的嵌套对象中的对象
- openlayers - 如何在 Chrome 中获取示例的源映射
- php - 在 PHP 中转换包含表情符号的请求输入并保存在 MySQL 中
- bash - 如何使用 sed 正则表达式模式匹配
- java - 从具有不同布局的 Activity 访问布局的 TextView