-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlttng-client.cpp
281 lines (233 loc) · 9.67 KB
/
lttng-client.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright (c) 2019 Erez Zadok
* Copyright (c) 2019-2020 Ibrahim Umit Akgun
* Copyright (c) 2020 Lukas Velikov */
// Copyright FSL Lab Stony Brook University
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
#include <boost/program_options.hpp>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#define PRE_LOG_MESSAGE std::left << std::setw(15) << ">>>>>>>>>>>"
#define POST_LOG_MESSAGE ""
#define PRINT_STATISTICS
boost::program_options::variables_map get_options(int argc, char *argv[]) {
namespace po = boost::program_options;
po::options_description generic("Generic options");
generic.add_options()("help,h", "lttng-client [-s, -d] -e [COMMAND]");
po::options_description config("Configuration");
config.add_options()("verbose,v", "prints execution logs")(
"session-directory,s", po::value<std::string>(),
"lttng session directory path")(
"exec,e", po::value<std::string>(),
"executable string which is going to be run through lttng")(
"ds-output,d", po::value<std::string>(), "ds output file path");
po::options_description test_program_parameters("test program parameters");
test_program_parameters.add_options()("test-program-parameters,p",
po::value<std::vector<std::string>>(),
"test program parameters");
po::options_description cmdline_options;
cmdline_options.add(generic).add(config).add(test_program_parameters);
po::options_description visible("Allowed options");
visible.add(generic).add(config);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv)
.options(cmdline_options)
.allow_unregistered()
.run(),
vm);
po::notify(vm);
if (vm.count("help") != 0u) {
std::cerr << visible << std::endl;
exit(EXIT_SUCCESS);
}
return vm;
}
void process_options(int argc, char *argv[], bool *verbose,
std::string *session_directory, std::string *exec_name,
std::string *ds_output_name, int *executable_idx) {
boost::program_options::variables_map options_vm = get_options(argc, argv);
if (options_vm.count("verbose") != 0u) {
*verbose = true;
}
if (options_vm.count("exec") != 0u) {
*exec_name = options_vm["exec"].as<std::string>();
} else {
assert(0);
}
if (options_vm.count("session-directory") != 0u) {
*session_directory = options_vm["session-directory"].as<std::string>();
} else {
*session_directory = "/strace2ds_test/session-capture";
}
if (options_vm.count("ds-output") != 0u) {
*ds_output_name = options_vm["ds-output"].as<std::string>();
} else {
*ds_output_name = (*exec_name) + ".ds";
}
for (int i = 0; i < argc; i++) {
auto parameter = argv[i];
std::string var(parameter);
if (var == "-e") {
*executable_idx = i;
}
}
}
void lttng_config(void) {
system(
"sudo lttng enable-channel channel0 -k --discard --num-subbuf 64 >> "
"lttng-client.log");
system(
"sudo lttng enable-event -s strace2ds-session -c channel0 --kernel "
"--all "
"--syscall >> lttng-client.log");
system(
"sudo lttng enable-event -s strace2ds-session -c channel0 --kernel "
"mm_filemap_add_to_page_cache >> lttng-client.log");
system(
"sudo lttng enable-event -s strace2ds-session -c channel0 --kernel "
"mm_filemap_fsl_read >> lttng-client.log");
system(
"sudo lttng enable-event -s strace2ds-session -c channel0 --kernel "
"fsl_writeback_dirty_page >> lttng-client.log");
// system(
// "lttng enable-event -s strace2ds-session -c channel0 --kernel "
// "writeback_dirty_inode >> lttng-client.log");
// system(
// "lttng enable-event -s strace2ds-session -c channel0 --kernel "
// "writeback_dirty_page >> lttng-client.log");
// system(
// "lttng enable-event -s strace2ds-session -c channel0 --kernel "
// "x86_exceptions_page_fault_user >> lttng-client.log");
system(
"sudo lttng add-context -k --session=strace2ds-session --type=tid >> "
"lttng-client.log");
system(
"sudo lttng add-context -k --session=strace2ds-session --type=pid >> "
"lttng-client.log");
}
int main(int argc, char *argv[]) {
int process_id, process_group_id;
int child_process_parameter_idx = 0;
bool verbose = false;
std::string executable_name, ds_output_name, session_directory;
std::ofstream report;
process_id = getpid();
process_group_id = getpgid(process_id);
std::chrono::high_resolution_clock::time_point *timers =
(std::chrono::high_resolution_clock::time_point *)mmap(
nullptr, sizeof(std::chrono::high_resolution_clock::time_point) * 2,
PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, 0, 0);
std::chrono::high_resolution_clock::time_point whole_trace_start =
std::chrono::high_resolution_clock::now();
process_options(argc, argv, &verbose, &session_directory, &executable_name,
&ds_output_name, &child_process_parameter_idx);
std::ios_base::fmtflags flags = std::cout.flags();
if (verbose) {
std::cout << PRE_LOG_MESSAGE << "parent pid " << process_id
<< " process group id " << process_group_id << POST_LOG_MESSAGE
<< std::endl;
}
int child_pid = fork();
if (child_pid == 0) {
process_id = getpid();
process_group_id = getpgid(process_id);
if (setpgid(process_id, process_id) != 0) {
std::cerr << "problem while setting process group id\n";
} else {
process_group_id = getpgid(process_id);
}
if (verbose) {
std::cout << PRE_LOG_MESSAGE << "child pid " << process_id
<< " child process group id " << process_group_id
<< POST_LOG_MESSAGE << std::endl;
}
std::string session_folder_create = "mkdir -p " + session_directory;
system(session_folder_create.c_str());
std::string create_session_cmd =
"sudo lttng create strace2ds-session --output=";
create_session_cmd += session_directory + " >> lttng-client.log";
system(create_session_cmd.c_str());
std::string tracking_str =
"sudo lttng track -k --pid=" + std::to_string(process_group_id) +
" >> lttng-client.log";
system(tracking_str.c_str());
lttng_config();
char *const exec_file = strdup(executable_name.c_str());
char *const env[] = {nullptr};
if (verbose) {
std::cout << PRE_LOG_MESSAGE << "lttng start capturing"
<< POST_LOG_MESSAGE << std::endl;
}
system("sudo lttng start strace2ds-session >> lttng-client.log");
timers[0] = std::chrono::high_resolution_clock::now();
execve(exec_file, (char *const *)&argv[child_process_parameter_idx + 1],
env);
}
waitpid(child_pid, nullptr, 0);
timers[1] = std::chrono::high_resolution_clock::now();
system("sudo lttng untrack -k --pid --all >> lttng-client.log");
if (verbose) {
std::cout << PRE_LOG_MESSAGE << "execution finished" << std::endl;
std::cout << PRE_LOG_MESSAGE << "lttng stop capturing" << POST_LOG_MESSAGE
<< std::endl;
}
system("sudo lttng stop strace2ds-session >> lttng-client.log");
std::chrono::high_resolution_clock::time_point whole_trace_end =
std::chrono::high_resolution_clock::now();
std::string permission_update = "sudo chmod -R 755 " + session_directory;
system(permission_update.c_str());
std::string babeltrace_cmd =
"babeltrace " + session_directory + "/kernel -w " + ds_output_name +
" -x /tmp/buffer-capture.dat" + " >> /dev/null";
// std::string babeltrace_cmd =
// "babeltrace " + session_directory + "/kernel " + " >> babeltrace.bt";
if (verbose) {
std::cout << PRE_LOG_MESSAGE << "babeltrace started" << std::endl;
}
std::chrono::high_resolution_clock::time_point babeltrace_start =
std::chrono::high_resolution_clock::now();
system(babeltrace_cmd.c_str());
std::chrono::high_resolution_clock::time_point babeltrace_stop =
std::chrono::high_resolution_clock::now();
if (verbose) {
std::cout << PRE_LOG_MESSAGE << "babeltrace ended" << std::endl;
}
auto babeltrace_timing =
std::chrono::duration_cast<std::chrono::milliseconds>(babeltrace_stop -
babeltrace_start)
.count();
auto total_tracing_timing =
std::chrono::duration_cast<std::chrono::milliseconds>(whole_trace_end -
whole_trace_start)
.count();
auto just_tracing_timing =
std::chrono::duration_cast<std::chrono::milliseconds>(timers[1] -
timers[0])
.count();
#ifdef PRINT_STATISTICS
std::cout << PRE_LOG_MESSAGE << std::left << std::setw(40)
<< "babeltrace timing" << std::left << std::setw(5) << ":"
<< std::left << std::setw(5) << babeltrace_timing << "\n";
std::cout << PRE_LOG_MESSAGE << std::left << std::setw(40)
<< "tracing total timing" << std::left << std::setw(5) << ":"
<< std::left << std::setw(5) << total_tracing_timing << "\n";
std::cout << PRE_LOG_MESSAGE << std::left << std::setw(40)
<< "tracing just for execution period timing" << std::left
<< std::setw(5) << ":" << std::left << std::setw(5)
<< just_tracing_timing << "\n";
#endif
report.open("report.txt", std::ios::app);
report << just_tracing_timing << "\t" << total_tracing_timing << "\t"
<< babeltrace_timing << "\n";
report.close();
system("sudo lttng destroy strace2ds-session >> /dev/null");
system("sudo rm -rf /tmp/buffer-capture.dat");
std::cout.flags(flags);
return 0;
}