• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include <chrono>
16 #include <iostream>
17 #include <string>
18 
19 #include "feature_analysis.h"
20 #include "file_util.h"
21 #include "hiview_logger.h"
22 #include "rule.h"
23 #include "smart_parser.h"
24 #include "tbox.h"
25 
26 DEFINE_LOG_TAG("SmartParser");
27 
28 static const char* const SMART_PARSER_PATH = "/system/etc/hiview/";
29 
PrintEventInfo(const std::map<std::string,std::string> & eventInfo,const std::string & msg)30 static void PrintEventInfo(const std::map<std::string, std::string>& eventInfo, const std::string& msg)
31 {
32     HIVIEW_LOGI("%{public}s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", msg.c_str());
33     HIVIEW_LOGI("eventInfo size : %{public}s", std::to_string(eventInfo.size()).c_str());
34     for (auto &[key, val] : eventInfo) {
35         std::istringstream iss(val);
36         std::string line;
37 
38         while (std::getline(iss, line)) {
39             HIVIEW_LOGI("[%{public}s] : %{public}s", key.c_str(), line.c_str());
40         }
41     }
42 }
43 
SmartParser(const std::string & eventPath,const std::string & eventType)44 static std::map<std::string, std::string> SmartParser(const std::string& eventPath, const std::string& eventType)
45 {
46     auto startTime = std::chrono::high_resolution_clock::now();
47     auto eventInfos = OHOS::HiviewDFX::SmartParser::Analysis(eventPath, SMART_PARSER_PATH, eventType);
48     auto endTime = std::chrono::high_resolution_clock::now();
49     auto diffTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
50     std::cout << "SmartParser::Analysis running time : " << diffTime << " ms" << std::endl;
51 
52     PrintEventInfo(eventInfos, "SmartParser::Analysis result:");
53 
54     return eventInfos;
55 }
56 
Tbox(std::map<std::string,std::string> & eventInfo,std::string & eventType)57 static void Tbox(std::map<std::string, std::string>& eventInfo, std::string& eventType)
58 {
59     auto startTime = std::chrono::high_resolution_clock::now();
60     OHOS::HiviewDFX::Tbox::FilterTrace(eventInfo, eventType);
61     auto endTime = std::chrono::high_resolution_clock::now();
62     auto diffTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
63     std::cout << "Tbox::FilterTrace running time : " << diffTime << " ms" << std::endl;
64 
65     PrintEventInfo(eventInfo, "Tbox::FilterTrace result:");
66 }
67 
SetCpuAffinity(uint16_t mask)68 static bool SetCpuAffinity(uint16_t mask)
69 {
70     cpu_set_t cpuset;
71     pthread_t thread = pthread_self();
72 
73     CPU_ZERO(&cpuset);
74     const int maxCores = 12;
75     for (int j = 0; j < maxCores; j++) {
76         if (mask & (1U << j)) {
77             CPU_SET(j, &cpuset);
78         }
79     }
80 
81     int ret = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
82     return ret == 0;
83 }
84 
main(int argc,char * argv[])85 int main(int argc, char *argv[])
86 {
87     static uint16_t cpuMask = 0x0f;
88     SetCpuAffinity(cpuMask);
89 
90     std::string eventType;
91     std::string logPath;
92     for (int i = 1; i < argc; i++) {
93         if (std::string(argv[i]) == "-t") {
94             if (i + 1 < argc) {
95                 eventType = argv[i + 1];
96             }
97         } else if (std::string(argv[i]) == "-f") {
98             if (i + 1 < argc) {
99                 logPath = argv[i + 1];
100             }
101         }
102     }
103 
104     if (eventType.empty() || logPath.empty()) {
105         std::cout << "Usage:" << std::endl;
106         std::cout << "\t" << argv[0] << " -t eventType -f filePath" << std::endl;
107         std::cout << "\teventType\t" <<
108             "The event name must match the event name configured in the configuration file." << std::endl;
109         std::cout << "\tfilePath\t" <<
110             "The parsed file path must match the path matching rule configured in the configuration file." << std::endl;
111         return -1;
112     }
113     std::cout << ">>>>> eventType : " << eventType << std::endl;
114     std::cout << ">>>>> logPath : " << logPath << std::endl;
115     std::cout << std::endl;
116 
117     auto eventInfos = SmartParser(logPath, eventType);
118     Tbox(eventInfos, eventType);
119 
120     return 0;
121 }
122