1 //
2 // Copyright (C) 2020 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <stdlib.h>
18 
19 #include <iostream>
20 
21 #include "libprofcollectd.h"
22 
23 namespace profcollectd = android::profcollectd;
24 
25 namespace {
26 
PrintHelp(const std::string & reason="")27 void PrintHelp(const std::string& reason = "") {
28   std::cout << reason;
29   std::cout << R"(
30 usage: profcollectctl [command]
31 command:
32     start       Schedule periodic collection.
33     stop        Terminate periodic collection.
34     once        Request an one-off trace.
35     process     Convert traces to perf profiles.
36     reconfig    Refresh configuration.
37     help        Print this message.
38 )";
39 }
40 
41 }  // namespace
42 
main(int argc,char ** argv)43 int main(int argc, char** argv) {
44   if (argc != 2) {
45     PrintHelp("Invalid arguments");
46     exit(EXIT_FAILURE);
47   }
48 
49   std::string command(argv[1]);
50   if (command == "start") {
51     std::cout << "Scheduling profile collection\n";
52     profcollectd::ScheduleCollection();
53   } else if (command == "stop") {
54     std::cout << "Terminating profile collection\n";
55     profcollectd::TerminateCollection();
56   } else if (command == "once") {
57     std::cout << "Trace once\n";
58     profcollectd::TraceOnce();
59   } else if (command == "process") {
60     std::cout << "Processing traces\n";
61     profcollectd::Process();
62   } else if (command == "reconfig") {
63     std::cout << "Refreshing configuration\n";
64     profcollectd::ReadConfig();
65   } else if (command == "help") {
66     PrintHelp();
67   } else {
68     PrintHelp("Unknown command: " + command);
69     exit(EXIT_FAILURE);
70   }
71 }
72