1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <sstream>
6 #include <string>
7
8 #include "base/logging.h"
9
10 #include "compat/string.h"
11 #include "file_utils.h"
12 #include "perf_recorder.h"
13
14 namespace {
15
16 const char kDefaultOutputFile[] = "/dev/stdout";
17
StringToInt(const string & s)18 int StringToInt(const string& s) {
19 int r;
20 std::stringstream ss;
21 ss << s;
22 ss >> r;
23 return r;
24 }
25
ParseArguments(int argc,char * argv[],std::vector<string> * perf_args,int * duration)26 bool ParseArguments(int argc, char* argv[], std::vector<string>* perf_args,
27 int* duration) {
28 if (argc < 3) {
29 LOG(ERROR) << "Invalid command line.";
30 LOG(ERROR) << "Usage: " << argv[0] << " <duration in seconds>"
31 << " <path to perf>"
32 << " <perf arguments>";
33 return false;
34 }
35
36 *duration = StringToInt(argv[1]);
37
38 for (int i = 2; i < argc; i++) {
39 perf_args->emplace_back(argv[i]);
40 }
41 return true;
42 }
43
44 } // namespace
45
46 // Usage is:
47 // <exe> <duration in seconds> <perf command line>
main(int argc,char * argv[])48 int main(int argc, char* argv[]) {
49 std::vector<string> perf_args;
50 int perf_duration;
51
52 if (!ParseArguments(argc, argv, &perf_args, &perf_duration)) return 1;
53
54 quipper::PerfRecorder perf_recorder;
55 string output_string;
56 if (!perf_recorder.RunCommandAndGetSerializedOutput(perf_args, perf_duration,
57 &output_string)) {
58 return 1;
59 }
60
61 if (!quipper::BufferToFile(kDefaultOutputFile, output_string)) return 1;
62
63 return 0;
64 }
65