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 41namespace Commonlibrary::Concurrent::Common::Helper { 42class ConcurrentHelper { 43public: 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 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 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 void 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 uv_async_init(loop, handle, reinterpret_cast<uv_async_cb>(func)); 87 } 88 89 template<typename T> 90 static void UvHandleClose(T* handle) 91 { 92 if (handle == nullptr) { 93 return; 94 } 95 uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) { 96 if (handle != nullptr) { 97 delete reinterpret_cast<T*>(handle); 98 handle = nullptr; 99 } 100 }); 101 } 102 103#if defined(OHOS_PLATFORM) 104 static std::optional<double> GetSystemMemoryRatio(); 105 static SystemMemoryLevel GetMemoryLevel(); 106 static uint64_t ParseLine(const std::string& line); 107#endif 108 static bool IsLowMemory(); 109 static bool IsModerateMemory(); 110}; 111} // namespace Commonlibrary::Concurrent::Common::Helper 112#endif // JS_CONCURRENT_MODULE_COMMON_HELPER_CONCURRENT_HELPER_H