Date

PROC(5)

/proc 是 Linux 下的一个特殊目录,目录中的文件大部分都是 0 字节(kcore 便是例外),但是如果通过 cat 命令查看则会返回大量的信息。这个目录中包含一系列以数字命名的子目录,这些数字便是当前系统中运行的进程的 PID,以及一些如 netfs 等包含某个类别数据的目录。除此之外还有一些比较零散的文件,下面简要地分析这些文件的作用

/proc/buddyinfo

此文件用于诊断内存碎片问题。通过 buddy 算法把所有空闲的内存,以 2 的幂次的形式,分成 11 个块链表,分别对应为 1、2、4、8、16、32、64、128、256、512、1024 个页块。比如下面的输出,以 DMA 行为例,可用的单页块的大小为 7*(2^0)*PAGE_SIZE,可用的双页块的大小为 7*(2^1)*PAGE_SIZE,可用的三页块的大小为 1*(2^2)*PAGE_SIZE

➜  cat /proc/buddyinfo
Node 0, zone      DMA      7      7      1      2     ...
Node 0, zone    DMA32  25521  11964   2432    831     ...
Node 0, zone   Normal   7437   3970    533    152     ...

哦,这样的计算实在是对我太残忍了。存在一个更好的方法

echo m > /proc/sysrq-trigger  # 向 /var/log/messages 中 dump 内存信息
➜  tail -f /var/log/messages  # 另一个 terminal

对应的输出部分如下

Aug  1 22:10:00 debian kernel: [ 5550.805132] Node 0 DMA: 7*4kB (UEM) 7*8kB (UEM) 1*16kB (U) 2*32kB (UE)
Aug  1 22:10:00 debian kernel: [ 5550.805149] Node 0 DMA32: 105*4kB (UEM) 3896*8kB (UEM) 2411*16kB (UEM)
Aug  1 22:10:00 debian kernel: [ 5550.805162] Node 0 Normal: 126*4kB (UEM) 3202*8kB (UEM) 791*16kB (UEM)

The DMA row references the first 16 MB on a system, the HighMem row references all memory greater than 4 GB on a system, and the Normal row references all memory in between.

/proc/cgroups

参考 man 7 cgroups

➜  cat /proc/cgroups
#subsys_name    hierarchy   num_cgroups enabled
cpuset  2   1   1
cpu 3   1   1
cpuacct 3   1   1
memory  0   1   0
devices 4   82  1
freezer 5   1   1
net_cls 6   1   1
blkio   7   1   1
perf_event  8   1   1
net_prio    6   1   1

/proc/cmdline

启动时向 Kernel 传递的参数

➜  cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-3.16.0-6-amd64 root=UUID=fb653b15-8e21-4721-925f-3757a73c42fb ro quiet

/proc/cpuinfo

如文件名所描述,返回机器的 CPU 信息

➜  cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 69
model name  : Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
stepping    : 1
microcode   : 0x17
cpu MHz     : 2505.562
cache size  : 3072 KB
physical id : 0
siblings    : 4
core id     : 0
cpu cores   : 2
apicid      : 0
initial apicid  : 0
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb xsaveopt pln pts dtherm invpcid_single tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid
bogomips    : 4788.99
clflush size    : 64
cache_alignment : 64
address sizes   : 39 bits physical, 48 bits virtual
power management:

可以通过此文件获取 CPU 个数及核心数目

# 查看物理 CPU 个数
➜  cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
1
# 查看每个物理 CPU 的核数
➜  cat /proc/cpuinfo| grep "cpu cores"| uniq
2
# 查看逻辑 CPU 个数
➜  cat /proc/cpuinfo| grep "processor"| wc -l
4

/proc/crypto

此文件列出了 Linux 内核使用的所有已安装的密码算法及详细信息

➜  cat /proc/crypto
name         : aes
driver       : aes-generic
module       : kernel
priority     : 100
refcnt       : 1
selftest     : passed
type         : cipher
blocksize    : 16
min keysize  : 16
max keysize  : 32
...

/proc/devices

包含当前配置的各种 Character devices 和 Block devices(不包含未加载的模块)

➜  cat /proc/devices
Character devices:
  1 mem
  4 /dev/vc/0
...

Block devices:
  2 fd
...

Character devices are similar to block devices, except for two basic differences:

  • Character devices do not require buffering. Block devices have a buffer available, allowing them to order requests before addressing them. This is important for devices designed to store information — such as hard drives — because the ability to order the information before writing it to the device allows it to be placed in a more efficient order.

  • Character devices send data with no preconfigured size. Block devices can send and receive information in blocks of a size configured per device.

/proc/diskstats

每块磁盘的 I/O 统计信息列表

➜  cat /proc/diskstats
254       0 vda 66794 18 862890 84850 600867 135646 6607008 408180 0 59664 474934
254       1 vda1 36 0 784 7 0 0 0 0 0 7 7
254       2 vda2 66666 18 857298 84827 600470 135646 6607008 408077 0 59630 474740

也可以通过 /sys/block/vda/stat 文件获取,/proc/diskstats 中块设备信息相较于 /sys/block/{device name}/stat 中多了三个字段(即前三个字段),它们分别是 major device numbers, minor device numbers, device name

参考文档 I/O statistics fields 可以知晓各个字段的含义

Field Quoted 解释
F1 reads completed successfully 成功完成的读请求次数
F2 reads merged 读请求的次数
F3 sectors read 读请求的扇区数总和
F4 time spent reading (ms) 读请求花费的时间总和
F5 writes completed 成功完成的写请求次数
F6 writes merged 写请求合并的次数
F7 sectors written 写请求的扇区数总和
F8 time spent writing (ms) 写请求花费的时间总和
F9 I/Os currently in progress 块设备队列中的IO请求数
F10 time spent doing I/Os (ms) 块设备队列非空时间总和
F11 weighted time spent doing I/Os (ms) 块设备队列非空时间加权总和

All fields except field 9 are cumulative since boot. Field 9 should go to zero as I/Os complete; all others only increase (unless they overflow and wrap). Yes, these are (32-bit or 64-bit) unsigned long (native word size) numbers, and on a very busy or long-lived system they may wrap. Applications should be prepared to deal with that; unless your observations are measured in large numbers of minutes or hours, they should not wrap twice before you notice them.

/proc/fb

包含 frame buffer devices 和对应的驱动

➜  cat /proc/fb
0 inteldrmfb

/proc/filesystems

包含内核当前支持的文件系统类型

➜  cat /proc/filesystems
nodev   sysfs
nodev   rootfs
nodev   ramfs
nodev   bdev
nodev   proc

被标示为 nodev 的文件系统表示不需要块设备的支持。mount 命令如果未指定文件系统类型,则会尝试此文件中未标记为 nodev 的文件系统

/proc/interrupts

系统上每个 IRQ 相关的中断号列表及发生次数

➜  cat /proc/interrupts
         CPU0       CPU1       CPU2       CPU3       
0:         28          0          0          0   IO-APIC-edge      timer
1:         10          0          0          0   IO-APIC-edge      i8042

/proc/iomem

内存地址和物理设备的地址映射

➜  cat /proc/iomem
00000000-00000fff : reserved
00001000-0009c7ff : System RAM
0009c800-0009ffff : reserved
000a0000-000bffff : PCI Bus 0000:00
000c0000-000cebff : Video ROM

/proc/ioports

包含当前用于与设备进行输入/输出通信的端口区域列表

➜  cat /proc/ioports
0000-0cf7 : PCI Bus 0000:00
  0000-001f : dma1
  0020-0021 : pic1
  0040-0043 : timer0
  0050-0053 : timer1

/proc/kcore

此文件的大小为系统已经使用的物理内存 + 4KB。另外此文件格式为 ELF core

/proc/kmsg

This file can be used instead of the syslog(2) system call to read kernel messages. A process must have superuser privi‐ leges to read this file, and only one process should read this file. This file should not be read if a syslog process is running which uses the syslog(2) system call facility to log kernel messages.

可以使用 dmsg 获取文件内容

/proc/loadavg

➜  cat /proc/loadavg
2.48 2.44 2.38 2/594 18789

前三列分别为 1/5/15 分钟内的负载。负载指在时间间隔内的平均进程数目,这些进程包含 State R 和 State D。关于 Linux 进程状态可以参考 Linux process states。第四列包含了以 / 分隔的连个数字,前者为当前运行的内核可调度实体(进程和线程);后者则表示当前系统中存在的内核调度实体。最后一列是系统最近创建的进程的 PID

/proc/locks

包含当前的文件锁信息

➜  cat /proc/locks
1: POSIX  ADVISORY  WRITE 18015 08:08:6958728 0 EOF
2: POSIX  ADVISORY  READ  1761 08:08:6819445 128 128
3: POSIX  ADVISORY  READ  1761 08:08:6817844 1073741826 1073742335
4: POSIX  ADVISORY  READ  1777 08:08:6819445 128 128
5: POSIX  ADVISORY  READ  1777 08:08:6817844 1073741826 1073742335
6: POSIX  ADVISORY  WRITE 1909 08:08:136749 0 EOF

第二列是锁的类型

  • FLOCK This is a BSD file lock created using flock(2).
  • OFDLCK This is an open file description (OFD) lock created using fcntl(2).
  • POSIX This is a POSIX byte-range lock created using fcntl(2).

第三列的值仅能为 ADVISORYMANDATORY 之一。ADVISORY 意味着上锁但并不阻止其他人访问数据,而 MANDATORY 意味着在上锁时时不允许其他人访问数据

第四列表示允许锁的持有者进行何种操作,READ 还是 WRITE

第五列则为持有锁的进程 PID

第六列为三个以冒号分隔的子字段,用于标识包含上锁文件所在文件系统的设备的 major device id, minor device id 和锁定文件的 inode 编号

第七和第八列为文件锁定区域的起始/终止偏移

/proc/meminfo

包含系统内存使用情况的统计信息

➜  cat /proc/meminfo
MemTotal:        3939472 kB
MemFree:          415132 kB
MemAvailable:     621044 kB
Buffers:            9336 kB
Cached:           598732 kB
SwapCached:        34696 kB
Active:          2458384 kB
Inactive:         866172 kB
...

字段比较多,可以通过 man 5 proc 查看

/proc/modules

系统载入的模块

➜  cat /proc/modules
bnep 17349 2 - Live 0xffffffffa0b05000
bluetooth 374236 5 bnep, Live 0xffffffffa0a8c000
...

第一列表示模块名;第二列表示此模块占用内存(字节);第三列表示此模块有多少实例被载入;第四列表示此模块所依赖的其它模块;第五列表示此模块的状态 Live/Loading/Unloading;第六列表示此模块在内核内存(kernel memory)中的偏移量

与此相关的是 lsmod 命令

➜  lsmod
Module                  Size  Used by
bnep                   17349  2
bluetooth             374236  5 bnep

/proc/mounts

自 kernel 2.4.19 引入 Mount Namespaces 后,此文件成为指向 /proc/self/mounts 的链接

➜  cat /proc/mounts
rootfs / rootfs rw 0 0
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
...

第一列表示挂载的设备;第二列表示挂载点;第三列表示文件系统的类型;第四列表示与文件系统相关的挂载属性;第五列和第六列分别和 dump(8)fsck(8) 有关

/proc/partitions

包含分区信息

➜  cat /proc/partitions
major minor  #blocks  name

   8        0  488386584 sda
   8        1   83891398 sda1
   8        2          1 sda2
   8        5  136319526 sda5
   8        6  136319526 sda6
   8        7    1325056 sda7
   8        8  130527232 sda8
  11        0    1048575 sr0

/proc/slabinfo

包含 kernel cache 信息,可参考 man 5 slabinfo

/proc/softirqs

软中断发生次数

➜  cat /proc/softirqs
                    CPU0       CPU1       CPU2       CPU3       
          HI:      71707       4953      43725       3678
       TIMER:     940999     953804    1206039    1142431
      NET_TX:        311        274        157        312
      NET_RX:       9133      10337       5852       8635
       BLOCK:      92827      17509     111643      22554
BLOCK_IOPOLL:          0          0          0          0
     TASKLET:       1489       4249        985    1430102
       SCHED:     787215     795157     791533     782256
     HRTIMER:      17491      18258      17125      17888
         RCU:     528269     536955     574611     558437

/proc/stat

内核/系统统计信息

➜  cat /proc/stat
cpu  996252 1178 76001 1265780 123345 2 3922 0 0 0
cpu0 227244 295 19094 346086 23740 0 170 0 0 0
intr 19660456 28 10 0 0 0 0 0 0 1 436 0 0 143 0 0 0 0 0 ...
ctxt 58426590
btime 1533558367
processes 6087
procs_running 4
procs_blocked 0
softirq 8905217 76639 3030381 918 31305 209848 0 1791242 2182284 47958 1534642

下面来逐行说一下每个字段的含义:

cpu  996252 1178 76001 1265780 123345 2 3922 0 0 0
cpu0 227244 295 19094 346086 23740 0 170 0 0 0

以 USER_HZ(在大多系统下是 1/100 秒)为单位的时间统计,自左至右分别为:

  • user: 处于用户态(user mode)时间
  • nice: 处于低优先级用户态(user mode with low priority)的时间
  • system: 处于内核态(system mode)的时间
  • idle: 空闲时间
  • iowait: I/O 等待时间 The CPU will not wait for I/O to complete; iowait is the time that a task is waiting for I/O to complete. When a CPU goes into idle state for outstanding task I/O, another task will be scheduled on this CPU. On a multi-core CPU, the task waiting for I/O to complete is not running on any CPU, so the iowait of each CPU is difficult to calculate.
  • irq: 硬中断处理时间
  • softirq: 软中断处理时间
  • steal: Stolen time, which is the time spent in other operating systems when running in a virtualized environment
  • guest: Time spent running a virtual CPU for guest operating systems under the control of the Linux kernel.
  • guest_nice: Time spent running a niced guest (virtual CPU for guest operating systems under the control of the Linux kernel).
intr 19660456 28 10 0 0 0 0 0 0 1 436 0 0 143 0 0 0 0 0 ...

此行包含中断发生次数,第一个列为发生的总次数,然后依次是每个中断对应的发生次数

ctxt 58426590

系统上下文切换发生次数

btime 1533558367

系统何时开机

processes 6087

自启动后发生了多少次 fork,即创建了多少进程

procs_running 4

处于 Runnable 状态的进程个数

procs_blocked 0

在等待 I/O 完成的进程个数

softirq 8905217 76639 3030381 918 31305 209848 0 1791242 2182284 47958 1534642

此行包含软中断发生次数,第一个列为发生的总次数,然后依次是每个中断对应的发生次数

/proc/swaps

包含 swap 信息

➜  cat /proc/swaps
Filename                Type          Size    Used    Priority
/dev/sda7       partition   1325052 386556  -1

/proc/sysrq-trigger

写入特殊的字符会触发 SysRq 函数,相当于 ALT-SysRq-。这里列举几个常见的

  • 立即重新启动计算机 echo "b" > /proc/sysrq-trigger

  • 立即关闭计算机 echo "o" > /proc/sysrq-trigger

  • 导出内存分配的信息(需要 /var/log/message 查看) echo "m" > /proc/sysrq-trigger

  • 导出当前CPU寄存器信息和标志位的信息 echo "p" > /proc/sysrq-trigger

  • 导出线程状态信息 echo "t" > /proc/sysrq-trigger

  • 故意让系统崩溃 echo "c" > /proc/sysrq-trigger

  • 立即重新挂载所有的文件系统 echo "s" > /proc/sysrq-trigger

  • 立即重新挂载所有的文件系统为只读 echo "u" > /proc/sysrq-trigger

/proc/uptime

第一列为系统启动时间(Sec),第二列为启动后的 CPU 空闲时间(Sec)

➜  cat /proc/uptime
6345.74 14566.28

/proc/version

系统版本信息

➜  cat /proc/version
Linux version 3.16.0-6-amd64 (debian-kernel@lists.debian.org)

另外不同系统的版本号一般唯一 /etc 下,比如 debian_verionarch_release

/proc/vmstat

包含虚拟内存统计数据

➜  /proc cat /proc/vmstat
nr_free_pages 98531
nr_alloc_batch 1744
nr_inactive_anon 186201
...

/proc/zoneinfo

memory zones info

➜  cat /proc/zoneinfo
Node 0, zone      DMA
  pages free     3909
        min      68
        low      85
        high     102
        scanned  8
        spanned  4095
        present  3995
        managed  3974
    nr_free_pages 3909
    nr_alloc_batch 17
    nr_inactive_anon 16