You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

530 lines
18 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2008 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/systeminfo.h"
  28. #ifdef WIN32
  29. #include "talk/base/win32.h" // NOLINT first because it brings in win32 stuff
  30. #ifndef EXCLUDE_D3D9
  31. #include <d3d9.h>
  32. #endif
  33. #include <intrin.h> // for __cpuid()
  34. #include "talk/base/scoped_ptr.h"
  35. #elif defined(OSX)
  36. #include <ApplicationServices/ApplicationServices.h>
  37. #include <CoreServices/CoreServices.h>
  38. #include <sys/sysctl.h>
  39. #include "talk/base/macconversion.h"
  40. #elif defined(IOS)
  41. #include <sys/sysctl.h> // NOLINT - lint thinks this is duplicate include
  42. #elif defined(LINUX) || defined(ANDROID)
  43. #include <unistd.h>
  44. #include "talk/base/linux.h"
  45. #endif
  46. #include "talk/base/common.h"
  47. #include "talk/base/logging.h"
  48. #include "talk/base/stringutils.h"
  49. namespace talk_base {
  50. // See Also: http://msdn.microsoft.com/en-us/library/ms683194(v=vs.85).aspx
  51. #ifdef WIN32
  52. typedef BOOL (WINAPI *LPFN_GLPI)(
  53. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION,
  54. PDWORD);
  55. static void GetProcessorInformation(int &physical_cpus, int &cache_size) {
  56. // GetLogicalProcessorInformation() is available on Windows XP SP3 and beyond.
  57. LPFN_GLPI glpi = reinterpret_cast<LPFN_GLPI>(GetProcAddress(
  58. GetModuleHandle(L"kernel32"),
  59. "GetLogicalProcessorInformation"));
  60. if (NULL == glpi) {
  61. return;
  62. }
  63. // Determine buffer size, allocate and get processor information.
  64. // Size can change between calls (unlikely), so a loop is done.
  65. DWORD return_length = 0;
  66. scoped_array<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> infos;
  67. while (!glpi(infos.get(), &return_length)) {
  68. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  69. infos.reset(new SYSTEM_LOGICAL_PROCESSOR_INFORMATION[
  70. return_length / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)]);
  71. } else {
  72. return;
  73. }
  74. }
  75. physical_cpus = 0;
  76. cache_size = 0;
  77. for (size_t i = 0;
  78. i < return_length / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ++i) {
  79. if (infos[i].Relationship == RelationProcessorCore) {
  80. ++physical_cpus;
  81. } else if (infos[i].Relationship == RelationCache) {
  82. int next_cache_size = static_cast<int>(infos[i].Cache.Size);
  83. if (next_cache_size >= cache_size) {
  84. cache_size = next_cache_size;
  85. }
  86. }
  87. }
  88. return;
  89. }
  90. #else
  91. // TODO(fbarchard): Use gcc 4.4 provided cpuid intrinsic
  92. // 32 bit fpic requires ebx be preserved
  93. #if (defined(__pic__) || defined(__APPLE__)) && defined(__i386__)
  94. static inline void __cpuid(int cpu_info[4], int info_type) {
  95. __asm__ volatile ( // NOLINT
  96. "mov %%ebx, %%edi\n"
  97. "cpuid\n"
  98. "xchg %%edi, %%ebx\n"
  99. : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
  100. : "a"(info_type)
  101. ); // NOLINT
  102. }
  103. #elif defined(__i386__) || defined(__x86_64__)
  104. static inline void __cpuid(int cpu_info[4], int info_type) {
  105. __asm__ volatile ( // NOLINT
  106. "cpuid\n"
  107. : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
  108. : "a"(info_type)
  109. ); // NOLINT
  110. }
  111. #endif
  112. #endif // WIN32
  113. // Note(fbarchard):
  114. // Family and model are extended family and extended model. 8 bits each.
  115. SystemInfo::SystemInfo()
  116. : physical_cpus_(1), logical_cpus_(1), cache_size_(0),
  117. cpu_family_(0), cpu_model_(0), cpu_stepping_(0),
  118. cpu_speed_(0), memory_(0) {
  119. // Initialize the basic information.
  120. #if defined(CPU_ARM)
  121. cpu_arch_ = SI_ARCH_ARM;
  122. #elif defined(__x86_64__) || defined(_M_X64)
  123. cpu_arch_ = SI_ARCH_X64;
  124. #elif defined(__i386__) || defined(_M_IX86)
  125. cpu_arch_ = SI_ARCH_X86;
  126. #else
  127. #error "Unknown architecture."
  128. #endif
  129. #if defined(WIN32)
  130. SYSTEM_INFO si;
  131. GetSystemInfo(&si);
  132. logical_cpus_ = si.dwNumberOfProcessors;
  133. GetProcessorInformation(physical_cpus_, cache_size_);
  134. if (physical_cpus_ <= 0) {
  135. physical_cpus_ = logical_cpus_;
  136. }
  137. cpu_family_ = si.wProcessorLevel;
  138. cpu_model_ = si.wProcessorRevision >> 8;
  139. cpu_stepping_ = si.wProcessorRevision & 0xFF;
  140. #elif defined(OSX) || defined(IOS)
  141. uint32_t sysctl_value;
  142. size_t length = sizeof(sysctl_value);
  143. if (!sysctlbyname("hw.physicalcpu_max", &sysctl_value, &length, NULL, 0)) {
  144. physical_cpus_ = static_cast<int>(sysctl_value);
  145. }
  146. length = sizeof(sysctl_value);
  147. if (!sysctlbyname("hw.logicalcpu_max", &sysctl_value, &length, NULL, 0)) {
  148. logical_cpus_ = static_cast<int>(sysctl_value);
  149. }
  150. uint64_t sysctl_value64;
  151. length = sizeof(sysctl_value64);
  152. if (!sysctlbyname("hw.l3cachesize", &sysctl_value64, &length, NULL, 0)) {
  153. cache_size_ = static_cast<int>(sysctl_value64);
  154. LOG(LS_INFO) << "l3cachesize " << cache_size_;
  155. }
  156. if (!cache_size_) {
  157. length = sizeof(sysctl_value64);
  158. if (!sysctlbyname("hw.l2cachesize", &sysctl_value64, &length, NULL, 0)) {
  159. cache_size_ = static_cast<int>(sysctl_value64);
  160. LOG(LS_INFO) << "l2cachesize " << cache_size_;
  161. }
  162. }
  163. length = sizeof(sysctl_value);
  164. if (!sysctlbyname("machdep.cpu.family", &sysctl_value, &length, NULL, 0)) {
  165. cpu_family_ = static_cast<int>(sysctl_value);
  166. }
  167. length = sizeof(sysctl_value);
  168. if (!sysctlbyname("machdep.cpu.model", &sysctl_value, &length, NULL, 0)) {
  169. cpu_model_ = static_cast<int>(sysctl_value);
  170. }
  171. length = sizeof(sysctl_value);
  172. if (!sysctlbyname("machdep.cpu.stepping", &sysctl_value, &length, NULL, 0)) {
  173. cpu_stepping_ = static_cast<int>(sysctl_value);
  174. }
  175. #else // LINUX || ANDROID
  176. ProcCpuInfo proc_info;
  177. if (proc_info.LoadFromSystem()) {
  178. proc_info.GetNumCpus(&logical_cpus_);
  179. proc_info.GetNumPhysicalCpus(&physical_cpus_);
  180. proc_info.GetCpuFamily(&cpu_family_);
  181. #if !defined(CPU_ARM)
  182. // These values aren't found on ARM systems.
  183. proc_info.GetSectionIntValue(0, "model", &cpu_model_);
  184. proc_info.GetSectionIntValue(0, "stepping", &cpu_stepping_);
  185. proc_info.GetSectionIntValue(0, "cpu MHz", &cpu_speed_);
  186. proc_info.GetSectionIntValue(0, "cache size", &cache_size_);
  187. cache_size_ *= 1024;
  188. #endif
  189. }
  190. // ProcCpuInfo reads cpu speed from "cpu MHz" under /proc/cpuinfo.
  191. // But that number is a moving target which can change on-the-fly according to
  192. // many factors including system workload.
  193. // See /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors.
  194. // The one in /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq is more
  195. // accurate. We use it as our cpu speed when it is available.
  196. // cpuinfo_max_freq is measured in KHz and requires conversion to MHz.
  197. int max_freq = talk_base::ReadCpuMaxFreq();
  198. if (max_freq > 0) {
  199. cpu_speed_ = max_freq / 1000;
  200. }
  201. #endif
  202. // For L2 CacheSize see also
  203. // http://www.flounder.com/cpuid_explorer2.htm#CPUID(0x800000006)
  204. #ifdef CPU_X86
  205. if (cache_size_ == 0) {
  206. int cpu_info[4];
  207. __cpuid(cpu_info, 0x80000000); // query maximum extended cpuid function.
  208. if (static_cast<uint32>(cpu_info[0]) >= 0x80000006) {
  209. __cpuid(cpu_info, 0x80000006);
  210. cache_size_ = (cpu_info[2] >> 16) * 1024;
  211. }
  212. }
  213. #endif
  214. }
  215. // Return the number of cpu threads available to the system.
  216. int SystemInfo::GetMaxCpus() {
  217. return logical_cpus_;
  218. }
  219. // Return the number of cpu cores available to the system.
  220. int SystemInfo::GetMaxPhysicalCpus() {
  221. return physical_cpus_;
  222. }
  223. // Return the number of cpus available to the process. Since affinity can be
  224. // changed on the fly, do not cache this value.
  225. // Can be affected by heat.
  226. int SystemInfo::GetCurCpus() {
  227. int cur_cpus;
  228. #ifdef WIN32
  229. DWORD_PTR process_mask, system_mask;
  230. ::GetProcessAffinityMask(::GetCurrentProcess(), &process_mask, &system_mask);
  231. for (cur_cpus = 0; process_mask; ++cur_cpus) {
  232. // Sparse-ones algorithm. There are slightly faster methods out there but
  233. // they are unintuitive and won't make a difference on a single dword.
  234. process_mask &= (process_mask - 1);
  235. }
  236. #elif defined(OSX) || defined(IOS)
  237. uint32_t sysctl_value;
  238. size_t length = sizeof(sysctl_value);
  239. int error = sysctlbyname("hw.ncpu", &sysctl_value, &length, NULL, 0);
  240. cur_cpus = !error ? static_cast<int>(sysctl_value) : 1;
  241. #else
  242. // Linux, Solaris, ANDROID
  243. cur_cpus = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
  244. #endif
  245. return cur_cpus;
  246. }
  247. // Return the type of this CPU.
  248. SystemInfo::Architecture SystemInfo::GetCpuArchitecture() {
  249. return cpu_arch_;
  250. }
  251. // Returns the vendor string from the cpu, e.g. "GenuineIntel", "AuthenticAMD".
  252. // See "Intel Processor Identification and the CPUID Instruction"
  253. // (Intel document number: 241618)
  254. std::string SystemInfo::GetCpuVendor() {
  255. if (cpu_vendor_.empty()) {
  256. #if defined(CPU_X86)
  257. int cpu_info[4];
  258. __cpuid(cpu_info, 0);
  259. cpu_info[0] = cpu_info[1]; // Reorder output
  260. cpu_info[1] = cpu_info[3];
  261. cpu_info[2] = cpu_info[2];
  262. cpu_info[3] = 0;
  263. cpu_vendor_ = std::string(reinterpret_cast<char*>(&cpu_info[0]));
  264. #elif defined(CPU_ARM)
  265. cpu_vendor_ = std::string("ARM");
  266. #else
  267. cpu_vendor_ = std::string("Undefined");
  268. #endif
  269. }
  270. return cpu_vendor_;
  271. }
  272. int SystemInfo::GetCpuCacheSize() {
  273. return cache_size_;
  274. }
  275. // Return the "family" of this CPU.
  276. int SystemInfo::GetCpuFamily() {
  277. return cpu_family_;
  278. }
  279. // Return the "model" of this CPU.
  280. int SystemInfo::GetCpuModel() {
  281. return cpu_model_;
  282. }
  283. // Return the "stepping" of this CPU.
  284. int SystemInfo::GetCpuStepping() {
  285. return cpu_stepping_;
  286. }
  287. // Return the clockrate of the primary processor in Mhz. This value can be
  288. // cached. Returns -1 on error.
  289. int SystemInfo::GetMaxCpuSpeed() {
  290. if (cpu_speed_) {
  291. return cpu_speed_;
  292. }
  293. #if defined(WIN32)
  294. HKEY key;
  295. static const WCHAR keyName[] =
  296. L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
  297. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName , 0, KEY_QUERY_VALUE, &key)
  298. == ERROR_SUCCESS) {
  299. DWORD data, len;
  300. len = sizeof(data);
  301. if (RegQueryValueEx(key, L"~Mhz", 0, 0, reinterpret_cast<LPBYTE>(&data),
  302. &len) == ERROR_SUCCESS) {
  303. cpu_speed_ = data;
  304. } else {
  305. LOG(LS_WARNING) << "Failed to query registry value HKLM\\" << keyName
  306. << "\\~Mhz";
  307. cpu_speed_ = -1;
  308. }
  309. RegCloseKey(key);
  310. } else {
  311. LOG(LS_WARNING) << "Failed to open registry key HKLM\\" << keyName;
  312. cpu_speed_ = -1;
  313. }
  314. #elif defined(IOS) || defined(OSX)
  315. uint64_t sysctl_value;
  316. size_t length = sizeof(sysctl_value);
  317. int error = sysctlbyname("hw.cpufrequency_max", &sysctl_value, &length,
  318. NULL, 0);
  319. cpu_speed_ = !error ? static_cast<int>(sysctl_value/1000000) : -1;
  320. #else
  321. // TODO(fbarchard): Implement using proc/cpuinfo
  322. cpu_speed_ = 0;
  323. #endif
  324. return cpu_speed_;
  325. }
  326. // Dynamically check the current clockrate, which could be reduced because of
  327. // powersaving profiles. Eventually for windows we want to query WMI for
  328. // root\WMI::ProcessorPerformance.InstanceName="Processor_Number_0".frequency
  329. int SystemInfo::GetCurCpuSpeed() {
  330. #ifdef WIN32
  331. // TODO(fbarchard): Add WMI check, requires COM initialization
  332. // NOTE(fbarchard): Testable on Sandy Bridge.
  333. return GetMaxCpuSpeed();
  334. #elif defined(IOS) || defined(OSX)
  335. uint64_t sysctl_value;
  336. size_t length = sizeof(sysctl_value);
  337. int error = sysctlbyname("hw.cpufrequency", &sysctl_value, &length, NULL, 0);
  338. return !error ? static_cast<int>(sysctl_value/1000000) : GetMaxCpuSpeed();
  339. #else // LINUX || ANDROID
  340. // TODO(fbarchard): Use proc/cpuinfo for Cur speed on Linux.
  341. return GetMaxCpuSpeed();
  342. #endif
  343. }
  344. // Returns the amount of installed physical memory in Bytes. Cacheable.
  345. // Returns -1 on error.
  346. int64 SystemInfo::GetMemorySize() {
  347. if (memory_) {
  348. return memory_;
  349. }
  350. #ifdef WIN32
  351. MEMORYSTATUSEX status = {0};
  352. status.dwLength = sizeof(status);
  353. if (GlobalMemoryStatusEx(&status)) {
  354. memory_ = status.ullTotalPhys;
  355. } else {
  356. LOG_GLE(LS_WARNING) << "GlobalMemoryStatusEx failed.";
  357. memory_ = -1;
  358. }
  359. #elif defined(OSX) || defined(IOS)
  360. size_t len = sizeof(memory_);
  361. int error = sysctlbyname("hw.memsize", &memory_, &len, NULL, 0);
  362. if (error || memory_ == 0) {
  363. memory_ = -1;
  364. }
  365. #else
  366. memory_ = static_cast<int64>(sysconf(_SC_PHYS_PAGES)) *
  367. static_cast<int64>(sysconf(_SC_PAGESIZE));
  368. if (memory_ < 0) {
  369. LOG(LS_WARNING) << "sysconf(_SC_PHYS_PAGES) failed."
  370. << "sysconf(_SC_PHYS_PAGES) " << sysconf(_SC_PHYS_PAGES)
  371. << "sysconf(_SC_PAGESIZE) " << sysconf(_SC_PAGESIZE);
  372. memory_ = -1;
  373. }
  374. #endif
  375. return memory_;
  376. }
  377. // Return the name of the machine model we are currently running on.
  378. // This is a human readable string that consists of the name and version
  379. // number of the hardware, i.e 'MacBookAir1,1'. Returns an empty string if
  380. // model can not be determined. The string is cached for subsequent calls.
  381. std::string SystemInfo::GetMachineModel() {
  382. if (!machine_model_.empty()) {
  383. return machine_model_;
  384. }
  385. #if defined(OSX) || defined(IOS)
  386. char buffer[128];
  387. size_t length = sizeof(buffer);
  388. int error = sysctlbyname("hw.model", buffer, &length, NULL, 0);
  389. if (!error) {
  390. machine_model_.assign(buffer, length - 1);
  391. } else {
  392. machine_model_.clear();
  393. }
  394. #else
  395. machine_model_ = "Not available";
  396. #endif
  397. return machine_model_;
  398. }
  399. #ifdef OSX
  400. // Helper functions to query IOKit for video hardware properties.
  401. static CFTypeRef SearchForProperty(io_service_t port, CFStringRef name) {
  402. return IORegistryEntrySearchCFProperty(port, kIOServicePlane,
  403. name, kCFAllocatorDefault,
  404. kIORegistryIterateRecursively | kIORegistryIterateParents);
  405. }
  406. static void GetProperty(io_service_t port, CFStringRef name, int* value) {
  407. if (!value) return;
  408. CFTypeRef ref = SearchForProperty(port, name);
  409. if (ref) {
  410. CFTypeID refType = CFGetTypeID(ref);
  411. if (CFNumberGetTypeID() == refType) {
  412. CFNumberRef number = reinterpret_cast<CFNumberRef>(ref);
  413. p_convertCFNumberToInt(number, value);
  414. } else if (CFDataGetTypeID() == refType) {
  415. CFDataRef data = reinterpret_cast<CFDataRef>(ref);
  416. if (CFDataGetLength(data) == sizeof(UInt32)) {
  417. *value = *reinterpret_cast<const UInt32*>(CFDataGetBytePtr(data));
  418. }
  419. }
  420. CFRelease(ref);
  421. }
  422. }
  423. static void GetProperty(io_service_t port, CFStringRef name,
  424. std::string* value) {
  425. if (!value) return;
  426. CFTypeRef ref = SearchForProperty(port, name);
  427. if (ref) {
  428. CFTypeID refType = CFGetTypeID(ref);
  429. if (CFStringGetTypeID() == refType) {
  430. CFStringRef stringRef = reinterpret_cast<CFStringRef>(ref);
  431. p_convertHostCFStringRefToCPPString(stringRef, *value);
  432. } else if (CFDataGetTypeID() == refType) {
  433. CFDataRef dataRef = reinterpret_cast<CFDataRef>(ref);
  434. *value = std::string(reinterpret_cast<const char*>(
  435. CFDataGetBytePtr(dataRef)), CFDataGetLength(dataRef));
  436. }
  437. CFRelease(ref);
  438. }
  439. }
  440. #endif
  441. // Fills a struct with information on the graphics adapater and returns true
  442. // iff successful.
  443. bool SystemInfo::GetGpuInfo(GpuInfo *info) {
  444. if (!info) return false;
  445. #if defined(WIN32) && !defined(EXCLUDE_D3D9)
  446. D3DADAPTER_IDENTIFIER9 identifier;
  447. HRESULT hr = E_FAIL;
  448. HINSTANCE d3d_lib = LoadLibrary(L"d3d9.dll");
  449. if (d3d_lib) {
  450. typedef IDirect3D9* (WINAPI *D3DCreate9Proc)(UINT);
  451. D3DCreate9Proc d3d_create_proc = reinterpret_cast<D3DCreate9Proc>(
  452. GetProcAddress(d3d_lib, "Direct3DCreate9"));
  453. if (d3d_create_proc) {
  454. IDirect3D9* d3d = d3d_create_proc(D3D_SDK_VERSION);
  455. if (d3d) {
  456. hr = d3d->GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0, &identifier);
  457. d3d->Release();
  458. }
  459. }
  460. FreeLibrary(d3d_lib);
  461. }
  462. if (hr != D3D_OK) {
  463. LOG(LS_ERROR) << "Failed to access Direct3D9 information.";
  464. return false;
  465. }
  466. info->device_name = identifier.DeviceName;
  467. info->description = identifier.Description;
  468. info->vendor_id = identifier.VendorId;
  469. info->device_id = identifier.DeviceId;
  470. info->driver = identifier.Driver;
  471. // driver_version format: product.version.subversion.build
  472. std::stringstream ss;
  473. ss << HIWORD(identifier.DriverVersion.HighPart) << "."
  474. << LOWORD(identifier.DriverVersion.HighPart) << "."
  475. << HIWORD(identifier.DriverVersion.LowPart) << "."
  476. << LOWORD(identifier.DriverVersion.LowPart);
  477. info->driver_version = ss.str();
  478. return true;
  479. #elif defined(OSX)
  480. // We'll query the IOKit for the gpu of the main display.
  481. io_service_t display_service_port = CGDisplayIOServicePort(
  482. kCGDirectMainDisplay);
  483. GetProperty(display_service_port, CFSTR("vendor-id"), &info->vendor_id);
  484. GetProperty(display_service_port, CFSTR("device-id"), &info->device_id);
  485. GetProperty(display_service_port, CFSTR("model"), &info->description);
  486. return true;
  487. #else // LINUX || ANDROID
  488. // TODO(fbarchard): Implement this on Linux
  489. return false;
  490. #endif
  491. }
  492. } // namespace talk_base