当前位置:网站首页>Makefile implements compilation time statistics
Makefile implements compilation time statistics
2022-07-20 22:47:00 【Remember to look up at the stars】
Preface
In a recent project , Want statistics make The time of the whole project , To add to Makefile in , After image generation , Automatically output the time to the screen .
Research. , Discovery can be achieved in the following two ways .
One 、time Tools
The easiest way , Use time
Tools to count the running time of scripts , Baidu Encyclopedia right time Explanation of the order :
time Commands are often used to measure the running time of a command
The test environment is ubuntu20.04
, Input... At the terminal time
command
time
real 0m0.000s
user 0m0.000s
sys 0m0.000s
- real : The whole execution process takes time , Unit second
- user: Time spent running programs in user space , Unit second
- sys: Time spent running programs in kernel space , Unit second
With one drive demo Take the compilation process as an example .
In the compile Directory , function Makefile Of make action .
make
make -C /lib/modules/`uname -r`/build M=/home/xxx/Desktop/xxx/test/driver modules
make[1]: Entering directory '/usr/src/linux-headers-5.13.0-41-generic'
CC [M] /home/xxx/Desktop/xxx/test/driver/msg_test.o
MODPOST /home/xxx/Desktop/xxx/test/driver/Module.symvers
CC [M] /home/xxx/Desktop/xxx/test/driver/msg_test.mod.o
LD [M] /home/xxx/Desktop/xxx/test/driver/msg_test.ko
BTF [M] /home/xxx/Desktop/xxx/test/driver/msg_test.ko
Skipping BTF generation for /home//home/xxx/Desktop/xxx/test/driver/msg_test.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-5.13.0-41-generic'
real 0m14.638s
user 0m0.663s
sys 0m0.560s
You can see the whole process , Time consuming 14.638
second , The time consumed in user mode and kernel mode are 0.663 second 、0.560 second .
Two 、 To achieve
Ideas
All we need to do is Makefile Start running , Record a timestamp .Makefile Record a timestamp when the execution is completed , In the subtraction of time , You can get the total running time .
1、 Get the start timestamp .
May adopt linux Systematic /proc/uptime
To get the current time .
You can analyze it first /proc/uptime
What's in it , It says Linux System running time .
cat /proc/uptime
4643.14 34329.17
- First column 4643.14, It is system startup , To
cat /proc/uptime
Command run time , The time that the system has been running , The unit is seconds . - Second column 34329.17, Is the system idle time , It has something to do with the system audit , At this time , Since the system was started , All cores , The sum of free time , The author's ubuntu yes 8 Check the number , This parameter doesn't care much , It has nothing to do with the content of this implementation .
2、 The idea of extracting timestamp
Only need to use awk
command , adopt .
Division /proc/uptime
What's in it , You can get the timestamp of seconds .
cat /proc/uptime | awk -F "." '{print $1}'
4643
As long as Makefile At the end of the run , Get it again /proc/time
The number of seconds inside , Subtract the values before and after , You can get the time difference , That is, the running time of the project .
3、date The command conversion timestamp is Mm / DD / yyyy HHM / S Format
Can pass data --help
Order to see data Usage of .
data -d @ Time stamp [format]
format Use the following parameters , Acquisition date 、 when 、 branch 、 second
%j day of year (001..366)
%H hour (00..23)
%M minute (00..59)
%S second (00..60)
At this time, the timestamp entered is in 1970 Started as an operation in . As obtained above 4643
, As bloggers UTC-8 Time for , So time + 了 8 Hours ,4643 The corresponding is 9 spot 17 branch 23 second .
date -d @4643
1970 year 01 month 01 Japan Thursday 09:17:23 CST
\- u Options
add -u
Options , You can make time in UTC For reference , No time zone offset .
date -u [email protected]
1970 year 01 month 01 Japan Thursday 01:17:23 UTC
You can see that the time is 1 spot 17 branch 23 second , It happens to be the running time of our system ,4643 second .
Format the interception time, minutes and seconds
date -u -d @4643 +%Hh:%Mm:%Ss
01h:17m:23s
+%Hh:%Mm:%Ss
In the command %H、%M、%S Hours, minutes and seconds are obtained respectively , And h:m:s
Output in this format . And printf Function similar to . At this point, you can get the hours, minutes and seconds of the running time .
Realization
The original Makefile
DIR = $(shell pwd)
KER_DIR = /lib/modules/`uname -r`/build
obj-m = msg_netlink.o
all:
make -C $(KER_DIR) M=$(PWD) modules
clean:
make -C $(KER_DIR) M=$(PWD) clean
After modification Makefile
DIR = $(shell pwd)
KER_DIR = /lib/modules/`uname -r`/build
START_TIME = $(shell cat /proc/uptime | awk -F "." '{print $$1}') # Makefile Get into , Get the timestamp
obj-m = msg_netlink.o
all: module showruntime
module:
make -C $(KER_DIR) M=$(PWD) modules
clean:
make -C $(KER_DIR) M=$(PWD) clean
showruntime:
@current_time=`cat /proc/uptime | awk -F "." '{print $$1}'`; \
time_interval=`expr $${
current_time} - $(START_TIME)`; \
runtime=`date -u -d @$${
time_interval} +%Hh:%Mm:%Ss`; \
echo "######## runtime: $${runtime} ########"
analysis
Little knowledge :
- Makefile in , call Makefile Own variables , use
$
Symbol , such as Makefile MediumSTART_TIME
. - Makefile in ,target Executive shell command , Variables defined , Need to use
$$
Symbol call , such as showruntime Mediumcurrent_time
, the reason being that shell Variables generated by the running process , Need to use$$current_time
To call . - Again
START_TIME = $(shell cat /proc/uptime | awk -F "." '{print $$1}')
in , In the terminal command , obtain awk Parameter use$1
that will do , But because it is in Makefile in , call shell, So we need to use$$1
To get the segmentation value . - Makefile Running target in , Such as showruntime, Inside shell command , If you need to do more shell command , Need to use
; \
Symbols to continue the whole shell command , Otherwise, an error will be reported , Detailed viewshowruntime
What's in it . - Makefile in
@ character
shell Commands can be hidden shell Command run print .
Such as
showruntime:
current_time=`cat /proc/uptime | awk -F "." '{print $$1}'`; \
time_interval=`expr $${
current_time} - $(START_TIME)`; \
runtime=`date -u -d @$${
time_interval} +%Hh:%Mm:%Ss`; \
echo "######## runtime: $${runtime} ########"
Output is as follows , You can see the whole shell The commands are printed :
make -C /lib/modules/`uname -r`/build M=/home/xxx/Desktop/xxx/test/driver modules
make[1]: Entering directory '/usr/src/linux-headers-5.13.0-41-generic'
make[1]: Leaving directory '/usr/src/linux-headers-5.13.0-41-generic'
current_time=`cat /proc/uptime | awk -F "." '{print $1}'`; \
time_interval=`expr ${
current_time} - 6710 `; \
runtime=`date -u -d @${
time_interval} +%Hh:%Mm:%Ss`; \
echo "######## runtime: ${runtime} ########"
######## runtime: 00h:00m:00s ########
add @ After the symbol , Only print echo The characters that come out .
showruntime:
@current_time=`cat /proc/uptime | awk -F "." '{print $$1}'`; \
time_interval=`expr $${
current_time} - $(START_TIME)`; \
runtime=`date -u -d @$${
time_interval} +%Hh:%Mm:%Ss`; \
echo "######## runtime: $${runtime} ########"
Output is as follows :
make -C /lib/modules/`uname -r`/build M=/home/xxx/Desktop/xxx/test/driver modules
make[1]: Entering directory '/usr/src/linux-headers-5.13.0-41-generic'
make[1]: Leaving directory '/usr/src/linux-headers-5.13.0-41-generic'
######## runtime: 00h:00m:00s ########
The running time here is 0, Because the driver has been compiled , No changes ,make clean after , recompile , That is, you can see the running time .
make clean
ake -C /lib/modules/`uname -r`/build M=/home/xxx/Desktop/xxx/test/driver clean
make[1]: Entering directory '/usr/src/linux-headers-5.13.0-41-generic'
CLEAN /home/xxx/Desktop/xxx/test/driver/Module.symvers
make[1]: Leaving directory '/usr/src/linux-headers-5.13.0-41-generic'
time make # It was used time Count the time together
make -C /lib/modules/`uname -r`/build M=/home/xxx/Desktop/xxx/test/driver modules
make[1]: Entering directory '/usr/src/linux-headers-5.13.0-41-generic'
CC [M] /home/xxx/Desktop/xxx/test/driver/msg_test.o
MODPOST /home/xxx/Desktop/xxx/test/driver/Module.symvers
CC [M] /home/xxx/Desktop/xxx/test/driver/msg_test.mod.o
LD [M] /home/xxx/Desktop/xxx/test/driver/msg_test.ko
BTF [M] /home/xxx/Desktop/xxx/test/driver/msg_test.ko
Skipping BTF generation for /home/xxx/Desktop/xxx/test/driver/msg_test.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-5.13.0-41-generic'
######## runtime: 00h:00m:02s ########
real 0m2.107s
user 0m0.821s
sys 0m0.301s
You can see time make
Count the time , And showruntime
The calculated time is 2 About seconds . experimental result ok.
summary
- be familiar with /proc/uptime The meaning of the parameters .
- Don't have to /proc/uptime To get the timestamp , It can be used
date +%s
Get the current timestamp , Instead ofcat /proc/uptime
- be familiar with date When ordered , Get the timestamp 、 Format get date .
- Watch carefully Makefile Realized Little knowledge , There are many things involved Details , It is recommended to watch it again and again .
- Makefile Medium target Dependence problem , Watch carefully
showruntime
target The position of . - Makefile in
$` Symbol
And$$ Symbol
The difference between
边栏推荐
- Typescript正则表达式使用
- [swoole series 2.5] asynchronous tasks
- Technology sharing | compile and install xtrabackup8 on domestic Kirin arm
- leetcode:632. Minimum interval
- 深入理解TCP协议的连接状态与可靠机制
- M matlab performance simulation of optical fiber communication system based on Fiber Bragg grating sensor network connected to GPON, including decoding, unpacking, demultiplexing, rate recovery, frami
- 2022 Beijing product manager certification enrollment brochure (NPDP)
- C语言程序环境和预处理
- EMQ映云科技成功入选《中国企业家》2022年度「新锐100」榜单
- mysql增删查改
猜你喜欢
【Swoole系列2.5】异步任務
How to do a good job in the design of test cases? The zero foundation of software testing must see
The second half of 2022 (soft exam advanced) information system project manager certification enrollment Brochure
[机缘参悟-47]:鬼谷子-第十一决篇-决策者,中庸也,利益合理化分配也
Easydss uses MySQL database and cannot be started. How to solve it?
IEC104 simulator tutorial
西门子S7 模拟器使用教程
【论文阅读|深读】VERSE: Versatile Graph Embeddings from Similarity Measures
M matlab based IEEE802.15.4 home network efficient and energy-saving effective access method
Database 7-12
随机推荐
XML to VOC, VOC to coco, coco to Yolo, coco partition, coco check, Yolo check, coco visualization
苹果等大厂Hold不住了!裁员和放缓招聘,「双管齐下」出方案
Huawei wireless device roaming configures non fast roaming between APs of the same service VLAN
IEC104 simulator tutorial
[激光器原理与应用-5]:激光二极管LD (Laser Diode)与激光二极管驱动器(LD驱动器)
散户炒股选哪个证券公司手续费低,手机上开户安全吗
MySQL constraint and aggregate query
实施MES管理系统的关键是什么
Particle dissipation of unity 3D characters
TIA博途_STEP7版本的升级与移植相关难点汇总
Easydss uses MySQL database and cannot be started. How to solve it?
Flink 的sink表插入到mysql表后,时间减少了8小时,谁知道 这是什么问题?
深入理解TCP协议的连接状态与可靠机制
Knowledge points of common interview questions: distributed lock
MySQL deadlock, lock timeout, slow SQL summary
How to do a good job in the design of test cases? The zero foundation of software testing must see
leetcode:146. Least recently used cache by LRU
UNIPRO multi terminal deployment to meet customers' diversified needs
数据库7-12
1053 Path of Equal Weight