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 "compiler_logger.h"
17 #include "optimizer/ir/graph.h"
18 #include "optimizer/ir/basicblock.h"
19 #include "pass_manager_statistics.h"
20
21 #include <iomanip>
22
23 namespace panda::compiler {
PassManagerStatistics(Graph * graph)24 PassManagerStatistics::PassManagerStatistics(Graph *graph)
25 : graph_(graph),
26 pass_stat_list_(graph->GetAllocator()->Adapter()),
27 pass_stat_stack_(graph->GetAllocator()->Adapter()),
28 enable_ir_stat_(options.IsCompilerEnableIrStats())
29 {
30 }
31
PrintStatistics() const32 void PassManagerStatistics::PrintStatistics() const
33 {
34 // clang-format off
35 #ifndef __clang_analyzer__
36 auto& out = std::cerr;
37 static constexpr size_t BUF_SIZE = 64;
38 static constexpr auto OFFSET_STAT = 2;
39 static constexpr auto OFFSET_ID = 4;
40 static constexpr auto OFFSET_DEFAULT = 12;
41 static constexpr auto OFFSET_PASS_NAME = 34;
42 static constexpr auto OFFSET_TOTAL = 41;
43 char space_buf[BUF_SIZE];
44 std::fill(space_buf, space_buf + BUF_SIZE, ' ');
45 size_t index = 0;
46 out << std::dec
47 << std::setw(OFFSET_ID) << std::right << "ID" << " " << std::left
48 << std::setw(OFFSET_PASS_NAME) << "Pass Name" << ": " << std::right
49 << std::setw(OFFSET_DEFAULT) << "IR mem" << std::right
50 << std::setw(OFFSET_DEFAULT) << "Local mem" << std::right
51 << std::setw(OFFSET_DEFAULT) << "Time,us" << std::endl;
52 out << "-----------------------------------------------------------------------------\n";
53 size_t total_time = 0;
54 for (const auto& stat : pass_stat_list_) {
55 auto indent = stat.run_depth * OFFSET_STAT;
56 space_buf[indent] = 0;
57 out << std::setw(OFFSET_ID) << std::right << index << space_buf << " " << std::left <<
58 std::setw(OFFSET_PASS_NAME - indent) << stat.pass_name << ": " << std::right <<
59 std::setw(OFFSET_DEFAULT) << stat.mem_used_ir << std::setw(OFFSET_DEFAULT) << stat.mem_used_local <<
60 std::setw(OFFSET_DEFAULT) << stat.time_us << std::endl;
61 space_buf[indent] = ' ';
62 index++;
63 total_time += stat.time_us;
64 }
65 out << "-----------------------------------------------------------------------------\n";
66 out << std::setw(OFFSET_TOTAL) << "TOTAL: "
67 << std::right << std::setw(OFFSET_DEFAULT) << graph_->GetAllocator()->GetAllocatedSize()
68 << std::setw(OFFSET_DEFAULT) << graph_->GetLocalAllocator()->GetAllocatedSize()
69 << std::setw(OFFSET_DEFAULT) << total_time << std::endl;
70 out << "PBC instruction number : " << pbc_inst_num_ << std::endl;
71 #endif
72 // clang-format on
73 }
74
ProcessBeforeRun(const Pass & pass)75 void PassManagerStatistics::ProcessBeforeRun(const Pass &pass)
76 {
77 size_t allocated_size = graph_->GetAllocator()->GetAllocatedSize();
78 constexpr auto OFFSET_NORMAL = 2;
79 std::string indent(pass_call_depth_ * OFFSET_NORMAL, '.');
80 COMPILER_LOG(DEBUG, PM) << "Run pass: " << indent << pass.GetPassName();
81
82 if (!pass_stat_stack_.empty()) {
83 auto top_pass = pass_stat_stack_.top();
84 ASSERT(allocated_size >= last_allocated_ir_);
85 top_pass->mem_used_ir += allocated_size - last_allocated_ir_;
86 if (!options.IsCompilerResetLocalAllocator()) {
87 ASSERT(graph_->GetLocalAllocator()->GetAllocatedSize() >= last_allocated_local_);
88 top_pass->mem_used_local += graph_->GetLocalAllocator()->GetAllocatedSize() - last_allocated_local_;
89 }
90 top_pass->time_us +=
91 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - last_timestamp_)
92 .count();
93 }
94
95 pass_stat_list_.push_back({pass_call_depth_, pass.GetPassName(), {0, 0}, {0, 0}, 0, 0, 0});
96 if (enable_ir_stat_) {
97 for (auto block : graph_->GetVectorBlocks()) {
98 if (block == nullptr) {
99 continue;
100 }
101 pass_stat_list_.back().before_pass.num_of_basicblocks++;
102 for ([[maybe_unused]] auto inst : block->Insts()) {
103 pass_stat_list_.back().before_pass.num_of_instructions++;
104 }
105 }
106 }
107
108 pass_stat_stack_.push(&pass_stat_list_.back());
109 // Call `GetAllocator()->GetAllocatedSize()` again to exclude allocations caused by PassManagerStatistics itself:
110 last_allocated_ir_ = graph_->GetAllocator()->GetAllocatedSize();
111 last_allocated_local_ = graph_->GetLocalAllocator()->GetAllocatedSize();
112 last_timestamp_ = std::chrono::steady_clock::now();
113
114 pass_call_depth_++;
115 }
116
ProcessAfterRun(size_t local_mem_used)117 void PassManagerStatistics::ProcessAfterRun(size_t local_mem_used)
118 {
119 auto top_pass = pass_stat_stack_.top();
120 ASSERT(graph_->GetAllocator()->GetAllocatedSize() >= last_allocated_ir_);
121 top_pass->mem_used_ir += graph_->GetAllocator()->GetAllocatedSize() - last_allocated_ir_;
122 if (options.IsCompilerResetLocalAllocator()) {
123 top_pass->mem_used_local = local_mem_used;
124 } else {
125 ASSERT(graph_->GetLocalAllocator()->GetAllocatedSize() >= last_allocated_local_);
126 top_pass->mem_used_local += graph_->GetLocalAllocator()->GetAllocatedSize() - last_allocated_local_;
127 }
128 top_pass->time_us +=
129 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - last_timestamp_)
130 .count();
131
132 if (enable_ir_stat_) {
133 for (auto block : graph_->GetVectorBlocks()) {
134 if (block == nullptr) {
135 continue;
136 }
137 top_pass->after_pass.num_of_basicblocks++;
138 for ([[maybe_unused]] auto inst : block->Insts()) {
139 top_pass->after_pass.num_of_instructions++;
140 }
141 }
142 }
143
144 pass_stat_stack_.pop();
145 last_allocated_ir_ = graph_->GetAllocator()->GetAllocatedSize();
146 last_allocated_local_ = graph_->GetLocalAllocator()->GetAllocatedSize();
147 last_timestamp_ = std::chrono::steady_clock::now();
148
149 pass_call_depth_--;
150 pass_run_index_++;
151 }
152
DumpStatisticsCsv(char sep) const153 void PassManagerStatistics::DumpStatisticsCsv(char sep) const
154 {
155 ASSERT(options.WasSetCompilerDumpStatsCsv());
156 static std::ofstream csv(options.GetCompilerDumpStatsCsv(), std::ofstream::trunc);
157 auto m_name = graph_->GetRuntime()->GetMethodFullName(graph_->GetMethod(), true);
158 for (const auto &i : pass_stat_list_) {
159 csv << "\"" << m_name << "\"" << sep;
160 csv << i.pass_name << sep;
161 csv << i.mem_used_ir << sep;
162 csv << i.mem_used_local << sep;
163 csv << i.time_us << sep;
164 if (enable_ir_stat_) {
165 csv << i.before_pass.num_of_basicblocks << sep;
166 csv << i.after_pass.num_of_basicblocks << sep;
167 csv << i.before_pass.num_of_instructions << sep;
168 csv << i.after_pass.num_of_instructions << sep;
169 }
170 csv << GetPbcInstNum();
171 csv << '\n';
172 }
173 // Flush stream because it is declared `static`:
174 csv << std::flush;
175 }
176 } // namespace panda::compiler
177