• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 JS_CONCURRENT_MODULE_COMMON_HELPER_CONCURRENT_HELPER_H
17 #define JS_CONCURRENT_MODULE_COMMON_HELPER_CONCURRENT_HELPER_H
18 
19 #include <chrono>
20 #include <fstream>
21 #include <sstream>
22 #include <uv.h>
23 #if defined(OHOS_PLATFORM)
24 #include <unistd.h>
25 #elif defined(WINDOWS_PLATFORM)
26 #include <windows.h>
27 #elif defined(MAC_PLATFORM) || defined(IOS_PLATFORM)
28 #include <sys/sysctl.h>
29 #elif defined(ANDROID_PLATFORM)
30 #include <sys/sysinfo.h>
31 #endif
32 
33 #if __GNUC__
34 #define LIKELY(x) __builtin_expect(!!(x), 1)
35 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
36 #else
37 #define LIKELY(x) (!!(x))
38 #define UNLIKELY(x) (!!(x))
39 #endif // __GNUC__
40 
41 namespace Commonlibrary::Concurrent::Common::Helper {
42 class ConcurrentHelper {
43 public:
44     using UvCallback = void(*)(const uv_async_t*);
45 
46     ConcurrentHelper() = delete;
47     ~ConcurrentHelper() = delete;
48 
49     enum class SystemMemoryLevel {
50         MEMORY_LEVEL_LOW,
51         MEMORY_LEVEL_MODERATE,
52         MEMORY_LEVEL_NORMAL
53     };
54 
GetMaxThreads()55     static uint32_t GetMaxThreads()
56     {
57 #if defined(OHOS_PLATFORM)
58         return sysconf(_SC_NPROCESSORS_ONLN) - 1;
59 #elif defined(WINDOWS_PLATFORM)
60         SYSTEM_INFO sysInfo;
61         GetSystemInfo(&sysInfo);
62         return sysInfo.dwNumberOfProcessors - 1;
63 #elif defined(MAC_PLATFORM) || defined(IOS_PLATFORM)
64         int32_t numCpu = 0;
65         size_t size = sizeof(numCpu);
66         sysctlbyname("hw.ncpu", &numCpu, &size, nullptr, 0);
67         return numCpu - 1;
68 #elif defined(ANDROID_PLATFORM)
69         return get_nprocs() - 1;
70 #else
71         return 1; // 1: default number
72 #endif
73     }
74 
GetMilliseconds()75     static uint64_t GetMilliseconds()
76     {
77         auto now = std::chrono::system_clock::now();
78         auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
79         return millisecs.count();
80     }
81 
82     static int32_t UvHandleInit(uv_loop_t* loop, uv_async_t*& handle, UvCallback func, void* data = nullptr)
83     {
84         handle = new uv_async_t;
85         handle->data = data;
86         int32_t initState = uv_async_init(loop, handle, reinterpret_cast<uv_async_cb>(func));
87         return initState;
88     }
89 
90     template<typename T>
UvHandleClose(T * & handle)91     static void UvHandleClose(T*& handle)
92     {
93         if (handle == nullptr) {
94             return;
95         }
96         uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
97             if (handle != nullptr) {
98                 delete reinterpret_cast<T*>(handle);
99                 handle = nullptr;
100             }
101         });
102         handle = nullptr;
103     }
104 
IsUvClosing(uv_async_t * handle)105     static bool IsUvClosing(uv_async_t* handle)
106     {
107         return uv_is_closing((uv_handle_t*)handle);
108     }
109 
IsUvActive(uv_async_t * handle)110     static bool IsUvActive(uv_async_t* handle)
111     {
112         return handle != nullptr && !IsUvClosing(handle);
113     }
114 
UvCheckAndAsyncSend(uv_async_t * handle)115     static void UvCheckAndAsyncSend(uv_async_t* handle)
116     {
117         if (LIKELY(IsUvActive(handle))) {
118             uv_async_send(handle);
119         }
120     }
121 
122 #if defined(OHOS_PLATFORM)
123     static std::optional<double> GetSystemMemoryRatio();
124     static SystemMemoryLevel GetMemoryLevel();
125     static uint64_t ParseLine(const std::string& line);
126 #endif
127     static bool IsLowMemory();
128     static bool IsModerateMemory();
129 };
130 } // namespace Commonlibrary::Concurrent::Common::Helper
131 #endif // JS_CONCURRENT_MODULE_COMMON_HELPER_CONCURRENT_HELPER_H