• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_FLOW_CONTROLLER_H
16 #define FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_FLOW_CONTROLLER_H
17 
18 #include <memory>
19 #include <string>
20 
21 #include "app_caller_event.h"
22 #include "hitrace_dump.h"
23 #include "trace_storage.h"
24 #include "telemetry_storage.h"
25 #include "trace_behavior_storage.h"
26 #include "app_event_task_storage.h"
27 
28 using OHOS::HiviewDFX::Hitrace::TraceErrorCode;
29 using OHOS::HiviewDFX::Hitrace::TraceRetInfo;
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace FlowController {
34 inline constexpr char DEFAULT_DB_PATH[] = "/data/log/hiview/unified_collection/trace/";
35 inline constexpr char DEFAULT_CONFIG_PATH[] = "/system/etc/hiview/";
36 }
37 
38 enum class CacheFlow {
39     SUCCESS,
40     OVER_FLOW,
41     EXIT
42 };
43 
44 class TraceFlowController {
45 public:
46     explicit TraceFlowController(const std::string &caller, const std::string& dbPath = FlowController::DEFAULT_DB_PATH,
47         const std::string& configPath = FlowController::DEFAULT_CONFIG_PATH);
48     ~TraceFlowController() = default;
49     bool IsOverLimit();
50     int64_t GetRemainingTraceSize();
51     void StoreDb(int64_t traceSize);
52     void DecreaseDynamicThreshold();
53 #ifdef TRACE_MANAGER_UNITTEST
SetTestDate(const std::string & testDate)54     void SetTestDate(const std::string& testDate)
55     {
56         if (traceStorage_ != nullptr) {
57             traceStorage_->SetTestDate(testDate);
58         }
59     }
60 #endif
61 
62     /**
63      * @brief app whether report jank event trace today
64      *
65      * @param uid app user id
66      * @param happenTime main thread jank happen time, millisecond
67      * @return true: has report trace event today; false: has not report trace event today
68      */
69     bool HasCallOnceToday(int32_t uid, uint64_t happenTime);
70 
71     /**
72      * @brief save who capture trace
73      *
74      * @param appEvent app caller
75      * @return true: save success; false: save fail
76      */
77     bool RecordCaller(std::shared_ptr<AppCallerEvent> appEvent);
78 
79     /**
80      * @brief clean which remain in share create by app
81      *
82      */
83     void CleanOldAppTrace(int32_t dateNum);
84     CacheFlow UseCacheTimeQuota(int32_t interval);
85     TelemetryRet InitTelemetryData(const std::string &telemetryId, int64_t &runningTime,
86         const std::map<std::string, int64_t> &flowControlQuotas);
87     TelemetryRet NeedTelemetryDump(const std::string& module);
88     void TelemetryStore(const std::string &module, int64_t zipTraceSize);
89     bool QueryRunningTime(int64_t &runningTime);
90     bool UpdateRunningTime(int64_t runningTime);
91     void ClearTelemetryData();
92 
93 private:
94     void InitTraceDb(const std::string& dbPath);
95     void InitTraceStorage(const std::string& caller, const std::string& configPath);
96 
97 private:
98     std::shared_ptr<NativeRdb::RdbStore> dbStore_;
99     std::shared_ptr<TraceStorage> traceStorage_;
100     std::shared_ptr<AppEventTaskStorage> appTaskStore_;
101     std::shared_ptr<TraceBehaviorStorage> behaviorTaskStore_;
102     std::shared_ptr<TeleMetryStorage> teleMetryStorage_;
103 };
104 } // HiViewDFX
105 } // OHOS
106 #endif // FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_FLOW_CONTROLLER_H
107