1370b324cSopenharmony_ci// Windows/System.cpp 2370b324cSopenharmony_ci 3370b324cSopenharmony_ci#include "StdAfx.h" 4370b324cSopenharmony_ci 5370b324cSopenharmony_ci#ifndef _WIN32 6370b324cSopenharmony_ci#include <unistd.h> 7370b324cSopenharmony_ci#include <limits.h> 8370b324cSopenharmony_ci#ifdef __APPLE__ 9370b324cSopenharmony_ci#include <sys/sysctl.h> 10370b324cSopenharmony_ci#else 11370b324cSopenharmony_ci#include <sys/sysinfo.h> 12370b324cSopenharmony_ci#endif 13370b324cSopenharmony_ci#endif 14370b324cSopenharmony_ci 15370b324cSopenharmony_ci#include "../Common/Defs.h" 16370b324cSopenharmony_ci// #include "../Common/MyWindows.h" 17370b324cSopenharmony_ci 18370b324cSopenharmony_ci// #include "../../C/CpuArch.h" 19370b324cSopenharmony_ci 20370b324cSopenharmony_ci#include "System.h" 21370b324cSopenharmony_ci 22370b324cSopenharmony_cinamespace NWindows { 23370b324cSopenharmony_cinamespace NSystem { 24370b324cSopenharmony_ci 25370b324cSopenharmony_ci#ifdef _WIN32 26370b324cSopenharmony_ci 27370b324cSopenharmony_ciUInt32 CountAffinity(DWORD_PTR mask) 28370b324cSopenharmony_ci{ 29370b324cSopenharmony_ci UInt32 num = 0; 30370b324cSopenharmony_ci for (unsigned i = 0; i < sizeof(mask) * 8; i++) 31370b324cSopenharmony_ci num += (UInt32)((mask >> i) & 1); 32370b324cSopenharmony_ci return num; 33370b324cSopenharmony_ci} 34370b324cSopenharmony_ci 35370b324cSopenharmony_ciBOOL CProcessAffinity::Get() 36370b324cSopenharmony_ci{ 37370b324cSopenharmony_ci #ifndef UNDER_CE 38370b324cSopenharmony_ci return GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask); 39370b324cSopenharmony_ci #else 40370b324cSopenharmony_ci return FALSE; 41370b324cSopenharmony_ci #endif 42370b324cSopenharmony_ci} 43370b324cSopenharmony_ci 44370b324cSopenharmony_ci 45370b324cSopenharmony_ciUInt32 GetNumberOfProcessors() 46370b324cSopenharmony_ci{ 47370b324cSopenharmony_ci // We need to know how many threads we can use. 48370b324cSopenharmony_ci // By default the process is assigned to one group. 49370b324cSopenharmony_ci // So we get the number of logical processors (threads) 50370b324cSopenharmony_ci // assigned to current process in the current group. 51370b324cSopenharmony_ci // Group size can be smaller than total number logical processors, for exammple, 2x36 52370b324cSopenharmony_ci 53370b324cSopenharmony_ci CProcessAffinity pa; 54370b324cSopenharmony_ci 55370b324cSopenharmony_ci if (pa.Get() && pa.processAffinityMask != 0) 56370b324cSopenharmony_ci return pa.GetNumProcessThreads(); 57370b324cSopenharmony_ci 58370b324cSopenharmony_ci SYSTEM_INFO systemInfo; 59370b324cSopenharmony_ci GetSystemInfo(&systemInfo); 60370b324cSopenharmony_ci // the number of logical processors in the current group 61370b324cSopenharmony_ci return (UInt32)systemInfo.dwNumberOfProcessors; 62370b324cSopenharmony_ci} 63370b324cSopenharmony_ci 64370b324cSopenharmony_ci#else 65370b324cSopenharmony_ci 66370b324cSopenharmony_ci 67370b324cSopenharmony_ciBOOL CProcessAffinity::Get() 68370b324cSopenharmony_ci{ 69370b324cSopenharmony_ci numSysThreads = GetNumberOfProcessors(); 70370b324cSopenharmony_ci 71370b324cSopenharmony_ci /* 72370b324cSopenharmony_ci numSysThreads = 8; 73370b324cSopenharmony_ci for (unsigned i = 0; i < numSysThreads; i++) 74370b324cSopenharmony_ci CpuSet_Set(&cpu_set, i); 75370b324cSopenharmony_ci return TRUE; 76370b324cSopenharmony_ci */ 77370b324cSopenharmony_ci 78370b324cSopenharmony_ci #ifdef Z7_AFFINITY_SUPPORTED 79370b324cSopenharmony_ci 80370b324cSopenharmony_ci // numSysThreads = sysconf(_SC_NPROCESSORS_ONLN); // The number of processors currently online 81370b324cSopenharmony_ci if (sched_getaffinity(0, sizeof(cpu_set), &cpu_set) != 0) 82370b324cSopenharmony_ci return FALSE; 83370b324cSopenharmony_ci return TRUE; 84370b324cSopenharmony_ci 85370b324cSopenharmony_ci #else 86370b324cSopenharmony_ci 87370b324cSopenharmony_ci // cpu_set = ((CCpuSet)1 << (numSysThreads)) - 1; 88370b324cSopenharmony_ci return TRUE; 89370b324cSopenharmony_ci // errno = ENOSYS; 90370b324cSopenharmony_ci // return FALSE; 91370b324cSopenharmony_ci 92370b324cSopenharmony_ci #endif 93370b324cSopenharmony_ci} 94370b324cSopenharmony_ci 95370b324cSopenharmony_ciUInt32 GetNumberOfProcessors() 96370b324cSopenharmony_ci{ 97370b324cSopenharmony_ci #ifndef Z7_ST 98370b324cSopenharmony_ci long n = sysconf(_SC_NPROCESSORS_CONF); // The number of processors configured 99370b324cSopenharmony_ci if (n < 1) 100370b324cSopenharmony_ci n = 1; 101370b324cSopenharmony_ci return (UInt32)n; 102370b324cSopenharmony_ci #else 103370b324cSopenharmony_ci return 1; 104370b324cSopenharmony_ci #endif 105370b324cSopenharmony_ci} 106370b324cSopenharmony_ci 107370b324cSopenharmony_ci#endif 108370b324cSopenharmony_ci 109370b324cSopenharmony_ci 110370b324cSopenharmony_ci#ifdef _WIN32 111370b324cSopenharmony_ci 112370b324cSopenharmony_ci#ifndef UNDER_CE 113370b324cSopenharmony_ci 114370b324cSopenharmony_ci#if !defined(_WIN64) && \ 115370b324cSopenharmony_ci (defined(__MINGW32_VERSION) || defined(Z7_OLD_WIN_SDK)) 116370b324cSopenharmony_ci 117370b324cSopenharmony_citypedef struct _MY_MEMORYSTATUSEX { 118370b324cSopenharmony_ci DWORD dwLength; 119370b324cSopenharmony_ci DWORD dwMemoryLoad; 120370b324cSopenharmony_ci DWORDLONG ullTotalPhys; 121370b324cSopenharmony_ci DWORDLONG ullAvailPhys; 122370b324cSopenharmony_ci DWORDLONG ullTotalPageFile; 123370b324cSopenharmony_ci DWORDLONG ullAvailPageFile; 124370b324cSopenharmony_ci DWORDLONG ullTotalVirtual; 125370b324cSopenharmony_ci DWORDLONG ullAvailVirtual; 126370b324cSopenharmony_ci DWORDLONG ullAvailExtendedVirtual; 127370b324cSopenharmony_ci} MY_MEMORYSTATUSEX, *MY_LPMEMORYSTATUSEX; 128370b324cSopenharmony_ci 129370b324cSopenharmony_ci#else 130370b324cSopenharmony_ci 131370b324cSopenharmony_ci#define MY_MEMORYSTATUSEX MEMORYSTATUSEX 132370b324cSopenharmony_ci#define MY_LPMEMORYSTATUSEX LPMEMORYSTATUSEX 133370b324cSopenharmony_ci 134370b324cSopenharmony_ci#endif 135370b324cSopenharmony_ci 136370b324cSopenharmony_citypedef BOOL (WINAPI *Func_GlobalMemoryStatusEx)(MY_LPMEMORYSTATUSEX lpBuffer); 137370b324cSopenharmony_ci 138370b324cSopenharmony_ci#endif // !UNDER_CE 139370b324cSopenharmony_ci 140370b324cSopenharmony_ci 141370b324cSopenharmony_cibool GetRamSize(UInt64 &size) 142370b324cSopenharmony_ci{ 143370b324cSopenharmony_ci size = (UInt64)(sizeof(size_t)) << 29; 144370b324cSopenharmony_ci 145370b324cSopenharmony_ci #ifndef UNDER_CE 146370b324cSopenharmony_ci MY_MEMORYSTATUSEX stat; 147370b324cSopenharmony_ci stat.dwLength = sizeof(stat); 148370b324cSopenharmony_ci #endif 149370b324cSopenharmony_ci 150370b324cSopenharmony_ci #ifdef _WIN64 151370b324cSopenharmony_ci 152370b324cSopenharmony_ci if (!::GlobalMemoryStatusEx(&stat)) 153370b324cSopenharmony_ci return false; 154370b324cSopenharmony_ci size = MyMin(stat.ullTotalVirtual, stat.ullTotalPhys); 155370b324cSopenharmony_ci return true; 156370b324cSopenharmony_ci 157370b324cSopenharmony_ci #else 158370b324cSopenharmony_ci 159370b324cSopenharmony_ci #ifndef UNDER_CE 160370b324cSopenharmony_ci const 161370b324cSopenharmony_ci Func_GlobalMemoryStatusEx fn = Z7_GET_PROC_ADDRESS( 162370b324cSopenharmony_ci Func_GlobalMemoryStatusEx, ::GetModuleHandleA("kernel32.dll"), 163370b324cSopenharmony_ci "GlobalMemoryStatusEx"); 164370b324cSopenharmony_ci if (fn && fn(&stat)) 165370b324cSopenharmony_ci { 166370b324cSopenharmony_ci size = MyMin(stat.ullTotalVirtual, stat.ullTotalPhys); 167370b324cSopenharmony_ci return true; 168370b324cSopenharmony_ci } 169370b324cSopenharmony_ci #endif 170370b324cSopenharmony_ci 171370b324cSopenharmony_ci { 172370b324cSopenharmony_ci MEMORYSTATUS stat2; 173370b324cSopenharmony_ci stat2.dwLength = sizeof(stat2); 174370b324cSopenharmony_ci ::GlobalMemoryStatus(&stat2); 175370b324cSopenharmony_ci size = MyMin(stat2.dwTotalVirtual, stat2.dwTotalPhys); 176370b324cSopenharmony_ci return true; 177370b324cSopenharmony_ci } 178370b324cSopenharmony_ci #endif 179370b324cSopenharmony_ci} 180370b324cSopenharmony_ci 181370b324cSopenharmony_ci#else 182370b324cSopenharmony_ci 183370b324cSopenharmony_ci// POSIX 184370b324cSopenharmony_ci// #include <stdio.h> 185370b324cSopenharmony_ci 186370b324cSopenharmony_cibool GetRamSize(UInt64 &size) 187370b324cSopenharmony_ci{ 188370b324cSopenharmony_ci size = (UInt64)(sizeof(size_t)) << 29; 189370b324cSopenharmony_ci 190370b324cSopenharmony_ci #ifdef __APPLE__ 191370b324cSopenharmony_ci 192370b324cSopenharmony_ci #ifdef HW_MEMSIZE 193370b324cSopenharmony_ci uint64_t val = 0; // support 2Gb+ RAM 194370b324cSopenharmony_ci int mib[2] = { CTL_HW, HW_MEMSIZE }; 195370b324cSopenharmony_ci #elif defined(HW_PHYSMEM64) 196370b324cSopenharmony_ci uint64_t val = 0; // support 2Gb+ RAM 197370b324cSopenharmony_ci int mib[2] = { CTL_HW, HW_PHYSMEM64 }; 198370b324cSopenharmony_ci #else 199370b324cSopenharmony_ci unsigned int val = 0; // For old system 200370b324cSopenharmony_ci int mib[2] = { CTL_HW, HW_PHYSMEM }; 201370b324cSopenharmony_ci #endif // HW_MEMSIZE 202370b324cSopenharmony_ci size_t size_sys = sizeof(val); 203370b324cSopenharmony_ci 204370b324cSopenharmony_ci sysctl(mib, 2, &val, &size_sys, NULL, 0); 205370b324cSopenharmony_ci if (val) 206370b324cSopenharmony_ci size = val; 207370b324cSopenharmony_ci 208370b324cSopenharmony_ci #elif defined(_AIX) 209370b324cSopenharmony_ci // fixme 210370b324cSopenharmony_ci #elif defined(__gnu_hurd__) 211370b324cSopenharmony_ci // fixme 212370b324cSopenharmony_ci #elif defined(__FreeBSD_kernel__) && defined(__GLIBC__) 213370b324cSopenharmony_ci // GNU/kFreeBSD Debian 214370b324cSopenharmony_ci // fixme 215370b324cSopenharmony_ci #else 216370b324cSopenharmony_ci 217370b324cSopenharmony_ci struct sysinfo info; 218370b324cSopenharmony_ci if (::sysinfo(&info) != 0) 219370b324cSopenharmony_ci return false; 220370b324cSopenharmony_ci size = (UInt64)info.mem_unit * info.totalram; 221370b324cSopenharmony_ci const UInt64 kLimit = (UInt64)1 << (sizeof(size_t) * 8 - 1); 222370b324cSopenharmony_ci if (size > kLimit) 223370b324cSopenharmony_ci size = kLimit; 224370b324cSopenharmony_ci 225370b324cSopenharmony_ci /* 226370b324cSopenharmony_ci printf("\n mem_unit = %lld", (UInt64)info.mem_unit); 227370b324cSopenharmony_ci printf("\n totalram = %lld", (UInt64)info.totalram); 228370b324cSopenharmony_ci printf("\n freeram = %lld", (UInt64)info.freeram); 229370b324cSopenharmony_ci */ 230370b324cSopenharmony_ci 231370b324cSopenharmony_ci #endif 232370b324cSopenharmony_ci 233370b324cSopenharmony_ci return true; 234370b324cSopenharmony_ci} 235370b324cSopenharmony_ci 236370b324cSopenharmony_ci#endif 237370b324cSopenharmony_ci 238370b324cSopenharmony_ci 239370b324cSopenharmony_ciunsigned long Get_File_OPEN_MAX() 240370b324cSopenharmony_ci{ 241370b324cSopenharmony_ci #ifdef _WIN32 242370b324cSopenharmony_ci return (1 << 24) - (1 << 16); // ~16M handles 243370b324cSopenharmony_ci #else 244370b324cSopenharmony_ci // some linux versions have default open file limit for user process of 1024 files. 245370b324cSopenharmony_ci long n = sysconf(_SC_OPEN_MAX); 246370b324cSopenharmony_ci // n = -1; // for debug 247370b324cSopenharmony_ci // n = 9; // for debug 248370b324cSopenharmony_ci if (n < 1) 249370b324cSopenharmony_ci { 250370b324cSopenharmony_ci // n = OPEN_MAX; // ??? 251370b324cSopenharmony_ci // n = FOPEN_MAX; // = 16 : <stdio.h> 252370b324cSopenharmony_ci #ifdef _POSIX_OPEN_MAX 253370b324cSopenharmony_ci n = _POSIX_OPEN_MAX; // = 20 : <limits.h> 254370b324cSopenharmony_ci #else 255370b324cSopenharmony_ci n = 30; // our limit 256370b324cSopenharmony_ci #endif 257370b324cSopenharmony_ci } 258370b324cSopenharmony_ci return (unsigned long)n; 259370b324cSopenharmony_ci #endif 260370b324cSopenharmony_ci} 261370b324cSopenharmony_ci 262370b324cSopenharmony_ciunsigned Get_File_OPEN_MAX_Reduced_for_3_tasks() 263370b324cSopenharmony_ci{ 264370b324cSopenharmony_ci unsigned long numFiles_OPEN_MAX = NSystem::Get_File_OPEN_MAX(); 265370b324cSopenharmony_ci const unsigned delta = 10; // the reserve for another internal needs of process 266370b324cSopenharmony_ci if (numFiles_OPEN_MAX > delta) 267370b324cSopenharmony_ci numFiles_OPEN_MAX -= delta; 268370b324cSopenharmony_ci else 269370b324cSopenharmony_ci numFiles_OPEN_MAX = 1; 270370b324cSopenharmony_ci numFiles_OPEN_MAX /= 3; // we suppose that we have up to 3 tasks in total for multiple file processing 271370b324cSopenharmony_ci numFiles_OPEN_MAX = MyMax(numFiles_OPEN_MAX, (unsigned long)3); 272370b324cSopenharmony_ci unsigned n = (UInt32)(UInt32)-1; 273370b324cSopenharmony_ci if (n > numFiles_OPEN_MAX) 274370b324cSopenharmony_ci n = (unsigned)numFiles_OPEN_MAX; 275370b324cSopenharmony_ci return n; 276370b324cSopenharmony_ci} 277370b324cSopenharmony_ci 278370b324cSopenharmony_ci}} 279