blob: 6778465ed063c80f30ddeec3640eadf338ed5378 [file] [log] [blame]
Yi Konge5576ae2020-08-05 02:00:05 +08001//
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//! Command to control profcollectd behaviour.
18
Yi Konge3aab142021-03-02 13:58:25 +080019use anyhow::{bail, Context, Result};
Yi Konge5576ae2020-08-05 02:00:05 +080020use std::env;
21
Yi Konge3aab142021-03-02 13:58:25 +080022const HELP_MSG: &str = r#"
Yi Konge5576ae2020-08-05 02:00:05 +080023usage: profcollectctl [command]
24
25Command to control profcollectd behaviour.
26
27command:
28 start Schedule periodic collection.
29 stop Terminate periodic collection.
30 once Request an one-off trace.
31 process Convert traces to perf profiles.
32 reconfig Refresh configuration.
Yi Kong34ebf872021-11-29 19:57:55 +080033 report Create a report containing all profiles.
34 reset Clear all local data.
Yi Konge5576ae2020-08-05 02:00:05 +080035 help Print this message.
Yi Konge3aab142021-03-02 13:58:25 +080036"#;
Yi Konge5576ae2020-08-05 02:00:05 +080037
Yi Konge3aab142021-03-02 13:58:25 +080038fn main() -> Result<()> {
39 libprofcollectd::init_logging();
40
Yi Konge5576ae2020-08-05 02:00:05 +080041 let args: Vec<String> = env::args().collect();
42 if args.len() != 2 {
Yi Konge3aab142021-03-02 13:58:25 +080043 bail!("This program only takes one argument{}", &HELP_MSG);
Yi Konge5576ae2020-08-05 02:00:05 +080044 }
45
46 let action = &args[1];
47 match action.as_str() {
48 "start" => {
49 println!("Scheduling profile collection");
Yi Konge3aab142021-03-02 13:58:25 +080050 libprofcollectd::schedule().context("Failed to schedule collection.")?;
Yi Konge5576ae2020-08-05 02:00:05 +080051 }
52 "stop" => {
53 println!("Terminating profile collection");
Yi Konge3aab142021-03-02 13:58:25 +080054 libprofcollectd::terminate().context("Failed to terminate collection.")?;
Yi Konge5576ae2020-08-05 02:00:05 +080055 }
56 "once" => {
57 println!("Trace once");
Yi Konge3aab142021-03-02 13:58:25 +080058 libprofcollectd::trace_once("manual").context("Failed to trace.")?;
Yi Konge5576ae2020-08-05 02:00:05 +080059 }
60 "process" => {
Yi Konge3aab142021-03-02 13:58:25 +080061 println!("Processing traces");
62 libprofcollectd::process().context("Failed to process traces.")?;
Yi Konge5576ae2020-08-05 02:00:05 +080063 }
Yi Kong4e25de72020-09-08 14:43:33 +080064 "report" => {
Yi Konge3aab142021-03-02 13:58:25 +080065 println!("Creating profile report");
Yi Kong037bde82021-03-23 14:25:38 +080066 let path = libprofcollectd::report().context("Failed to create profile report.")?;
67 println!("Report created at: {}", &path);
Yi Kong4e25de72020-09-08 14:43:33 +080068 }
Yi Kong34ebf872021-11-29 19:57:55 +080069 "reset" => {
70 libprofcollectd::reset().context("Failed to reset.")?;
71 println!("Reset done.");
72 }
Yi Konge3aab142021-03-02 13:58:25 +080073 "help" => println!("{}", &HELP_MSG),
74 arg => bail!("Unknown argument: {}\n{}", &arg, &HELP_MSG),
Yi Konge5576ae2020-08-05 02:00:05 +080075 }
Yi Konge3aab142021-03-02 13:58:25 +080076 Ok(())
Yi Konge5576ae2020-08-05 02:00:05 +080077}