• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef SUBCOMMAND_STAT_H_
16 #define SUBCOMMAND_STAT_H_
17 
18 #include "option.h"
19 #include "perf_events.h"
20 #include "subcommand.h"
21 
22 namespace OHOS {
23 namespace Developtools {
24 namespace HiPerf {
25 class SubCommandStat : public SubCommand {
26 public:
SubCommandStat()27     SubCommandStat()
28         : SubCommand("stat", "Collect performance counter information",
29                      // clang-format off
30         "Usage: hiperf stat [options] [command [command-args]]\n"
31         "       Collect performance counter information of running [command].\n"
32         "       The default options are: -c -1 -d 10000.0\n"
33         "   -a\n"
34         "         Collect system-wide information.\n"
35         "         for measures all processes/threads\n"
36         "         This requires CAP_PERFMON (since Linux 5.8) or\n"
37         "          CAP_SYS_ADMIN capability or a\n"
38         "         /proc/sys/kernel/perf_event_paranoid value of less than 1.\n"
39         "   -c <cpuid>[<,cpuid>]\n"
40         "         cpuid should be 0,1,2...\n"
41         "         Limit the CPU that collects data.\n"
42         "         0 means cpu0, 1 means cpu1 ...\n"
43         "   -d <sec>\n"
44         "         stop in <sec> seconds.\n"
45         "         floating point number.\n"
46         "         default is 10000.0\n"
47         "   -i <ms>\n"
48         "         print stat info every <ms>.\n"
49         "   -e event1[:<u|k>][,event1[:<u|k>]]...\n"
50         "         Customize the name of the event that needs to be counted.\n"
51         "         The name can use the names listed in the list parameter.\n"
52         "         It can also be represented by the value of 0x<hex>.\n"
53         "            u - monitor user space events only\n"
54         "            k - monitor kernel space events only\n"
55         "   -g <event1[:<u|k>]>[,event1[:<u|k>]]...\n"
56         "         The grouping function is added on the basis of the function of the -e parameter\n"
57         "         PMU is required to report data in designated groups\n"
58         "         limited by HW capability, too many events cannot be reported in the same sampling)\n"
59         "   --no-inherit\n"
60         "         Don't track new processes/threads.\n"
61         "   -p <pid1>[,pid2]...\n"
62         "         Limit the process id of the collection target. Conflicts with the -a option.\n"
63         "   -t <tid1>[,tid2]...\n"
64         "         Limit the thread id of the collection target. Conflicts with the -a option.\n"
65         "   --verbose\n"
66         "         Show more detailed reports.\n"
67                      // clang-format on
68                      ),
69           targetSystemWide_(false)
70     {
71     }
72 
73     bool OnSubCommand(std::vector<std::string> &args) override;
74     bool ParseOption(std::vector<std::string> &args) override;
75     void DumpOptions(void) const override;
76 
77 private:
78     PerfEvents perfEvents_;
79     bool targetSystemWide_ {false};
80     std::vector<int> selectCpus_ = {};
81     float timeStopSec_ = PerfEvents::DEFAULT_TIMEOUT;
82     int timeReportMs_ {0};
83     std::vector<std::vector<std::string>> selectEvents_;
84     std::vector<std::vector<std::string>> selectGroups_;
85     bool noCreateNew_ {false};
86     std::vector<pid_t> selectPids_;
87     std::vector<pid_t> selectTids_;
88     bool verboseReport_ {false};
89     std::vector<std::string> trackedCommand_ {};
90     bool helpOption_ {false};
91     bool CheckOptionPid(std::vector<pid_t> pids);
92     static bool FindEventCount(
93         const std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> &countEvents,
94         const std::string &configName, const __u64 group_id, __u64 &eventcount, double &scale);
95     static void GetComments(
96         const std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> &countEvents,
97         std::map<std::string, std::string> &comments);
98     static bool FindRunningTime(
99         const std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> &countEvents,
100         double &running_time_in_sec, __u64 &group_id, double &main_scale);
101     static bool IsMonitoredAtAllTime(const double &scale);
102     static std::string GetCommentConfigName(
103         const std::unique_ptr<PerfEvents::CountEvent> &countEvent, std::string eventName);
104 
105     static void Report(const std::map<std::string, std::unique_ptr<PerfEvents::CountEvent>> &);
106 
107     void PrintUsage();
HelpOption()108     inline bool HelpOption()
109     {
110         return helpOption_;
111     }
112     bool PrepairEvents();
113     bool CheckOptions(const std::vector<pid_t> &pids);
114     bool CheckSelectCpuPidOption();
115 };
116 
117 bool RegisterSubCommandStat(void);
118 } // namespace HiPerf
119 } // namespace Developtools
120 } // namespace OHOS
121 #endif // SUBCOMMAND_STAT_H_
122