• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 
16 #ifndef HIVIEW_SERVICE_ABILITY_H
17 #define HIVIEW_SERVICE_ABILITY_H
18 
19 #include <string>
20 #include <memory>
21 
22 #include "hiview_err_code.h"
23 #include "hiview_file_info.h"
24 #include "hiview_service.h"
25 #include "hiview_service_ability_stub.h"
26 #include "ihiview_service_ability.h"
27 #include "hiview_logger.h"
28 #include "singleton.h"
29 #include "system_ability.h"
30 #include "client/trace_collector.h"
31 #include "client/memory_collector.h"
32 
33 namespace OHOS {
34 namespace HiviewDFX {
35 class HiviewServiceAbility : public SystemAbility,
36     public HiviewServiceAbilityStub {
37     DECLARE_SYSTEM_ABILITY(HiviewServiceAbility);
38 
39 public:
40     HiviewServiceAbility();
41     ~HiviewServiceAbility();
42     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
43     static void StartService(HiviewService *service);
44     static void StartServiceAbility(int sleepS);
45     static HiviewService* GetOrSetHiviewService(HiviewService *service = nullptr);
46 
47     int32_t List(const std::string& logType, std::vector<HiviewFileInfo>& fileInfos) override;
48     int32_t Copy(const std::string& logType, const std::string& logName, const std::string& dest) override;
49     int32_t Move(const std::string& logType, const std::string& logName, const std::string& dest) override;
50     int32_t Remove(const std::string& logType, const std::string& logName) override;
51 
52     CollectResultParcelable<int32_t> OpenSnapshotTrace(const std::vector<std::string>& tagGroups) override;
53     CollectResultParcelable<std::vector<std::string>> DumpSnapshotTrace(int32_t caller) override;
54     CollectResultParcelable<int32_t> OpenRecordingTrace(const std::string& tags) override;
55     CollectResultParcelable<int32_t> RecordingTraceOn() override;
56     CollectResultParcelable<std::vector<std::string>> RecordingTraceOff() override;
57     CollectResultParcelable<int32_t> CloseTrace() override;
58     CollectResultParcelable<int32_t> RecoverTrace() override;
59     CollectResultParcelable<int32_t> CaptureDurationTrace(UCollectClient::AppCaller &appCaller) override;
60     CollectResultParcelable<double> GetSysCpuUsage() override;
61     CollectResultParcelable<int32_t> SetAppResourceLimit(UCollectClient::MemoryCaller& memoryCaller) override;
62     CollectResultParcelable<int32_t> SetSplitMemoryValue(std::vector<UCollectClient::MemoryCaller>& memList) override;
63 
64 protected:
65     void OnDump() override;
66     void OnStart() override;
67     void OnStop() override;
68     CollectResultParcelable<int32_t> GetGraphicUsage(int32_t pid) override;
69 
70 private:
71     template<typename T>
TraceCalling(std::function<CollectResult<T> (HiviewService *)> traceRetHandler)72     CollectResultParcelable<T> TraceCalling(std::function<CollectResult<T>(HiviewService*)> traceRetHandler)
73     {
74         auto traceRet = CollectResultParcelable<T>::Init();
75         if (traceRetHandler == nullptr) {
76             return traceRet;
77         }
78         auto service = GetOrSetHiviewService();
79         if (service == nullptr) {
80             return traceRet;
81         }
82         auto collectRet = traceRetHandler(service);
83         return CollectResultParcelable<T>(collectRet);
84     }
85 
86 private:
87     void GetFileInfoUnderDir(const std::string& dirPath, std::vector<HiviewFileInfo>& fileInfos);
88     int32_t CopyOrMoveFile(
89         const std::string& logType, const std::string& logName, const std::string& dest, bool isMove);
90 };
91 
92 class HiviewServiceAbilityDeathRecipient : public IRemoteObject::DeathRecipient {
93 public:
94     HiviewServiceAbilityDeathRecipient();
95     ~HiviewServiceAbilityDeathRecipient();
96     void OnRemoteDied(const wptr<IRemoteObject> &object) override;
97 };
98 } // namespace HiviewDFX
99 } // namespace OHOS
100 #endif // IHIVIEW_SERVICE_H