• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef DPROF_LIBSTORAGE_DPROF_STORAGE_H
17 #define DPROF_LIBSTORAGE_DPROF_STORAGE_H
18 
19 #include "macros.h"
20 
21 #include <functional>
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 namespace ark::dprof {
28 class AppData {
29 public:
30     using FeaturesMap = std::unordered_map<std::string, std::vector<uint8_t>>;
31     ~AppData() = default;
32 
33     static std::unique_ptr<AppData> CreateByParams(const std::string &name, uint64_t hash, uint32_t pid,
34                                                    FeaturesMap &&featuresMap);
35     static std::unique_ptr<AppData> CreateByBuffer(const std::vector<uint8_t> &buffer);
36 
37     bool ToBuffer(std::vector<uint8_t> &buffer) const;
38 
GetName()39     std::string GetName() const
40     {
41         return commonInfo_.name;
42     }
43 
GetHash()44     uint64_t GetHash() const
45     {
46         return commonInfo_.hash;
47     }
48 
GetPid()49     uint32_t GetPid() const
50     {
51         return commonInfo_.pid;
52     }
53 
GetFeaturesMap()54     const FeaturesMap &GetFeaturesMap() const
55     {
56         return featuresMap_;
57     }
58 
59 private:
60     AppData() = default;
61 
62     NO_COPY_SEMANTIC(AppData);
63     NO_MOVE_SEMANTIC(AppData);
64 
65     struct CommonInfo {
66         std::string name {};
67         uint64_t hash {};
68         uint32_t pid {};
69     };
70 
71     CommonInfo commonInfo_;
72     FeaturesMap featuresMap_;
73 };
74 
75 class AppDataStorage {
76 public:
77     ~AppDataStorage() = default;
78 
79     static const size_t MAX_BUFFER_SIZE = 16 * 1024 * 1024;  // 16MB
80 
81     static std::unique_ptr<AppDataStorage> Create(const std::string &storageDir, bool createDir = false);
82 
83     bool SaveAppData(const AppData &appData);
84     void ForEachApps(const std::function<bool(std::unique_ptr<AppData> &&)> &callback) const;
85 
86 private:
AppDataStorage(std::string storageDir)87     explicit AppDataStorage(std::string storageDir) : storageDir_(std::move(storageDir)) {}
88 
89     NO_COPY_SEMANTIC(AppDataStorage);
90     NO_MOVE_SEMANTIC(AppDataStorage);
91 
92     std::string MakeAppPath(const std::string &name, uint64_t hash, uint32_t pid) const;
93 
94     std::string storageDir_;
95 };
96 }  // namespace ark::dprof
97 
98 #endif  // DPROF_LIBSTORAGE_DPROF_STORAGE_H
99