1 /*
2 * Copyright (c) 2021 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
16 #include <sys/utsname.h>
17 #include "subcommand_list.h"
18
19 namespace OHOS {
20 namespace Developtools {
21 namespace HiPerf {
OnSubCommand(std::vector<std::string> & args)22 HiperfError SubCommandList::OnSubCommand(std::vector<std::string>& args)
23 {
24 std::vector<perf_type_id> requestEventTypes;
25
26 SetHM();
27
28 if (args.empty()) {
29 // all the list
30 for (auto it : PERF_TYPES) {
31 requestEventTypes.push_back(it.first);
32 }
33 } else {
34 std::string requestEventType = args.front();
35 auto it = SUPPORT_NAME_OPTIONS.find(requestEventType);
36 if (it == SUPPORT_NAME_OPTIONS.end()) {
37 printf("not support option: '%s'\n", requestEventType.c_str());
38 return HiperfError::OPTION_NOT_SUPPORT;
39 } else {
40 requestEventTypes.push_back(it->second);
41 }
42 }
43 ShowSupportEventsTypes(requestEventTypes);
44 return HiperfError::NO_ERR;
45 }
46
ShowSupportEventsTypes(std::vector<perf_type_id> & requestEventTypes)47 bool SubCommandList::ShowSupportEventsTypes(std::vector<perf_type_id> &requestEventTypes)
48 {
49 // each type
50 for (perf_type_id id : requestEventTypes) {
51 // each config
52 auto configs = perfEvents_.GetSupportEvents(id);
53 printf("\nSupported events for %s:\n", PERF_TYPES.at(id).c_str());
54 for (auto config : configs) {
55 printf("\t%s\n", config.second.c_str());
56 }
57 if (configs.size() == 0) {
58 return false; // support nothing
59 }
60 }
61 std::cout << std::endl;
62 return true;
63 };
64
RegisterSubCommandList()65 void SubCommandList::RegisterSubCommandList()
66 {
67 SubCommand::RegisterSubCommand("list", SubCommandList::GetInstance);
68 }
69
SetHM()70 void SubCommandList::SetHM()
71 {
72 utsname unameBuf;
73 if ((uname(&unameBuf)) == 0) {
74 std::string osrelease = unameBuf.release;
75 isHM_ = osrelease.find(HMKERNEL) != std::string::npos;
76 }
77 perfEvents_.SetHM(isHM_);
78 HLOGD("Set isHM_: %d", isHM_);
79 }
80
GetInstance()81 SubCommand& SubCommandList::GetInstance()
82 {
83 static SubCommandList subCommand;
84 return subCommand;
85 }
86 } // namespace HiPerf
87 } // namespace Developtools
88 } // namespace OHOS
89