/proc/stat explained

本文详细解析了Linux内核活动的日志文件 /proc/stat 的各个部分,包括 CPU 使用情况、中断计数、上下文切换、系统启动时间、进程创建数、运行进程数和阻塞进程数等关键指标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from: http://www.linuxhowtos.org/System/procstat.htm

/proc/stat explained

Various pieces of information about kernel activity are available in the
/proc/stat file.
All of the numbers reported in this file are aggregates since the system first booted.

For a quick look, simply cat the file:

> cat /proc/stat
cpu  2255 34 2290 22625563 6290 127 456
cpu0 1132 34 1441 11311718 3675 127 438
cpu1 1123 0 849 11313845 2614 0 18
intr 114930548 113199788 3 0 5 263 0 4 [... lots more numbers ...]
ctxt 1990473
btime 1062191376
processes 2915
procs_running 1
procs_blocked 0

The very first "cpu" line aggregates the numbers in all of the other "cpuN" lines.

These numbers identify the amount of time the CPU has spent performing different kinds of work. Time units are in USER_HZ or Jiffies (typically hundredths of a second).

The meanings of the columns are as follows, from left to right:

  • user: normal processes executing in user mode
  • nice: niced processes executing in user mode
  • system: processes executing in kernel mode
  • idle: twiddling thumbs
  • iowait: waiting for I/O to complete
  • irq: servicing interrupts
  • softirq: servicing softirqs

The "intr" line gives counts of interrupts serviced since boot time, for each
of the possible system interrupts. The first column is the total of all interrupts serviced; each subsequent column is the total for that particular interrupt.



The "ctxt" line gives the total number of context switches across all CPUs.



The "btime" line gives the time at which the system booted, in seconds since
the Unix epoch.



The "processes" line gives the number of processes and threads created, which includes (but is not limited to) those created by calls to the fork() and clone() system calls.



The "procs_running" line gives the number of processes currently running on CPUs.



The "procs_blocked" line gives the number of processes currently blocked, waiting for I/O to complete.

copied from the kernel documentation of the /proc filesystem

Note: On my 2.6.18 kernel, cpu lines have 8 numeric fields, not 7. 
Wonder what that one means...

Note:
The 8th column is called steal_time. It counts the ticks spent
executing other virtual hosts (in virtualised environments like Xen)

Note2:
With Linux 2.6.24 there is 9th column for (virtual) guest systems. See man 5 proc.

### /proc/net/stat 文件详解 #### 1. ARP Cache (Address Resolution Protocol Cache) ARP 缓存用于存储 IP 地址与其对应的 MAC 地址之间的映射关系。通过这种方式,系统可以快速查找目标设备的物理地址而无需每次都发送广播请求。`arp_cache` 统计信息主要关注以下几个方面: - **Entries**: 表示当前缓存中的条目数量。 - **Destroys**: 被销毁的缓存项总数,这可能是由于超时或其他原因导致的清理操作[^1]。 此文件对于诊断网络连接问题非常有用,因为它可以帮助识别是否存在过多的 ARP 请求或响应失败的情况。 --- #### 2. NDISC Cache (Neighbor Discovery Protocol Cache) NDISC 是 IPv6 中类似于 ARP 的机制,但它不仅处理二层地址解析还支持路由器发现等功能。`ndisc_cache` 提供关于邻居表的信息,具体包括但不限于以下几点: - 类似于 ARP 缓存的作用范围扩展到了 IPv6 环境下的主机通信管理; - 反映了本地子网内活跃节点间的联系状况以及它们之间最近一次通讯时间戳等细节数据[^3]。 相比传统的 ARP 协议,在安全性上有显著提升,并且更加适合现代复杂的互联网架构需求。 --- #### 3. NF Conntrack (Netfilter Connection Tracking) NF conntrack 主要负责追踪穿过防火墙的数据包所属会话状态。这对于实施 NAT(Network Address Translation),过滤策略制定等方面至关重要。以下是几个重要指标解释: - **Expectations**: 当前正在等待匹配的新连接数目; - **Early Drops**: 因资源限制提前丢弃的连接数; - 更多详细的字段描述了不同阶段中被监控对象的行为特征及其变化趋势[^4]. 它是构建高级网络安全防护体系不可或缺的一环,允许管理员细粒度控制进出流量的同时保持高效能运转。 --- #### 4. RT Cache (Routing Cache) 路由缓存旨在加速转发决策过程,减少每次寻找最佳路径所需消耗的时间成本。尽管较新版本内核已逐步淘汰显式的 rt_cache 实现转而依赖更灵活高效的算法模型,但仍可通过某些遗留接口获取相关信息。关键参数如下所示: - **In Hits/Misses**: 分别表示命中次数与未命中的情况发生频率; - 此外还有针对特殊场景优化后的额外选项可用作进一步分析依据[^5]. 值得注意的是随着技术进步传统意义上面向固定拓扑结构设计出来的解决方案逐渐暴露出局限性从而促使业界探索新型替代品比如 FIB(Fast Information Base). --- ### 示例代码片段 下面给出一段简单的 Python 脚本来提取并打印上述提到的部分核心统计值: ```python import re def parse_proc_stat(file_path): with open(file_path, 'r') as f: content = f.read() pattern = r'(\w+)\s*:\s*(\d+)' matches = re.findall(pattern, content) result = {} for match in matches: key, value = match result[key.strip()] = int(value.strip()) return result if __name__ == "__main__": files_to_check = [ '/proc/net/stat/arp_cache', '/proc/net/stat/ndisc_cache', '/proc/net/stat/nf_conntrack', '/proc/net/stat/rt_cache' ] all_stats = {} for file_name in files_to_check: stats = parse_proc_stat(file_name) base_key = file_name.split('/')[-1].replace('_cache', '') all_stats[base_key] = stats from pprint import pprint pprint(all_stats) ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值