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 
16 #ifndef HIVIEW_PLUGINS_UNIFIED_COLLECTOR_STORAGE_INCLUDE_CPU_STORAGE_H
17 #define HIVIEW_PLUGINS_UNIFIED_COLLECTOR_STORAGE_INCLUDE_CPU_STORAGE_H
18 
19 #include <memory>
20 #include <unordered_set>
21 
22 #include "resource/cpu.h"
23 #include "rdb_helper.h"
24 #include "rdb_store.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 class CpuStorageDbCallback : public NativeRdb::RdbOpenCallback {
29 public:
30     int OnCreate(NativeRdb::RdbStore& rdbStore) override;
31     int OnUpgrade(NativeRdb::RdbStore& rdbStore, int oldVersion, int newVersion) override;
32 }; // CpuStorageDbCallback
33 
34 class CpuStorage {
35 public:
36     CpuStorage(const std::string& workPath);
37     ~CpuStorage() = default;
38     void StoreProcessDatas(const std::vector<ProcessCpuStatInfo>& cpuCollections);
39     void StoreThreadDatas(const std::vector<ThreadCpuStatInfo>& cpuCollections);
40     void Report();
41 
42 private:
43     void InitDbStorePath();
44     void InitDbStore();
45     void StoreProcessData(const ProcessCpuStatInfo& cpuCollection, const std::unordered_set<int32_t>& memcgProcs);
46     void StoreThreadData(const ThreadCpuStatInfo& cpuCollection);
47     bool NeedReport();
48     void PrepareOldDbFilesBeforeReport();
49     void ResetDbStore();
50     void ReportCpuCollectionEvent();
51     void PrepareNewDbFilesAfterReport();
52     std::string GetStoredSysVersion();
53     void ReportDbRecords();
54 
55 private:
56     std::string workPath_;
57     std::string dbStorePath_;
58     std::string dbStoreUploadPath_;
59     std::shared_ptr<NativeRdb::RdbStore> dbStore_;
60 }; // CpuStorage
61 } // namespace HiviewDFX
62 } // namespace OHOS
63 #endif // HIVIEW_PLUGINS_UNIFIED_COLLECTOR_STORAGE_INCLUDE_CPU_STORAGE_H
64