1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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/file.h>
17 #include <unistd.h>
18 
19 #include "common_types.pb.h"
20 #include "trace_file_reader.h"
21 #include "xpower_decoder.h"
22 
23 namespace {
24 using UsageHandle = std::function<void(void)>;
25 static std::map<std::string, std::string> params;
26 
ParseArgs(int argc, char** argv, UsageHandle usage)27 int ParseArgs(int argc, char** argv, UsageHandle usage)
28 {
29     params.clear();
30     for (int i = 1; i < argc;) {
31         std::string key = argv[i];
32         i++;
33         if (i >= argc) {
34             if (usage) usage();
35             break;
36         }
37         std::string val = argv[i];
38         i++;
39         params.insert(std::make_pair(key, val));
40     }
41     return params.size();
42 }
43 
GetStringArg(const char* name, std::string& val, const char* defaultVal)44 int GetStringArg(const char* name, std::string& val, const char* defaultVal)
45 {
46     val = params[name];
47     if (val.empty()) {
48         val = defaultVal;
49     }
50     return val.size();
51 }
52 
Usage()53 void Usage()
54 {
55     printf("usage: xpowerdecoder [-f filepath] \n");
56 }
57 
58 } // namespace
59 
60 
main(int argc, char* argv[])61 int main(int argc, char* argv[])
62 {
63     std::string filePath;
64     int ret = ParseArgs(argc, argv, Usage);
65     if (ret == 0) {
66         std::cout << "parse parameters error!" << std::endl;
67         return 0;
68     }
69     GetStringArg("-f", filePath, "/data/local/tmp/hiprofiler_data.htrace");
70     auto reader = std::make_shared<TraceFileReader>();
71     if (!reader->Open(filePath)) {
72         std::cout << "open file :" << filePath << "failed!" << std::endl;
73         return 0;
74     }
75     long bytes = 0;
76     do {
77         ProfilerPluginData data{};
78         bytes = reader->Read(data);
79         if (data.data().size() <= 0) {
80             continue;
81         }
82         std::cout << "name=" << data.name() << ",status=" << data.status() << ",tv_sec=" << data.tv_sec()
83                   << ",tv_nsec=" << data.tv_nsec() << ",version=" << data.version() << std::endl;
84 
85         OptimizeReport dataProto;
86         if (!dataProto.ParseFromArray(data.data().data(), data.data().size())) {
87             std::cout << "parse profiler plugin data failed!" << std::endl;
88             continue;
89         }
90         std::unique_ptr<XpowerDecoder> decoderPtr = std::make_unique<XpowerDecoder>();
91         std::cout << decoderPtr->DecodeXpowerMessage(dataProto) << std::endl;
92     } while (bytes > 0);
93     return 0;
94 }
95