1 // Copyright 2022 The SwiftShader Authors. All Rights Reserved.
2 //
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 #include "SpirvProfiler.hpp"
16
17 #include "System/Debug.hpp"
18
19 #include "spirv-tools/libspirv.h"
20
21 #include <string.h>
22 #include <atomic>
23 #include <fstream>
24
25 namespace {
GetSpvOpName(const spv::Op & op)26 std::string GetSpvOpName(const spv::Op &op)
27 {
28 return std::string("Op") + spvOpcodeString(static_cast<uint32_t>(op));
29 }
30
ConcatPath(const std::string & base,const std::string & file)31 std::string ConcatPath(const std::string &base, const std::string &file)
32 {
33 if(base.size() == 0)
34 {
35 return file;
36 }
37 char lastChar = *base.rend();
38 if(lastChar == '\\' || lastChar == '/')
39 {
40 return base + file;
41 }
42 return base + "/" + file;
43 }
44 } // namespace
45
46 namespace sw {
47
SpirvProfiler(const Configuration & config)48 SpirvProfiler::SpirvProfiler(const Configuration &config)
49 : cfg(config)
50 {
51 reportFilePath = ConcatPath(cfg.spvProfilingReportDir, "spirv_profile.txt");
52
53 reportThreadStop = false;
54 reportThread = std::thread{ [this] {
55 while(!reportThreadStop.load(std::memory_order_acquire))
56 {
57 std::this_thread::sleep_for(std::chrono::milliseconds(cfg.spvProfilingReportPeriodMs));
58 ReportSnapshot();
59 }
60 } };
61 }
62
~SpirvProfiler()63 SpirvProfiler::~SpirvProfiler()
64 {
65 reportThreadStop.store(true, std::memory_order_release);
66 reportThread.join();
67 }
68
ReportSnapshot()69 void SpirvProfiler::ReportSnapshot()
70 {
71 std::ofstream f{ reportFilePath };
72
73 if(!f)
74 {
75 warn("Error writing SPIR-V profile to file %s: %s\n", reportFilePath.c_str(), strerror(errno));
76 return;
77 }
78
79 auto profiles = GetRegisteredProfilesSnapshot();
80 for(const auto &[shaderId, profileData] : profiles)
81 {
82 f << "[Shader " << shaderId << "]" << std::endl;
83
84 f << "[SPIR-V operand execution count]" << std::endl;
85 for(const auto &[spvOp, execCount] : profileData->spvOpExecutionCount)
86 {
87 f << GetSpvOpName(spvOp) << ": " << execCount << std::endl;
88 }
89
90 f << std::endl;
91 }
92
93 f.close();
94 }
95
RegisterShaderForProfiling(std::string shaderId,std::unique_ptr<SpirvProfileData> profData)96 void SpirvProfiler::RegisterShaderForProfiling(std::string shaderId, std::unique_ptr<SpirvProfileData> profData)
97 {
98 marl::lock lock{ profileMux };
99 shaderProfiles[shaderId] = std::move(profData);
100 }
101
GetRegisteredProfilesSnapshot()102 std::unordered_map<std::string, SpirvProfileData *> SpirvProfiler::GetRegisteredProfilesSnapshot()
103 {
104 marl::lock lock{ profileMux };
105 std::unordered_map<std::string, SpirvProfileData *> snapshot;
106 for(const auto &[shaderId, profileData] : shaderProfiles)
107 {
108 snapshot.emplace(shaderId, profileData.get());
109 }
110 return snapshot;
111 }
112 } // namespace sw