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