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 RENDER_PROFILER_APPLICATION
24 #include "rs_adapt.h"
25 #else
26 #include "common/rs_macros.h"
27 #endif
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 bool FileDelete(const std::string& path);
105     static FILE* FileOpen(const std::string& path, const std::string& options);
106     static void FileClose(FILE* file);
107     static bool IsFileValid(FILE* file);
108     static size_t FileSize(FILE* file);
109     static size_t FileTell(FILE* file);
110     static void FileSeek(FILE* file, int64_t offset, int32_t origin);
111     static void FileRead(FILE* file, void* data, size_t size);
112     static void FileWrite(FILE* file, const void* data, size_t size);
113 
114     template<typename T>
FileRead(FILE* file, T* data, size_t size)115     static void FileRead(FILE* file, T* data, size_t size)
116     {
117         FileRead(file, reinterpret_cast<void*>(data), size);
118     }
119 
120     template<typename T>
FileWrite(FILE* file, const T* data, size_t size)121     static void FileWrite(FILE* file, const T* data, size_t size)
122     {
123         FileWrite(file, reinterpret_cast<const void*>(data), size);
124     }
125 
126     // deprecated
127     static void FileRead(void* data, size_t size, size_t count, FILE* file);
128     static void FileWrite(const void* data, size_t size, size_t count, FILE* file);
129 
130     template<typename T>
FileRead(T* data, size_t size, size_t count, FILE* file)131     static void FileRead(T* data, size_t size, size_t count, FILE* file)
132     {
133         FileRead(reinterpret_cast<void*>(data), size, count, file);
134     }
135 
136     template<typename T>
FileWrite(const T* data, size_t size, size_t count, FILE* file)137     static void FileWrite(const T* data, size_t size, size_t count, FILE* file)
138     {
139         FileWrite(reinterpret_cast<const void*>(data), size, count, file);
140     }
141     // end of deprecation
142 
143     // NodeId/Pid routines
ExtractPid(uint64_t id)144     static constexpr pid_t ExtractPid(uint64_t id)
145     {
146         constexpr uint32_t bits = 32u;
147         return static_cast<pid_t>(id >> bits);
148     }
149 
GetMockPid(pid_t pid)150     static constexpr pid_t GetMockPid(pid_t pid)
151     {
152         constexpr uint32_t bits = 30u;
153         return static_cast<pid_t>((1u << bits) | static_cast<uint32_t>(pid));
154     }
155 
ExtractNodeId(uint64_t id)156     static constexpr uint64_t ExtractNodeId(uint64_t id)
157     {
158         constexpr uint32_t mask = 0xFFFFFFFF;
159         return (id & mask);
160     }
161 
ComposeNodeId(uint64_t pid, uint64_t nodeId)162     static constexpr uint64_t ComposeNodeId(uint64_t pid, uint64_t nodeId)
163     {
164         constexpr uint32_t bits = 32u;
165         return (pid << bits) | nodeId;
166     }
167 
ComposeMockNodeId(uint64_t id, uint64_t nodeId)168     static constexpr uint64_t ComposeMockNodeId(uint64_t id, uint64_t nodeId)
169     {
170         return ComposeNodeId(GetMockPid(id), nodeId);
171     }
172 
ComposeDataId(pid_t pid, uint32_t id)173     static uint64_t ComposeDataId(pid_t pid, uint32_t id)
174     {
175         constexpr uint32_t bits = 31u;
176         return ComposeNodeId(static_cast<uint32_t>(pid) | (1u << bits), id);
177     }
178 
GetRootNodeId(uint64_t id)179     static constexpr uint64_t GetRootNodeId(uint64_t id)
180     {
181         return ComposeNodeId(id, 1);
182     }
183 
PatchNodeId(uint64_t id)184     static constexpr uint64_t PatchNodeId(uint64_t id)
185     {
186         constexpr uint32_t bits = 62u;
187         return id | (static_cast<uint64_t>(1) << bits);
188     }
189 
IsNodeIdPatched(uint64_t id)190     static constexpr bool IsNodeIdPatched(uint64_t id)
191     {
192         constexpr uint32_t bits = 62u;
193         return id & (static_cast<uint64_t>(1) << bits);
194     }
195 };
196 
197 } // namespace OHOS::Rosen
198 
199 #endif // RS_PROFILER_UTILS_H