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 #ifndef _MUTEX_PERF_H_ 17 #define _MUTEX_PERF_H_ 18 19 // open -O0 -DMUTEX_PERF in Makefile 20 21 #include <string> 22 #include <unordered_map> 23 #include <mutex> 24 25 namespace ffrt { 26 struct MutexStatistic { 27 ~MutexStatistic(); 28 std::mutex mtx_; 29 std::unordered_map<std::string, uint32_t> cycles_; 30 }; 31 32 void AddMutexCycles(std::string key, uint32_t val); 33 34 namespace xx { 35 class mutex : public std::mutex { 36 public: mutex()37 mutex() : label_("undefined") {}; mutex(const std::string & label)38 explicit mutex(const std::string& label) : label_(label) {}; 39 ~mutex() = default; 40 lock()41 inline void lock() 42 { 43 #ifdef __x86_64__ 44 uint32_t t_s = __builtin_ia32_rdtsc(); 45 #endif 46 impl_.lock(); 47 #ifdef __x86_64__ 48 uint32_t t_e = __builtin_ia32_rdtsc(); 49 AddMutexCycles(label_, t_e - t_s); 50 #endif 51 } 52 unlock()53 inline void unlock() 54 { 55 #ifdef __x86_64__ 56 uint32_t t_s = __builtin_ia32_rdtsc(); 57 #endif 58 impl_.unlock(); 59 #ifdef __x86_64__ 60 uint32_t t_e = __builtin_ia32_rdtsc(); 61 AddMutexCycles(label_, t_e - t_s); 62 #endif 63 } 64 65 private: 66 std::string label_; 67 std::mutex impl_; 68 }; 69 } // namespace xx 70 } // namespace ffrt 71 72 #endif