1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. 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 17 #ifndef FFRT_PROFILER_H 18 #define FFRT_PROFILER_H 19 20 #include <atomic> 21 #include <string> 22 #include <mutex> 23 #include <thread> 24 #include "param/sys_param.h" 25 #include "ffrt_profiler_common.h" 26 #include "ffrt_profiler_socker_client.h" 27 28 namespace OHOS::Developtools::Profiler { 29 class EXPORT_API FfrtProfiler { 30 public: 31 static FfrtProfiler* GetInstance(); 32 virtual ~FfrtProfiler(); 33 34 bool IsProfilerEnabled(); 35 void FfrtProfilerTrace(const uint8_t traceType, const std::string& lable = "", uint64_t cookie = 0); 36 void FfrtProfiling(const EventType type, const char* payload, size_t payloadSize); 37 SetClockId(int32_t type)38 void SetClockId(int32_t type) 39 { 40 clockType_ = type; 41 } 42 SetEnableFlag(bool flag)43 void SetEnableFlag(bool flag) 44 { 45 enable_ = flag; 46 } 47 48 private: 49 FfrtProfiler(); 50 void CheckFfrtProfilerParam(); 51 void Enable(); 52 void Disable(); 53 void FfrtProfilerSendData(const void* src, size_t size, const char* payload = nullptr, size_t payloadSize = 0); 54 void GetThreadName(const void* src); 55 56 private: 57 static FfrtProfiler* instance_; 58 std::atomic_bool enable_{false}; 59 std::string processName_; 60 int32_t pid_{0}; 61 std::shared_ptr<FfrtProfilerSocketClient> socketClent_{nullptr}; 62 int32_t clockType_{0}; 63 }; 64 } // OHOS::Developtools::Profiler 65 #define FfrtProfilerIns OHOS::Developtools::Profiler::FfrtProfiler::GetInstance() 66 67 #endif // FFRT_PROFILER_H