week_7_内核参数、proc目录、initrd、循环

2019-03-14  本文已影响0人  人間失格_430b

Q:

1、列举常见的内核参数以及参数的意义
2、如何通过/proc查看top中展示的进程状态
3、分别用while、for循环检测10.0.0.1/24网段存活的IP地址
4、说明initrd的作用


A:

1、内核参数

/proc目录下


文件名为数值的目录:PID为该数字的进程的参数(只读
/proc/sys目录下:内核参数(可写)/proc/sys/net/ip_forward相当于net.ip_forward
可以通过sysctl命令查看和修改内核参数,也可以用echo输入重定向到参数文件(暂时的,当前运行内核有效
修改配置文件:/etc/sysctl.conf , /etc/syctl.d/*.conf (永久有效)

sysctl -p [sysctl.conf name]重读配置文件

常见内核参数:
net.ipv4.ip_forward:核心转发 0|1
vm.drop_caches:回收内存 0|1|2
kernel.hostname:主机名
net.ipv4.icmp_echo_ignore_all:忽略所有icmp包 0|1

2、用/proc查看top输出内容


kernel中关于proc文件系统的文档

PID
[root@localhost ~]# cut -d ' ' -f1 /proc/1/stat
1

USER
[root@localhost ~]# sed -r '9!d;9s/^[^0-9][^0-9]*//'  /proc/1/status |cut  -f1 |id -un
root

优先级
[root@localhost ~]# cut -d' ' -f18 /proc/1/stat
20

nice
[root@localhost ~]# cut -d ' ' -f19 /proc/1/stat
0

virtual memory size
[root@localhost ~]# cut -d' ' -f23 /proc/1/stat
198320128

RSS
[root@localhost ~]# cut -d' ' -f24 /proc/1/stat
1680

SHR
[root@localhost ~]# cat /proc/1/statm | cut -d' ' -f3
1041

status
[root@localhost ~]# cut -d ' ' -f3 /proc/1/stat
S

COMMAND
[root@localhost ~]# cut -d ' ' -f2 /proc/1/stat |sed 's/[()]//g'
systemd

TIME+ 得到jiffies的值

[root@localhost ~]# bash /scripts/week7-3.sh
cpu jiffies: 1377
[root@localhost ~]# cat /scripts/week7-3.sh
#!/bin/bash

cpu=0
for i in `cut -d ' ' -f14-17 /proc/1/stat`;do
    let cpu+=i
done
echo "cpu jiffies: $cpu"

%MEN

[root@localhost ~]# RES=$(grep 'VmRSS' /proc/1/status |grep -o '[0-9]\+')
[root@localhost ~]# echo $RES
6720
[root@localhost ~]# TOTAL=$(grep 'MemTotal' /proc/meminfo |grep -o '[0-9]\+')
[root@localhost ~]# echo $TOTAL
1005656
[root@localhost ~]# echo "scale=3 ;$RES / $TOTAL " | bc -l
.006

%CPU



以top进程为例,可以看到top进程PID为9060



看到top运行在CPU2上
[root@localhost ~]# cat /scripts/week7-2.sh
#!/bin/bash

Tcpu1=0
Pcpu1=0
Tcpu2=0
Pcpu2=0
for i in `grep 'cpu2' /proc/stat |cut -d ' ' -f2- `;do
    let Tcpu1+=i
done
echo "Tcpu1: $Tcpu1"

for i in `cut -d ' ' -f14-17 /proc/9060/stat`;do
    let Pcpu1+=i
done
echo "Pcpu1: $Pcpu1"

sleep 3

for i in `grep 'cpu2' /proc/stat |cut -d ' ' -f2- `;do
    let Tcpu2+=i
done
echo "Tcpu2: $Tcpu2"

for i in `cut -d ' ' -f14-17 /proc/9060/stat`;do
    let Pcpu2+=i
done
echo "Pcpu2: $Pcpu2"

echo "scala=4;( $Pcpu2 - $Pcpu1 ) / ( $Tcpu2 - $Tcpu1 )" | bc -l

运行脚本得出百分比约为0.7%和top查看一致

[root@localhost ~]# bash /scripts/week7-2.sh 
Tcpu1: 2968291
Pcpu1: 491
Tcpu2: 2968591
Pcpu2: 493
.00666666666666666666

/proc/stat中CPU行各列含义

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

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 (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: In a word, iowait stands for waiting for I/O to complete. But there
  are several problems:
  1. Cpu will not wait for I/O to complete, iowait is the time that a task is
     waiting for I/O to complete. When cpu goes into idle state for
     outstanding task io, another task will be scheduled on this CPU.
  2. In 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.
  3. The value of iowait field in /proc/stat will decrease in certain
     conditions.
  So, the iowait is not reliable by reading from /proc/stat.
- irq: servicing interrupts
- softirq: servicing softirqs
- steal: involuntary wait
- guest: running a normal guest
- guest_nice: running a niced guest
[root@localhost ~]# cat /proc/1/stat
1 (systemd) S 0 1 1 0 -1 4202752 15416 1133171 64 195 8 142 274 394 20
 0 1 0 4 198320128 1680 18446744073709551615 94362041683968 
94362043125930 140736565788864 140736565785440 140486511723651 
0 671173123 4096 1260 18446744071931751118 0 0 17 1 0 0 107 0 0 
94362045223320 94362045367864 94362049138688 140736565796764 
140736565796831 140736565796831 140736565796831 0



Table 1-3: Contents of the statm files (as of 2.6.8-rc3)
..............................................................................
 Field    Content
 size     total program size (pages)        (same as VmSize in status)
 resident size of memory portions (pages)   (same as VmRSS in status)
 shared   number of pages that are shared   (i.e. backed by a file, same
                        as RssFile+RssShmem in status)
 trs      number of pages that are 'code'   (not including libs; broken,
                            includes data segment)
 lrs      number of pages of library        (always 0 on 2.6)
 drs      number of pages of data/stack     (including libs; broken,
                            includes library text)
 dt       number of dirty pages         (always 0 on 2.6)
..............................................................................

Table 1-4: Contents of the stat files (as of 2.6.30-rc7)
..............................................................................
 Field          Content
  pid           进程ID
  tcomm         可执行文件名
  state         状态 (R is running, S is sleeping, D is sleeping in an
                uninterruptible wait, Z is zombie, T is traced or stopped)
  ppid          父进程ID
  pgrp          进程组
  sid           会话ID
  tty_nr        使用的tty
  tty_pgrp      tty的进程组
  flags         task flags
  min_flt       number of minor faults
  cmin_flt      number of minor faults with child's
  maj_flt       number of major faults
  cmaj_flt      number of major faults with child's
  utime         user mode jiffies
  stime         kernel mode jiffies
  cutime        user mode jiffies with child's
  cstime        kernel mode jiffies with child's
  priority      优先级
  nice          nice值
  num_threads   线程数
  it_real_value (obsolete, always 0)
  start_time    开始时间
  vsize         虚拟内存大(byte)
  rss           常驻内存大小(pages)
  rsslim        current limit in bytes on the rss
  start_code    address above which program text can run
  end_code      address below which program text can run
  start_stack   address of the start of the main process stack
  esp           current value of ESP
  eip           current value of EIP
  pending       bitmap of pending signals
  blocked       bitmap of blocked signals
  sigign        bitmap of ignored signals
  sigcatch      bitmap of caught signals
  0     (place holder, used to be the wchan address, use /proc/PID/wchan instead)
  0             (place holder)
  0             (place holder)
  exit_signal   signal to send to parent thread on exit
  task_cpu      which CPU the task is scheduled on
  rt_priority   realtime priority
  policy        scheduling policy (man sched_setscheduler)
  blkio_ticks   time spent waiting for block IO
  gtime         guest time of the task in jiffies
  cgtime        guest time of the task children in jiffies
  start_data    address above which program data+bss is placed
  end_data      address below which program data+bss is placed
  start_brk     address above which program heap can be expanded with brk()
  arg_start     address above which program command line is placed
  arg_end       address below which program command line is placed
  env_start     address above which program environment is placed
  env_end       address below which program environment is placed
  exit_code     the thread's exit_code in the form reported by the waitpid system call

3、分别用while、for循环检测10.0.0.1/24网段存活的IP地址

[root@localhost ~]# cat /scripts/week7-4.sh
#!/bin/bash
count=0
for i in {1..254};do
    ping -c 1 10.0.0.$i &> /dev/null
    if [ $? -eq 0 ];then
        let count+=1
    fi
done
echo "for loop : $count"

count=0
ip=1
while [[ $ip -lt 255 ]];do
    ping -c 1 10.0.0.$ip &> /dev/null
    if [ $? -eq 0 ];then
        let count+=1
    fi
    let ip+=1
done
echo "while loop : $count"

4、initrd的作用

系统启动流程:
POST(Power On Self Test) -->BIOS(boot sequence):查找第一个有引导程序(bootloader)的设备-->启动bootloader:在启动设备的主引导记录(MBR)上-->加载kernel到内存:识别硬件,加载驱动,以只读挂载根文件系统到/下-->运行/sbin/init-->设定默认运行级别-->系统初始化脚本-->关闭或启动对应级别下的服务

bootloader在MBR上的前446字节,空间有限,grub实现在kernel之前先加载一个程序grub stage1来启动stage2,再由grub stage2来加载kernel
加载kernel时,需要有硬盘的驱动程序在/lib/modules/VERSION-release下,如果kernel无法驱动还需要借助ramfs来辅助加载根文件系统

Linux初始RAM磁盘(initrd)是在系统引导过程中挂载的一个临时根文件系统,用来支持两阶段的引导过程。initrd文件中包含了各种可执行程序和驱动程序,它们可以用来挂载实际的根文件系统,然后再将这个 initrd RAM磁盘卸载,并释放内存。在很多嵌入式Linux系统中,initrd 就是最终的根文件系统。

上一篇下一篇

猜你喜欢

热点阅读