• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 #ifndef DPROF_CONVERTER_FEATURES_HOTNESS_COUNTERS_H
17 #define DPROF_CONVERTER_FEATURES_HOTNESS_COUNTERS_H
18 
19 #include "macros.h"
20 #include "features_manager.h"
21 #include "dprof/storage.h"
22 #include "utils/logger.h"
23 #include "serializer/serializer.h"
24 
25 #include <list>
26 
27 namespace ark::dprof {
28 // NOLINTNEXTLINE(modernize-avoid-c-arrays)
29 inline const char HCOUNTERS_FEATURE_NAME[] = "hotness_counters.v1";
30 
31 class HCountersFunctor : public FeaturesManager::Functor {
32     struct HCountersInfo {
33         struct MethodInfo {
34             std::string name;
35             uint32_t value;
36         };
37         std::string appName;
38         uint64_t hash;
39         uint32_t pid;
40         std::list<MethodInfo> methodsList;
41     };
42 
43 public:
HCountersFunctor(std::ostream & out)44     explicit HCountersFunctor(std::ostream &out) : out_(out) {}
45     ~HCountersFunctor() = default;
46 
operator()47     bool operator()(const AppData &appData, const std::vector<uint8_t> &data) override
48     {
49         std::unordered_map<std::string, uint32_t> methodInfoMap;
50         if (!serializer::BufferToType(data.data(), data.size(), methodInfoMap)) {
51             LOG(ERROR, DPROF) << "Cannot deserialize methodInfoMap";
52             return false;
53         }
54 
55         std::list<HCountersInfo::MethodInfo> methodsList;
56         for (auto &it : methodInfoMap) {
57             methodsList.emplace_back(HCountersInfo::MethodInfo {it.first, it.second});
58         }
59 
60         hcountersInfoList_.emplace_back(
61             HCountersInfo {appData.GetName(), appData.GetHash(), appData.GetPid(), std::move(methodsList)});
62 
63         return true;
64     }
65 
ShowInfo(const std::string & format)66     bool ShowInfo(const std::string &format)
67     {
68         if (hcountersInfoList_.empty()) {
69             return false;
70         }
71 
72         if (format == "text") {
73             ShowText();
74         } else if (format == "json") {
75             ShowJson();
76         } else {
77             LOG(ERROR, DPROF) << "Unknown format: " << format << std::endl;
78             return false;
79         }
80         return true;
81     }
82 
83 private:
ShowText()84     void ShowText()
85     {
86         out_ << "Feature: " << HCOUNTERS_FEATURE_NAME << std::endl;
87         for (auto &hcountersInfo : hcountersInfoList_) {
88             out_ << "  app: name=" << hcountersInfo.appName << " pid=" << hcountersInfo.pid
89                  << " hash=" << hcountersInfo.hash << std::endl;
90 
91             for (auto &methodInfo : hcountersInfo.methodsList) {
92                 out_ << "    " << methodInfo.name << ":" << methodInfo.value << std::endl;
93             }
94         }
95     }
96 
ShowJson()97     void ShowJson()
98     {
99         out_ << "{" << std::endl;
100         out_ << "  \"" << HCOUNTERS_FEATURE_NAME << "\": [" << std::endl;
101         for (auto &hcountersInfo : hcountersInfoList_) {
102             out_ << "    {" << std::endl;
103             out_ << R"(      "app_name": ")" << hcountersInfo.appName << "\"," << std::endl;
104             out_ << R"(      "pid": ")" << hcountersInfo.pid << "\"," << std::endl;
105             out_ << R"(      "hash": ")" << hcountersInfo.hash << "\"," << std::endl;
106             out_ << R"(      "counters": [)" << std::endl;
107             for (auto &methodInfo : hcountersInfo.methodsList) {
108                 out_ << "        {" << std::endl;
109                 out_ << R"(          "name": ")" << methodInfo.name << "\"," << std::endl;
110                 out_ << R"(          "value": ")" << methodInfo.value << "\"" << std::endl;
111                 out_ << "        }";
112                 if (&methodInfo != &hcountersInfo.methodsList.back()) {
113                     out_ << ",";
114                 }
115                 out_ << std::endl;
116             }
117             out_ << "      ]" << std::endl;
118             out_ << "    }";
119             if (&hcountersInfo != &hcountersInfoList_.back()) {
120                 out_ << ",";
121             }
122             out_ << std::endl;
123         }
124         out_ << "  ]" << std::endl;
125         out_ << "}" << std::endl;
126     }
127 
128     std::list<HCountersInfo> hcountersInfoList_;
129     std::ostream &out_;
130 
131     NO_COPY_SEMANTIC(HCountersFunctor);
132     NO_MOVE_SEMANTIC(HCountersFunctor);
133 };
134 }  // namespace ark::dprof
135 
136 #endif  // DPROF_CONVERTER_FEATURES_HOTNESS_COUNTERS_H
137