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
19using namespace std;
20namespace OHOS {
21namespace Developtools {
22namespace HiPerf {
23bool SubCommandList::OnSubCommand(vector<string> &args)
24{
25    vector<perf_type_id> requestEventTypes;
26
27    SetHM();
28
29    if (args.empty()) {
30        // all the list
31        for (auto it : PERF_TYPES) {
32            requestEventTypes.push_back(it.first);
33        }
34    } else {
35        string requestEventType = args.front().c_str();
36        auto it = SUPPORT_NAME_OPTIONS.find(requestEventType);
37        if (it == SUPPORT_NAME_OPTIONS.end()) {
38            printf("not support option: '%s'\n", requestEventType.c_str());
39            return false;
40        } else {
41            requestEventTypes.push_back(it->second);
42        }
43    }
44    ShowSupportEventsTypes(requestEventTypes);
45    return true;
46}
47bool SubCommandList::ShowSupportEventsTypes(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
65void SubCommandList::RegisterSubCommandList()
66{
67    SubCommand::RegisterSubCommand("list", make_unique<SubCommandList>());
68}
69
70void 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} // namespace HiPerf
81} // namespace Developtools
82} // namespace OHOS
83