• 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 
27 using OHOS::HiviewDFX::Hitrace::TraceErrorCode;
28 using OHOS::HiviewDFX::Hitrace::TraceRetInfo;
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 namespace {
33 const std::string DB_PATH = "/data/log/hiview/unified_collection/trace/";
34 }
35 
36 enum class CacheFlow {
37     SUCCESS,
38     OVER_FLOW,
39     EXIT
40 };
41 
42 class TraceFlowController {
43 public:
44     explicit TraceFlowController(const std::string &caller, const std::string& dbPath = DB_PATH);
45     ~TraceFlowController() = default;
46     bool NeedDump();
47     bool NeedUpload(int64_t traceSize);
48     void StoreDb();
49 
50     /**
51      * @brief app whether report jank event trace today
52      *
53      * @param uid app user id
54      * @param happenTime main thread jank happen time, millisecond
55      * @return true: has report trace event today; false: has not report trace event today
56      */
57     bool HasCallOnceToday(int32_t uid, uint64_t happenTime);
58 
59     /**
60      * @brief save who capture trace
61      *
62      * @param appEvent app caller
63      * @return true: save success; false: save fail
64      */
65     bool RecordCaller(std::shared_ptr<AppCallerEvent> appEvent);
66 
67     /**
68      * @brief clean which remain in share create by app
69      *
70      */
71     void CleanOldAppTrace(int32_t dateNum);
72 
73     CacheFlow UseCacheTimeQuota(int32_t interval);
74 
75     TelemetryRet InitTelemetryData(const std::string &telemetryId, int64_t &beginTime,
76         const std::map<std::string, int64_t> &flowControlQuotas);
77 
78     TelemetryRet NeedTelemetryDump(const std::string& module, int64_t traceSize);
79 
80     void ClearTelemetryData();
81 
82 private:
83     void InitTraceDb(const std::string& dbPath);
84     void InitTraceStorage(const std::string& caller);
85 
86 private:
87     std::shared_ptr<NativeRdb::RdbStore> dbStore_;
88     std::shared_ptr<TraceStorage> traceStorage_;
89     std::shared_ptr<AppEventTaskStorage> appTaskStore_;
90     std::shared_ptr<TraceBehaviorStorage> behaviorTaskStore_;
91     std::shared_ptr<TeleMetryStorage> teleMetryStorage_;
92 };
93 } // HiViewDFX
94 } // OHOS
95 #endif // FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_FLOW_CONTROLLER_H
96