1 /* 2 * Copyright (c) 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 "frame_trace_adapter_impl.h" 17 18 #include <unistd.h> 19 20 #include "frame_trace.h" 21 #include "parameters.h" 22 23 #include "base/log/log_wrapper.h" 24 25 namespace OHOS::Ace { 26 27 constexpr char INTERVAL_LIMIT[] = "ffrt.interval.limit"; 28 AccessFrameTrace()29bool FrameTraceAdapterImpl::AccessFrameTrace() 30 { 31 #ifdef FRAME_TRACE_ENABLE 32 return true; 33 #else 34 return false; 35 #endif 36 } 37 QuickExecute(std::function<void ()> && func)38void FrameTraceAdapterImpl::QuickExecute(std::function<void()> && func) 39 { 40 if (AccessFrameTrace()) { 41 FRAME_TRACE::TraceAndExecute(std::move(func), FRAME_TRACE::TraceType::QUICK_TRACE); 42 } 43 } 44 SlowExecute(std::function<void ()> && func)45void FrameTraceAdapterImpl::SlowExecute(std::function<void()> && func) 46 { 47 if (AccessFrameTrace()) { 48 FRAME_TRACE::TraceAndExecute(std::move(func), FRAME_TRACE::TraceType::SLOW_TRACE); 49 } 50 } 51 EnableFrameTrace(const std::string & traceTag)52bool FrameTraceAdapterImpl::EnableFrameTrace(const std::string &traceTag) 53 { 54 if (AccessFrameTrace()) { 55 return FRAME_TRACE::FrameAwareTraceEnable(traceTag); 56 } 57 return false; 58 } 59 GetInstance()60FrameTraceAdapter* FrameTraceAdapter::GetInstance() 61 { 62 static FrameTraceAdapterImpl instance; 63 return &instance; 64 } 65 IsEnabled()66bool FrameTraceAdapterImpl::IsEnabled() 67 { 68 if (AccessFrameTrace()) { 69 return FRAME_TRACE::IsEnabled(); 70 } 71 return false; 72 } 73 SetFrameTraceLimit()74void FrameTraceAdapterImpl::SetFrameTraceLimit() 75 { 76 bool limitEnabled = OHOS::system::GetBoolParameter(INTERVAL_LIMIT, false); 77 if (!limitEnabled) { 78 LOGI("Set FrameTraceLimit"); 79 OHOS::system::SetParameter(INTERVAL_LIMIT, "true"); 80 } 81 } 82 } 83