Skip to content

Commit db23440

Browse files
committed
improve process tracing, update examples and test names
1 parent bdadc3c commit db23440

File tree

12 files changed

+132
-50
lines changed

12 files changed

+132
-50
lines changed

CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,25 @@ target_link_libraries(${PROJECT_NAME}proc dl)
7070
install(TARGETS ${PROJECT_NAME}proc DESTINATION lib)
7171

7272
# Example - ex001 - regular program / non-instrumented
73-
add_executable(ex001 tests/ex.c)
73+
add_executable(ex001 tests/ex001.c)
7474
target_link_libraries(ex001 pthread)
7575

7676
# Example - ex002 - instrumented
77-
add_executable(ex002 tests/ex.c)
77+
add_executable(ex002 tests/ex002.c)
7878
set_target_properties(ex002 PROPERTIES COMPILE_FLAGS "-rdynamic -finstrument-functions")
7979
target_link_libraries(ex002 pthread cpuusage)
8080

8181
# Example - ex003 - manually instrumented
82-
add_executable(ex003 tests/ex3.cpp src/cpuusage.h)
82+
add_executable(ex003 tests/ex003.cpp src/cpuusage.h)
8383
target_link_libraries(ex003 pthread cpuusage)
8484

8585
# Example - ex004 - simple program (sleep)
86-
add_executable(ex004 tests/ex4.cpp)
86+
add_executable(ex004 tests/ex004.cpp)
8787
target_link_libraries(ex004)
8888

8989
# 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)
90+
configure_file(tests/ex005.sh ${CMAKE_CURRENT_BINARY_DIR}/ex005.sh COPYONLY)
91+
configure_file(tests/ex005b.sh ${CMAKE_CURRENT_BINARY_DIR}/ex005b.sh COPYONLY)
9292

9393
# Tests
9494
enable_testing()

src/cpuusage

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,33 @@ showusage()
5252
echo " [ARGS] optional arguments to the program"
5353
echo ""
5454
echo "Examples:"
55-
echo "cpuusage -o cutrace.html -a ./build/cutest1"
56-
echo " trace all standard POSIX function calls in ./build/cutest1"
57-
echo " and write log to cutrace.html"
58-
echo ""
59-
echo "cpuusage -o cutrace.html -c ./build/cutest2"
60-
echo " trace instrumented functions in ./build/cutest2 and write"
55+
echo "cpuusage -o cutrace.html -a ./build/ex001"
56+
echo " trace all standard POSIX function calls and write"
6157
echo " log to cutrace.html"
6258
echo ""
63-
echo "cpuusage -o cutrace.html -f fopen,fclose,fread,fwrite ./build/cutest1"
59+
echo "cpuusage -o cutrace.html -c ./build/ex002"
60+
echo " trace instrumented functions and write log to"
61+
echo " cutrace.html"
62+
echo ""
63+
echo "cpuusage -o cutrace.html -f fopen,fclose,fread,fwrite ./build/ex001"
6464
echo " trace calls to fopen, fclose, fread and fwrite, and write"
6565
echo " log to cutrace.html"
6666
echo ""
67-
echo "cpuusage -o cutrace.html -i stdio.h ./build/cutest1"
67+
echo "cpuusage -o cutrace.html -i stdio.h ./build/ex001"
6868
echo " trace calls to all POSIX functions in stdio.h and write"
6969
echo " log to cutrace.html"
7070
echo ""
71+
echo "cpuusage -o cutrace.html -p ./build/ex005.sh"
72+
echo " trace process durations and write to cutrace.html"
73+
echo ""
7174
echo "Report bugs at https://github.com/d99kris/cpuusage"
7275
echo ""
7376
}
7477

7578
# Version
7679
showversion()
7780
{
78-
echo "cpuusage v1.43"
81+
echo "cpuusage v1.46"
7982
echo ""
8083
echo "Copyright (C) 2017-2021 Kristofer Berggren"
8184
echo ""

src/cpuusage.1

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
2-
.TH CPUUSAGE "1" "February 2021" "cpuusage v1.43" "User Commands"
2+
.TH CPUUSAGE "1" "December 2021" "cpuusage v1.46" "User Commands"
33
.SH NAME
44
cpuusage \- instrumentation CPU profiler
55
.SH SYNOPSIS
@@ -98,25 +98,29 @@ program to run and profile
9898
[ARGS]
9999
optional arguments to the program
100100
.SH EXAMPLES
101-
cpuusage \-o cutrace.html \-a ./build/cutest1
101+
cpuusage \-o cutrace.html \-a ./build/ex001
102102
.IP
103-
trace all standard POSIX function calls in ./build/cutest1
104-
and write log to cutrace.html
103+
trace all standard POSIX function calls and write
104+
log to cutrace.html
105105
.PP
106-
cpuusage \-o cutrace.html \-c ./build/cutest2
106+
cpuusage \-o cutrace.html \-c ./build/ex002
107107
.IP
108-
trace instrumented functions in ./build/cutest2 and write
109-
log to cutrace.html
108+
trace instrumented functions and write log to
109+
cutrace.html
110110
.PP
111-
cpuusage \-o cutrace.html \-f fopen,fclose,fread,fwrite ./build/cutest1
111+
cpuusage \-o cutrace.html \-f fopen,fclose,fread,fwrite ./build/ex001
112112
.IP
113113
trace calls to fopen, fclose, fread and fwrite, and write
114114
log to cutrace.html
115115
.PP
116-
cpuusage \-o cutrace.html \-i stdio.h ./build/cutest1
116+
cpuusage \-o cutrace.html \-i stdio.h ./build/ex001
117117
.IP
118118
trace calls to all POSIX functions in stdio.h and write
119119
log to cutrace.html
120+
.PP
121+
cpuusage \-o cutrace.html \-p ./build/ex005.sh
122+
.IP
123+
trace process durations and write to cutrace.html
120124
.SH AUTHOR
121125
Written by Kristofer Berggren
122126
.SH "REPORTING BUGS"

src/cupmain.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -244,35 +244,31 @@ static void cup_handleevent(bool p_IsStart)
244244
{
245245
if (getenv("LD_PRELOAD") == NULL) return;
246246

247-
// @todo: figure out why isFirstProcess static is not retained for final call
248-
static bool isFirstProcess = false;
249247
static char default_path[32];
250248
static char* path = NULL;
251249
static int64_t beginTime = 0;
252250

253-
if (p_IsStart)
251+
// determine report path
252+
path = getenv("CU_FILE");
253+
if (path == NULL)
254254
{
255-
// determine report path
256-
path = getenv("CU_FILE");
257-
if (path == NULL)
258-
{
259-
snprintf(default_path, sizeof(default_path), "./culog-%d.json", getpid());
260-
path = default_path;
261-
}
255+
snprintf(default_path, sizeof(default_path), "./culog-%d.json", getpid());
256+
path = default_path;
257+
}
262258

259+
if (p_IsStart)
260+
{
263261
// check if file does not exist
264262
if (access(path, F_OK) == -1)
265263
{
266264
// remember this is the main process
267-
isFirstProcess = true;
265+
std::string pid = std::to_string(getpid());
266+
setenv("CU_FIRST_PROCESS", pid.c_str(), true);
268267

269268
// write header
270269
cup_writeheader(path);
271270
}
272271

273-
// store first process flag in environment
274-
setenv("CU_IS_FIRST_PROCESS", isFirstProcess ? "1" : "0", true);
275-
276272
// store begin timestamp
277273
struct timeval tv;
278274
gettimeofday(&tv, NULL);
@@ -285,9 +281,10 @@ static void cup_handleevent(bool p_IsStart)
285281
gettimeofday(&tv, NULL);
286282
const int64_t endTime = ((int64_t)tv.tv_sec * 1000000ll) + ((int64_t)tv.tv_usec);
287283

288-
// read back first process flag from environment
289-
char* isFirstStr = getenv("CU_IS_FIRST_PROCESS");
290-
isFirstProcess = (isFirstStr != NULL) && (strncmp(isFirstStr, "1", 1) == 0);
284+
// read back first process id from environment
285+
std::string firstpid = std::string(getenv("CU_FIRST_PROCESS"));
286+
std::string pid = std::to_string(getpid());
287+
const bool isFirstProcess = (pid == firstpid);
291288

292289
char* isExpandStr = getenv("CU_EXPAND_PROCESSES");
293290
bool isExpandProcesses = (isExpandStr != NULL) && (strncmp(isExpandStr, "1", 1) == 0);

tests/ex.c renamed to tests/ex001.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* ex.c
2+
* ex001.c
33
*
44
* Copyright (C) 2017 Kristofer Berggren
55
* All rights reserved.

tests/ex002.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* ex002.c
3+
*
4+
* Copyright (C) 2017 Kristofer Berggren
5+
* All rights reserved.
6+
*
7+
* cpuusage is distributed under the BSD 3-Clause license, see LICENSE for details.
8+
*
9+
*/
10+
11+
/* ----------- Includes ------------------------------------------ */
12+
#include <fcntl.h>
13+
#include <limits.h>
14+
#include <stdio.h>
15+
#include <stdlib.h>
16+
#include <string.h>
17+
#include <unistd.h>
18+
19+
20+
/* ----------- Function Prototypes ------------------------------- */
21+
void copy_large_file();
22+
void copy_medium_file();
23+
void copy_small_file();
24+
25+
26+
/* ----------- Global Functions ---------------------------------- */
27+
int main(void)
28+
{
29+
copy_large_file();
30+
copy_medium_file();
31+
copy_small_file();
32+
return 0;
33+
}
34+
35+
void copy_large_file()
36+
{
37+
int large_size = 1024 * 1024 * 1024;
38+
char *buf = calloc(1, large_size);
39+
FILE* fin = fopen("/dev/null", "r");
40+
FILE* fout = fopen("/dev/null", "w");
41+
fread(buf, large_size, 1, fin);
42+
fclose(fin);
43+
fwrite(buf, large_size, 1, fout);
44+
fclose(fout);
45+
free(buf);
46+
}
47+
48+
void copy_medium_file()
49+
{
50+
int medium_size = 128 * 1024 * 1024;
51+
char *buf = calloc(1, medium_size);
52+
FILE* fin = fopen("/dev/null", "r");
53+
FILE* fout = fopen("/dev/null", "w");
54+
fread(buf, medium_size, 1, fin);
55+
fclose(fin);
56+
fwrite(buf, medium_size, 1, fout);
57+
fclose(fout);
58+
free(buf);
59+
}
60+
61+
void copy_small_file()
62+
{
63+
int small_size = 1024 * 1024;
64+
char *buf = calloc(1, small_size);
65+
FILE* fin = fopen("/dev/null", "r");
66+
FILE* fout = fopen("/dev/null", "w");
67+
fread(buf, small_size, 1, fin);
68+
fclose(fin);
69+
fwrite(buf, small_size, 1, fout);
70+
fclose(fout);
71+
free(buf);
72+
}
73+

tests/ex3.cpp renamed to tests/ex003.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* ex3.cpp
2+
* ex003.cpp
33
*
44
* Copyright (C) 2018 Kristofer Berggren
55
* All rights reserved.

tests/ex4.cpp renamed to tests/ex004.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* ex4.cpp
2+
* ex004.cpp
33
*
44
* Copyright (C) 2020 Kristofer Berggren
55
* All rights reserved.

tests/ex005.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
# ex005.sh
4+
5+
sleep 1
6+
SCRIPTPATH="$(cd -- "$(dirname "$0")" >/dev/null 2>&1; pwd -P)"
7+
${SCRIPTPATH}/ex005b.sh
8+
sleep 2
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/usr/bin/env bash
22

3+
# ex005b.sh
4+
35
sleep 1

0 commit comments

Comments
 (0)