• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Windows/System.cpp
2 
3 #include "StdAfx.h"
4 
5 #ifndef _WIN32
6 #include <unistd.h>
7 #include <limits.h>
8 #ifdef __APPLE__
9 #include <sys/sysctl.h>
10 #else
11 #include <sys/sysinfo.h>
12 #endif
13 #endif
14 
15 #include "../Common/Defs.h"
16 // #include "../Common/MyWindows.h"
17 
18 // #include "../../C/CpuArch.h"
19 
20 #include "System.h"
21 
22 namespace NWindows {
23 namespace NSystem {
24 
25 #ifdef _WIN32
26 
CountAffinity(DWORD_PTR mask)27 UInt32 CountAffinity(DWORD_PTR mask)
28 {
29   UInt32 num = 0;
30   for (unsigned i = 0; i < sizeof(mask) * 8; i++)
31     num += (UInt32)((mask >> i) & 1);
32   return num;
33 }
34 
Get()35 BOOL CProcessAffinity::Get()
36 {
37   #ifndef UNDER_CE
38   return GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask);
39   #else
40   return FALSE;
41   #endif
42 }
43 
44 
GetNumberOfProcessors()45 UInt32 GetNumberOfProcessors()
46 {
47   // We need to know how many threads we can use.
48   // By default the process is assigned to one group.
49   // So we get the number of logical processors (threads)
50   // assigned to current process in the current group.
51   // Group size can be smaller than total number logical processors, for exammple, 2x36
52 
53   CProcessAffinity pa;
54 
55   if (pa.Get() && pa.processAffinityMask != 0)
56     return pa.GetNumProcessThreads();
57 
58   SYSTEM_INFO systemInfo;
59   GetSystemInfo(&systemInfo);
60   // the number of logical processors in the current group
61   return (UInt32)systemInfo.dwNumberOfProcessors;
62 }
63 
64 #else
65 
66 
67 BOOL CProcessAffinity::Get()
68 {
69   numSysThreads = GetNumberOfProcessors();
70 
71   /*
72   numSysThreads = 8;
73   for (unsigned i = 0; i < numSysThreads; i++)
74     CpuSet_Set(&cpu_set, i);
75   return TRUE;
76   */
77 
78   #ifdef Z7_AFFINITY_SUPPORTED
79 
80   // numSysThreads = sysconf(_SC_NPROCESSORS_ONLN); // The number of processors currently online
81   if (sched_getaffinity(0, sizeof(cpu_set), &cpu_set) != 0)
82     return FALSE;
83   return TRUE;
84 
85   #else
86 
87   // cpu_set = ((CCpuSet)1 << (numSysThreads)) - 1;
88   return TRUE;
89   // errno = ENOSYS;
90   // return FALSE;
91 
92   #endif
93 }
94 
95 UInt32 GetNumberOfProcessors()
96 {
97   #ifndef Z7_ST
98   long n = sysconf(_SC_NPROCESSORS_CONF);  // The number of processors configured
99   if (n < 1)
100     n = 1;
101   return (UInt32)n;
102   #else
103   return 1;
104   #endif
105 }
106 
107 #endif
108 
109 
110 #ifdef _WIN32
111 
112 #ifndef UNDER_CE
113 
114 #if !defined(_WIN64) && \
115   (defined(__MINGW32_VERSION) || defined(Z7_OLD_WIN_SDK))
116 
117 typedef struct _MY_MEMORYSTATUSEX {
118   DWORD dwLength;
119   DWORD dwMemoryLoad;
120   DWORDLONG ullTotalPhys;
121   DWORDLONG ullAvailPhys;
122   DWORDLONG ullTotalPageFile;
123   DWORDLONG ullAvailPageFile;
124   DWORDLONG ullTotalVirtual;
125   DWORDLONG ullAvailVirtual;
126   DWORDLONG ullAvailExtendedVirtual;
127 } MY_MEMORYSTATUSEX, *MY_LPMEMORYSTATUSEX;
128 
129 #else
130 
131 #define MY_MEMORYSTATUSEX MEMORYSTATUSEX
132 #define MY_LPMEMORYSTATUSEX LPMEMORYSTATUSEX
133 
134 #endif
135 
136 typedef BOOL (WINAPI *Func_GlobalMemoryStatusEx)(MY_LPMEMORYSTATUSEX lpBuffer);
137 
138 #endif // !UNDER_CE
139 
140 
GetRamSize(UInt64 & size)141 bool GetRamSize(UInt64 &size)
142 {
143   size = (UInt64)(sizeof(size_t)) << 29;
144 
145   #ifndef UNDER_CE
146     MY_MEMORYSTATUSEX stat;
147     stat.dwLength = sizeof(stat);
148   #endif
149 
150   #ifdef _WIN64
151 
152     if (!::GlobalMemoryStatusEx(&stat))
153       return false;
154     size = MyMin(stat.ullTotalVirtual, stat.ullTotalPhys);
155     return true;
156 
157   #else
158 
159     #ifndef UNDER_CE
160       const
161       Func_GlobalMemoryStatusEx fn = Z7_GET_PROC_ADDRESS(
162       Func_GlobalMemoryStatusEx, ::GetModuleHandleA("kernel32.dll"),
163           "GlobalMemoryStatusEx");
164       if (fn && fn(&stat))
165       {
166         size = MyMin(stat.ullTotalVirtual, stat.ullTotalPhys);
167         return true;
168       }
169     #endif
170 
171     {
172       MEMORYSTATUS stat2;
173       stat2.dwLength = sizeof(stat2);
174       ::GlobalMemoryStatus(&stat2);
175       size = MyMin(stat2.dwTotalVirtual, stat2.dwTotalPhys);
176       return true;
177     }
178   #endif
179 }
180 
181 #else
182 
183 // POSIX
184 // #include <stdio.h>
185 
GetRamSize(UInt64 & size)186 bool GetRamSize(UInt64 &size)
187 {
188   size = (UInt64)(sizeof(size_t)) << 29;
189 
190   #ifdef __APPLE__
191 
192     #ifdef HW_MEMSIZE
193       uint64_t val = 0; // support 2Gb+ RAM
194       int mib[2] = { CTL_HW, HW_MEMSIZE };
195     #elif defined(HW_PHYSMEM64)
196       uint64_t val = 0; // support 2Gb+ RAM
197       int mib[2] = { CTL_HW, HW_PHYSMEM64 };
198     #else
199       unsigned int val = 0; // For old system
200       int mib[2] = { CTL_HW, HW_PHYSMEM };
201     #endif // HW_MEMSIZE
202       size_t size_sys = sizeof(val);
203 
204       sysctl(mib, 2, &val, &size_sys, NULL, 0);
205       if (val)
206         size = val;
207 
208   #elif defined(_AIX)
209   // fixme
210   #elif defined(__gnu_hurd__)
211   // fixme
212   #elif defined(__FreeBSD_kernel__) && defined(__GLIBC__)
213   // GNU/kFreeBSD Debian
214   // fixme
215   #else
216 
217   struct sysinfo info;
218   if (::sysinfo(&info) != 0)
219     return false;
220   size = (UInt64)info.mem_unit * info.totalram;
221   const UInt64 kLimit = (UInt64)1 << (sizeof(size_t) * 8 - 1);
222   if (size > kLimit)
223     size = kLimit;
224 
225   /*
226   printf("\n mem_unit  = %lld", (UInt64)info.mem_unit);
227   printf("\n totalram  = %lld", (UInt64)info.totalram);
228   printf("\n freeram   = %lld", (UInt64)info.freeram);
229   */
230 
231   #endif
232 
233   return true;
234 }
235 
236 #endif
237 
238 
Get_File_OPEN_MAX()239 unsigned long Get_File_OPEN_MAX()
240 {
241  #ifdef _WIN32
242   return (1 << 24) - (1 << 16); // ~16M handles
243  #else
244   // some linux versions have default open file limit for user process of 1024 files.
245   long n = sysconf(_SC_OPEN_MAX);
246   // n = -1; // for debug
247   // n = 9; // for debug
248   if (n < 1)
249   {
250     // n = OPEN_MAX;  // ???
251     // n = FOPEN_MAX; // = 16 : <stdio.h>
252    #ifdef _POSIX_OPEN_MAX
253     n = _POSIX_OPEN_MAX; // = 20 : <limits.h>
254    #else
255     n = 30; // our limit
256    #endif
257   }
258   return (unsigned long)n;
259  #endif
260 }
261 
Get_File_OPEN_MAX_Reduced_for_3_tasks()262 unsigned Get_File_OPEN_MAX_Reduced_for_3_tasks()
263 {
264   unsigned long numFiles_OPEN_MAX = NSystem::Get_File_OPEN_MAX();
265   const unsigned delta = 10; // the reserve for another internal needs of process
266   if (numFiles_OPEN_MAX > delta)
267     numFiles_OPEN_MAX -= delta;
268   else
269     numFiles_OPEN_MAX = 1;
270   numFiles_OPEN_MAX /= 3; // we suppose that we have up to 3 tasks in total for multiple file processing
271   numFiles_OPEN_MAX = MyMax(numFiles_OPEN_MAX, (unsigned long)3);
272   unsigned n = (UInt32)(UInt32)-1;
273   if (n > numFiles_OPEN_MAX)
274     n = (unsigned)numFiles_OPEN_MAX;
275   return n;
276 }
277 
278 }}
279