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