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 HIVIEW_PLUGINS_UNIFIED_COLLECTOR_INCLUDE_UNIFIED_COLLECTOR_H
16 #define HIVIEW_PLUGINS_UNIFIED_COLLECTOR_INCLUDE_UNIFIED_COLLECTOR_H
17 
18 #include <memory>
19 
20 #include "app_caller_event.h"
21 #include "app_trace_context.h"
22 #include "cpu_collection_task.h"
23 #include "plugin.h"
24 #include "sys_event.h"
25 #include "unified_collection_stat.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 class UnifiedCollector : public Plugin {
30 public:
31     void OnLoad() override;
32     void OnUnload() override;
33     bool OnEvent(std::shared_ptr<Event>& event) override;
34     void OnEventListeningCallback(const Event& event) override;
35     void Dump(int fd, const std::vector<std::string>& cmds) override;
IsEnableRecordTrace()36     static bool IsEnableRecordTrace() { return isEnableRecordTrace_; }
SetRecordTraceStatus(bool isEnable)37     static void SetRecordTraceStatus(bool isEnable) { isEnableRecordTrace_ = isEnable; }
38 
39 private:
40     void Init();
41     void InitWorkLoop();
42     void InitWorkPath();
43     void RunCpuCollectionTask();
44     void CpuCollectionFfrtTask();
45     void RegisterWorker();
46     void RunIoCollectionTask();
47     void RunUCollectionStatTask();
48     void IoCollectionTask();
49     void UCollectionStatTask();
50     void CleanDataFiles();
51     void LoadHitraceService();
52     void ExitHitraceService();
53     void OnMainThreadJank(SysEvent& sysEvent);
54     bool OnStartAppTrace(std::shared_ptr<AppCallerEvent> appCallerEvent);
55     bool OnStopAppTrace(std::shared_ptr<AppCallerEvent> appCallerEvent);
56     bool OnDumpAppTrace(std::shared_ptr<AppCallerEvent> appCallerEvent);
57     void RunRecordTraceTask();
58     static void OnSwitchStateChanged(const char* key, const char* value, void* context);
59 
60 private:
61     std::string workPath_;
62     std::shared_ptr<CpuCollectionTask> cpuCollectionTask_;
63     std::list<uint64_t> taskList_;
64     volatile bool isCpuTaskRunning_;
65     static bool isEnableRecordTrace_;
66     std::shared_ptr<AppTraceContext> appTraceContext_;
67 }; // UnifiedCollector
68 } // namespace HiviewDFX
69 } // namespace OHOS
70 #endif // HIVIEW_PLUGINS_UNIFIED_COLLECTOR_INCLUDE_UNIFIED_COLLECTOR_H
71