利用valgrind检测内存泄漏
$ valgrind --tool=memcheck ./memtest
==16014== Memcheck, a memory error detector
==16014== Copyright (C) 2002-2015, andGNUGPL'd, by Julian Seward et al.
==16014== UsingValgrind-3.11.0andLibVEX; rerun with -h for copyright info
==16014== Command: ./memtest
==16014==
==16014==
==16014== HEAPSUMMARY:==16014== in use at exit:1,164 bytes in3 blocks
==16014== total heap usage:3 allocs, 0 frees, 1,164 bytes allocated
==16014==
==16014== LEAKSUMMARY:==16014== definitely lost:612 bytes in2 blocks
==16014== indirectly lost:0 bytes in0 blocks
==16014== possibly lost:0 bytes in0 blocks
==16014== still reachable:552 bytes in1 blocks
==16014== suppressed:0 bytes in0 blocks
==16014== Rerun with --leak-check=full to see details of leaked memory
==16014==
==16014== For counts of detected and suppressed errors, rerun with: -v
==16014== ERRORSUMMARY:0 errors from 0 contexts (suppressed:0 from 0)
第5章 性能优化的流程
性能评价
优化也要考虑到可移植性以及普适性,不要因为优化过度导致其他问题的出现。
优化流程
性能分析
系统相关# cat /proc/stat
cpu 38472290264265739640152000
cpu0 38472290264265739640152000
intr 6931250000000000000000000000000000000000364850000000000000000045314000057165330740000000000000000000000000511878010200818900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
ctxt 848641
btime 1536194949
processes 2555
procs_running 1
procs_blocked 0
softirq 3524443162321124953039019900166307412333
cpu0后面的数字单位是jiffy,分别对应:
user:从系统启动开始累计到当前时刻,用户态CPU时间,不包含nice值为负的进程。
nice:从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间。
system:从系统启动开始累计到当前时刻,内核所占用的CPU时间。
idle:从系统启动开始累计到当前时刻,除硬盘IO等待时间以外其他等待时间。
iowait:从系统启动开始累计到当前时刻,硬盘IO等待时间。
irq:从系统启动开始累计到当前时刻,硬中断时间。
softirq:从系统启动开始累计到当前时刻,软中断时间。
steal:从系统启动开始累计到当前时刻,involuntary wait
guest:running asanormal guest
guest_nice:running asa niced guest
由此可以粗略计算cpu运行时间,cpu的利用率和IO利用率
查看负载# cat /proc/loadavg0.000.010.051/552565
1、5、15分钟平均负载;
1/55:在采样时刻,运行队列任务数目和系统中活跃任务数目。
2565:最大pid值,包括线程。
查看进程# top
Mem: 61368K used, 186308K free, 48K shrd, 1436K buff, 23688K cached
CPU: 0.0% usr 0.0% sys 0.0% nic 100% idle 0.0% io 0.0% irq 0.0% sirq
Load average: 0.000.010.051/552568
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
25681142 root R 23520.900.0 top
422 root SW 00.000.0 [kworker/0:1]
11201 root S 3313213.300.0 /usr/sbin/minidlnad
10861 root S 137125.500.0 /usr/sbin/ntpd -g
11141 root S 43401.700.0 /usr/sbin/sshd
11001099 www-data S 42481.700.0 nginx: worker process
10901 mosquitt S 41161.600.0 /usr/sbin/mosquitto -c /etc/mosqui
10991 root S 40641.600.0 nginx: master process /usr/sbin/ng
11421 root S 23520.900.0 -/bin/sh
10521 root S 23480.900.0 /sbin/syslogd -n
10 root S 23480.900.0 init
10551 root S 23480.900.0 /sbin/klogd -n
10371 root S 23480.900.0 /sbin/udhcpc -i eth0
10711 dbus S 20960.800.0 dbus-daemon --system
11281 root S 20200.800.0 rpc.statd
11371 root S 19400.700.0 rpc.mountd
10791 root S 16040.600.0 /usr/bin/rpcbind
9922 root SW< 00.000.0 [kworker/u3:2]
5392 root SW 00.000.0 [kswapd0]
62 root SW 00.000.0 [kworker/u2:0]
第6章 进程启动速度
进程启动分为两部分:
(1)进程启动,加载动态库,知道main函数之前。
(2)main函数之后,知道对用户操作响应。
查看进程启动过程
strace
利用strace查看程序执行流程,-tt可以打印微妙级别时间戳。
strace -tt xxx
LD_DEBUG
通过设置LD_DEBUG环境变量,可以打印程序启动流程。
比如LD_DEBUG=libs xxx,可以查看程序运行过程中查找哪些动态库。
# LD_DEBUG=help ls
Valid options forthe LD_DEBUG environment variable are:
libs display library search paths
reloc display relocation processing
files display progress for input file
symbols display symbol table processing
bindings display information about symbol binding
versions display version dependencies
scopes display scope information
all all previous options combined
statistics display relocation statistics
unused determined unused DSOs
help display this help message and exit
To direct the debugging output intoafile instead of standard output
a filename can be specified usingthe LD_DEBUG_OUTPUT environment variable.