Linux proc filesystem (procfs)
参考:/proc
/proc简介
本文着重关注/proc目录,查看其中文件并熟悉它。 /proc目录存在于所有Linux系统上,无论什么发行版或体系结构。首先,必须澄清一个误解:
就文件系统这一术语而言,/ proc目录并不是真正的文件系统,它是一个虚拟文件系统,有时也被称为进程信息伪文件系统。
procfs中包含有关进程和其他系统信息的信息。 它映射到/ proc目录并在OS启动时挂载(mount)。
切换到/proc目录,除了普通文件就是一些数字目录。数字目录代表进程,更为熟知的一种叫法是PID。
事实上,相当多Linux命令都是调用此目录中的文件来显示系统相关信息。
例如,'lsmod'与'cat /proc/modules相同,而'lspci'是'cat /proc/pci'相同。
通过更改位于此目录中的文件,您甚至可以在系统运行时读取/更改内核参数(sysctl)。
/proc目录下的文件还有个有趣的想像,就是大多数目录或文件大小都是0(除了kcore,mtrr和self)。对于大小为0的文件或目录,可以把它想象成一扇窗户。内核是一个黑盒子,为了方便我们观察里面到底发生了什么,开个窗户方便我们窥探里面的秘密。
/proc/<fileneme>
/proc/<fileneme>不代表某一具体文件,而是/proc目录下除数字目录外的文件。
/proc/meminfo
首先查看meminfo
[root@localhost proc]# more /proc/meminfo MemTotal: 4028432 kB MemFree: 3719100 kB MemAvailable: 3648008 kB Buffers: 2108 kB Cached: 87436 kB SwapCached: 0 kB Active: 90512 kB Inactive: 68592 kB Active(anon): 70192 kB Inactive(anon): 11080 kB Active(file): 20320 kB Inactive(file): 57512 kB Unevictable: 0 kB Mlocked: 0 kB SwapTotal: 4063228 kB SwapFree: 4063228 kB Dirty: 0 kB Writeback: 0 kB AnonPages: 69572 kB Mapped: 23944 kB Shmem: 11712 kB Slab: 63616 kB SReclaimable: 21248 kB SUnreclaim: 42368 kB KernelStack: 4256 kB PageTables: 3840 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 6077444 kB Committed_AS: 287024 kB VmallocTotal: 34359738367 kB VmallocUsed: 187504 kB VmallocChunk: 34359310332 kB HardwareCorrupted: 0 kB AnonHugePages: 12288 kB CmaTotal: 0 kB CmaFree: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB DirectMap4k: 94016 kB DirectMap2M: 3051520 kB DirectMap1G: 3145728 kB
/ proc / meminfo包含有关系统内存的大量信息,包括总物理内存大小,当前可用物理内存大小(以kb为单位),见输出前两行。
使用man手册查看/proc/<fileneme>帮助信息
man 5 /proc/<filename>
/proc/<fileneme>其他文件速览
- /proc/cmdline – Kernel command line information.
- /proc/console – Information about current consoles including tty.
- /proc/devices – Device drivers currently configured for the running kernel.
- /proc/dma – Info about current DMA channels.
- /proc/fb – Framebuffer devices.
- /proc/filesystems – Current filesystems supported by the kernel.
- /proc/iomem – Current system memory map for devices.
- /proc/ioports – Registered port regions for input output communication with device.
- /proc/loadavg – System load average.
- /proc/locks – Files currently locked by kernel.
- /proc/meminfo – Info about system memory (see above example).
- /proc/misc – Miscellaneous drivers registered for miscellaneous major device.
- /proc/modules – Currently loaded kernel modules.
- /proc/mounts – List of all mounts in use by system.
- /proc/partitions – Detailed info about partitions available to the system.
- /proc/pci – Information about every PCI device.
- /proc/stat – Record or various statistics kept from last reboot.
- /proc/swap – Information about swap space.
- /proc/uptime – Uptime information (in seconds).
- /proc/version – Kernel version, gcc version, and Linux distribution installed.
/proc/<numbers>
在/proc的数字目录中,可以找到一些文件和链接。这些目录的数字编号与其中运行的命令的PID相关。举个栗子,在我的系统上sshd进程的PID为1018
[root@localhost proc]# ps ax | grep sshd 1018 ? Ss 0:00 /usr/sbin/sshd -D 1278 ? Ss 0:00 sshd: root@pts/0 1520 pts/0 S+ 0:00 grep --color=auto sshd
切换到/proc/1018目录,有如下内容
[root@localhost 1018]# ls attr cmdline environ io mem ns pagemap sched stack task autogroup comm exe limits mountinfo numa_maps patch_state schedstat stat timers auxv coredump_filter fd loginuid mounts oom_adj personality sessionid statm uid_map cgroup cpuset fdinfo map_files mountstats oom_score projid_map setgroups status wchan clear_refs cwd gid_map maps net oom_score_adj root smaps syscall
随便点开一个文件看看,以status为例
[root@localhost 1018]# cat status Name: sshd Umask: 0022 State: S (sleeping) Tgid: 1018 Ngid: 0 Pid: 1018 PPid: 1 TracerPid: 0 Uid: 0 0 0 0 Gid: 0 0 0 0 FDSize: 64 Groups: VmPeak: 112816 kB VmSize: 112796 kB VmLck: 0 kB VmPin: 0 kB VmHWM: 4292 kB VmRSS: 4292 kB RssAnon: 1024 kB RssFile: 3268 kB RssShmem: 0 kB VmData: 628 kB VmStk: 132 kB VmExe: 800 kB VmLib: 12304 kB VmPTE: 232 kB VmSwap: 0 kB Threads: 1 SigQ: 0/15640 SigPnd: 0000000000000000 ShdPnd: 0000000000000000 SigBlk: 0000000000000000 SigIgn: 0000000000001000 SigCgt: 0000000180014005 CapInh: 0000000000000000 CapPrm: 0000001fffffffff CapEff: 0000001fffffffff CapBnd: 0000001fffffffff CapAmb: 0000000000000000 Seccomp: 0 Cpus_allowed: ffffffff,ffffffff,ffffffff,ffffffff Cpus_allowed_list: 0-127 Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001 Mems_allowed_list: 0 voluntary_ctxt_switches: 22 nonvoluntary_ctxt_switches: 0
文件里面内容是什么意思? 好吧,重要的部分是在顶部。 我们可以从状态文件中看到此进程属于sshd。 当前状态正在休眠,其进程ID显然是1018。 我们也可以看到谁在运行它,因为UID和GID为0,表明此进程属于root用户。
在任何数字目录中,抖有类似的文件结构。 最重要最常用的一些文件描述如下:
- cmdline – command line of the process
- environ – environmental variables
- fd – file descriptors
- limits – contains information about the limits of the process
- mounts – related information
除了普通文件外,还有一些链接文件
- cwd – a link to the current working directory of the process
- exe – link to the executable of the process
- root – link to the work directory of the process