1 /*
2  * Copyright (c) 2021-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_PLATFORM_MANAGEMENT_INFO_EXPORT_SERVICE_H
16 #define HIVIEW_PLATFORM_MANAGEMENT_INFO_EXPORT_SERVICE_H
17 
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "audit_log_parser.h"
24 #include "utility/trace_collector.h"
25 #include "utility/cpu_collector.h"
26 #include "client/trace_collector.h"
27 #include "client/memory_collector.h"
28 #include "utility/graphic_memory_collector.h"
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 class HiviewService {
33 public:
34     HiviewService();
~HiviewService()35     ~HiviewService(){};
36 
37     void StartService();
38     // Dump interface
39     void DumpRequestDispatcher(int fd, const std::vector<std::string>& cmds);
40 
41     int32_t Copy(const std::string& srcFilePath, const std::string& destFilePath);
42     int32_t Move(const std::string& srcFilePath, const std::string& destFilePath);
43     int32_t Remove(const std::string& filePath);
44 
45     // Trace interfaces
46     CollectResult<int32_t> OpenSnapshotTrace(const std::vector<std::string>& tagGroups);
47     CollectResult<std::vector<std::string>> DumpSnapshotTrace(UCollect::TraceCaller caller);
48     CollectResult<int32_t> OpenRecordingTrace(const std::string& tags);
49     CollectResult<int32_t> RecordingTraceOn();
50     CollectResult<std::vector<std::string>> RecordingTraceOff();
51     CollectResult<int32_t> CloseTrace();
52     CollectResult<int32_t> RecoverTrace();
53     CollectResult<int32_t> CaptureDurationTrace(UCollectClient::AppCaller &appCaller);
54     CollectResult<double> GetSysCpuUsage();
55     CollectResult<int32_t> SetAppResourceLimit(UCollectClient::MemoryCaller& memoryCaller);
56     CollectResult<int32_t> GetGraphicUsage(int32_t pid);
57 
58 private:
59     void DumpDetailedInfo(int fd);
60     void DumpPluginInfo(int fd, const std::vector<std::string>& cmds) const;
61     void DumpLoadedPluginInfo(int fd) const;
62     void DumpPluginUsageInfo(int fd);
63     void DumpPluginUsageInfo(int fd, const std::string& pluginName) const;
64 
65     void DumpThreadUsageInfo(int fd) const;
66     void DumpThreadUsageInfo(int fd, const std::string& threadName) const;
67 
68     void DumpPipelineUsageInfo(int fd) const;
69     void DumpPipelineUsageInfo(int fd, const std::string& pipelineName) const;
70 
71     int CopyFile(const std::string& srcFilePath, const std::string& destFilePath);
72 
73     void PrintUsage(int fd) const;
74 
75 private:
76     std::unique_ptr<AuditLogParser> parser_ = nullptr;
77     std::shared_ptr<UCollectUtil::TraceCollector> traceCollector_;
78     std::shared_ptr<UCollectUtil::CpuCollector> cpuCollector_;
79     std::shared_ptr<UCollectUtil::GraphicMemoryCollector> graphicMemoryCollector_;
80 };
81 } // namespace HiviewDFX
82 } // namespace OHOS
83 #endif // HIVIEW_PLATFORM_MANAGEMENT_INFO_EXPORT_SERVICE_H
84