2 * Copyright 2008-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
17 #include "time_stats.h"
20 #define SCHEDULING_ANALYSIS_BUFFER_SIZE 10 * 1024 * 1024
23 extern const char* __progname
;
25 static const char* kUsage
=
26 "Usage: %s [ <options> ] <command line>\n"
27 "Executes the given command line <command line> and print an analysis of\n"
28 "the user and kernel times of all threads that ran during that time.\n"
31 " -b <size> - When doing scheduling analysis: the size of the buffer\n"
33 " -h, --help - Print this usage info.\n"
34 " -o <output> - Print the results to file <output>.\n"
35 " -s - Also perform a scheduling analysis over the time the\n"
36 " child process ran. This requires that scheduler kernel\n"
37 " tracing had been enabled at compile time.\n"
42 print_usage_and_exit(bool error
)
44 fprintf(error
? stderr
: stdout
, kUsage
, __progname
);
50 main(int argc
, const char* const* argv
)
52 const char* outputFile
= NULL
;
53 bool schedulingAnalysis
= false;
54 size_t bufferSize
= SCHEDULING_ANALYSIS_BUFFER_SIZE
;
57 static struct option sLongOptions
[] = {
58 { "help", no_argument
, 0, 'h' },
59 { "output", required_argument
, 0, 'o' },
63 opterr
= 0; // don't print errors
64 int c
= getopt_long(argc
, (char**)argv
, "+b:ho:s", sLongOptions
, NULL
);
70 bufferSize
= atol(optarg
);
71 if (bufferSize
< 1 || bufferSize
> 1024) {
72 fprintf(stderr
, "Error: Invalid buffer size. Should be "
73 "between 1 and 1024 MB\n");
76 bufferSize
*= 1024 * 1024;
79 print_usage_and_exit(false);
85 schedulingAnalysis
= true;
89 print_usage_and_exit(true);
95 print_usage_and_exit(true);
97 // open output file, if specified
99 if (outputFile
!= NULL
) {
100 outFD
= open(outputFile
, O_WRONLY
| O_CREAT
| O_TRUNC
,
101 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
103 fprintf(stderr
, "Error: Failed to open \"%s\": %s\n", outputFile
,
109 do_timing_analysis(argc
- optind
, argv
+ optind
, schedulingAnalysis
, outFD
,