Oolite
Loading...
Searching...
No Matches
OOCPUInfo.m
Go to the documentation of this file.
1/*
2
3OOCPUInfo.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "OOCPUInfo.h"
26#include <stdlib.h>
27
28#if OOLITE_MAC_OS_X
29#include <sys/sysctl.h>
30#elif (OOLITE_LINUX || OOLITE_WINDOWS)
31// Workaround for clang/glibc incompatibility.
32#if !OOLITE_HAVE_CLANG
33#define __block __glibc_block
34#endif
35#include <unistd.h>
36#if !OOLITE_HAVE_CLANG
37#undef __block
38#endif
39#endif
40
41#if 0
42// Confirm settings
43#if OOLITE_BIG_ENDIAN
44#warning Big-endian.
45#endif
46#if OOLITE_LITTLE_ENDIAN
47#warning Little-endian.
48#endif
49#if OOLITE_NATIVE_64_BIT
50#warning 64-bit.
51#else
52#warning 32-bit.
53#endif
54#endif
55
56static BOOL sInited = NO;
57
58static NSUInteger sNumberOfCPUs = 0; // Yes, really 0.
59
60void OOCPUInfoInit(void)
61{
62 if (sInited)
63 return;
64
65 // Verify correctness of endian macros
66 uint8_t endianTag[4] = { 0x12, 0x34, 0x56, 0x78 };
67
68#if OOLITE_BIG_ENDIAN
69 if (*(uint32_t*)endianTag != 0x12345678) {
70 OOLog(@"cpuInfo.endianTest.failed", @"%@", @"OOLITE_BIG_ENDIAN is set, but the system is not big-endian -- aborting.");
71 exit(EXIT_FAILURE);
72 }
73#endif
74
75#if OOLITE_LITTLE_ENDIAN
76 if (*(uint32_t*)endianTag != 0x78563412) {
77 OOLog(@"cpuInfo.endianTest.failed", @"%@", @"OOLITE_LITTLE_ENDIAN is set, but the system is not little-endian -- aborting.");
78 exit(EXIT_FAILURE);
79 }
80#endif
81
82 // Count processors
83#if OOLITE_MAC_OS_X
84 sNumberOfCPUs = [[NSProcessInfo processInfo] processorCount];
85#elif OOLITE_WINDOWS
86 SYSTEM_INFO sysInfo;
87
88 GetSystemInfo(&sysInfo);
89 sNumberOfCPUs = sysInfo.dwNumberOfProcessors;
90#elif defined _SC_NPROCESSORS_ONLN
91 sNumberOfCPUs = sysconf(_SC_NPROCESSORS_ONLN);
92#else
93#warning Do not know how to find number of CPUs on this architecture.
94#endif // OS selection
95
96 sInited = YES;
97}
98
99NSUInteger OOCPUCount(void)
100{
101 if (!sInited)
103 return (sNumberOfCPUs != 0) ? sNumberOfCPUs : 1;
104}
105
106#if (OOLITE_WINDOWS || OOLITE_LINUX)
107#if OOLITE_LINUX
108#define OO_GNU_INLINE __attribute__((gnu_inline))
109#else
110#define OO_GNU_INLINE
111#endif
112/*
113Taken straight out of the x64 gcc's __cpuid because our 32-bit compiler does not define it
114*/
115inline OO_GNU_INLINE void OOCPUID(int CPUInfo[4], int InfoType)
116{
117 __asm__ __volatile__(
118/* Fixes building on 32-bit systems where %EBX is used for the GOT pointer */
119#if (OOLITE_LINUX && !defined __LP64__)
120 " pushl %%ebx\n"
121 " cpuid\n"
122 " mov %%ebx, %1\n"
123 " popl %%ebx"
124 : "=a"(CPUInfo[0]), "=r"(CPUInfo[1]), "=c"(CPUInfo[2]), "=d"(CPUInfo[3])
125#else
126 "cpuid"
127 : "=a"(CPUInfo[0]), "=b"(CPUInfo[1]), "=c"(CPUInfo[2]), "=d"(CPUInfo[3])
128#endif
129 : "a"(InfoType));
130}
131
132NSString* OOCPUDescription(void)
133{
134 // This code taken from https://stackoverflow.com/questions/850774
135 int CPUInfo[4] = { -1 };
136 unsigned nExIds, i = 0;
137 char CPUBrandString[0x40];
138 // Get the information associated with each extended ID.
139 OOCPUID(CPUInfo, 0x80000000);
140 nExIds = CPUInfo[0];
141 for (i = 0x80000000; i <= nExIds; ++i) {
142 OOCPUID(CPUInfo, i);
143 // Interpret CPU brand string
144 if (i == 0x80000002)
145 memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
146 else if (i == 0x80000003)
147 memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
148 else if (i == 0x80000004)
149 memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
150 }
151 return [NSString stringWithCString:CPUBrandString];
152}
153
154OOMemoryStatus OOSystemMemoryStatus(void)
155{
156 const unsigned int toMegabyte = 1024 * 1024;
157
158 OOMemoryStatus systemMemoryStatus;
159 memset(&systemMemoryStatus, 0, sizeof(systemMemoryStatus));
160
161#if OOLITE_WINDOWS
162 MEMORYSTATUSEX memStatus;
163 memStatus.dwLength = sizeof(memStatus);
164 if (GlobalMemoryStatusEx(&memStatus)) {
165 // return memory size in MB
166 systemMemoryStatus.ooPhysicalMemory = memStatus.ullTotalPhys / toMegabyte;
167 systemMemoryStatus.ooAvailableMemory = memStatus.ullAvailPhys / toMegabyte;
168 }
169#else
170 struct sysinfo si;
171 sysinfo(&si);
172 systemMemoryStatus.ooPhysicalMemory = (unsigned long long)(si.totalram / toMegabyte);
173 systemMemoryStatus.ooAvailableMemory = (unsigned long long)(si.freeram / toMegabyte);
174#endif
175
176 return systemMemoryStatus;
177}
178#endif //(OOLITE_WINDOWS || OOLITE_LINUX)
179
180#if OOLITE_WINDOWS
181NSString* operatingSystemFullVersion(void)
182{
183 OSVERSIONINFOW osver;
184 char outUBRString[65] = "";
185
186 osver.dwOSVersionInfoSize = sizeof(osver);
187 GetVersionExW(&osver);
188
189 // get the Update Build Revision from the Registry
190 HKEY hKey;
191 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
192 DWORD dwUBRSize = sizeof(DWORD);
193 DWORD dwUBR = 0;
194 if (RegQueryValueEx(hKey, "UBR", NULL, NULL, (BYTE*)&dwUBR, &dwUBRSize) == ERROR_SUCCESS) {
195 char strUBR[64] = "";
196 ltoa(dwUBR, strUBR, 10);
197 strcpy(outUBRString, ".");
198 strcat(outUBRString, strUBR);
199 }
200 }
201
202 return [NSString stringWithFormat:@"%lu.%lu.%lu%s %S",
203 osver.dwMajorVersion, osver.dwMinorVersion, osver.dwBuildNumber, outUBRString, (const WCHAR*)osver.szCSDVersion];
204}
205
206/*
207is64bitSystem: Detect operating system bitness. This function is based mainly on code by Mark S. Kolich, as
208seen in http://mark.koli.ch/2009/10/reliably-checking-os-bitness-32-or-64-bit-on-windows-with-a-tiny-c-app.html
209*/
210BOOL is64BitSystem(void)
211{
212#if defined(_WIN64)
213 // if we have been compiled as a 64-bit app and we are running, we are obviously on a 64-bit system
214 return YES;
215#else
216 BOOL is64Bit = NO;
217
218 IW64PFP IW64P = (IW64PFP)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
219 if (IW64P != NULL) {
220 IW64P(GetCurrentProcess(), &is64Bit);
221 }
222
223 return is64Bit;
224#endif
225}
226#endif // OOLITE_WINDOWS
void OOCPUInfoInit(void)
Definition OOCPUInfo.m:60
static BOOL sInited
Definition OOCPUInfo.m:56
static NSUInteger sNumberOfCPUs
Definition OOCPUInfo.m:58
NSUInteger OOCPUCount(void)
Definition OOCPUInfo.m:99
#define OOLITE_LINUX
#define OOLog(class, format,...)
Definition OOLogging.h:84
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque