Skip to content

Commit c0e9c62

Browse files
authored
add support for process-level tracing (#2)
1 parent cc5f71e commit c0e9c62

17 files changed

+732
-51
lines changed

CMakeLists.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/share/man/man1/cpuusage.1 DESTINATION
1616

1717
# Catapult
1818
if (NOT IS_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/share/cpuusage/catapult)
19-
execute_process(COMMAND ${CMAKE_COMMAND} -E tar -xzvf ${CMAKE_SOURCE_DIR}/catapult.tar.gz)
19+
execute_process(COMMAND ${CMAKE_COMMAND} -E tar -xzvf ${CMAKE_SOURCE_DIR}/ext/catapult.tar.gz)
2020
endif ()
2121
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/share/cpuusage/catapult DESTINATION share/cpuusage)
2222

@@ -64,6 +64,11 @@ target_link_libraries(${PROJECT_NAME} dl)
6464
target_link_libraries(${PROJECT_NAME}static dl)
6565
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
6666

67+
# Cpuusageproc library build
68+
add_library(${PROJECT_NAME}proc SHARED src/cupmain.cpp)
69+
target_link_libraries(${PROJECT_NAME}proc dl)
70+
install(TARGETS ${PROJECT_NAME}proc DESTINATION lib)
71+
6772
# Example - ex001 - regular program / non-instrumented
6873
add_executable(ex001 tests/ex.c)
6974
target_link_libraries(ex001 pthread)
@@ -77,6 +82,14 @@ target_link_libraries(ex002 pthread cpuusage)
7782
add_executable(ex003 tests/ex3.cpp src/cpuusage.h)
7883
target_link_libraries(ex003 pthread cpuusage)
7984

85+
# Example - ex004 - simple program (sleep)
86+
add_executable(ex004 tests/ex4.cpp)
87+
target_link_libraries(ex004)
88+
89+
# Example - ex005 - simple scripts
90+
configure_file(tests/ex5.sh ${CMAKE_CURRENT_BINARY_DIR}/ex005.sh COPYONLY)
91+
configure_file(tests/ex5b.sh ${CMAKE_CURRENT_BINARY_DIR}/ex005b.sh COPYONLY)
92+
8093
# Tests
8194
enable_testing()
8295

@@ -89,3 +102,11 @@ add_test(test002 "${PROJECT_BINARY_DIR}/test002")
89102
configure_file(tests/test003 ${CMAKE_CURRENT_BINARY_DIR}/test003 COPYONLY)
90103
add_test(test003 "${PROJECT_BINARY_DIR}/test003")
91104

105+
configure_file(tests/test004 ${CMAKE_CURRENT_BINARY_DIR}/test004 COPYONLY)
106+
add_test(test004 "${PROJECT_BINARY_DIR}/test004")
107+
108+
if(UNIX AND NOT APPLE)
109+
configure_file(tests/test005 ${CMAKE_CURRENT_BINARY_DIR}/test005 COPYONLY)
110+
add_test(test005 "${PROJECT_BINARY_DIR}/test005")
111+
endif()
112+

DESIGN.md

Lines changed: 0 additions & 38 deletions
This file was deleted.

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ General syntax:
5858
cpuusage -c [OPTIONS] PROG [ARGS..]
5959
cpuusage -f <FUNCTIONS> [OPTIONS] PROG [ARGS..]
6060
cpuusage -i <INCLUDES> [OPTIONS] PROG [ARGS..]
61+
cpuusage -p [OPTIONS] PROG [ARGS..]
6162
cpuusage -r JSONFILE [OPTIONS]
6263
cpuusage --help|-h
6364
cpuusage --version|-v
@@ -77,6 +78,8 @@ Options:
7778
-i <INCLUDES>
7879
trace functions in specified POSIX headers
7980

81+
-p trace child process durations
82+
8083
--help, -h
8184
display this help and exit
8285

@@ -101,6 +104,8 @@ Options:
101104

102105
-v verbose mode
103106

107+
-y auto-open resulting html file
108+
104109
PROG program to run and profile
105110

106111
[ARGS] optional arguments to the program
@@ -146,6 +151,18 @@ Resulting visualization:
146151
Resulting visualization:
147152
![culog4 screenshot](/doc/culog4.png)
148153

154+
**Tracing process durations:**
155+
156+
$ cpuusage -v -o cutrace5.html -p ./ex005.sh
157+
cpuusage: https://github.com/d99kris/cpuusage
158+
cpuusage: initializing
159+
cpuusage: starting program ./ex005.sh
160+
cpuusage: processing output trace
161+
cpuusage: completed processing 5 samples
162+
163+
Resulting visualization:
164+
![culog5 screenshot](/doc/culog5.png)
165+
149166
Alternatives
150167
============
151168
There are many CPU profilers available for Linux and macOS. Most of them are
@@ -157,7 +174,8 @@ sample-based, and here is a list of some of them:
157174

158175
Technical Details
159176
=================
160-
Refer to [DESIGN.md](DESIGN.md) for design and implementation details.
177+
Refer to [ext/README.md](/ext/README.md) for details on the external components
178+
used by cpuusage.
161179

162180
License
163181
=======

doc/culog5.png

19.5 KB
Loading

ext/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
External Components
2+
===================
3+
This directory contains external components used by cpuusage.
4+
5+
Catapult
6+
--------
7+
Catapult is the most important third-party component of cpuusage. Not the entire
8+
Catapult software is needed by cpuusage, so only a part of the original package
9+
is distributed with cpuusage. It is compressed and located in catapult.tar.gz
10+
11+
### Trace File Format
12+
Cpuusage generates JSON files following the
13+
[Trace Event Format](/ext/catapult_trace_event_format.pdf) which subsequently
14+
is converted by Catapult into a HTML report.
15+
16+
The Trace Event Format PDF was generated from the Google Document
17+
[Trace Event Format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/edit)
18+
linked from [Catapult Tracing](https://chromium.googlesource.com/catapult/+/HEAD/tracing/README.md).
19+
20+
### Updating Catapult
21+
In case Catapult needs to be updated (it happened when updating to Python 3.x
22+
support) the recommended method is:
23+
24+
1. Build cpuusage
25+
26+
./genman.sh
27+
28+
2. Run tests
29+
30+
cd build && ctest --output-on-failure
31+
32+
3. Perform any necessary updates to the Catapult copy in
33+
build/share/cpuusage/catapult to make tests pass.
34+
35+
4. When all tests are passing, create a new tar-ball and make sure to exclude
36+
test_data, e.g. `rm -rf share/cpuusage/catapult/tracing/test_data`
37+
The tar-ball can be created like this:
38+
39+
tar -czvf ~/c1.tar.gz share/cpuusage/catapult
40+
41+
Then replace the original file:
42+
43+
cp ~/c1.tar.gz ../ext/catapult.tar.gz
44+
45+
5. Finally delete the previous build directory and try build and run tests
46+
again.
47+
File renamed without changes.

ext/catapult_trace_event_format.pdf

265 KB
Binary file not shown.

genman.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

run.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env bash
2+
3+
BUILD="0"
4+
INSTALL="0"
5+
TESTS="0"
6+
case "${1%/}" in
7+
build)
8+
BUILD="1"
9+
;;
10+
11+
install)
12+
BUILD="1"
13+
INSTALL="1"
14+
;;
15+
16+
tests)
17+
BUILD="1"
18+
TESTS="1"
19+
;;
20+
21+
*)
22+
echo "usage: make.sh <build|install|tests>"
23+
echo " build - build"
24+
echo " install - perform build, tests and install"
25+
echo " tests - perform build and run tests"
26+
exit 1
27+
;;
28+
esac
29+
30+
if [[ "${BUILD}" == "1" ]]; then
31+
if [[ "$(which help2man)" != "" ]]; then
32+
cd src && help2man -n "instrumentation CPU profiler" -N -o cpuusage.1 ./cpuusage && cd - > /dev/null
33+
if [[ "${?}" == "0" ]]; then
34+
if [[ "$(uname)" == "Darwin" ]]; then
35+
SED="gsed -i"
36+
else
37+
SED="sed -i"
38+
fi
39+
${SED} "s/\.\\\\\" DO NOT MODIFY THIS FILE\! It was generated by help2man.*/\.\\\\\" DO NOT MODIFY THIS FILE\! It was generated by help2man./g" src/cpuusage.1
40+
else
41+
echo "generate man-page failed, exiting."
42+
exit 1
43+
fi
44+
else
45+
echo "generate man-page skipped, help2man not present."
46+
fi
47+
48+
mkdir -p build && cd build && cmake .. && make -s
49+
if [[ "${?}" != "0" ]]; then
50+
echo "build failed, exiting."
51+
exit 1
52+
fi
53+
cd - > /dev/null
54+
fi
55+
56+
if [[ "${TESTS}" == "1" ]]; then
57+
cd build && ctest --output-on-failure
58+
if [[ "${?}" != "0" ]]; then
59+
echo "tests failed, exiting."
60+
exit 1
61+
fi
62+
cd - > /dev/null
63+
fi
64+
65+
if [[ "${INSTALL}" == "1" ]]; then
66+
INSTALLCMD=""
67+
if [[ "$(uname)" == "Linux" ]]; then
68+
INSTALLCMD="sudo"
69+
fi
70+
71+
cd build && ${INSTALLCMD} make -s install
72+
if [[ "${?}" != "0" ]]; then
73+
echo "install failed, exiting."
74+
exit 1
75+
fi
76+
cd - > /dev/null
77+
fi
78+
79+
exit 0

0 commit comments

Comments
 (0)