PageRenderTime 52ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llprocessor.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 887 lines | 668 code | 123 blank | 96 comment | 88 complexity | 584a85bd935d6d2f20071a6af25844bb MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llprocessor.cpp
  3. * @brief Code to figure out the processor. Originally by Benjamin Jurke.
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llprocessor.h"
  28. #include "llerror.h"
  29. //#include <memory>
  30. #if LL_WINDOWS
  31. # define WIN32_LEAN_AND_MEAN
  32. # include <winsock2.h>
  33. # include <windows.h>
  34. # define _interlockedbittestandset _renamed_interlockedbittestandset
  35. # define _interlockedbittestandreset _renamed_interlockedbittestandreset
  36. # include <intrin.h>
  37. # undef _interlockedbittestandset
  38. # undef _interlockedbittestandreset
  39. #endif
  40. #include "llsd.h"
  41. #if LL_MSVC && _M_X64
  42. # define LL_X86_64 1
  43. # define LL_X86 1
  44. #elif LL_MSVC && _M_IX86
  45. # define LL_X86 1
  46. #elif LL_GNUC && ( defined(__amd64__) || defined(__x86_64__) )
  47. # define LL_X86_64 1
  48. # define LL_X86 1
  49. #elif LL_GNUC && ( defined(__i386__) )
  50. # define LL_X86 1
  51. #elif LL_GNUC && ( defined(__powerpc__) || defined(__ppc__) )
  52. # define LL_PPC 1
  53. #endif
  54. class LLProcessorInfoImpl; // foward declaration for the mImpl;
  55. namespace
  56. {
  57. enum cpu_info
  58. {
  59. eBrandName = 0,
  60. eFrequency,
  61. eVendor,
  62. eStepping,
  63. eFamily,
  64. eExtendedFamily,
  65. eModel,
  66. eExtendedModel,
  67. eType,
  68. eBrandID,
  69. eFamilyName
  70. };
  71. const char* cpu_info_names[] =
  72. {
  73. "Processor Name",
  74. "Frequency",
  75. "Vendor",
  76. "Stepping",
  77. "Family",
  78. "Extended Family",
  79. "Model",
  80. "Extended Model",
  81. "Type",
  82. "Brand ID",
  83. "Family Name"
  84. };
  85. enum cpu_config
  86. {
  87. eMaxID,
  88. eMaxExtID,
  89. eCLFLUSHCacheLineSize,
  90. eAPICPhysicalID,
  91. eCacheLineSize,
  92. eL2Associativity,
  93. eCacheSizeK,
  94. eFeatureBits,
  95. eExtFeatureBits
  96. };
  97. const char* cpu_config_names[] =
  98. {
  99. "Max Supported CPUID level",
  100. "Max Supported Ext. CPUID level",
  101. "CLFLUSH cache line size",
  102. "APIC Physical ID",
  103. "Cache Line Size",
  104. "L2 Associativity",
  105. "Cache Size",
  106. "Feature Bits",
  107. "Ext. Feature Bits"
  108. };
  109. // *NOTE:Mani - this contains the elements we reference directly and extensions beyond the first 32.
  110. // The rest of the names are referenced by bit maks returned from cpuid.
  111. enum cpu_features
  112. {
  113. eSSE_Ext=25,
  114. eSSE2_Ext=26,
  115. eSSE3_Features=32,
  116. eMONTIOR_MWAIT=33,
  117. eCPLDebugStore=34,
  118. eThermalMonitor2=35,
  119. eAltivec=36
  120. };
  121. const char* cpu_feature_names[] =
  122. {
  123. "x87 FPU On Chip",
  124. "Virtual-8086 Mode Enhancement",
  125. "Debugging Extensions",
  126. "Page Size Extensions",
  127. "Time Stamp Counter",
  128. "RDMSR and WRMSR Support",
  129. "Physical Address Extensions",
  130. "Machine Check Exception",
  131. "CMPXCHG8B Instruction",
  132. "APIC On Chip",
  133. "Unknown1",
  134. "SYSENTER and SYSEXIT",
  135. "Memory Type Range Registers",
  136. "PTE Global Bit",
  137. "Machine Check Architecture",
  138. "Conditional Move/Compare Instruction",
  139. "Page Attribute Table",
  140. "Page Size Extension",
  141. "Processor Serial Number",
  142. "CFLUSH Extension",
  143. "Unknown2",
  144. "Debug Store",
  145. "Thermal Monitor and Clock Ctrl",
  146. "MMX Technology",
  147. "FXSAVE/FXRSTOR",
  148. "SSE Extensions",
  149. "SSE2 Extensions",
  150. "Self Snoop",
  151. "Hyper-threading Technology",
  152. "Thermal Monitor",
  153. "Unknown4",
  154. "Pend. Brk. EN.", // 31 End of FeatureInfo bits
  155. "SSE3 New Instructions", // 32
  156. "MONITOR/MWAIT",
  157. "CPL Qualified Debug Store",
  158. "Thermal Monitor 2",
  159. "Altivec"
  160. };
  161. std::string intel_CPUFamilyName(int composed_family)
  162. {
  163. switch(composed_family)
  164. {
  165. case 3: return "Intel i386";
  166. case 4: return "Intel i486";
  167. case 5: return "Intel Pentium";
  168. case 6: return "Intel Pentium Pro/2/3, Core";
  169. case 7: return "Intel Itanium (IA-64)";
  170. case 0xF: return "Intel Pentium 4";
  171. case 0x10: return "Intel Itanium 2 (IA-64)";
  172. }
  173. return "Unknown";
  174. }
  175. std::string amd_CPUFamilyName(int composed_family)
  176. {
  177. switch(composed_family)
  178. {
  179. case 4: return "AMD 80486/5x86";
  180. case 5: return "AMD K5/K6";
  181. case 6: return "AMD K7";
  182. case 0xF: return "AMD K8";
  183. case 0x10: return "AMD K8L";
  184. }
  185. return "Unknown";
  186. }
  187. std::string compute_CPUFamilyName(const char* cpu_vendor, int composed_family)
  188. {
  189. const char* intel_string = "GenuineIntel";
  190. const char* amd_string = "AuthenticAMD";
  191. if(!strncmp(cpu_vendor, intel_string, strlen(intel_string)))
  192. {
  193. return intel_CPUFamilyName(composed_family);
  194. }
  195. else if(!strncmp(cpu_vendor, amd_string, strlen(amd_string)))
  196. {
  197. return amd_CPUFamilyName(composed_family);
  198. }
  199. return "Unknown";
  200. }
  201. std::string compute_CPUFamilyName(const char* cpu_vendor, int family, int ext_family)
  202. {
  203. const char* intel_string = "GenuineIntel";
  204. const char* amd_string = "AuthenticAMD";
  205. if(!strncmp(cpu_vendor, intel_string, strlen(intel_string)))
  206. {
  207. U32 composed_family = family + ext_family;
  208. return intel_CPUFamilyName(composed_family);
  209. }
  210. else if(!strncmp(cpu_vendor, amd_string, strlen(amd_string)))
  211. {
  212. U32 composed_family = (family == 0xF)
  213. ? family + ext_family
  214. : family;
  215. return amd_CPUFamilyName(composed_family);
  216. }
  217. return "Unknown";
  218. }
  219. } // end unnamed namespace
  220. // The base class for implementations.
  221. // Each platform should override this class.
  222. class LLProcessorInfoImpl
  223. {
  224. public:
  225. LLProcessorInfoImpl()
  226. {
  227. mProcessorInfo["info"] = LLSD::emptyMap();
  228. mProcessorInfo["config"] = LLSD::emptyMap();
  229. mProcessorInfo["extension"] = LLSD::emptyMap();
  230. }
  231. virtual ~LLProcessorInfoImpl() {}
  232. F64 getCPUFrequency() const
  233. {
  234. return getInfo(eFrequency, 0).asReal();
  235. }
  236. bool hasSSE() const
  237. {
  238. return hasExtension(cpu_feature_names[eSSE_Ext]);
  239. }
  240. bool hasSSE2() const
  241. {
  242. return hasExtension(cpu_feature_names[eSSE2_Ext]);
  243. }
  244. bool hasAltivec() const
  245. {
  246. return hasExtension("Altivec");
  247. }
  248. std::string getCPUFamilyName() const { return getInfo(eFamilyName, "Unknown").asString(); }
  249. std::string getCPUBrandName() const { return getInfo(eBrandName, "Unknown").asString(); }
  250. // This is virtual to support a different linux format.
  251. // *NOTE:Mani - I didn't want to screw up server use of this data...
  252. virtual std::string getCPUFeatureDescription() const
  253. {
  254. std::ostringstream out;
  255. out << std::endl << std::endl;
  256. out << "// CPU General Information" << std::endl;
  257. out << "//////////////////////////" << std::endl;
  258. out << "Processor Name: " << getCPUBrandName() << std::endl;
  259. out << "Frequency: " << getCPUFrequency() << " MHz" << std::endl;
  260. out << "Vendor: " << getInfo(eVendor, "Unknown").asString() << std::endl;
  261. out << "Family: " << getCPUFamilyName() << " (" << getInfo(eFamily, 0) << ")" << std::endl;
  262. out << "Extended family: " << getInfo(eExtendedFamily, 0) << std::endl;
  263. out << "Model: " << getInfo(eModel, 0) << std::endl;
  264. out << "Extended model: " << getInfo(eExtendedModel, 0) << std::endl;
  265. out << "Type: " << getInfo(eType, 0) << std::endl;
  266. out << "Brand ID: " << getInfo(eBrandID, 0) << std::endl;
  267. out << std::endl;
  268. out << "// CPU Configuration" << std::endl;
  269. out << "//////////////////////////" << std::endl;
  270. // Iterate through the dictionary of configuration options.
  271. LLSD configs = mProcessorInfo["config"];
  272. for(LLSD::map_const_iterator cfgItr = configs.beginMap(); cfgItr != configs.endMap(); ++cfgItr)
  273. {
  274. out << cfgItr->first << " = " << cfgItr->second << std::endl;
  275. }
  276. out << std::endl;
  277. out << "// CPU Extensions" << std::endl;
  278. out << "//////////////////////////" << std::endl;
  279. for(LLSD::map_const_iterator itr = mProcessorInfo["extension"].beginMap(); itr != mProcessorInfo["extension"].endMap(); ++itr)
  280. {
  281. out << " " << itr->first << std::endl;
  282. }
  283. return out.str();
  284. }
  285. protected:
  286. void setInfo(cpu_info info_type, const LLSD& value)
  287. {
  288. setInfo(cpu_info_names[info_type], value);
  289. }
  290. LLSD getInfo(cpu_info info_type, const LLSD& defaultVal) const
  291. {
  292. return getInfo(cpu_info_names[info_type], defaultVal);
  293. }
  294. void setConfig(cpu_config config_type, const LLSD& value)
  295. {
  296. setConfig(cpu_config_names[config_type], value);
  297. }
  298. LLSD getConfig(cpu_config config_type, const LLSD& defaultVal) const
  299. {
  300. return getConfig(cpu_config_names[config_type], defaultVal);
  301. }
  302. void setExtension(const std::string& name) { mProcessorInfo["extension"][name] = "true"; }
  303. bool hasExtension(const std::string& name) const
  304. {
  305. return mProcessorInfo["extension"].has(name);
  306. }
  307. private:
  308. void setInfo(const std::string& name, const LLSD& value) { mProcessorInfo["info"][name]=value; }
  309. LLSD getInfo(const std::string& name, const LLSD& defaultVal) const
  310. {
  311. if(mProcessorInfo["info"].has(name))
  312. {
  313. return mProcessorInfo["info"][name];
  314. }
  315. return defaultVal;
  316. }
  317. void setConfig(const std::string& name, const LLSD& value) { mProcessorInfo["config"][name]=value; }
  318. LLSD getConfig(const std::string& name, const LLSD& defaultVal) const
  319. {
  320. LLSD r = mProcessorInfo["config"].get(name);
  321. return r.isDefined() ? r : defaultVal;
  322. }
  323. private:
  324. LLSD mProcessorInfo;
  325. };
  326. #ifdef LL_MSVC
  327. // LL_MSVC and not LLWINDOWS because some of the following code
  328. // uses the MSVC compiler intrinsics __cpuid() and __rdtsc().
  329. // Delays for the specified amount of milliseconds
  330. static void _Delay(unsigned int ms)
  331. {
  332. LARGE_INTEGER freq, c1, c2;
  333. __int64 x;
  334. // Get High-Res Timer frequency
  335. if (!QueryPerformanceFrequency(&freq))
  336. return;
  337. // Convert ms to High-Res Timer value
  338. x = freq.QuadPart/1000*ms;
  339. // Get first snapshot of High-Res Timer value
  340. QueryPerformanceCounter(&c1);
  341. do
  342. {
  343. // Get second snapshot
  344. QueryPerformanceCounter(&c2);
  345. }while(c2.QuadPart-c1.QuadPart < x);
  346. // Loop while (second-first < x)
  347. }
  348. static F64 calculate_cpu_frequency(U32 measure_msecs)
  349. {
  350. if(measure_msecs == 0)
  351. {
  352. return 0;
  353. }
  354. // After that we declare some vars and check the frequency of the high
  355. // resolution timer for the measure process.
  356. // If there"s no high-res timer, we exit.
  357. unsigned __int64 starttime, endtime, timedif, freq, start, end, dif;
  358. if (!QueryPerformanceFrequency((LARGE_INTEGER *) &freq))
  359. {
  360. return 0;
  361. }
  362. // Now we can init the measure process. We set the process and thread priority
  363. // to the highest available level (Realtime priority). Also we focus the
  364. // first processor in the multiprocessor system.
  365. HANDLE hProcess = GetCurrentProcess();
  366. HANDLE hThread = GetCurrentThread();
  367. unsigned long dwCurPriorityClass = GetPriorityClass(hProcess);
  368. int iCurThreadPriority = GetThreadPriority(hThread);
  369. unsigned long dwProcessMask, dwSystemMask, dwNewMask = 1;
  370. GetProcessAffinityMask(hProcess, &dwProcessMask, &dwSystemMask);
  371. SetPriorityClass(hProcess, REALTIME_PRIORITY_CLASS);
  372. SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
  373. SetProcessAffinityMask(hProcess, dwNewMask);
  374. //// Now we call a CPUID to ensure, that all other prior called functions are
  375. //// completed now (serialization)
  376. //__asm cpuid
  377. int cpu_info[4] = {-1};
  378. __cpuid(cpu_info, 0);
  379. // We ask the high-res timer for the start time
  380. QueryPerformanceCounter((LARGE_INTEGER *) &starttime);
  381. // Then we get the current cpu clock and store it
  382. start = __rdtsc();
  383. // Now we wart for some msecs
  384. _Delay(measure_msecs);
  385. // Sleep(uiMeasureMSecs);
  386. // We ask for the end time
  387. QueryPerformanceCounter((LARGE_INTEGER *) &endtime);
  388. // And also for the end cpu clock
  389. end = __rdtsc();
  390. // Now we can restore the default process and thread priorities
  391. SetProcessAffinityMask(hProcess, dwProcessMask);
  392. SetThreadPriority(hThread, iCurThreadPriority);
  393. SetPriorityClass(hProcess, dwCurPriorityClass);
  394. // Then we calculate the time and clock differences
  395. dif = end - start;
  396. timedif = endtime - starttime;
  397. // And finally the frequency is the clock difference divided by the time
  398. // difference.
  399. F64 frequency = (F64)dif / (((F64)timedif) / freq);
  400. // At last we just return the frequency that is also stored in the call
  401. // member var uqwFrequency - converted to MHz
  402. return frequency / (F64)1000000;
  403. }
  404. // Windows implementation
  405. class LLProcessorInfoWindowsImpl : public LLProcessorInfoImpl
  406. {
  407. public:
  408. LLProcessorInfoWindowsImpl()
  409. {
  410. getCPUIDInfo();
  411. setInfo(eFrequency, calculate_cpu_frequency(50));
  412. }
  413. private:
  414. void getCPUIDInfo()
  415. {
  416. // http://msdn.microsoft.com/en-us/library/hskdteyh(VS.80).aspx
  417. // __cpuid with an InfoType argument of 0 returns the number of
  418. // valid Ids in cpu_info[0] and the CPU identification string in
  419. // the other three array elements. The CPU identification string is
  420. // not in linear order. The code below arranges the information
  421. // in a human readable form.
  422. int cpu_info[4] = {-1};
  423. __cpuid(cpu_info, 0);
  424. unsigned int ids = (unsigned int)cpu_info[0];
  425. setConfig(eMaxID, (S32)ids);
  426. char cpu_vendor[0x20];
  427. memset(cpu_vendor, 0, sizeof(cpu_vendor));
  428. *((int*)cpu_vendor) = cpu_info[1];
  429. *((int*)(cpu_vendor+4)) = cpu_info[3];
  430. *((int*)(cpu_vendor+8)) = cpu_info[2];
  431. setInfo(eVendor, cpu_vendor);
  432. // Get the information associated with each valid Id
  433. for(unsigned int i=0; i<=ids; ++i)
  434. {
  435. __cpuid(cpu_info, i);
  436. // Interpret CPU feature information.
  437. if (i == 1)
  438. {
  439. setInfo(eStepping, cpu_info[0] & 0xf);
  440. setInfo(eModel, (cpu_info[0] >> 4) & 0xf);
  441. int family = (cpu_info[0] >> 8) & 0xf;
  442. setInfo(eFamily, family);
  443. setInfo(eType, (cpu_info[0] >> 12) & 0x3);
  444. setInfo(eExtendedModel, (cpu_info[0] >> 16) & 0xf);
  445. int ext_family = (cpu_info[0] >> 20) & 0xff;
  446. setInfo(eExtendedFamily, ext_family);
  447. setInfo(eBrandID, cpu_info[1] & 0xff);
  448. setInfo(eFamilyName, compute_CPUFamilyName(cpu_vendor, family, ext_family));
  449. setConfig(eCLFLUSHCacheLineSize, ((cpu_info[1] >> 8) & 0xff) * 8);
  450. setConfig(eAPICPhysicalID, (cpu_info[1] >> 24) & 0xff);
  451. if(cpu_info[2] & 0x1)
  452. {
  453. setExtension(cpu_feature_names[eSSE3_Features]);
  454. }
  455. if(cpu_info[2] & 0x8)
  456. {
  457. setExtension(cpu_feature_names[eMONTIOR_MWAIT]);
  458. }
  459. if(cpu_info[2] & 0x10)
  460. {
  461. setExtension(cpu_feature_names[eCPLDebugStore]);
  462. }
  463. if(cpu_info[2] & 0x100)
  464. {
  465. setExtension(cpu_feature_names[eThermalMonitor2]);
  466. }
  467. unsigned int feature_info = (unsigned int) cpu_info[3];
  468. for(unsigned int index = 0, bit = 1; index < eSSE3_Features; ++index, bit <<= 1)
  469. {
  470. if(feature_info & bit)
  471. {
  472. setExtension(cpu_feature_names[index]);
  473. }
  474. }
  475. }
  476. }
  477. // Calling __cpuid with 0x80000000 as the InfoType argument
  478. // gets the number of valid extended IDs.
  479. __cpuid(cpu_info, 0x80000000);
  480. unsigned int ext_ids = cpu_info[0];
  481. setConfig(eMaxExtID, 0);
  482. char cpu_brand_string[0x40];
  483. memset(cpu_brand_string, 0, sizeof(cpu_brand_string));
  484. // Get the information associated with each extended ID.
  485. for(unsigned int i=0x80000000; i<=ext_ids; ++i)
  486. {
  487. __cpuid(cpu_info, i);
  488. // Interpret CPU brand string and cache information.
  489. if (i == 0x80000002)
  490. memcpy(cpu_brand_string, cpu_info, sizeof(cpu_info));
  491. else if (i == 0x80000003)
  492. memcpy(cpu_brand_string + 16, cpu_info, sizeof(cpu_info));
  493. else if (i == 0x80000004)
  494. {
  495. memcpy(cpu_brand_string + 32, cpu_info, sizeof(cpu_info));
  496. setInfo(eBrandName, cpu_brand_string);
  497. }
  498. else if (i == 0x80000006)
  499. {
  500. setConfig(eCacheLineSize, cpu_info[2] & 0xff);
  501. setConfig(eL2Associativity, (cpu_info[2] >> 12) & 0xf);
  502. setConfig(eCacheSizeK, (cpu_info[2] >> 16) & 0xffff);
  503. }
  504. }
  505. }
  506. };
  507. #elif LL_DARWIN
  508. #include <mach/machine.h>
  509. #include <sys/sysctl.h>
  510. class LLProcessorInfoDarwinImpl : public LLProcessorInfoImpl
  511. {
  512. public:
  513. LLProcessorInfoDarwinImpl()
  514. {
  515. getCPUIDInfo();
  516. uint64_t frequency = getSysctlInt64("hw.cpufrequency");
  517. setInfo(eFrequency, (F64)frequency / (F64)1000000);
  518. }
  519. virtual ~LLProcessorInfoDarwinImpl() {}
  520. private:
  521. int getSysctlInt(const char* name)
  522. {
  523. int result = 0;
  524. size_t len = sizeof(int);
  525. int error = sysctlbyname(name, (void*)&result, &len, NULL, 0);
  526. return error == -1 ? 0 : result;
  527. }
  528. uint64_t getSysctlInt64(const char* name)
  529. {
  530. uint64_t value = 0;
  531. size_t size = sizeof(value);
  532. int result = sysctlbyname(name, (void*)&value, &size, NULL, 0);
  533. if ( result == 0 )
  534. {
  535. if ( size == sizeof( uint64_t ) )
  536. ;
  537. else if ( size == sizeof( uint32_t ) )
  538. value = (uint64_t)(( uint32_t *)&value);
  539. else if ( size == sizeof( uint16_t ) )
  540. value = (uint64_t)(( uint16_t *)&value);
  541. else if ( size == sizeof( uint8_t ) )
  542. value = (uint64_t)(( uint8_t *)&value);
  543. else
  544. {
  545. LL_WARNS("Unknown type returned from sysctl!") << LL_ENDL;
  546. }
  547. }
  548. return result == -1 ? 0 : value;
  549. }
  550. void getCPUIDInfo()
  551. {
  552. size_t len = 0;
  553. char cpu_brand_string[0x40];
  554. len = sizeof(cpu_brand_string);
  555. memset(cpu_brand_string, 0, len);
  556. sysctlbyname("machdep.cpu.brand_string", (void*)cpu_brand_string, &len, NULL, 0);
  557. cpu_brand_string[0x3f] = 0;
  558. setInfo(eBrandName, cpu_brand_string);
  559. char cpu_vendor[0x20];
  560. len = sizeof(cpu_vendor);
  561. memset(cpu_vendor, 0, len);
  562. sysctlbyname("machdep.cpu.vendor", (void*)cpu_vendor, &len, NULL, 0);
  563. cpu_vendor[0x1f] = 0;
  564. setInfo(eVendor, cpu_vendor);
  565. setInfo(eStepping, getSysctlInt("machdep.cpu.stepping"));
  566. setInfo(eModel, getSysctlInt("machdep.cpu.model"));
  567. int family = getSysctlInt("machdep.cpu.family");
  568. int ext_family = getSysctlInt("machdep.cpu.extfamily");
  569. setInfo(eFamily, family);
  570. setInfo(eExtendedFamily, ext_family);
  571. setInfo(eFamilyName, compute_CPUFamilyName(cpu_vendor, family, ext_family));
  572. setInfo(eExtendedModel, getSysctlInt("machdep.cpu.extmodel"));
  573. setInfo(eBrandID, getSysctlInt("machdep.cpu.brand"));
  574. setInfo(eType, 0); // ? where to find this?
  575. //setConfig(eCLFLUSHCacheLineSize, ((cpu_info[1] >> 8) & 0xff) * 8);
  576. //setConfig(eAPICPhysicalID, (cpu_info[1] >> 24) & 0xff);
  577. setConfig(eCacheLineSize, getSysctlInt("machdep.cpu.cache.linesize"));
  578. setConfig(eL2Associativity, getSysctlInt("machdep.cpu.cache.L2_associativity"));
  579. setConfig(eCacheSizeK, getSysctlInt("machdep.cpu.cache.size"));
  580. uint64_t feature_info = getSysctlInt64("machdep.cpu.feature_bits");
  581. S32 *feature_infos = (S32*)(&feature_info);
  582. setConfig(eFeatureBits, feature_infos[0]);
  583. for(unsigned int index = 0, bit = 1; index < eSSE3_Features; ++index, bit <<= 1)
  584. {
  585. if(feature_info & bit)
  586. {
  587. setExtension(cpu_feature_names[index]);
  588. }
  589. }
  590. // *NOTE:Mani - I didn't find any docs that assure me that machdep.cpu.feature_bits will always be
  591. // The feature bits I think it is. Here's a test:
  592. #ifndef LL_RELEASE_FOR_DOWNLOAD
  593. #if defined(__i386__) && defined(__PIC__)
  594. /* %ebx may be the PIC register. */
  595. #define __cpuid(level, a, b, c, d) \
  596. __asm__ ("xchgl\t%%ebx, %1\n\t" \
  597. "cpuid\n\t" \
  598. "xchgl\t%%ebx, %1\n\t" \
  599. : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
  600. : "0" (level))
  601. #else
  602. #define __cpuid(level, a, b, c, d) \
  603. __asm__ ("cpuid\n\t" \
  604. : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
  605. : "0" (level))
  606. #endif
  607. unsigned int eax, ebx, ecx, edx;
  608. __cpuid(0x1, eax, ebx, ecx, edx);
  609. if(feature_infos[0] != (S32)edx)
  610. {
  611. llerrs << "machdep.cpu.feature_bits doesn't match expected cpuid result!" << llendl;
  612. }
  613. #endif // LL_RELEASE_FOR_DOWNLOAD
  614. uint64_t ext_feature_info = getSysctlInt64("machdep.cpu.extfeature_bits");
  615. S32 *ext_feature_infos = (S32*)(&ext_feature_info);
  616. setConfig(eExtFeatureBits, ext_feature_infos[0]);
  617. }
  618. };
  619. #elif LL_LINUX
  620. const char CPUINFO_FILE[] = "/proc/cpuinfo";
  621. class LLProcessorInfoLinuxImpl : public LLProcessorInfoImpl
  622. {
  623. public:
  624. LLProcessorInfoLinuxImpl()
  625. {
  626. get_proc_cpuinfo();
  627. }
  628. virtual ~LLProcessorInfoLinuxImpl() {}
  629. private:
  630. void get_proc_cpuinfo()
  631. {
  632. std::map< std::string, std::string > cpuinfo;
  633. LLFILE* cpuinfo_fp = LLFile::fopen(CPUINFO_FILE, "rb");
  634. if(cpuinfo_fp)
  635. {
  636. char line[MAX_STRING];
  637. memset(line, 0, MAX_STRING);
  638. while(fgets(line, MAX_STRING, cpuinfo_fp))
  639. {
  640. // /proc/cpuinfo on Linux looks like:
  641. // name\t*: value\n
  642. char* tabspot = strchr( line, '\t' );
  643. if (tabspot == NULL)
  644. continue;
  645. char* colspot = strchr( tabspot, ':' );
  646. if (colspot == NULL)
  647. continue;
  648. char* spacespot = strchr( colspot, ' ' );
  649. if (spacespot == NULL)
  650. continue;
  651. char* nlspot = strchr( line, '\n' );
  652. if (nlspot == NULL)
  653. nlspot = line + strlen( line ); // Fallback to terminating NUL
  654. std::string linename( line, tabspot );
  655. std::string llinename(linename);
  656. LLStringUtil::toLower(llinename);
  657. std::string lineval( spacespot + 1, nlspot );
  658. cpuinfo[ llinename ] = lineval;
  659. }
  660. fclose(cpuinfo_fp);
  661. }
  662. # if LL_X86
  663. // *NOTE:Mani - eww, macros! srry.
  664. #define LLPI_SET_INFO_STRING(llpi_id, cpuinfo_id) \
  665. if (!cpuinfo[cpuinfo_id].empty()) \
  666. { setInfo(llpi_id, cpuinfo[cpuinfo_id]);}
  667. #define LLPI_SET_INFO_INT(llpi_id, cpuinfo_id) \
  668. {\
  669. S32 result; \
  670. if (!cpuinfo[cpuinfo_id].empty() \
  671. && LLStringUtil::convertToS32(cpuinfo[cpuinfo_id], result)) \
  672. { setInfo(llpi_id, result);} \
  673. }
  674. F64 mhz;
  675. if (LLStringUtil::convertToF64(cpuinfo["cpu mhz"], mhz)
  676. && 200.0 < mhz && mhz < 10000.0)
  677. {
  678. setInfo(eFrequency,(F64)(mhz));
  679. }
  680. LLPI_SET_INFO_STRING(eBrandName, "model name");
  681. LLPI_SET_INFO_STRING(eVendor, "vendor_id");
  682. LLPI_SET_INFO_INT(eStepping, "stepping");
  683. LLPI_SET_INFO_INT(eModel, "model");
  684. S32 family;
  685. if (!cpuinfo["cpu family"].empty()
  686. && LLStringUtil::convertToS32(cpuinfo["cpu family"], family))
  687. {
  688. setInfo(eFamily, family);
  689. }
  690. setInfo(eFamilyName, compute_CPUFamilyName(cpuinfo["vendor_id"].c_str(), family));
  691. // setInfo(eExtendedModel, getSysctlInt("machdep.cpu.extmodel"));
  692. // setInfo(eBrandID, getSysctlInt("machdep.cpu.brand"));
  693. // setInfo(eType, 0); // ? where to find this?
  694. //setConfig(eCLFLUSHCacheLineSize, ((cpu_info[1] >> 8) & 0xff) * 8);
  695. //setConfig(eAPICPhysicalID, (cpu_info[1] >> 24) & 0xff);
  696. //setConfig(eCacheLineSize, getSysctlInt("machdep.cpu.cache.linesize"));
  697. //setConfig(eL2Associativity, getSysctlInt("machdep.cpu.cache.L2_associativity"));
  698. //setConfig(eCacheSizeK, getSysctlInt("machdep.cpu.cache.size"));
  699. // Read extensions
  700. std::string flags = " " + cpuinfo["flags"] + " ";
  701. LLStringUtil::toLower(flags);
  702. if( flags.find( " sse " ) != std::string::npos )
  703. {
  704. setExtension(cpu_feature_names[eSSE_Ext]);
  705. }
  706. if( flags.find( " sse2 " ) != std::string::npos )
  707. {
  708. setExtension(cpu_feature_names[eSSE2_Ext]);
  709. }
  710. # endif // LL_X86
  711. }
  712. std::string getCPUFeatureDescription() const
  713. {
  714. std::ostringstream s;
  715. // *NOTE:Mani - This is for linux only.
  716. LLFILE* cpuinfo = LLFile::fopen(CPUINFO_FILE, "rb");
  717. if(cpuinfo)
  718. {
  719. char line[MAX_STRING];
  720. memset(line, 0, MAX_STRING);
  721. while(fgets(line, MAX_STRING, cpuinfo))
  722. {
  723. line[strlen(line)-1] = ' ';
  724. s << line;
  725. s << std::endl;
  726. }
  727. fclose(cpuinfo);
  728. s << std::endl;
  729. }
  730. else
  731. {
  732. s << "Unable to collect processor information" << std::endl;
  733. }
  734. return s.str();
  735. }
  736. };
  737. #endif // LL_MSVC elif LL_DARWIN elif LL_LINUX
  738. //////////////////////////////////////////////////////
  739. // Interface definition
  740. LLProcessorInfo::LLProcessorInfo() : mImpl(NULL)
  741. {
  742. // *NOTE:Mani - not thread safe.
  743. if(!mImpl)
  744. {
  745. #ifdef LL_MSVC
  746. static LLProcessorInfoWindowsImpl the_impl;
  747. mImpl = &the_impl;
  748. #elif LL_DARWIN
  749. static LLProcessorInfoDarwinImpl the_impl;
  750. mImpl = &the_impl;
  751. #else
  752. static LLProcessorInfoLinuxImpl the_impl;
  753. mImpl = &the_impl;
  754. #endif // LL_MSVC
  755. }
  756. }
  757. LLProcessorInfo::~LLProcessorInfo() {}
  758. F64 LLProcessorInfo::getCPUFrequency() const { return mImpl->getCPUFrequency(); }
  759. bool LLProcessorInfo::hasSSE() const { return mImpl->hasSSE(); }
  760. bool LLProcessorInfo::hasSSE2() const { return mImpl->hasSSE2(); }
  761. bool LLProcessorInfo::hasAltivec() const { return mImpl->hasAltivec(); }
  762. std::string LLProcessorInfo::getCPUFamilyName() const { return mImpl->getCPUFamilyName(); }
  763. std::string LLProcessorInfo::getCPUBrandName() const { return mImpl->getCPUBrandName(); }
  764. std::string LLProcessorInfo::getCPUFeatureDescription() const { return mImpl->getCPUFeatureDescription(); }