2020
2121 * [ strace] ( #strace )
2222
23+ * [ vmstat] ( #vmstat )
24+
25+ * [ pidstat] ( #pidstat )
26+
27+ * [ mpstat] ( #mpstat )
28+
29+
2330* [ Linux 性能优化] ( #linux性能优化 )
2431
2532
2936
3037## Linux基础
3138
32- ### which 命令
39+ * which 命令
40+
41+ which + 可执行文件 ,用来查找它的路径和存放的地方。
42+ ```
43+ which python
44+ /usr/bin/python
45+ ```
46+
47+ * tcpdump 命令
3348
49+ [tcpdump命令](https://github.com/zhaozhengcoder/CoderNoteBook/blob/master/note/linux%E5%91%BD%E4%BB%A4_tcpdump.md)
3450
35- ### tcpdump 命令
51+ * ps 命令
3652
37- [ tcpdump命令] ( https://github.com/zhaozhengcoder/CoderNoteBook/blob/master/note/linux%E5%91%BD%E4%BB%A4_tcpdump.md )
53+ ```
54+ ps -aux # 我常用的
55+ ps -T -p pid # ps查看线程
56+ ```
57+
58+ * find 命令
59+
60+ 经常的需求就是,比如说要寻找home目录下面某个文件或文件夹。
61+
62+ find <指定目录> <指定条件> <指定动作>
63+ ```
64+ $find . -name 'my*' # 查找当前目录下面的,名字前缀是my开头的文件或文件夹
65+ $find . -name 'my*' -ls # 搜索当前目录中,所有文件名以my开头的文件,并显示它们的详细信息。
66+ $find . -name '*.cpp' -ls # 搜索当前目录中,以cpp结尾的文件,并显示它们的详细信息。
67+ ```
3868
39- ### ps命令
69+ **但是,find有一个问题就在文件比较多的时候,很慢。**
70+ 这个时候locate命令就会更好一点了。
4071
41- * 我常用的
4272 ```
43- ps -aux
73+ locate ~/code/*.cpp # 查找~/code目录下面 以cpp结尾的文件
4474 ```
4575
46- * ps查看线程
76+ * grep 命令
77+
78+ 查找当前文件夹下面,包含iostream字符串的文件。
4779 ```
48- ps -T -p pid
80+ grep -rn "iostream"
4981 ```
82+ * sz / rz 命令
83+
84+ sz 从服务器上面下载一个文件
85+
86+ rz 上传一个文件给服务器
87+
88+ **这个命令很适合使用xshell或crt的人,真的很方便。**
89+
90+ * scp 命令
91+
5092## Linux工具命令
5193
5294### GDB
@@ -277,21 +319,11 @@ RES 表示的是:占用的物理内存
277319
278320SHR 表示的是:占用的是共享内存
279321
280- ### netstat
281-
282- ### iostat
283-
284- ### ifstat
285-
286- ### vmstat
287-
288322### mpstat
289-
290323mpstat是Multiprocessor Statistics的缩写,是实时系统监控工具。
291-
292324```
293325# 使用
294- mpstat -P ALL 5 3
326+ mpstat -P ALL 5 3 # 查看所有的cpu的状态,每个5s输出一次,连续输出三次
295327
296328# 解释
297329
@@ -301,6 +333,91 @@ mpstat [-P {|ALL}] [internal [count]]
301333internal 相邻的两次采样的间隔时间、
302334count 采样的次数,count只能和delay一起使用
303335```
336+
337+ ```
338+ # 对应的输出
339+
340+ 11时01分59秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
341+ 11时02分04秒 all 0.03 0.00 0.03 0.03 0.00 0.00 0.00 0.00 0.00 99.92
342+ 11时02分04秒 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
343+ 11时02分04秒 1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
344+ 11时02分04秒 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
345+ 11时02分04秒 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
346+ 11时02分04秒 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
347+ 11时02分04秒 5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
348+ 11时02分04秒 6 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
349+ 11时02分04秒 7 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
350+
351+ 平均时间: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
352+ 平均时间: all 0.02 0.00 0.07 0.02 0.00 0.00 0.00 0.00 0.00 99.90
353+ 平均时间: 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
354+ 平均时间: 1 0.00 0.00 0.07 0.13 0.00 0.00 0.00 0.00 0.00 99.80
355+ 平均时间: 2 0.00 0.00 0.07 0.00 0.00 0.00 0.00 0.00 0.00 99.93
356+ 平均时间: 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
357+ 平均时间: 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
358+ 平均时间: 5 0.00 0.00 0.13 0.00 0.00 0.00 0.00 0.00 0.00 99.87
359+ 平均时间: 6 0.07 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 99.73
360+ 平均时间: 7 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
361+
362+ # 解释
363+ %usr 在internal时间段里,用户态的CPU时间(%)
364+ %nice 在internal时间段里,nice值为负进程的CPU时间(%)
365+ %sys 在internal时间段里,核心时间(%)
366+ %iowait 在internal时间段里,硬盘IO等待时间(%)
367+ %irq 在internal时间段里,硬中断时间(%)
368+ %soft 在internal时间段里,软中断时间(%)
369+ %steal 显示虚拟机管理器在服务另一个虚拟处理器时虚拟CPU处在非自愿等待下花费时间的百分比
370+ %guest 显示运行虚拟处理器时CPU花费时间的百分比
371+ %gnice
372+ %idle 在internal时间段里,CPU除去等待磁盘IO操作外的因为任何原因而空闲的时间闲置时间(%)
373+ ```
374+
375+ ### vmstat
376+ 对操作系统的虚拟内存、进程、CPU活动进行监控
377+ ```
378+ vmstat 5 3 #采样间隔5s,采样次数2次
379+ procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
380+ r b swpd free buff cache si so bi bo in cs us sy id wa st
381+ 0 0 0 10412656 1056828 3802120 0 0 0 0 0 0 1 1 99 0 0
382+
383+ r表示正在运行的进程的数量
384+ b表示被阻塞的进程的数量
385+
386+ 关于system里面的两个参数:
387+ in 表示的是 interrupt 中断数
388+ cs 表示的是 content switch 上下文切换
389+
390+ cpu的里面的参数:
391+ us 表示 user time ,用户态占用的cpu时间
392+ sy 表示 system time,内核态占用的cpu时间
393+ id 表示 idle tiem,闲置的时间
394+ wa 表示 等待IO时间
395+ ```
396+
397+ ### pidstat
398+
399+ pidstat 默认显示了所有进程的cpu,内存,io的使用率。
400+
401+ ```
402+ pidstat -u #查看进程的cpu 使用率
403+ pidstat -r #查看进程的内存使用率
404+ pidstat -d #查看进程的io 使用率
405+ ```
406+
407+ ```
408+ pidstat -t -p 2831 #查看多线程
409+ ```
410+
411+ ```
412+ pidstat -u 1 # 每秒1次的频率输出
413+ ```
414+
415+ ### iostat
416+
417+ ### ifstat
418+
419+ ### netstat
420+
304421### strace
305422
306423跟踪系统运行的过程中,执行的系统调用和信号。这个简直就是debug的神器呀。
@@ -371,6 +488,15 @@ accept(3,
371488
372489 stress命令
373490
491+ stress可以对某个和cpu模拟出计算密集型的任务和io密集的任务。
492+
493+ sysbench 命令
494+
495+ 可以模拟出线程的切换
496+ ```
497+ sysbench --num-threads=30 --max-time=30000 --test=threads run
498+ ```
499+
374500---
375501## 参考资料
376502
0 commit comments