分享

Linux内核的proc文件系统

 wythe 2023-05-26 发布于广东

Linux内核的proc文件系统是一种虚拟文件系统,可以用来访问内核运行时信息和进程信息包括中断子系统信息、内核版本信息等。它以文件系统的方式呈现内核内部数据结构,将它们作为文件结构提供给用户空间程序。

例如

在proc文件系统中,每个进程都会在/proc目录下创建一个以进程ID命名的子目录,该目录中包含了关于该进程的各种信息,比如进程的运行状态、资源占用情况和相关的文件描述符等。此外,/proc目录下还会有其他类似的文件和目录,比如系统的硬件信息、内存使用情况等。

通过proc文件系统,用户可以方便地访问内核和进程的信息,帮助诊断和调试系统问题。

查看进程状态信息

root@xxxxxxx:/proc/1# ls -al
total 0
dr-xr-xr-x   9 root root 0 5月  25 13:56 .
dr-xr-xr-x 404 root root 0 5月  25 13:56 ..
dr-xr-xr-x   2 root root 0 5月  26 17:45 attr
-rw-r--r--   1 root root 0 5月  26 17:45 autogroup
-r--------   1 root root 0 5月  26 17:45 auxv
-r--r--r--   1 root root 0 5月  25 13:56 cgroup
--w-------   1 root root 0 5月  26 17:45 clear_refs
-r--r--r--   1 root root 0 5月  25 13:56 cmdline
-rw-r--r--   1 root root 0 5月  25 13:56 comm
-rw-r--r--   1 root root 0 5月  26 17:45 coredump_filter
-r--r--r--   1 root root 0 5月  26 17:45 cpuset
lrwxrwxrwx   1 root root 0 5月  26 17:45 cwd -> /
-r--------   1 root root 0 5月  25 13:56 environ
lrwxrwxrwx   1 root root 0 5月  25 13:56 exe -> /lib/systemd/systemd
dr-x------   2 root root 0 5月  26 17:45 fd
dr-x------   2 root root 0 5月  25 13:56 fdinfo
-rw-r--r--   1 root root 0 5月  26 17:45 gid_map
-r--------   1 root root 0 5月  26 17:45 io
-r--r--r--   1 root root 0 5月  25 13:56 limits
-rw-r--r--   1 root root 0 5月  25 13:56 loginuid
dr-x------   2 root root 0 5月  26 17:45 map_files
-r--r--r--   1 root root 0 5月  25 13:56 maps
-rw-------   1 root root 0 5月  26 17:45 mem
-r--r--r--   1 root root 0 5月  25 13:56 mountinfo
-r--r--r--   1 root root 0 5月  26 17:45 mounts
-r--------   1 root root 0 5月  26 17:45 mountstats
dr-xr-xr-x   5 root root 0 5月  25 13:56 net
dr-x--x--x   2 root root 0 5月  26 17:45 ns
-r--r--r--   1 root root 0 5月  26 17:45 numa_maps
-rw-r--r--   1 root root 0 5月  26 17:45 oom_adj
-r--r--r--   1 root root 0 5月  26 17:45 oom_score
-rw-r--r--   1 root root 0 5月  25 13:56 oom_score_adj
-r--------   1 root root 0 5月  26 17:45 pagemap
-r--------   1 root root 0 5月  26 17:45 patch_state
-r--------   1 root root 0 5月  26 17:45 personality
-rw-r--r--   1 root root 0 5月  26 17:45 projid_map
lrwxrwxrwx   1 root root 0 5月  25 13:56 root -> /
-rw-r--r--   1 root root 0 5月  26 17:45 sched
-r--r--r--   1 root root 0 5月  26 17:45 schedstat
-r--r--r--   1 root root 0 5月  25 13:56 sessionid
-rw-r--r--   1 root root 0 5月  26 17:45 setgroups
-r--r--r--   1 root root 0 5月  26 17:45 smaps
-r--r--r--   1 root root 0 5月  26 17:45 smaps_rollup
-r--------   1 root root 0 5月  26 17:45 stack
-r--r--r--   1 root root 0 5月  25 13:56 stat
-r--r--r--   1 root root 0 5月  26 17:45 statm
-r--r--r--   1 root root 0 5月  25 13:56 status
-r--------   1 root root 0 5月  26 17:45 syscall
dr-xr-xr-x   3 root root 0 5月  26 17:45 task
-r--r--r--   1 root root 0 5月  26 17:45 timers
-rw-rw-rw-   1 root root 0 5月  26 17:45 timerslack_ns
-rw-r--r--   1 root root 0 5月  26 17:45 uid_map
-r--r--r--   1 root root 0 5月  26 17:45 wchan

例如上面打开了,pid 为1进程状态信息:

    fd文件夹:描述进程打开的所有文件

root@alited:/proc/1/fd# ls -al
total 0
dr-x------ 2 root root  0 5月  26 17:45 .
dr-xr-xr-x 9 root root  0 5月  25 13:56 ..
lrwx------ 1 root root 64 5月  26 17:48 0 -> /dev/null
lrwx------ 1 root root 64 5月  26 17:48 1 -> /dev/null
lr-x------ 1 root root 64 5月  26 17:48 10 -> /proc/1/mountinfo
lr-x------ 1 root root 64 5月  26 17:48 11 -> anon_inode:inotify
lr-x------ 1 root root 64 5月  26 17:48 12 -> /proc/swaps
lrwx------ 1 root root 64 5月  26 17:48 13 -> socket:[40619]
lrwx------ 1 root root 64 5月  26 17:48 14 -> socket:[40620]
lrwx------ 1 root root 64 5月  26 17:48 15 -> socket:[40621]
lrwx------ 1 root root 64 5月  26 17:48 16 -> anon_inode:[timerfd]
lrwx------ 1 root root 64 5月  26 17:48 18 -> socket:[101793]
lrwx------ 1 root root 64 5月  26 17:48 19 -> socket:[101792]
lrwx------ 1 root root 64 5月  26 17:48 2 -> /dev/null
lrwx------ 1 root root 64 5月  26 17:48 21 -> socket:[40625]
lrwx------ 1 root root 64 5月  26 17:48 23 -> socket:[40626]
lrwx------ 1 root root 64 5月  26 17:48 24 -> socket:[40627]
lr-x------ 1 root root 64 5月  26 17:48 25 -> anon_inode:inotify
lrwx------ 1 root root 64 5月  26 17:48 26 -> socket:[40628]
lrwx------ 1 root root 64 5月  26 17:48 27 -> /run/systemd/initctl/fifo
lr-x------ 1 root root 64 5月  26 17:48 28 -> anon_inode:inotify
lrwx------ 1 root root 64 5月  26 17:48 29 -> socket:[40629]
l-wx------ 1 root root 64 5月  26 17:48 3 -> /dev/kmsg
lrwx------ 1 root root 64 5月  26 17:48 30 -> socket:[40630]
lrwx------ 1 root root 64 5月  26 17:48 31 -> socket:[40633]
lrwx------ 1 root root 64 5月  26 17:48 32 -> socket:[40634]
lrwx------ 1 root root 64 5月  26 17:48 33 -> socket:[40637]
lr-x------ 1 root root 64 5月  26 17:48 34 -> /dev/autofs
lr-x------ 1 root root 64 5月  26 17:48 35 -> pipe:[40656]
lrwx------ 1 root root 64 5月  26 17:48 36 -> socket:[40666]
lrwx------ 1 root root 64 5月  26 17:48 37 -> socket:[18573]
lrwx------ 1 root root 64 5月  26 17:48 38 -> socket:[18539]
lrwx------ 1 root root 64 5月  26 17:48 39 -> /dev/rfkill
lrwx------ 1 root root 64 5月  26 17:48 4 -> anon_inode:[eventpoll]
lr-x------ 1 root root 64 5月  26 17:48 40 -> anon_inode:inotify
lrwx------ 1 root root 64 5月  26 17:48 41 -> socket:[43237]
lrwx------ 1 root root 64 5月  26 17:48 42 -> socket:[29888]
lrwx------ 1 root root 64 5月  26 17:48 43 -> socket:[23885]
lrwx------ 1 root root 64 5月  26 17:48 44 -> socket:[23860]
lrwx------ 1 root root 64 5月  26 17:48 45 -> socket:[23861]
lrwx------ 1 root root 64 5月  26 17:48 46 -> socket:[23862]
lrwx------ 1 root root 64 5月  26 17:48 47 -> socket:[23863]
lrwx------ 1 root root 64 5月  26 17:48 48 -> socket:[23864]
lrwx------ 1 root root 64 5月  26 17:48 49 -> anon_inode:[timerfd]
lrwx------ 1 root root 64 5月  26 17:48 5 -> anon_inode:[signalfd]
lr-x------ 1 root root 64 5月  26 17:48 50 -> anon_inode:inotify
lr-x------ 1 root root 64 5月  26 17:48 51 -> anon_inode:inotify
lrwx------ 1 root root 64 5月  26 17:48 52 -> socket:[23865]
lrwx------ 1 root root 64 5月  26 17:48 53 -> socket:[23866]
lrwx------ 1 root root 64 5月  26 17:48 54 -> socket:[42136]
lrwx------ 1 root root 64 5月  26 17:48 55 -> socket:[42137]
lrwx------ 1 root root 64 5月  26 17:48 56 -> socket:[43245]
lrwx------ 1 root root 64 5月  26 17:48 57 -> socket:[37127]
lrwx------ 1 root root 64 5月  26 17:48 58 -> socket:[19717]
lrwx------ 1 root root 64 5月  26 17:48 59 -> socket:[42148]
lr-x------ 1 root root 64 5月  26 17:48 6 -> /sys/fs/cgroup/systemd
lrwx------ 1 root root 64 5月  26 17:48 61 -> socket:[40786]
lrwx------ 1 root root 64 5月  26 17:48 62 -> socket:[360829]
lrwx------ 1 root root 64 5月  26 17:48 64 -> socket:[43246]
lrwx------ 1 root root 64 5月  26 17:48 65 -> socket:[43247]
lrwx------ 1 root root 64 5月  26 17:48 66 -> socket:[40791]
lrwx------ 1 root root 64 5月  26 17:48 67 -> socket:[43248]
lrwx------ 1 root root 64 5月  26 17:48 68 -> socket:[40793]
lrwx------ 1 root root 64 5月  26 17:48 7 -> anon_inode:[timerfd]
lrwx------ 1 root root 64 5月  26 17:48 70 -> socket:[4327]
lrwx------ 1 root root 64 5月  26 17:48 72 -> socket:[40797]
lrwx------ 1 root root 64 5月  26 17:48 75 -> socket:[4328]
lrwx------ 1 root root 64 5月  26 17:48 77 -> socket:[4339]
lrwx------ 1 root root 64 5月  26 17:48 79 -> socket:[4343]
lrwx------ 1 root root 64 5月  26 17:48 8 -> socket:[40617]
lrwx------ 1 root root 64 5月  26 17:48 80 -> socket:[4344]
lrwx------ 1 root root 64 5月  26 17:48 81 -> socket:[37128]
lrwx------ 1 root root 64 5月  26 17:48 82 -> socket:[4358]
lrwx------ 1 root root 64 5月  26 17:48 9 -> anon_inode:[eventpoll]

mounts文件:可以查看进程mount文件系统以及挂载点;

status:进程基本信息gid uid等

root@xxxxxx:/proc/1# cat status 
Name:   systemd
Umask:  0000
State:  S (sleeping)
Tgid:   1
Ngid:   0
Pid:    1
PPid:   0
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 128
Groups:  
NStgid: 1
NSpid:  1
NSpgid: 1
NSsid:  1
VmPeak:   251020 kB
VmSize:   185484 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:      5960 kB
VmRSS:      5960 kB
RssAnon:            1980 kB
RssFile:            3980 kB
RssShmem:              0 kB
VmData:    18464 kB
VmStk:       132 kB
VmExe:      1408 kB
VmLib:      3712 kB
VmPTE:       124 kB
VmSwap:        0 kB
HugetlbPages:          0 kB
CoreDumping:    0
Threads:        1
SigQ:   0/126430
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 7be3c0fe28014a03
SigIgn: 0000000000001000
SigCgt: 00000001800004ec
CapInh: 0000000000000000
CapPrm: 0000003fffffffff
CapEff: 0000003fffffffff
CapBnd: 0000003fffffffff
CapAmb: 0000000000000000
NoNewPrivs:     0
Seccomp:        0
Speculation_Store_Bypass:       thread vulnerable
Cpus_allowed:   ffffffff
Cpus_allowed_list:      0-31
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,00000003
Mems_allowed_list:      0-1
voluntary_ctxt_switches:        3454
nonvoluntary_ctxt_switches:     177

可以看到 中断 IO内存 IO端口 interrupts iomem ioports

可以查看系统注册中断是用情况

xxxxx:/proc # cat interrupts
           CPU0       CPU1       CPU2       CPU3
 17:      59928      56909      50881      48328     GICv2  60 Level     /soc/aon/timer@40050000
 18:     168077     149849     161105     146858     GICv2  30 Level     arch_timer
 19:          0          0          0          0     GICv2  26 Level     arch_timer
 20:          0          0          0          3     GICv2  35 Level
 21:          0          0          0          0     GICv2  43 Level     70500000.i2c
 22:          6          0          0          0     GICv2  44 Level     70600000.i2c
 23:          0          0          0          0     GICv2  45 Level     70700000.i2c
 24:       3375          0          0          0     GICv2  46 Level     70800000.i2c
 26:          3          0          0          0     GICv2  82 Level     sprd_dma
 27:      27332          0          0          0     GICv2  78 Level     DISPC
 28:         24          0          0          0     GICv2  83 Level     gsp
 29:          0          0          0          0     GICv2  80 Level     DSI_INT0
 30:          0          0          0          0     GICv2  81 Level     DSI_INT1
 31:          0          0          0          0     GICv2  75 Level     VSP
 32:      15475          0      32995          0     GICv2  92 Level     mmc0
 33:          0          0          0          0     GICv2  89 Level     mmc1
 34:       1251          0          0          0     GICv2  87 Level     musb-hdrc.0.auto
 35:          0          0          0          0     GICv2  52 Edge      sprd_codec_dp
 36:          0          0          0          0     GICv2 100 Level     sprd-mailbox_source
 37:       1031          0          0          0     GICv2 101 Level     sprd-mailbox_target
 50:       6452          0          0          0     GICv2  71 Level     60000000.gpu, 60000000.gpu, 60000000.gpu
 51:          0          0          0          0     GICv2  74 Level     JPG
 52:          0          0          0          0     GICv2 124 Level     arm-pmu
 53:          0          0          0          0     GICv2 125 Level     arm-pmu
 54:          0          0          0          0     GICv2 126 Level     arm-pmu
 55:          0          0          0          0     GICv2 127 Level     arm-pmu
 56:        137          1          6          0  sprd-gpio  53 Edge      ANDRIOD2MH
 57:        302          1          6          0  sprd-gpio  54 Edge      MH2ANDRIOD
 58:       1375          0          0          0  sprd-gpio 144 Edge      gt9xx
 59:          0          0          1          0     GICv2  29 Edge      trusty
 60:          0          0          0          0     GICv2  84 Edge      trusty
 61:        595          0          0          0     GICv2  85 Edge      trusty
 62:          0          0          0          0     GICv2  95 Edge      trusty
 63:          0          0          0          0     GICv2  66 Level     aon-busmonitor
 64:         36          0          0          0     GICv2  70 Level     spi3.0
 65:         14          0          0          0    spi3.0   1 Level     sc27xx-rtc
 66:          0          0          0          0    spi3.0  10 Level     sc27xx-typec
 67:         21          0          1          0    spi3.0   4 Level     sc27xx-eic
 69:          0          0          0          0    spi3.0   3 Level     sc27xx-fgu
 70:          0          0          0          0  sc27xx-eic   9 Edge      sc27xx-fgu
 71:          1          0          0          0  sprd-gpio  78 Edge      20300000.sdio cd
 72:          2          0          0          0  sprd-gpio 137 Edge      extcon-gpio
 73:          6          0          0          0  sc27xx-eic   0 Edge      extcon-gpio
 74:          8          0          0          0  sc27xx-eic   1 Edge      Power Key
 75:          1          0          0          0  sprd-gpio 156 Edge      Volume Up Key
 76:          0          0          0          0  40210000.gpio   4 Edge      Volume Down Key
 77:          0          0          0          0    spi3.0   6 Edge      sprd_codec_ap
 81:          0          0          0          0  sc27xx-eic  13 Level     headset_detect
 82:          0          0          0          0  sc27xx-eic   3 Level     headset_button
IPI0:          0          0          0          0  CPU wakeup interrupts
IPI1:      24112      34988      40219      42248  Timer broadcast interrupts
IPI2:      75366      92904      78553      78071  Rescheduling interrupts
IPI3:       1367        776        594        831  Function call interrupts
IPI4:          0          0          0          0  CPU stop interrupts
IPI5:         22         17          7         19  IRQ work interrupts
IPI6:          0          0          0          0  completion interrupts

分别是,中断号,发生的CPU次数 中断一些基本信息描述;

系统的IO内存使用情况

xxxxxxxx:/proc # cat iomem
01003000-01003fff : /soc/etb@1003000
  01003000-01003fff : /soc/etb@1003000
20100000-20103fff : /soc/ap-ahb/dma-controller@20100000
20200000-20201fff : /soc/ap-ahb/usb@20200000
  20200000-20201fff : /soc/ap-ahb/usb@20200000
20300000-20300fff : /soc/ap-ahb/sdio@20300000
20600000-20600fff : /soc/ap-ahb/sdio@20600000
20a00000-20a00fff : /soc/ap-ahb/gsp@20a00000
21500000-21500fff : /soc/clock-controller@21500000
21900000-2190bfff : /soc/ap-ahb/video-codec@21900000
30000000-30000fff : /soc/pub/dmc-controller@30000000
33000000-3300ffff : /soc/aon/hwspinlock@33000000
40000000-40001fff : /soc/aon/audio-codec@40000000
40050000-40050013 : /soc/aon/timer@40050000
40050020-40050033 : /soc/aon/timer@40050020
40100000-40103fff : /soc/aon/dma-controller@40100000
40210000-4021007f : /soc/aon/gpio@40210000
40210080-4021009f : /soc/aon/gpio@40210080
402100a0-402100bf : /soc/aon/gpio@402100a0
402100c0-402100df : /soc/aon/gpio@402100c0
40240000-4024ffff : /soc/aon/efuse@40240000
40260000-4026ffff : /soc/aon/pwm@40260000
40280000-40280fff : /soc/aon/gpio@40280000
402a0000-402affff : /soc/aon/pinctrl@402a0000
402c0000-402c007f : /soc/aon/gpio@40210000
402c0080-402c009f : /soc/aon/gpio@40210080
402c00a0-402c00bf : /soc/aon/gpio@402100a0
402c00c0-402c00df : /soc/aon/gpio@402100c0
402d0000-402d0fff : /soc/clock-controller@402d0000
402f0000-402f00ff : /soc/aon/thermal@402f0000
40300000-403000ff : /soc/aon/thermal@40300000
40340000-40340fff : /soc/aon/djtag@40340000
40370000-4037007f : /soc/aon/gpio@40210000
40370080-4037009f : /soc/aon/gpio@40210080
403700a0-403700bf : /soc/aon/gpio@402100a0
403700c0-403700df : /soc/aon/gpio@402100c0
40380000-4038ffff : /soc/aon/spi@40380000
403e0000-403e0fff : /soc/aon/modem-dbg-log@40470000
40420000-40420fff : /soc/aon/modem-dbg-log@40470000
40470000-40470fff : /soc/aon/modem-dbg-log@40470000
50820000-50822fff : /soc/clock-controller@50820000
508d0000-508d00ff : /soc/aon/serial@508d0000
60000000-60003fff : 60000000.gpu
60100000-60100fff : /soc/clock-controller@60100000
60b00000-60b07fff : /soc/mm/jpeg-codec@60b00000
60e00000-60e00fff : /soc/clock-controller@60e00000
70100000-701000ff : /soc/ap-apb/serial@70100000
70500000-705000ff : /soc/ap-apb/i2c@70500000
70600000-706000ff : /soc/ap-apb/i2c@70600000
70700000-707000ff : /soc/ap-apb/i2c@70700000
70800000-708000ff : /soc/ap-apb/i2c@70800000
70a00000-70a00fff : /soc/ap-apb/spi@70a00000
70d00000-70dfffff : /soc/ap-apb/i2s@70d00000
80000000-bffbffff : System RAM
  80008000-80efffff : Kernel code
  81000000-811f4cd7 : Kernel data

可以看到,CPU使用了统一编址的,外设映射在物理地址上,例如System RAM就是物理内存System RAM映射在40370000-4037007f上,其中一组gpio寄存器映射物理地址40370000-4037007f上。

Linux的proc文件还有很多信息,可以通过Linux内核的帮助文档查看更多。

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多