1 /*
2 * Copyright (c) 2023 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 "maple_phase_support.h"
17
18 #include "cgfunc.h"
19 namespace maple {
20 #ifdef ARK_LITECG_DEBUG
RunBeforePhase(const MaplePhaseInfo & pi)21 void PhaseTimeHandler::RunBeforePhase(const MaplePhaseInfo &pi)
22 {
23 (void)pi;
24 if (isMultithread) {
25 std::thread::id tid = std::this_thread::get_id();
26 if (!multiTimers.count(tid)) {
27 multiTimers.emplace(std::make_pair(tid, allocator.New<MPLTimer>()));
28 }
29 multiTimers[tid]->Start();
30 } else {
31 timer.Start();
32 }
33 }
34
RunAfterPhase(const MaplePhaseInfo & pi)35 void PhaseTimeHandler::RunAfterPhase(const MaplePhaseInfo &pi)
36 {
37 long usedTime = 0;
38 if (isMultithread) {
39 std::thread::id tid = std::this_thread::get_id();
40 if (multiTimers.count(tid)) {
41 multiTimers[tid]->Stop();
42 usedTime += multiTimers[tid]->ElapsedMicroseconds();
43 } else {
44 DEBUG_ASSERT(false, " phase time handler create failed");
45 }
46 } else {
47 timer.Stop();
48 usedTime = timer.ElapsedMicroseconds();
49 }
50 std::string phaseName = pi.PhaseName();
51 if (phaseTimeRecord.count(phaseName)) {
52 phaseTimeRecord[phaseName] += usedTime;
53 } else {
54 auto ret = phaseTimeRecord.emplace(std::make_pair(phaseName, usedTime));
55 if (ret.second) {
56 originOrder.push_back(ret.first);
57 }
58 }
59 phaseTotalTime += usedTime;
60 }
61
DumpPhasesTime()62 void PhaseTimeHandler::DumpPhasesTime()
63 {
64 auto TimeLogger = [](const std::string &itemName, time_t itemTimeUs, time_t totalTimeUs) {
65 LogInfo::MapleLogger() << std::left << std::setw(25) << std::setfill(' ') << itemName << std::setw(10)
66 << std::setfill(' ') << std::right << std::fixed << std::setprecision(2)
67 << (maplebe::kPercent * itemTimeUs / totalTimeUs) << "%" << std::setw(10)
68 << std::setfill(' ') << std::setprecision(4)
69 << (itemTimeUs / (maplebe::kMicroSecPerMilliSec * maplebe::kMicroSecPerMilliSec))
70 << "s\n";
71 };
72
73 LogInfo::MapleLogger() << "\n================== TIMEPHASES ==================\n";
74 LogInfo::MapleLogger() << "================================================\n";
75 for (auto phaseIt : originOrder) {
76 /*
77 * output information by specified format, setw function parameter specifies show width
78 * setprecision function parameter specifies precision
79 */
80 TimeLogger(phaseIt->first, phaseIt->second, phaseTotalTime);
81 }
82 LogInfo::MapleLogger() << "================================================\n";
83 TimeLogger("Total", phaseTotalTime, phaseTotalTime);
84 LogInfo::MapleLogger() << "================================================\n\n";
85 LogInfo::MapleLogger().unsetf(std::ios::fixed);
86 }
87 #endif
88
GetRequiredPhase() const89 const MapleVector<MaplePhaseID> &AnalysisDep::GetRequiredPhase() const
90 {
91 return required;
92 }
GetPreservedPhase() const93 const MapleSet<MaplePhaseID> &AnalysisDep::GetPreservedPhase() const
94 {
95 return preserved;
96 }
GetPreservedExceptPhase() const97 const MapleSet<MaplePhaseID> &AnalysisDep::GetPreservedExceptPhase() const
98 {
99 return preservedExcept;
100 }
101 } // namespace maple
102