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