1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2022 The SwiftShader Authors. All Rights Reserved. 2*03ce13f7SAndroid Build Coastguard Worker // 3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); 4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License. 5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at 6*03ce13f7SAndroid Build Coastguard Worker // 7*03ce13f7SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0 8*03ce13f7SAndroid Build Coastguard Worker // 9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License. 14*03ce13f7SAndroid Build Coastguard Worker 15*03ce13f7SAndroid Build Coastguard Worker #ifndef sw_SpirvProfiler_hpp 16*03ce13f7SAndroid Build Coastguard Worker #define sw_SpirvProfiler_hpp 17*03ce13f7SAndroid Build Coastguard Worker 18*03ce13f7SAndroid Build Coastguard Worker #include "System/SwiftConfig.hpp" 19*03ce13f7SAndroid Build Coastguard Worker 20*03ce13f7SAndroid Build Coastguard Worker #define SPV_ENABLE_UTILITY_CODE 21*03ce13f7SAndroid Build Coastguard Worker #include <spirv/unified1/spirv.hpp> 22*03ce13f7SAndroid Build Coastguard Worker 23*03ce13f7SAndroid Build Coastguard Worker #include "marl/mutex.h" 24*03ce13f7SAndroid Build Coastguard Worker 25*03ce13f7SAndroid Build Coastguard Worker #include <atomic> 26*03ce13f7SAndroid Build Coastguard Worker #include <cstdint> 27*03ce13f7SAndroid Build Coastguard Worker #include <thread> 28*03ce13f7SAndroid Build Coastguard Worker #include <unordered_map> 29*03ce13f7SAndroid Build Coastguard Worker 30*03ce13f7SAndroid Build Coastguard Worker namespace sw { 31*03ce13f7SAndroid Build Coastguard Worker 32*03ce13f7SAndroid Build Coastguard Worker // SpirvProfileData contains bookkeeping data structures for SPIR-V 33*03ce13f7SAndroid Build Coastguard Worker // instrumentation. 34*03ce13f7SAndroid Build Coastguard Worker class SpirvProfileData 35*03ce13f7SAndroid Build Coastguard Worker { 36*03ce13f7SAndroid Build Coastguard Worker public: 37*03ce13f7SAndroid Build Coastguard Worker SpirvProfileData() = default; 38*03ce13f7SAndroid Build Coastguard Worker SpirvProfileData(const SpirvProfileData &) = delete; 39*03ce13f7SAndroid Build Coastguard Worker SpirvProfileData(SpirvProfileData &&) = delete; 40*03ce13f7SAndroid Build Coastguard Worker SpirvProfileData &operator=(const SpirvProfileData &) = delete; 41*03ce13f7SAndroid Build Coastguard Worker SpirvProfileData &operator=(SpirvProfileData &&) = delete; 42*03ce13f7SAndroid Build Coastguard Worker 43*03ce13f7SAndroid Build Coastguard Worker // SPIR-V instruction execution count, by opcode. 44*03ce13f7SAndroid Build Coastguard Worker std::unordered_map<spv::Op, int64_t> spvOpExecutionCount; 45*03ce13f7SAndroid Build Coastguard Worker }; 46*03ce13f7SAndroid Build Coastguard Worker 47*03ce13f7SAndroid Build Coastguard Worker class SpirvProfiler 48*03ce13f7SAndroid Build Coastguard Worker { 49*03ce13f7SAndroid Build Coastguard Worker public: 50*03ce13f7SAndroid Build Coastguard Worker SpirvProfiler(const Configuration &config); 51*03ce13f7SAndroid Build Coastguard Worker ~SpirvProfiler(); 52*03ce13f7SAndroid Build Coastguard Worker 53*03ce13f7SAndroid Build Coastguard Worker void RegisterShaderForProfiling(std::string shaderId, std::unique_ptr<SpirvProfileData> profData); 54*03ce13f7SAndroid Build Coastguard Worker 55*03ce13f7SAndroid Build Coastguard Worker private: 56*03ce13f7SAndroid Build Coastguard Worker void ReportSnapshot(); 57*03ce13f7SAndroid Build Coastguard Worker std::unordered_map<std::string, SpirvProfileData *> GetRegisteredProfilesSnapshot(); 58*03ce13f7SAndroid Build Coastguard Worker 59*03ce13f7SAndroid Build Coastguard Worker const Configuration &cfg; 60*03ce13f7SAndroid Build Coastguard Worker std::string reportFilePath; 61*03ce13f7SAndroid Build Coastguard Worker 62*03ce13f7SAndroid Build Coastguard Worker std::thread reportThread; 63*03ce13f7SAndroid Build Coastguard Worker std::atomic<bool> reportThreadStop; 64*03ce13f7SAndroid Build Coastguard Worker 65*03ce13f7SAndroid Build Coastguard Worker // Map from shader ID to associated profile data. 66*03ce13f7SAndroid Build Coastguard Worker std::unordered_map<std::string, std::unique_ptr<SpirvProfileData>> shaderProfiles GUARDED_BY(profileMux); 67*03ce13f7SAndroid Build Coastguard Worker marl::mutex profileMux; 68*03ce13f7SAndroid Build Coastguard Worker }; 69*03ce13f7SAndroid Build Coastguard Worker 70*03ce13f7SAndroid Build Coastguard Worker } // namespace sw 71*03ce13f7SAndroid Build Coastguard Worker 72*03ce13f7SAndroid Build Coastguard Worker #endif // sw_SpirvProfiler_hpp