Echoshell
  • Selfservice book
  • MacBook Setup
  • Linux SystemAdmin
    • Linux useful commands
    • Analyze tools
      • Hardware overview
      • Vmstat
      • Top
      • Sar
      • ltrace
      • strace
      • Netstat/SS
      • lsof
      • iostat
      • iotop
    • Performance Tuning
      • CPU
      • Momory
      • Disk/Storage
      • Network
      • Process
  • SRE vs DevOps
    • Ansible
    • Git
    • Jenkins
    • HashiCorp-Terraform
      • Terraform VS. CloudFormation
      • Main elements
        • Provider
        • State Storage and Locking
        • Backends
      • Basic Elements
        • Install
        • Resource
        • Data
        • Output
        • Locals
      • Commands
        • terraform plan
        • terraform apply
        • terraform import
    • HashiCorp-Vault
    • HashiCorp-Consul
    • CI/CD
      • GoCD
  • Automation/Script Language
    • Shell&Bash
      • Useful links
    • Python
      • 基础
        • py2 vs py3
        • 字符编码
        • 输入和输出
      • 数据类型Data Types
        • 字符串String
        • 列表List
        • 元组Tuple
        • 字典Dict
        • 集合Set
      • 函数Function
        • 定义函数
        • 函数参数
      • 函数式编程
        • 匿名函数lamba
        • map/reduce/filter
        • 闭包
        • 装饰器
      • 类Class
        • 类和实例
        • 继承和多态
        • 类方法和静态方法
        • 定制类和魔法方法
        • 元类-陌生的 metaclass
      • 高级特性
        • 迭代器iterator
        • 生成器generator
        • 上下文管理
      • 文件和目录
        • 读写文本文件
        • os 模块
      • 进程、线程和协程
        • 进程
        • 线程
        • ThreadLocal
        • 协程
      • 异常处理Exception
      • 正则表达式Regular-Expressions
        • re 模块
      • 标准模块
        • datetime
        • argparse
      • 第三方模块Three Parts
        • Click
        • JIRA
        • Email
        • Jenkins
      • 系统相关模块System modules
        • Subprocess
        • shutil
  • Learning notes from organization
  • IT Tech Team Leader/Senior System administrator
    • Helpdesk
    • Windows Servers
      • Radius Server
      • NFS
      • Munki
      • 802.1X
    • Networks
  • Personal views and ideas from my careen path
    • Team management
    • Career path change from IT to devops
Powered by GitBook
On this page
  • Troubleshooting steps
  • free
  • Sar
  • VMstat
  • Check system swap usage thread value
  • 清理缓存

Was this helpful?

  1. Linux SystemAdmin
  2. Performance Tuning

Momory

points

  • 内存超载

  • 使用swap,整个OS性能下降

  • 缓存过多,进程结束后不释放内存

  • 缓存(cached)是把读取过的数据保存起来,重新读取时若命中(找到需要的数据)就不要去读硬盘了,若没有命中就读硬盘。其中的数据会根据读取频率进行组织,把最频繁读取的内容放在最容易找到的位置,把不再读的内容不断往后排,直至从中删除

  • 缓冲(buffers)是根据磁盘的读写设计的,把分散的写操作集中进行,减少磁盘碎片和硬盘的反复寻道,从而提高系统性能。linux有一个守护进程定期 清空缓冲内容(即写入磁盘),也可以通过sync命令手动清空缓冲。举个例子吧:我这里有一个ext2的U盘,我往里面cp一个3M的MP3,但U盘的灯 没有跳动,过了一会儿(或者手动输入sync)U盘的灯就跳动起来了。卸载设备时会清空缓冲,所以有些时候卸载一个设备时要等上几秒钟

  • 修改/etc/sysctl.conf中的vm.swappiness右边的数字可以在下次开机时调节swap使用策略。该数字范围是0~100,数字越大越倾向于使用swap。默认为60,可以改一下试试。–两者都是RAM中的数据

  • buffer是即将要被写入磁盘的,而cache是被从磁盘中读出来的

Troubleshooting steps

#overall check usage memory
free -mh
#overall check usage
sar -r 2 2
#check swap   Report swap space utilization statistics
sar -S 2  2
# Report swapping statistics
sar -W 2 2
#check memory info
cat /proc/meminfo
# clean swap 
sync
echo 3 > /proc/sys/vm/drop_caches
#check swap use thread value
sysctl -q vm.swappiness
# swap off/on
swapoff
swapon

free

free -mh

 root@shubuntu  ~  free -mh                                                                              ✔  ⚡  25  05:08:08
              total        used        free      shared  buff/cache   available
Mem:           912M        110M         92M        4.3M        708M        781M
Swap:            0B          0B          0B
    * free:读取自文件:/proc/meminfo
    * buffer存放要写回到磁盘的数据
    * cache存放从磁盘上读出的数据
    * -buffers/cache,表示一个应用程序认为系统被用掉多少内存;被程序实实在在占用的内存
    * +buffers/cache,表示一个应用程序认为系统还有多少内存;可用的内存数。

Sar

sar -r 2

 root@shubuntu  ~  sar -r 2                                                                              ✔  ⚡  26  05:34:34
Linux 4.15.0-1031-raspi2 (shubuntu) 	07/18/19 	_aarch64_	(4 CPU)

05:35:04    kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
05:35:06        95052    800684    838888     89.82    111368    485472    397660     42.58    458196    181020        48
05:35:08        94924    800556    839016     89.84    111376    485472    397660     42.58    458204    181020        40
05:35:10        94892    800524    839048     89.84    111376    485472    397660     42.58    458204    181020        40
kbmemfree:这个值和free命令中的free值基本一致,所以它不包括buffer和cache的空间.
kbmemused:这个值和free命令中的used值基本一致,所以它包括buffer和cache的空间.
%memused:物理内存使用率,这个值是kbmemused和内存总量(不包括swap)的一个百分比.
kbbuffers和kbcached:这两个值就是free命令中的buffer和cache.
kbcommit:保证当前系统所需要的内存,即为了确保不溢出而需要的内存(RAM+swap).
%commit:这个值是kbcommit与内存总量(包括swap)的一个百分比.

sar -S 2 # check swap

 root@shubuntu  ~  sar -S 2                                                                    SIGINT(2) ↵  ⚡  30  05:48:05
Linux 4.15.0-1031-raspi2 (shubuntu) 	07/18/19 	_aarch64_	(4 CPU)

05:49:55    kbswpfree kbswpused  %swpused  kbswpcad   %swpcad
05:49:57            0         0      0.00         0      0.00
05:49:59            0         0      0.00         0      0.00
05:50:01            0         0      0.00         0      0.00

sar -W 2 2

 root@shubuntu  ~  sar -W 2 2                                                                                                                                   127 ↵  ⚡  52  09:13:57
Linux 4.15.0-1031-raspi2 (shubuntu) 	07/19/19 	_aarch64_	(4 CPU)

09:25:18     pswpin/s pswpout/s
09:25:20         0.00      0.00
09:25:22         0.00      0.00
Average:         0.00      0.00

pswpout/s Total number of swap pages the system brought out per second.

pswpin/s Total number of swap pages the system brought in per second.

VMstat

 root@shubuntu  ~  vmstat 2                                                                              ✔  ⚡  28  05:45:14
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0  94892 111444 613880    0    0     0     2   10   13  0  0 100  0  0
 0  0      0  94860 111444 613880    0    0     0     0 8321   62  0  0 100  0  0
 0  0      0  94860 111444 613880    0    0     0     0 8314   50  0  0 100  0  0
 0  0      0  94860 111452 613872    0    0     0    18 8397  119  0  0 100  0  0
 0  0      0  94892 111452 613876    0    0     0     0 8315   54  0  0 100  0  0
* r:等待运行的进程数,r<5表示状态好
* b:处于非中断睡眠状态的进程数,b≈0表示状态好
* id:CPU闲置时间
* 如果r经常大于3或4,且id经常小于50,表示CPU负荷很重

Check system swap usage thread value

sysctl -q vm.swappiness

清理缓存

To free pagecache:
    echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
    echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
    echo 3 > /proc/sys/vm/drop_caches

Reference :

PreviousCPUNextDisk/Storage

Last updated 5 years ago

Was this helpful?

12. sar 找出系统瓶颈的利器 — Linux Tools Quick Tutorial
Logo