• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     static float Kilobytes(size_t bytes);
39     static float Megabytes(size_t bytes);
40     static float Gigabytes(size_t bytes);
41 
42     // Time routines
43     static uint64_t Now();
44     static double ToSeconds(uint64_t nano);
45     static uint64_t ToNanoseconds(double seconds);
46 
47     // Cpu routines
48     static int32_t GetCpuId();
49     static void SetCpuAffinity(uint32_t cpu);
50     static bool GetCpuAffinity(uint32_t cpu);
51 
52     // Process routines
53     static pid_t GetPid();
54 
55     // String routines
56     static std::string Format(const char* format, va_list args);
57     static std::string Format(const char* format, ...);
58     static std::vector<std::string> Split(const std::string& string);
59     static void Replace(const std::string& susbtring, std::string& string);
60 
61     static std::string ExtractNumber(const std::string& string);
62 
63     static int8_t ToInt8(const std::string& string);
64     static int16_t ToInt16(const std::string& string);
65     static int32_t ToInt32(const std::string& string);
66     static int64_t ToInt64(const std::string& string);
67     static uint8_t ToUint8(const std::string& string);
68     static uint16_t ToUint16(const std::string& string);
69     static uint32_t ToUint32(const std::string& string);
70     static uint64_t ToUint64(const std::string& string);
71     static float ToFp32(const std::string& string);
72     static double ToFp64(const std::string& string);
73 
74     static void ToNumber(const std::string& string, int8_t& number);
75     static void ToNumber(const std::string& string, int16_t& number);
76     static void ToNumber(const std::string& string, int32_t& number);
77     static void ToNumber(const std::string& string, int64_t& number);
78     static void ToNumber(const std::string& string, uint8_t& number);
79     static void ToNumber(const std::string& string, uint16_t& number);
80     static void ToNumber(const std::string& string, uint32_t& number);
81     static void ToNumber(const std::string& string, uint64_t& number);
82     static void ToNumber(const std::string& string, float& number);
83     static void ToNumber(const std::string& string, double& number);
84 
85     template<typename T>
ToNumber(const std::string & string)86     static T ToNumber(const std::string& string)
87     {
88         T number = 0;
89         ToNumber(string, number);
90         return number;
91     }
92 
93     // Memory routines
94     static bool Move(void* destination, size_t destinationSize, const void* source, size_t size);
95     static bool Set(void* data, size_t size, int32_t value, size_t count);
96 
97     // File system routines
98     static std::string GetRealPath(const std::string& path);
99     static std::string MakePath(const std::string& directory, const std::string& file);
100     static std::string NormalizePath(const std::string& path);
101     static std::string GetFileName(const std::string& path);
102     static std::string GetDirectory(const std::string& path);
103     static bool IsDirectory(const std::string& path);
104     static void IterateDirectory(const std::string& path, std::vector<std::string>& files);
105     static void LoadLine(const std::string& path, std::string& line);
106     static void LoadLines(const std::string& path, std::vector<std::string>& lines);
107     static void LoadContent(const std::string& path, std::string& content);
108 
109     static bool FileExists(const std::string& path);
110     static bool FileDelete(const std::string& path);
111     static FILE* FileOpen(const std::string& path, const std::string& options);
112     static void FileClose(FILE* file);
113     static bool IsFileValid(FILE* file);
114     static size_t FileSize(FILE* file);
115     static size_t FileTell(FILE* file);
116     static void FileSeek(FILE* file, int64_t offset, int32_t origin);
117     static void FileRead(FILE* file, void* data, size_t size);
118     static void FileWrite(FILE* file, const void* data, size_t size);
119 
120     template<typename T>
FileRead(FILE * file,T * data,size_t size)121     static void FileRead(FILE* file, T* data, size_t size)
122     {
123         FileRead(file, reinterpret_cast<void*>(data), size);
124     }
125 
126     template<typename T>
FileWrite(FILE * file,const T * data,size_t size)127     static void FileWrite(FILE* file, const T* data, size_t size)
128     {
129         FileWrite(file, reinterpret_cast<const void*>(data), size);
130     }
131 
132     // deprecated
133     static void FileRead(void* data, size_t size, size_t count, FILE* file);
134     static void FileWrite(const void* data, size_t size, size_t count, FILE* file);
135 
136     template<typename T>
FileRead(T * data,size_t size,size_t count,FILE * file)137     static void FileRead(T* data, size_t size, size_t count, FILE* file)
138     {
139         FileRead(reinterpret_cast<void*>(data), size, count, file);
140     }
141 
142     template<typename T>
FileWrite(const T * data,size_t size,size_t count,FILE * file)143     static void FileWrite(const T* data, size_t size, size_t count, FILE* file)
144     {
145         FileWrite(reinterpret_cast<const void*>(data), size, count, file);
146     }
147     // end of deprecation
148 
149     // NodeId/Pid routines
ExtractPid(uint64_t id)150     static constexpr pid_t ExtractPid(uint64_t id)
151     {
152         constexpr uint32_t bits = 32u;
153         return static_cast<pid_t>(id >> bits);
154     }
155 
GetMockPid(pid_t pid)156     static constexpr pid_t GetMockPid(pid_t pid)
157     {
158         constexpr uint32_t bits = 30u;
159         return static_cast<pid_t>((1u << bits) | static_cast<uint32_t>(pid));
160     }
161 
ExtractNodeId(uint64_t id)162     static constexpr uint64_t ExtractNodeId(uint64_t id)
163     {
164         constexpr uint32_t mask = 0xFFFFFFFF;
165         return (id & mask);
166     }
167 
ComposeNodeId(uint64_t pid,uint64_t nodeId)168     static constexpr uint64_t ComposeNodeId(uint64_t pid, uint64_t nodeId)
169     {
170         constexpr uint32_t bits = 32u;
171         return (pid << bits) | nodeId;
172     }
173 
ComposeMockNodeId(uint64_t id,uint64_t nodeId)174     static constexpr uint64_t ComposeMockNodeId(uint64_t id, uint64_t nodeId)
175     {
176         return ComposeNodeId(GetMockPid(id), nodeId);
177     }
178 
ComposeDataId(pid_t pid,uint32_t id)179     static uint64_t ComposeDataId(pid_t pid, uint32_t id)
180     {
181         constexpr uint32_t bits = 31u;
182         return ComposeNodeId(static_cast<uint32_t>(pid) | (1u << bits), id);
183     }
184 
GetRootNodeId(uint64_t id)185     static constexpr uint64_t GetRootNodeId(uint64_t id)
186     {
187         return ComposeNodeId(id, 1);
188     }
189 
PatchNodeId(uint64_t id)190     static constexpr uint64_t PatchNodeId(uint64_t id)
191     {
192         constexpr uint32_t bits = 62u;
193         return id | (static_cast<uint64_t>(1) << bits);
194     }
195 
IsNodeIdPatched(uint64_t id)196     static constexpr bool IsNodeIdPatched(uint64_t id)
197     {
198         constexpr uint32_t bits = 62u;
199         return id & (static_cast<uint64_t>(1) << bits);
200     }
201 };
202 
203 } // namespace OHOS::Rosen
204 
205 #endif // RS_PROFILER_UTILS_H