• 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 
16 #include "prof_dump_json.h"
17 #include "prof_convert_json.h"
18 #include "ecmascript/log_wrapper.h"
19 #include "ecmascript/pgo_profiler/pgo_profiler_manager.h"
20 #include "ecmascript/pgo_profiler/pgo_utils.h"
21 
22 namespace native {
23 using namespace panda::ecmascript::pgo;
24 static std::string g_result = "";
25 
ConvertApToJson(const char * path,const size_t pathSize)26 size_t ConvertApToJson(const char *path, const size_t pathSize)
27 {
28     std::string apFilePath(path, pathSize);
29     PGOProfilerDecoder decoder(apFilePath, 1);
30     if (!decoder.LoadFull()) {
31         return 0;
32     }
33     // parse recordDetailInofo
34     std::vector<ProfileType::jModuleType> modules;
35     auto recordDetailInfos = decoder.GetRecordDetailInfosPtr();
36     auto recordInfos = recordDetailInfos->GetRecordInfos();
37     auto recordPool = recordDetailInfos->GetRecordPool();
38     for (auto iter = recordInfos.begin(); iter != recordInfos.end(); iter++) {
39         std::string moduleName(recordPool->GetName(iter->first));
40         if (moduleName.empty()) {
41             continue;
42         }
43         ProfileType::jModuleType oneModule;
44         oneModule.insert(std::make_pair(DumpJsonUtils::MODULE_NAME, moduleName));
45         iter->second->ProcessToJson(1, oneModule);
46         modules.push_back(oneModule);
47     }
48     // parse abcFilePool
49     cJSON *allMessage = cJSON_CreateObject();
50     if (allMessage == nullptr) {
51         return 0;
52     }
53 
54     JsonConverter convert;
55     cJSON* recordDetailInofo = convert.Convert(modules);
56     cJSON_AddItemToObject(allMessage, DumpJsonUtils::RECORD_DETAIL.c_str(), recordDetailInofo);
57 
58     std::vector<ProfileType::StringMap> abcFilePoolMessage;
59     decoder.GetAbcFilePool()->GetPool()->ProcessToJson(abcFilePoolMessage);
60     cJSON* abcFilePool = convert.ConvertStr(abcFilePoolMessage);
61     cJSON_AddItemToObject(allMessage, DumpJsonUtils::ABC_FILE_POOL.c_str(), abcFilePool);
62 
63     char *data = cJSON_PrintUnformatted(allMessage);
64     if (data == nullptr) {
65         cJSON_Delete(allMessage);
66         LOG_NO_TAG(ERROR) << "Failed to convert cJSON object to string";
67         return 0;
68     }
69     g_result = std::string(data);
70     cJSON_Delete(allMessage);
71     cJSON_free(data);
72     return g_result.length();
73 }
74 
GetConvertResult(char * buffer,size_t length)75 bool GetConvertResult(char* buffer, size_t length)
76 {
77     if (!g_result.empty()) {
78         if (memcpy_s(buffer, length, g_result.c_str(), g_result.length()) != EOK) {
79             return false;
80         }
81         g_result.clear();
82         return true;
83     }
84     return false;
85 }
86 } // namespace native
87