1 /*
2 * Copyright (c) 2021-2022 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 "runtime/include/relayout_profiler.h"
17
18 #include "libpandabase/os/filesystem.h"
19 #include "runtime/include/runtime.h"
20
21 namespace panda {
WriteProfileData()22 void RelayoutProfiler::WriteProfileData()
23 {
24 LOG(INFO, RUNTIME) << "start writing profile data!";
25 LOG(INFO, RUNTIME) << "relayout profile location is: "
26 << Runtime::GetCurrent()->GetOptions().GetAbcLayoutProfileLocation();
27 PandaString profile_record_root(Runtime::GetCurrent()->GetOptions().GetAbcLayoutProfileLocation());
28
29 for (auto &file : relayoutItems_) {
30 auto pos = file.first.find_last_of('/') + 1;
31 PandaString file_name = file.first.substr(pos, file.first.length() - pos);
32 PandaString profile_file_name = profile_record_root + file_name + ".txt";
33
34 panda::os::unix::file::File profile_file =
35 panda::os::file::Open(profile_file_name, panda::os::file::Mode::READWRITECREATE);
36 if (!profile_file.IsValid()) {
37 LOG(ERROR, RUNTIME) << "Cannot open the profile file " << profile_file_name;
38 return;
39 }
40 panda::os::file::FileHolder fholder(profile_file);
41
42 for (auto &item : file.second[RelayoutProfiler::RelayoutItemType::CLASS_ITEM]) {
43 // write classItem
44 PandaString item_with_line = PandaString("class_item:") + item + "\n";
45 auto ret = profile_file.Write(item_with_line.c_str(), item_with_line.size());
46 if (!ret) {
47 LOG(ERROR, RUNTIME) << "record " << item_with_line << " failed! " << ret.Error().ToString();
48 }
49 }
50 for (auto &item : file.second[RelayoutProfiler::RelayoutItemType::STRING_ITEM]) {
51 // write StringItem
52 PandaString item_with_line = PandaString("string_item:") + item + "\n";
53 auto ret = profile_file.Write(item_with_line.c_str(), item_with_line.size());
54 if (!ret) {
55 LOG(ERROR, RUNTIME) << "record " << item_with_line << " failed! " << ret.Error().ToString();
56 }
57 }
58 for (auto &item : file.second[RelayoutProfiler::RelayoutItemType::CODE_ITEM]) {
59 // write CodeItem
60 PandaString item_with_line = PandaString("code_item:") + item + "\n";
61 auto ret = profile_file.Write(item_with_line.c_str(), item_with_line.size());
62 if (!ret) {
63 LOG(ERROR, RUNTIME) << "record " << item_with_line << " failed! " << ret.Error().ToString();
64 }
65 }
66 }
67 }
68
AddProfileItem(const std::string & file_name,const std::string & item,RelayoutItemType type)69 void RelayoutProfiler::AddProfileItem(const std::string &file_name, const std::string &item, RelayoutItemType type)
70 {
71 auto panda_file_name = ConvertToString(file_name);
72 auto panda_item = ConvertToString(item);
73
74 if (relayoutItems_.count(panda_file_name) == 0) {
75 PandaUnorderedMap<RelayoutItemType, PandaSet<PandaString>, std::hash<int>> items;
76 relayoutItems_.insert(std::make_pair(panda_file_name, std::move(items)));
77 }
78
79 relayoutItems_[panda_file_name][type].insert(panda_item);
80 }
81
AddProfileClassItem(Class * klass)82 void RelayoutProfiler::AddProfileClassItem(Class *klass)
83 {
84 std::string file_name =
85 klass->GetPandaFile() == nullptr ? std::string("anonymous") : klass->GetPandaFile()->GetFilename();
86 std::string item = klass->GetName();
87 AddProfileItem(file_name, item, RelayoutProfiler::RelayoutItemType::CLASS_ITEM);
88 }
89
AddProfileCodeItem(Method * method)90 void RelayoutProfiler::AddProfileCodeItem(Method *method)
91 {
92 std::string file_name = method->GetPandaFile()->GetFilename();
93 std::string item = PandaStringToStd(method->GetFullName());
94 AddProfileItem(file_name, item, RelayoutProfiler::RelayoutItemType::CODE_ITEM);
95 }
96
GetProfileData()97 ProfileDataType *RelayoutProfiler::GetProfileData()
98 {
99 return &relayoutItems_;
100 }
101 } // namespace panda
102