macCPU.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #import <sys/types.h>
  23. #import <sys/sysctl.h>
  24. #import <mach/machine.h>
  25. #import <math.h>
  26. #import <CoreServices/CoreServices.h>
  27. #import "platform/platformAssert.h"
  28. #import "console/console.h"
  29. #import "core/stringTable.h"
  30. #import "platform/platformCPUCount.h"
  31. // Gestalt has been deprecated
  32. // we now have to use NSProcessInfo
  33. #import <Foundation/Foundation.h>
  34. //recently removed in Xcode 8 - most likely don't need these anymore
  35. #ifndef CPUFAMILY_INTEL_YONAH
  36. #define CPUFAMILY_INTEL_YONAH 0x73d67300
  37. #endif
  38. #ifndef CPUFAMILY_INTEL_MEROM
  39. #define CPUFAMILY_INTEL_MEROM 0x426f69ef
  40. #endif
  41. // Original code by Sean O'Brien (http://www.garagegames.com/community/forums/viewthread/81815).
  42. // Reads sysctl() string value into buffer at DEST with maximum length MAXLEN
  43. // Return: 0 on success, non-zero is error in accordance with stdlib and <errno.h>
  44. int _getSysCTLstring(const char key[], char * dest, size_t maxlen) {
  45. size_t len = 0;
  46. int err;
  47. // Call with NULL for 'dest' to have the required size stored in 'len'. If the 'key'
  48. // doesn't exist, 'err' will be -1 and if all goes well, it will be 0.
  49. err = sysctlbyname(key, NULL, &len, NULL, 0);
  50. if (err == 0) {
  51. AssertWarn((len <= maxlen), ("Insufficient buffer length for SYSCTL() read. Truncating.\n"));
  52. if (len > maxlen)
  53. len = maxlen;
  54. // Call with actual pointers to 'dest' and clamped 'len' fields to perform the read.
  55. err = sysctlbyname(key, dest, &len, NULL, 0);
  56. }
  57. return err;
  58. }
  59. // TEMPLATED Reads sysctl() integer value into variable DEST of type T
  60. // The two predominant types used are unsigned longs and unsiged long longs
  61. // and the size of the argument is on a case-by-case value. As a "guide" the
  62. // resources at Apple claim that any "byte count" or "frequency" values will
  63. // be returned as ULL's and most everything else will be UL's.
  64. // Return: 0 on success, non-zero is error in accordance with stdlib and <errno.h>
  65. template <typename T>
  66. int _getSysCTLvalue(const char key[], T * dest) {
  67. size_t len = 0;
  68. int err;
  69. // Call with NULL for 'dest' to get the size. If the 'key' doesn't exist, the
  70. // 'err' returned will be -1, so 0 indicates success.
  71. err = sysctlbyname(key, NULL, &len, NULL, 0);
  72. if (err == 0) {
  73. AssertFatal((len == sizeof(T)), "Mis-matched destination type for SYSCTL() read.\n");
  74. // We're just double-checking that we're being called with the correct type of
  75. // pointer for 'dest' so we don't clobber anything nearby when writing back.
  76. err = sysctlbyname(key, dest, &len, NULL, 0);
  77. }
  78. return err;
  79. }
  80. Platform::SystemInfo_struct Platform::SystemInfo;
  81. #define BASE_MHZ_SPEED 0
  82. //TODO update cpu list
  83. void Processor::init()
  84. {
  85. U32 procflags;
  86. int err, cpufam, cputype, cpusub;
  87. char buf[255];
  88. U32 lraw;
  89. U64 llraw;
  90. Con::printf( "System & Processor Information:" );
  91. // Gestalt has been deprecated since Mac OSX Mountain Lion and has stopped working on
  92. // Mac OSX Yosemite. we have to use NSProcessInfo now.
  93. // Availability: Mac OS 10.2 or greater.
  94. NSString *osVersionStr = [[NSProcessInfo processInfo] operatingSystemVersionString];
  95. Con::printf( " OSX Version: %s", [osVersionStr UTF8String]);
  96. err = _getSysCTLstring("kern.ostype", buf, sizeof(buf));
  97. if (err)
  98. Con::printf( " Unable to determine OS type\n" );
  99. else
  100. Con::printf( " Mac OS Kernel name: %s", buf);
  101. err = _getSysCTLstring("kern.osrelease", buf, sizeof(buf));
  102. if (err)
  103. Con::printf( " Unable to determine OS release number\n" );
  104. else
  105. Con::printf( " Mac OS Kernel version: %s", buf );
  106. err = _getSysCTLvalue<U64>("hw.memsize", &llraw);
  107. if (err)
  108. Con::printf( " Unable to determine amount of physical RAM\n" );
  109. else
  110. Con::printf( " Physical memory installed: %d MB", (llraw >> 20));
  111. err = _getSysCTLvalue<U32>("hw.usermem", &lraw);
  112. if (err)
  113. Con::printf( " Unable to determine available user address space\n");
  114. else
  115. Con::printf( " Addressable user memory: %d MB", (lraw >> 20));
  116. ////////////////////////////////
  117. // Values for the Family Type, CPU Type and CPU Subtype are defined in the
  118. // SDK files for the Mach Kernel ==> mach/machine.h
  119. ////////////////////////////////
  120. // CPU Family, Type, and Subtype
  121. cpufam = 0;
  122. cputype = 0;
  123. cpusub = 0;
  124. err = _getSysCTLvalue<U32>("hw.cpufamily", &lraw);
  125. if (err)
  126. Con::printf( " Unable to determine 'family' of CPU\n");
  127. else {
  128. cpufam = (int) lraw;
  129. err = _getSysCTLvalue<U32>("hw.cputype", &lraw);
  130. if (err)
  131. Con::printf( " Unable to determine CPU type\n");
  132. else {
  133. cputype = (int) lraw;
  134. err = _getSysCTLvalue<U32>("hw.cpusubtype", &lraw);
  135. if (err)
  136. Con::printf( " Unable to determine CPU subtype\n");
  137. else
  138. cpusub = (int) lraw;
  139. // If we've made it this far,
  140. Con::printf( " Installed processor ID: Family 0x%08x Type %d Subtype %d",cpufam, cputype,cpusub);
  141. }
  142. }
  143. // The Gestalt version was known to have issues with some Processor Upgrade cards
  144. // but it is uncertain whether this version has similar issues.
  145. err = _getSysCTLvalue<U64>("hw.cpufrequency", &llraw);
  146. if (err) {
  147. llraw = BASE_MHZ_SPEED;
  148. Con::printf( " Unable to determine CPU Frequency. Defaulting to %d MHz\n", llraw);
  149. } else {
  150. llraw /= 1000000;
  151. Con::printf( " Installed processor clock frequency: %d MHz", llraw);
  152. }
  153. Platform::SystemInfo.processor.mhz = (unsigned int)llraw;
  154. // Here's one that the original version of this routine couldn't do -- number
  155. // of processors (cores)
  156. U32 ncpu = 1;
  157. err = _getSysCTLvalue<U32>("hw.ncpu", &lraw);
  158. if (err)
  159. Con::printf( " Unable to determine number of processor cores\n");
  160. else
  161. {
  162. ncpu = lraw;
  163. Con::printf( " Installed/available processor cores: %d", lraw);
  164. }
  165. // Now use CPUFAM to determine and then store the processor type
  166. // and 'friendly name' in GG-accessible structure. Note that since
  167. // we have access to the Family code, the Type and Subtypes are useless.
  168. //
  169. // NOTE: Even this level of detail is almost assuredly not needed anymore
  170. // and the Optional Capability flags (further down) should be more than enough.
  171. switch(cpufam)
  172. {
  173. case CPUFAMILY_INTEL_YONAH:
  174. Platform::SystemInfo.processor.type = CPU_Intel_Core;
  175. if( ncpu == 2 )
  176. Platform::SystemInfo.processor.name = StringTable->insert("Intel Core Duo");
  177. else
  178. Platform::SystemInfo.processor.name = StringTable->insert("Intel Core");
  179. break;
  180. case CPUFAMILY_INTEL_PENRYN:
  181. case CPUFAMILY_INTEL_MEROM:
  182. Platform::SystemInfo.processor.type = CPU_Intel_Core2;
  183. if( ncpu == 4 )
  184. Platform::SystemInfo.processor.name = StringTable->insert("Intel Core 2 Quad");
  185. else
  186. Platform::SystemInfo.processor.name = StringTable->insert("Intel Core 2 Duo");
  187. break;
  188. case CPUFAMILY_INTEL_NEHALEM:
  189. Platform::SystemInfo.processor.type = CPU_Intel_Core2;
  190. Platform::SystemInfo.processor.name = StringTable->insert( "Intel 'Nehalem' Core Processor" );
  191. break;
  192. default:
  193. // explain why we can't get the processor type.
  194. Con::warnf( " Unknown Processor (family, type, subtype): 0x%x\t%d %d", cpufam, cputype, cpusub);
  195. // for now, identify it as an x86 processor, because Apple is moving to Intel chips...
  196. Platform::SystemInfo.processor.type = CPU_X86Compatible;
  197. Platform::SystemInfo.processor.name = StringTable->insert("Unknown Processor, assuming x86 Compatible");
  198. break;
  199. }
  200. // Now we can directly query the system about a litany of "Optional" processor capabilities
  201. // and determine the status by using BOTH the 'err' value and the 'lraw' value. If we request
  202. // a non-existant feature from SYSCTL(), the 'err' result will be -1; 0 denotes it exists
  203. // >>>> BUT <<<<<
  204. // it may not be supported, only defined. Thus we need to check 'lraw' to determine if it's
  205. // actually supported/implemented by the processor: 0 = no, 1 = yes, others are undefined.
  206. procflags = 0;
  207. // Seriously this one should be an Assert()
  208. err = _getSysCTLvalue<U32>("hw.optional.floatingpoint", &lraw);
  209. if ((err==0)&&(lraw==1)) procflags |= CPU_PROP_FPU;
  210. // List of chip-specific features
  211. err = _getSysCTLvalue<U32>("hw.optional.mmx", &lraw);
  212. if ((err==0)&&(lraw==1)) procflags |= CPU_PROP_MMX;
  213. err = _getSysCTLvalue<U32>("hw.optional.sse", &lraw);
  214. if ((err==0)&&(lraw==1)) procflags |= CPU_PROP_SSE;
  215. err = _getSysCTLvalue<U32>("hw.optional.sse2", &lraw);
  216. if ((err==0)&&(lraw==1)) procflags |= CPU_PROP_SSE2;
  217. err = _getSysCTLvalue<U32>("hw.optional.sse3", &lraw);
  218. if ((err==0)&&(lraw==1)) procflags |= CPU_PROP_SSE3;
  219. err = _getSysCTLvalue<U32>("hw.optional.supplementalsse3", &lraw);
  220. if ((err==0)&&(lraw==1)) procflags |= CPU_PROP_SSE3xt;
  221. err = _getSysCTLvalue<U32>("hw.optional.sse4_1", &lraw);
  222. if ((err==0)&&(lraw==1)) procflags |= CPU_PROP_SSE4_1;
  223. err = _getSysCTLvalue<U32>("hw.optional.sse4_2", &lraw);
  224. if ((err==0)&&(lraw==1)) procflags |= CPU_PROP_SSE4_2;
  225. // Finally some architecture-wide settings
  226. err = _getSysCTLvalue<U32>("hw.ncpu", &lraw);
  227. if ((err==0)&&(lraw>1)) procflags |= CPU_PROP_MP;
  228. err = _getSysCTLvalue<U32>("hw.cpu64bit_capable", &lraw);
  229. if ((err==0)&&(lraw==1)) procflags |= CPU_PROP_64bit;
  230. err = _getSysCTLvalue<U32>("hw.byteorder", &lraw);
  231. if ((err==0)&&(lraw==1234)) procflags |= CPU_PROP_LE;
  232. Platform::SystemInfo.processor.properties = procflags;
  233. Con::printf( "%s, %2.2f GHz", Platform::SystemInfo.processor.name, F32( Platform::SystemInfo.processor.mhz ) / 1000.0 );
  234. if (Platform::SystemInfo.processor.properties & CPU_PROP_MMX)
  235. Con::printf( " MMX detected");
  236. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE)
  237. Con::printf( " SSE detected");
  238. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE2)
  239. Con::printf( " SSE2 detected");
  240. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE3)
  241. Con::printf( " SSE3 detected");
  242. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE4_1)
  243. Con::printf( " SSE4.1 detected");
  244. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE4_2)
  245. Con::printf( " SSE4.2 detected");
  246. Con::printf( "" );
  247. // Trigger the signal
  248. Platform::SystemInfoReady.trigger();
  249. }
  250. namespace CPUInfo {
  251. EConfig CPUCount(U32 &logical, U32 &numCores, U32 &numPhysical) {
  252. // todo properly implement this
  253. logical = [[NSProcessInfo processInfo] activeProcessorCount];
  254. numCores = [[NSProcessInfo processInfo] activeProcessorCount];
  255. numPhysical = [[NSProcessInfo processInfo] processorCount];
  256. // todo check for hyperthreading
  257. if (numCores > 1)
  258. return CONFIG_MultiCoreAndHTNotCapable;
  259. return CONFIG_SingleCoreAndHTNotCapable;
  260. }
  261. }