当前位置:首页 > > 嵌入式微处理器
[导读]本文介绍了Linux查看资源使用情况和性能调优常用的命令,包括top、htop、ps、free、vmstat、iostat、netstat、df和du命令。

目录

  • 前言

  • top命令

  • htop命令

  • ps命令

  • free命令

  • vmstat命令

  • iosat命令

  • netstat命令

  • df命令

  • du命令

前言

一般来说,Linux性能调优涉及CPU、内存和 IO,包括进程、线程、程序堆栈、内存、Swap、CPU调度、内存调度、网络连接和IO读写等。

本文介绍了Linux查看资源使用情况和性能调优常用的命令,包括tophtoppsfreevmstatiostatnetstatdfdu命令。

top命令

top命令用来监控系统的资源使用情况,包括CPU、内存、Swap、进程和线程等。

命令:

# 运行top
top

# 显示进程的完整命令
top -c

推荐使用下面的htop命令来代替top命令。

htop命令

htop命令用来监控系统的资源使用情况,包括CPU、内存、Swap、进程和线程等。相比top命令,htop命令更能更强大,显示也更加直观。

命令:

# 运行htop
htop

概要信息说明:

  • CPU使用率:数字1、2、...表示CPU有几个核,后面的进度条用不同颜色来表示不同维度的CPU使用率,最后是CPU使用率;
  • 内存使用率:Mem后面的进度条用不同颜色来表示不同维度的内存使用率(绿色:used,蓝色:buffers,橙色:cache),最后是内存使用率(used / total);
  • Swap使用率:Swap后面的进度条显示已用的Swap,最后是Swap使用率(used / total),如果系统关闭了Swap,则进度条为空,Swap使用率为0K/0K;
  • 进程和线程统计:Tasks:进程数,thr:线程数,running:正在运行的进程数;
  • 系统平均负载统计:Load average:前1分钟平均负载、前5分钟平均负载和前15分钟平均负载;负载越低说明CPU越空闲,负载越高说明CPU越忙:
    • 负载低于0.7 * CPU核数:CPU使用率正常;(道路通畅)
    • 负载高于0.7 * CPU核数:CPU使用率较高,需要关注;(道路堵车,需要交警到场疏导交通)
    • 负载高于1 * CPU核数:CPU使用率过高,需要关注;(道路严重堵车,可能发生了交通事故,需要交警马上到现场处理)
    • 负载高于5 * CPU核数:系统超负荷运转,无法正常响应;(交通瘫痪,可能道路已经被水淹无法通行,需要投入抢险力量恢复交通)
  • 系统已连续运行时间:Uptime后面为系统从上次启动后,已连续运行时间,可以倒推系统上次启动时间;

详细信息说明:

  • PID:进程ID;
  • USER:进程所有者;
  • PRI:进程优先级;
  • NI:nice值,负值表示高优先级,正值表示低优先级;
  • VIRT:进程使用的虚拟内存(virtual memory);
  • RES:进程使用的物理内存(physical memory);
  • SHR:进程使用的共享内存(shared memory);
  • S:进程状态,R(Running)正在运行的进程、S(Sleeping)休眠的进程、T/S(Traced/Stopped) 已停止或中止的进程 或 Z(Zommbie)僵尸进程;
  • CPU%:进程使用的CPU时间百分比;
  • MEM%:进程使用的内存百分比;
  • TIME+:进程已连续运行时间;
  • Command:进程的执行命令;

常用快捷键:

  • 上下箭头:上下滚动查看进程;
  • u:显示指定用户的进程;
  • P:按照进程使用的CPU时间百分比排序;
  • M:按照进程使用的内存百分比排序;
  • T:按照进程已连续运行时间排序;

参考:

  • https://linuxtogether.org/htop-command-explanation/
  • https://www.softprayog.in/tutorials/htop-command-in-linux
  • https://scoutapm.com/blog/understanding-load-averages
  • htop官网

ps命令

# 查看java进程
# e(every) 所有进程
# f(full format) 完整输出格式
ps -ef | grep nginx

# 不显示grep本身
ps -ef | grep nginx | grep -v grep

# 获取指定进程的PID
ps -ef | grep java | grep jenkins | grep -v grep | awk '{print $2}'

# 根据进程的PID中止进程
ps -ef | grep java | grep jenkins | grep -v grep | awk '{print $2}' | xargs kill -9

# 查看进程堆栈
# p(path) 显示完整路径
pmap  -p

# 查看9091端口是哪个容器进程
docker ps | grep 9091

ps -ef格式:

  • UID:进程的UID(用户)
  • PID:进程ID
  • PPID:父进程ID
  • C:CPU使用时间百分比
  • STIME
  • TTY
  • TIME
  • CMD:启动进程的命令

free命令

free命令用来查看内存和Swap的使用情况。

命令:

# 以适合阅读的形式输出(h: human)
free -h

# 以MB格式输出(m: MB)
free -m

# 合计Mem和Swap(t: total)
free -h -t

# 统计3次,每秒1次(c: count)
free -h -c 3

# 统计5次,每2秒统计1次(s: second)
free -h -t -c 5 -s 2

说明:

  • Total: The amount of RAM installed in your system.
  • Used: Equal to  Total-( Free+ Buffers+ Cache).
  • Free: The amount of memory completely unused by anything.
  • Shared: Memory taken by the  tmpfs file systems.
  • Buffer: The data structures that are maintained to provide an index for everything stored in  Cache.
  • Cache: Data read from the hard drive, modified data waiting to be written back to the hard drive, and other computed values.
  • Available: What’s really free. An estimate of the memory in  FreeBuffer, and  Cache that could be used to satisfy a memory request.

简单来说,total是系统总内存,used就是系统已用内存,total - used 就是系统可用内存。

参考:

  • https://www.howtogeek.com/456943/how-to-use-the-free-command-on-linux/

vmstat命令

vmstat命令用来统计进程、内存、Swap、IO和CPU等信息。

命令:

# 运行vmstat
vmstat

# 每5秒统计1次
vmstat 5

# 每5秒统计1次,共统计4次
vmstat 5 4

# 以MB格式输出内存使用情况
vmstat 5 4 -S M

# 显示统计概要信息
vmstat -s

说明:

Proc

  • r: The number of runnable processes. These are processes that have been launched and are either running or are waiting for their next time-sliced burst of CPU cycles.
  • b: The number of processes in uninterruptible sleep. The process isn’t sleeping, it is performing a blocking system call, and it cannot be interrupted until it has completed its current action. Typically the process is a device driver waiting for some resource to come free. Any queued interrupts for that process are handled when the process resumes its usual activity.

Memory

  • swpd: the amount of virtual memory used. In other words, how much memory has been swapped out.,
  • free: the amount of idle (currently unused) memory.
  • buff: the amount of memory used as buffers.
  • cache: the amount of memory used as cache.

Swap

  • si: Amount of virtual memory swapped  in from swap space.
  • so: Amount of virtual memory swapped  out to swap space.

IO

  • bi: Blocks received from a block device. The number of data blocks used to swap virtual memory back into RAM.
  • bo: Blocks sent to a block device. The number of data blocks used to swap virtual memory out of RAM and into swap space.

System

  • in: The number of interrupts per second, including the clock.
  • cs: The number of context switches per second. A context switch is when the kernel swaps from system mode processing into user mode processing.

CPU

These values are all percentages of the total CPU time.

  • us: Time spent running non-kernel code. That is, how much time is spent in user time processing and in nice time processing.
  • sy: Time spent running kernel code.
  • id: Time spent idle.
  • wa: Time spent waiting for input or output.
  • st: Time stolen from a virtual machine. This is the time a virtual machine has to wait for the hypervisor to finish servicing other virtual machines before it can come back and attend to this virtual machine.

参考:

  • https://www.howtogeek.com/424334/how-to-use-the-vmstat-command-on-linux/
  • https://www.tecmint.com/linux-performance-monitoring-with-vmstat-and-iostat-commands/

iosat命令

iostat命令用来统计磁盘IO和CPU等信息。

命令:

# 运行iostat
iostat

# 以MB格式输出
iostat -m

# 查看指定磁盘(分区)
iostat -m -p vda

# 每2秒统计一次,共统计3次
iostat -m 2 3

说明:

CPU信息:

  • %user : It shows the percentage of CPU being utilization that while executing at the user level.
  • %nice : It shows the percentage of CPU utilization that occurred while executing at the user level with a nice priority.
  • %system : It shows the percentage of CPU utilization that occurred while executing at the system (kernel) level.
  • %iowait : It shows the percentage of the time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.
  • %steal : It shows the percentage of time being spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing by another virtual processor.
  • %idle : It shows the percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.

磁盘信息:

  • Device : The device/partition name is listed in  /dev directory.
  • tps : The number of transfers per second that were issued to the device. Higher tps means the processor is busier.
  • Blk_read/s : It shows the amount of data read from the device expressed in a number of blocks (kilobytes, megabytes) per second.
  • Blk_wrtn/s : The amount of data written to the device expressed in a number of blocks (kilobytes, megabytes) per second.
  • Blk_read : It shows the total number of blocks read.
  • Blk_wrtn : It shows the total number of blocks written.

参考:

  • https://www.geeksforgeeks.org/iostat-command-in-linux-with-examples/
  • https://www.techrepublic.com/article/how-to-use-the-linux-iostat-command-to-check-on-your-storage-subsystem/

netstat命令

netstat命令用来统计网络连接和端口等信息。

命令:

# 显示在侦听的TCP/UDP网络连接,包括程序和端口信息
# u(UDP)
# t(TCP)
# n(port)
# l(listen)
# p(program)
netstat -utnlp

# 只显示在侦听的TCP网络连接,包括程序和端口信息
netstat -tnlp

# 查看某个端口被哪个进程使用
# a(all)
netstat -anp | grep 6379

# 查看某个进程在哪个端口
netstat -anp | grep redis

# 查看docker启动的进程在哪个端口
docker ps | grep jenkins

df命令

df命令用来统计磁盘使用情况。

命令:

# 查看全部文件系统的磁盘使用情况
df -h

# 查看指定目录所在的文件系统的磁盘使用情况
df -h ~

# 不显示docker文件系统
df -h | grep -v docker

du命令

du命令用来统计指定目录的大小。

命令:

# 统计某个目录大小
du -sh ~

# 统计目录下各个子目录和文件的大小
du -h ~

# 统计目录下各个子目录和文件的大小,并显示合计大小
du -ch ~

# 只统计下一级目录大小
du -h --max-depth 1

# 只统计下一级目录大小,单位为MB,从大到小排序
du -m --max-depth 1 | sort -rn

# 只统计下一级目录大小,单位为MB,从大到小排序,返回最大的10个文件(目录)
du -m --max-depth 1 | sort -rn | head -11


-END-


整理文章为传播相关技术,版权归原作者所有 |

如有侵权,请联系删除 |


【1】超长干货为你解析:从串口驱动到Linux驱动模型,嵌入式必会!

【2】超全!嵌入式必懂的CAN总线一文讲通了

【3】干货:嵌入式系统设计开发大全!(万字总结)

【4】嵌入式系统求职回忆录:广嵌、迈瑞、华为、智光……

【5】嵌入式行业真的没有前途吗?


免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!

嵌入式ARM

扫描二维码,关注更多精彩内容

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

LED驱动电源的输入包括高压工频交流(即市电)、低压直流、高压直流、低压高频交流(如电子变压器的输出)等。

关键字: 驱动电源

在工业自动化蓬勃发展的当下,工业电机作为核心动力设备,其驱动电源的性能直接关系到整个系统的稳定性和可靠性。其中,反电动势抑制与过流保护是驱动电源设计中至关重要的两个环节,集成化方案的设计成为提升电机驱动性能的关键。

关键字: 工业电机 驱动电源

LED 驱动电源作为 LED 照明系统的 “心脏”,其稳定性直接决定了整个照明设备的使用寿命。然而,在实际应用中,LED 驱动电源易损坏的问题却十分常见,不仅增加了维护成本,还影响了用户体验。要解决这一问题,需从设计、生...

关键字: 驱动电源 照明系统 散热

根据LED驱动电源的公式,电感内电流波动大小和电感值成反比,输出纹波和输出电容值成反比。所以加大电感值和输出电容值可以减小纹波。

关键字: LED 设计 驱动电源

电动汽车(EV)作为新能源汽车的重要代表,正逐渐成为全球汽车产业的重要发展方向。电动汽车的核心技术之一是电机驱动控制系统,而绝缘栅双极型晶体管(IGBT)作为电机驱动系统中的关键元件,其性能直接影响到电动汽车的动力性能和...

关键字: 电动汽车 新能源 驱动电源

在现代城市建设中,街道及停车场照明作为基础设施的重要组成部分,其质量和效率直接关系到城市的公共安全、居民生活质量和能源利用效率。随着科技的进步,高亮度白光发光二极管(LED)因其独特的优势逐渐取代传统光源,成为大功率区域...

关键字: 发光二极管 驱动电源 LED

LED通用照明设计工程师会遇到许多挑战,如功率密度、功率因数校正(PFC)、空间受限和可靠性等。

关键字: LED 驱动电源 功率因数校正

在LED照明技术日益普及的今天,LED驱动电源的电磁干扰(EMI)问题成为了一个不可忽视的挑战。电磁干扰不仅会影响LED灯具的正常工作,还可能对周围电子设备造成不利影响,甚至引发系统故障。因此,采取有效的硬件措施来解决L...

关键字: LED照明技术 电磁干扰 驱动电源

开关电源具有效率高的特性,而且开关电源的变压器体积比串联稳压型电源的要小得多,电源电路比较整洁,整机重量也有所下降,所以,现在的LED驱动电源

关键字: LED 驱动电源 开关电源

LED驱动电源是把电源供应转换为特定的电压电流以驱动LED发光的电压转换器,通常情况下:LED驱动电源的输入包括高压工频交流(即市电)、低压直流、高压直流、低压高频交流(如电子变压器的输出)等。

关键字: LED 隧道灯 驱动电源
关闭