perf

1
2
3
4
5
6
7
8
9
10
11
12
#进程热点函数分析
perf top -g -p 21515
perf record -F 99 -a -g -p $pid -- sleep 60
-e选项允许您在perf list命令中列出的多个类别中选择一个事件类别。
perf report -i 文件 -g
perf report -g graph,0.3
#默认0.5,低于不显示堆栈

#隐藏CPU
perf sched record -C 0 -- sleep 5 (-C后面的参数,填CPU使用率高的cpu序号,0表示第一个CPU)
perf report 选择 sched:sched_switch 按回车键
perf sched latency --sort max

strace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
strace -tt -T -v -f -e trace=file -o /data/log/strace.log -s 1024 -p 23489

● -tt:在每行输出的前面,显示毫秒级别的时间
● -T:显示每次系统调用所花费的时间
● -v:对于某些相关调用,把完整的环境变量,文件 stat 结构等打出来。
● -f:跟踪目标进程,以及目标进程创建的所有子进程
● -e:控制要跟踪的事件和跟踪行为,比如指定要跟踪的系统调用名称
● -o:把 strace 的输出单独写到指定的文件
● -s:当系统调用的某个参数是字符串时,最多输出指定长度的内容,默认是 32 个字节
● -p:指定要跟踪的进程 pid,要同时跟踪多个 pid,重复多次 -p 选项即可。
-e trace=file 跟踪和文件访问相关的调用(参数中有文件名)
-e trace=process 和进程管理相关的调用,比如fork/exec/exit_group
-e trace=network 和网络通信相关的调用,比如socket/sendto/connect
-e trace=signal 信号发送和处理相关,比如kill/sigaction
-e trace=desc 和文件描述符相关,比如write/read/select/epoll等

dd

1
2
3
4
5
6
dd if=/dev/zero  of=/data/test  bs=10M status=progress  count=10000 oflag=direct  写入

dd if=/data/test of=/dev/null bs=10M count=10000 oflag=direct status=progress 读取

#direct                读写数据采用直接IO方式;
#status=progress 显示进度

tcpdump

1
2
3
4
5
6
7
8
9
10
11
• 选择所有端口,指定host域名抓包,抓包文件存入/tmp/中:
tcpdump –nni any –s 0 host www.ex.com –w /tmp/ex.com.pcap
• 指定eth0,指定目标IP和端口,并指定抓取1000个报文
tcpdump –nni eth0 host 1.1.1.1 and port 80 –c 1000
• 指定某网段,如下表达式默认为/24位
tcpdump –nni eth0 net 1.1.1.1 and portrange 8000-8080
常用语句
•循环抓包(抓取前500个字节,500M一个文件,保存10个)
tcpdump -i eth0 -s 500 host 1.1.1.1 -w /tmp/test.pcap -C 500 -W 10
特殊场景语句,抓取特定的tcp置位报文
tcpdump -nni any tcp[tcpflags]=tcp-syn/tcp-rst/tcp-ack/tcp-ack

iperf3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Client/Server: # 客户端和服务端公有的参数
指定端口号,默认为5201
-p, --port # server port to listen on/connect to
回显报告的间隔时间
-i, --interval # seconds between periodic bandwidth reports
显示帮助菜单
-h, --help print this message and quit
显示版本
-v, --version print version information and quit

Server specific: #服务端私有参数
指定以服务端运行
-s, --server run in server mode

Client specific: #客户端私有参数
带宽参数,单位:字节每秒:KMG,为2的n次方,比如1K=1024,;设置为0代表无限制,此参数UDP默认1M/s,TCP无限制
-b, --bandwidth #[KMG][/#] target bandwidth in bits/sec (0 for unlimited)
(default 1 Mbit/sec for UDP, unlimited for TCP)
(optional slash and packet count for burst mode)
指定以客户端运行,后面要带服务端的IP地址
-c, --client <host> run in client mode, connecting to <host>
udp模式,不带-u默认为tcp模式
-u, --udp use UDP rather than TCP
指定测试时间,不带参数默认测试10s
-t, --time # time in seconds to transmit for (default 10 secs)
翻转测试,这是iperf3比iperf2方便的主要亮点,iperf2不支持此功能,无法使用
-R, --reverse reverse the test (client receives, server sends)
tcp窗口大小,默认无上限,可以不设此参数,作为udp模式测试时也不需要此参数 ,单位:KM,1K=1024
-w, --window #[KMG] set window size / socket buffer size


iperf -s -i 1
# 作为服务端运行,报告回显间隔时间1s
iperf3 -c 192.168.3.250 -i 1 -t 10 -b 7M
#作为客户端,连接服务端ip地址192.168.3.250,报告回显间隔1s,测试时间10s,带宽限制为7M。
iperf3 -c 192.168.3.250 -i 1 -t 10 -b 7M -R
#作为客户端,连接服务端ip地址192.168.3.250,报告回显间隔1s,测试时间10s,带宽限制为7M,-R为反向测试,这个参数也是iperf3的主要亮点,支持直接转换数据发送方向


#udp不指定-b默认1M
iperf -s -i 1
# 作为服务端运行,报告回显间隔时间1s,服务端不区分tcp或udp
iperf3 -u -c 192.168.3.250 -b 70M -i 1 -t 10
#作为客户端运行,限制带宽70M,报告回显间隔1s,测试时间10s
iperf3 -u -c 192.168.3.250 -b 70M -i 1 -t 10 -R
#作为客户端运行,限制带宽70M,报告回显间隔1s,测试时间10s

额外常用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
时间
while true; do date +%Y-%m-%d' '%H:%M:%S.%N | cut -b 1-23 && sleep 1;done

curl
curl localhost:3000/api/json -X POST -d '{"hello": "world"}' --header "Content-Type: application/json
curl -v -o output.html "https://dana.lobn.com.cn/" \
--resolve dana.lobn.com.cn:443:8.152.0.157
curl -L -XPOST -H 'Cookie: authToken $token' -F "uploadFiles=@/home/user/test1" https://gdw-3.com/api/v1/detail/upload

端口
nmap -v -sS -sV -T 5 39.105.26.196 -p 9984

sed -i.bak '/^\s*[^#].*\/data\s/s/^/#/' /etc/fstab
badblocks -sv /dev/vda
● -v:显示详情。
● -s:显示进度。
● -w:写模式测试(慎用!会覆盖数据)

登录日志
lastb | awk '{ print" IP地址为:" $3}' | sort | uniq -c | sort -nr|head


网络
netstat -ant | awk '/^tcp/ {++y[$NF]} END {for(w in y) print w, y[w]}'

while true;do netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}';sleep 1;done | grep TIME_WAIT

traceroute -n -T global-cs.aceux.net -p 80 || yum install traceroute -y


netstat -s |grep -e "passive connections rejected because of time stamp"
netstat -s | grep "SYNs to LISTEN"
netstat -s|egrep -i 'syn|ignore|overflow|reject|becau|error|drop|back'


进程
ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]'
ps -e -L h o state,cmd,pid | awk '{if($1=="R"||$1=="D"){print $0}}' | sort | uniq -c | sort -k 1nr

ps -auxww --sort=-%cpu|head
ps -eT -o%cpu,pid,tid,ppid,comm | grep -v CPU | sort -n -r | head -20
ps -auxww --sort=-%mem|head
ps -T -o%cpu,pid,tid,ppid,comm -p # 查看指定进程的所有线程
ps -Lf <PID> # 显示线程的详细信息
ps axjf --no-header #进程树


systemctl list-units --type=service --state=running
systemctl list-units --type=service --state=running --no-pager |grep running |awk '{print $1}' |xargs systemctl cat |grep -i execstart

lsof -nw|awk '{print $2}'| sort | uniq -c | sort -nr | head

# 自定义终端提示符
PS1='\[\e[38;5;208m\]\u@\h\[\e[0m\] \[\e[38;5;34m\]\D{%H:%M:%S}\[\e[0m\] \[\e[38;5;33m\]\w\[\e[0m\] \[\e[1;31m\]$(git branch 2>/dev/null | grep "^*" | colrm 1 2)\[\e[0m\]\n\[\e[1;32m\]➜ \[\e[0m\]'

PS1='\[\033[1;31m\][\t] \[\033[1;32m\]\u\[\033[1;36m\]@\[\033[1;34m\]\h \[\033[1;33m\]\w\[\033[0m\] '

#python
pip cache purge
pip cache remove package-name
pip install --no-cache-dir torch
pip show torch
pip install --target=/mnt/xinference \ --cache-dir=/mnt/xinference/pip_cache \ "xinference[all]"
python -c "import os; print('\n'.join(dir(os)))" | grep 'kill'
nginx -V 2>&1 | grep -o with-http_stub_status_module