Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 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 | //! Command to control profcollectd behaviour. |
| 18 | |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 19 | use anyhow::{bail, Context, Result}; |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 20 | use std::env; |
| 21 | |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 22 | const HELP_MSG: &str = r#" |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 23 | usage: profcollectctl [command] |
| 24 | |
| 25 | Command to control profcollectd behaviour. |
| 26 | |
| 27 | command: |
| 28 | start Schedule periodic collection. |
| 29 | stop Terminate periodic collection. |
Yi Kong | c26c15b | 2024-07-06 17:20:44 +0900 | [diff] [blame] | 30 | trace Request an one-off system-wide trace. |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 31 | process Convert traces to perf profiles. |
Yi Kong | 34ebf87 | 2021-11-29 19:57:55 +0800 | [diff] [blame] | 32 | report Create a report containing all profiles. |
| 33 | reset Clear all local data. |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 34 | help Print this message. |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 35 | "#; |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 36 | |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 37 | fn main() -> Result<()> { |
| 38 | libprofcollectd::init_logging(); |
| 39 | |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 40 | let args: Vec<String> = env::args().collect(); |
| 41 | if args.len() != 2 { |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 42 | bail!("This program only takes one argument{}", &HELP_MSG); |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | let action = &args[1]; |
| 46 | match action.as_str() { |
| 47 | "start" => { |
| 48 | println!("Scheduling profile collection"); |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 49 | libprofcollectd::schedule().context("Failed to schedule collection.")?; |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 50 | } |
| 51 | "stop" => { |
| 52 | println!("Terminating profile collection"); |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 53 | libprofcollectd::terminate().context("Failed to terminate collection.")?; |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 54 | } |
Yi Kong | c26c15b | 2024-07-06 17:20:44 +0900 | [diff] [blame] | 55 | "trace" => { |
| 56 | println!("Performing system-wide trace"); |
| 57 | libprofcollectd::trace_system("manual").context("Failed to trace.")?; |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 58 | } |
| 59 | "process" => { |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 60 | println!("Processing traces"); |
| 61 | libprofcollectd::process().context("Failed to process traces.")?; |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 62 | } |
Yi Kong | 4e25de7 | 2020-09-08 14:43:33 +0800 | [diff] [blame] | 63 | "report" => { |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 64 | println!("Creating profile report"); |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 65 | let path = libprofcollectd::report().context("Failed to create profile report.")?; |
| 66 | println!("Report created at: {}", &path); |
Yi Kong | 4e25de7 | 2020-09-08 14:43:33 +0800 | [diff] [blame] | 67 | } |
Yi Kong | 34ebf87 | 2021-11-29 19:57:55 +0800 | [diff] [blame] | 68 | "reset" => { |
| 69 | libprofcollectd::reset().context("Failed to reset.")?; |
| 70 | println!("Reset done."); |
| 71 | } |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 72 | "help" => println!("{}", &HELP_MSG), |
| 73 | arg => bail!("Unknown argument: {}\n{}", &arg, &HELP_MSG), |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 74 | } |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 75 | Ok(()) |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 76 | } |