1 /*
2  * Copyright (c) 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 RS_PROFILER_UTILS_H
17 #define RS_PROFILER_UTILS_H
18 
19 #include <cstdlib>
20 #include <string>
21 #include <vector>
22 
23 #ifdef REPLAY_TOOL_CLIENT
24 #include "rs_adapt.h"
25 #else
26 #include "common/rs_macros.h"
27 #endif // REPLAY_TOOL_CLIENT
28 
29 namespace OHOS::Rosen {
30 
31 class RSB_EXPORT Utils final {
32 public:
33     static constexpr float MILLI = 1e-3f; // NOLINT
34     static constexpr float MICRO = 1e-6f; // NOLINT
35     static constexpr float NANO = 1e-9f;  // NOLINT
36 
37 public:
38     // Time routines
39     static uint64_t Now();
40     static double ToSeconds(uint64_t nano);
41     static uint64_t ToNanoseconds(double seconds);
42 
43     // Cpu routines
44     static int32_t GetCpuId();
45     static void SetCpuAffinity(uint32_t cpu);
46     static bool GetCpuAffinity(uint32_t cpu);
47 
48     // Process routines
49     static pid_t GetPid();
50 
51     // String routines
52     static std::vector<std::string> Split(const std::string& string);
53     static void Replace(const std::string& susbtring, std::string& string);
54 
55     static std::string ExtractNumber(const std::string& string);
56 
57     static int8_t ToInt8(const std::string& string);
58     static int16_t ToInt16(const std::string& string);
59     static int32_t ToInt32(const std::string& string);
60     static int64_t ToInt64(const std::string& string);
61     static uint8_t ToUint8(const std::string& string);
62     static uint16_t ToUint16(const std::string& string);
63     static uint32_t ToUint32(const std::string& string);
64     static uint64_t ToUint64(const std::string& string);
65     static float ToFp32(const std::string& string);
66     static double ToFp64(const std::string& string);
67 
68     static void ToNumber(const std::string& string, int8_t& number);
69     static void ToNumber(const std::string& string, int16_t& number);
70     static void ToNumber(const std::string& string, int32_t& number);
71     static void ToNumber(const std::string& string, int64_t& number);
72     static void ToNumber(const std::string& string, uint8_t& number);
73     static void ToNumber(const std::string& string, uint16_t& number);
74     static void ToNumber(const std::string& string, uint32_t& number);
75     static void ToNumber(const std::string& string, uint64_t& number);
76     static void ToNumber(const std::string& string, float& number);
77     static void ToNumber(const std::string& string, double& number);
78 
79     template<typename T>
ToNumber(const std::string & string)80     static T ToNumber(const std::string& string)
81     {
82         T number = 0;
83         ToNumber(string, number);
84         return number;
85     }
86 
87     // Memory routines
88     static bool Move(void* destination, size_t destinationSize, const void* source, size_t size);
89     static bool Set(void* data, size_t size, int32_t value, size_t count);
90 
91     // File system routines
92     static std::string GetRealPath(const std::string& path);
93     static std::string MakePath(const std::string& directory, const std::string& file);
94     static std::string NormalizePath(const std::string& path);
95     static std::string GetFileName(const std::string& path);
96     static std::string GetDirectory(const std::string& path);
97     static bool IsDirectory(const std::string& path);
98     static void IterateDirectory(const std::string& path, std::vector<std::string>& files);
99     static void LoadLine(const std::string& path, std::string& line);
100     static void LoadLines(const std::string& path, std::vector<std::string>& lines);
101     static void LoadContent(const std::string& path, std::string& content);
102 
103     static bool FileExists(const std::string& path);
104     static FILE* FileOpen(const std::string& path, const std::string& options);
105     static void FileClose(FILE* file);
106     static bool IsFileValid(FILE* file);
107     static size_t FileSize(FILE* file);
108     static size_t FileTell(FILE* file);
109     static void FileSeek(FILE* file, int64_t offset, int32_t origin);
110     static void FileRead(FILE* file, void* data, size_t size);
111     static void FileWrite(FILE* file, const void* data, size_t size);
112 
113     template<typename T>
FileRead(FILE * file,T * data,size_t size)114     static void FileRead(FILE* file, T* data, size_t size)
115     {
116         FileRead(file, reinterpret_cast<void*>(data), size);
117     }
118 
119     template<typename T>
FileWrite(FILE * file,const T * data,size_t size)120     static void FileWrite(FILE* file, const T* data, size_t size)
121     {
122         FileWrite(file, reinterpret_cast<const void*>(data), size);
123     }
124 
125     // deprecated
126     static void FileRead(void* data, size_t size, size_t count, FILE* file);
127     static void FileWrite(const void* data, size_t size, size_t count, FILE* file);
128 
129     template<typename T>
FileRead(T * data,size_t size,size_t count,FILE * file)130     static void FileRead(T* data, size_t size, size_t count, FILE* file)
131     {
132         FileRead(reinterpret_cast<void*>(data), size, count, file);
133     }
134 
135     template<typename T>
FileWrite(const T * data,size_t size,size_t count,FILE * file)136     static void FileWrite(const T* data, size_t size, size_t count, FILE* file)
137     {
138         FileWrite(reinterpret_cast<const void*>(data), size, count, file);
139     }
140     // end of deprecation
141 
142     // NodeId/Pid routines
ExtractPid(uint64_t id)143     static constexpr pid_t ExtractPid(uint64_t id)
144     {
145         constexpr uint32_t bits = 32u;
146         return static_cast<pid_t>(id >> bits);
147     }
148 
GetMockPid(pid_t pid)149     static constexpr pid_t GetMockPid(pid_t pid)
150     {
151         constexpr uint32_t bits = 30u;
152         return static_cast<pid_t>((1 << bits) | static_cast<uint32_t>(pid));
153     }
154 
ExtractNodeId(uint64_t id)155     static constexpr uint64_t ExtractNodeId(uint64_t id)
156     {
157         constexpr uint32_t mask = 0xFFFFFFFF;
158         return (id & mask);
159     }
160 
ComposeNodeId(uint64_t pid,uint64_t nodeId)161     static constexpr uint64_t ComposeNodeId(uint64_t pid, uint64_t nodeId)
162     {
163         constexpr uint32_t bits = 32u;
164         return (pid << bits) | nodeId;
165     }
166 
ComposeMockNodeId(uint64_t id,uint64_t nodeId)167     static constexpr uint64_t ComposeMockNodeId(uint64_t id, uint64_t nodeId)
168     {
169         return ComposeNodeId(GetMockPid(id), nodeId);
170     }
171 
ComposeDataId(pid_t pid,uint32_t id)172     static uint64_t ComposeDataId(pid_t pid, uint32_t id)
173     {
174         constexpr uint32_t bits = 31u;
175         return ComposeNodeId(static_cast<uint32_t>(pid) | (1 << bits), id);
176     }
177 
GetRootNodeId(uint64_t id)178     static constexpr uint64_t GetRootNodeId(uint64_t id)
179     {
180         return ComposeNodeId(id, 1);
181     }
182 
PatchNodeId(uint64_t id)183     static constexpr uint64_t PatchNodeId(uint64_t id)
184     {
185         constexpr uint32_t bits = 62u;
186         return id | (static_cast<uint64_t>(1) << bits);
187     }
188 
IsNodeIdPatched(uint64_t id)189     static constexpr bool IsNodeIdPatched(uint64_t id)
190     {
191         constexpr uint32_t bits = 62u;
192         return id & (static_cast<uint64_t>(1) << bits);
193     }
194 };
195 
196 // ArgList
197 
198 class ArgList final {
199 public:
200     explicit ArgList() = default;
ArgList(std::vector<std::string> args)201     explicit ArgList(std::vector<std::string> args) : args_(std::move(args)) {}
202 
Count()203     size_t Count() const
204     {
205         return args_.size();
206     }
207 
Empty()208     bool Empty() const
209     {
210         return args_.empty();
211     }
212 
Clear()213     void Clear()
214     {
215         args_.clear();
216     }
217 
218     const std::string& String(size_t index = 0u) const
219     {
220         static const std::string EMPTY;
221         return index < Count() ? args_[index] : EMPTY;
222     }
223 
224     int8_t Int8(size_t index = 0u) const
225     {
226         return Utils::ToInt8(String(index));
227     }
228 
229     int16_t Int16(size_t index = 0u) const
230     {
231         return Utils::ToInt16(String(index));
232     }
233 
234     int32_t Int32(size_t index = 0u) const
235     {
236         return Utils::ToInt32(String(index));
237     }
238 
239     int64_t Int64(size_t index = 0u) const
240     {
241         return Utils::ToInt64(String(index));
242     }
243 
244     uint8_t Uint8(size_t index = 0u) const
245     {
246         return Utils::ToUint8(String(index));
247     }
248 
249     uint16_t Uint16(size_t index = 0u) const
250     {
251         return Utils::ToUint16(String(index));
252     }
253 
254     uint32_t Uint32(size_t index = 0u) const
255     {
256         return Utils::ToUint32(String(index));
257     }
258 
259     uint64_t Uint64(size_t index = 0u) const
260     {
261         return Utils::ToUint64(String(index));
262     }
263 
264     float Fp32(size_t index = 0u) const
265     {
266         return Utils::ToFp32(String(index));
267     }
268 
269     double Fp64(size_t index = 0u) const
270     {
271         return Utils::ToFp64(String(index));
272     }
273 
274     pid_t Pid(size_t index = 0u) const
275     {
276         return Uint32(index);
277     }
278 
279     uint64_t Node(size_t index = 0u) const
280     {
281         return Uint64(index);
282     }
283 
284 protected:
285     std::vector<std::string> args_;
286 };
287 } // namespace OHOS::Rosen
288 
289 #endif // RS_PROFILER_UTILS_H