1 /*
2  * Copyright (c) 2023-2024 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 "hitrace_dump.h"
22 #include "trace_collector.h"
23 #include "trace_storage.h"
24 #include "app_caller_event.h"
25 
26 using OHOS::HiviewDFX::Hitrace::TraceErrorCode;
27 using OHOS::HiviewDFX::Hitrace::TraceRetInfo;
28 using OHOS::HiviewDFX::UCollect::UcError;
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 class TraceFlowController {
33 public:
34     TraceFlowController(UCollect::TraceCaller caller = UCollect::TraceCaller::INVALIDITY);
35     ~TraceFlowController() = default;
36     bool NeedDump();
37     bool NeedUpload(TraceRetInfo ret);
38     void StoreDb();
39 
40     /**
41      * @brief app whether report jank event trace today
42      *
43      * @param uid app user id
44      * @param happenTime main thread jank happen time, millisecond
45      * @return true: has report trace event today; false: has not report trace event today
46      */
47     bool HasCallOnceToday(int32_t uid, uint64_t happenTime);
48 
49     /**
50      * @brief save who capture trace
51      *
52      * @param appEvent app caller
53      * @return true: save success; false: save fail
54      */
55     bool RecordCaller(std::shared_ptr<AppCallerEvent> appEvent);
56 
57     /**
58      * @brief clean which remain in share create by app
59      *
60      */
61     void CleanOldAppTrace();
62 private:
63     void InitTraceDb();
64     void InitTraceStorage();
65     std::string GetDate();
66     int64_t GetTraceSize(Hitrace::TraceRetInfo ret);
67     bool IsLowerLimit(int64_t nowSize, int64_t traceSize, int64_t limitSize);
68     TraceFlowRecord QueryDb();
69 
70 private:
71     struct TraceFlowRecord traceFlowRecord_;
72     std::shared_ptr<TraceStorage> traceStorage_;
73     UCollect::TraceCaller caller_;
74 };
75 } // HiViewDFX
76 } // OHOS
77 #endif // FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_FLOW_CONTROLLER_H
78