PageRenderTime 21ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/linuxsystem.cpp

http://geekinfo.googlecode.com/
C++ | 433 lines | 341 code | 67 blank | 25 comment | 53 complexity | 821b210bc5437db5f75f51cf0477a132 MD5 | raw file
  1. /*
  2. linuxsystem.cpp
  3. Copyright (c) 2006-2009 Primate Labs
  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 deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include "linuxsystem.h"
  21. #include "platform.h"
  22. #include <sstream>
  23. #include <cstdlib>
  24. #if !defined(PLATFORM_LINUX)
  25. #error Wrong platform
  26. #endif
  27. #include <unistd.h>
  28. #include <sys/sysinfo.h>
  29. #include <sys/utsname.h>
  30. #include <regex.h>
  31. #include <fstream>
  32. #include <set>
  33. #if defined(ARCH_PPC)
  34. #include "macosxmodel.h"
  35. #endif
  36. #if defined(ARCH_X86)
  37. #include "x86processor.h"
  38. struct Registers {
  39. uint32 eax;
  40. uint32 ebx;
  41. uint32 ecx;
  42. uint32 edx;
  43. };
  44. static Registers cpuID(uint32 level)
  45. {
  46. Registers r;
  47. asm volatile("cpuid"
  48. : "=a" (r.eax), "=b" (r.ebx), "=c" (r.ecx), "=d" (r.edx)
  49. : "a" (level));
  50. return r;
  51. }
  52. static std::string cpu()
  53. {
  54. std::string result;
  55. Registers r; // r.eax, r.ebx, r.ecx, r.edx;
  56. r = cpuID(0x80000000);
  57. if (r.eax & 0x80000000 && r.eax >= 0x80000004) {
  58. uint32 brand[13];
  59. char * p;
  60. r = cpuID(0x80000002);
  61. brand[0] = r.eax;
  62. brand[1] = r.ebx;
  63. brand[2] = r.ecx;
  64. brand[3] = r.edx;
  65. r = cpuID(0x80000003);
  66. brand[4] = r.eax;
  67. brand[5] = r.ebx;
  68. brand[6] = r.ecx;
  69. brand[7] = r.edx;
  70. r = cpuID(0x80000004);
  71. brand[8] = r.eax;
  72. brand[9] = r.ebx;
  73. brand[10] = r.ecx;
  74. brand[11] = r.edx;
  75. brand[12] = 0;
  76. p = (char *)brand;
  77. while (*p != 0 && *p == ' ') {
  78. p++;
  79. }
  80. result = p;
  81. }
  82. return result;
  83. }
  84. static std::string cpuid()
  85. {
  86. std::ostringstream s;
  87. Registers r;
  88. uint32 family; //, extendedFamily;
  89. uint32 model; //, extendedModel;
  90. uint32 stepping;
  91. uint32 vendor[4];
  92. char * p;
  93. r = cpuID(1);
  94. family = (r.eax & 0xf00) >> 8;
  95. model = (r.eax & 0xf0) >> 4;
  96. stepping = r.eax & 0xf;
  97. if (family == 0x06 || family == 0x0f) {
  98. if (family == 0x0f) {
  99. family += ((r.eax & 0xff00000) >> 20);
  100. }
  101. model += ((r.eax & 0x000f0000) >> 12);
  102. }
  103. //std::cout << "!" << std::endl;
  104. r = cpuID(0);
  105. vendor[0] = r.ebx;
  106. vendor[1] = r.edx;
  107. vendor[2] = r.ecx;
  108. vendor[3] = 0;
  109. p = (char *)vendor;
  110. s << p << " Family " << family << " Model " << model << " Stepping " << stepping;
  111. return s.str();
  112. }
  113. static bool cpusse()
  114. {
  115. Registers r;
  116. r = cpuID(0x00000001);
  117. if (r.edx & 0x02000000) {
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. }
  123. #else
  124. static std::string cpu()
  125. {
  126. return "";
  127. }
  128. static std::string cpuid()
  129. {
  130. return "";
  131. }
  132. #endif
  133. #define COMPILE_RE(v,r) if (regcomp(v, r, REG_ICASE)) { return false; }
  134. bool LinuxSystem::parseCpuInfo()
  135. {
  136. std::ifstream cpuinfo("/proc/cpuinfo");
  137. std::string buffer;
  138. regmatch_t match[2];
  139. std::set< int > physicalid;
  140. regex_t processorRE;
  141. regex_t physicalidRE;
  142. regex_t mhzRE;
  143. regex_t cacheRE;
  144. regex_t clockRE;
  145. regex_t cpuRE;
  146. regex_t machineRE;
  147. regex_t motherboardRE;
  148. regex_t altivecRE;
  149. if (!cpuinfo.is_open()) {
  150. return false;
  151. }
  152. COMPILE_RE(&processorRE, "processor[[:space:]]*: \\([[:digit:]]*\\)");
  153. COMPILE_RE(&physicalidRE, "physical id[[:space:]]*: \\([[:digit:]]*\\)");
  154. COMPILE_RE(&mhzRE, "cpu MHz[[:space:]]*: \\([[:digit:]]*\\)");
  155. COMPILE_RE(&cacheRE, "cache size[[:space:]]*: \\([[:digit:]]*\\)");
  156. COMPILE_RE(&clockRE, "clock[[:space:]]*: \\([[:digit:]]*\\)");
  157. COMPILE_RE(&cpuRE, "cpu[[:space:]]*: \\([^$]*\\)");
  158. COMPILE_RE(&machineRE, "machine[[:space:]]*: \\([^$]*\\)");
  159. COMPILE_RE(&motherboardRE, "motherboard[[:space:]]*: \\([^$]*\\)");
  160. COMPILE_RE(&altivecRE, "cpu[[:space:]]*: .* altivec supported");
  161. if (cpuinfo.is_open()) {
  162. while (getline(cpuinfo, buffer)) {
  163. if (regexec(&processorRE, buffer.c_str(), 2, match, 0) == 0) {
  164. m_logicalCount += 1;
  165. }
  166. if (regexec(&physicalidRE, buffer.c_str(), 2, match, 0) == 0) {
  167. int id = strtol(buffer.substr(match[1].rm_so, match[1].rm_eo).c_str(), NULL, 0);
  168. physicalid.insert(id);
  169. }
  170. if (regexec(&mhzRE, buffer.c_str(), 2, match, 0) == 0) {
  171. m_mhz = strtol(buffer.substr(match[1].rm_so, match[1].rm_eo).c_str(), NULL, 0);
  172. }
  173. if (regexec(&cacheRE, buffer.c_str(), 2, match, 0) == 0) {
  174. m_cacheSize = strtol(buffer.substr(match[1].rm_so, match[1].rm_eo).c_str(), NULL, 0);
  175. }
  176. if (regexec(&clockRE, buffer.c_str(), 2, match, 0) == 0) {
  177. m_mhz = strtol(buffer.substr(match[1].rm_so, match[1].rm_eo).c_str(), NULL, 0);
  178. }
  179. if (regexec(&cpuRE, buffer.c_str(), 2, match, 0) == 0) {
  180. m_cpu = buffer.substr(match[1].rm_so, match[1].rm_eo);
  181. }
  182. if (regexec(&machineRE, buffer.c_str(), 2, match, 0) == 0) {
  183. m_model = buffer.substr(match[1].rm_so, match[1].rm_eo);
  184. }
  185. if (regexec(&motherboardRE, buffer.c_str(), 2, match, 0) == 0) {
  186. m_motherboard =buffer.substr(match[1].rm_so, match[1].rm_eo);
  187. }
  188. if (regexec(&altivecRE, buffer.c_str(), 2, match, 0) == 0) {
  189. m_altivec = true;
  190. }
  191. }
  192. }
  193. m_physicalCount = physicalid.size();
  194. if (m_physicalCount == 0) {
  195. m_physicalCount = 1;
  196. }
  197. return true;
  198. }
  199. LinuxSystem::LinuxSystem()
  200. {
  201. m_logicalCount = 0;
  202. m_physicalCount = 0;
  203. m_mhz = 0;
  204. m_cacheSize = 0;
  205. m_altivec = false;
  206. if (!parseCpuInfo()) {
  207. m_logicalCount = 1;
  208. m_physicalCount = 1;
  209. }
  210. }
  211. LinuxSystem::~LinuxSystem()
  212. {
  213. }
  214. #if defined(ARCH_X86)
  215. static uint32 getL1Dcache()
  216. {
  217. Registers r;
  218. uint32 result = 0;
  219. r = cpuID(0x80000000);
  220. if (r.eax & 0x80000000 && r.eax >= 0x80000005) {
  221. r = cpuID(0x80000005);
  222. result = 1024 * (r.ecx >> 24); // drop lower 24 bits, convert to bytes
  223. }
  224. return result;
  225. }
  226. static uint32 getL1Ccache()
  227. {
  228. Registers r;
  229. uint32 result = 0;
  230. r = cpuID(0x80000000);
  231. if (r.eax & 0x80000000 && r.eax >= 0x80000005) {
  232. r = cpuID(0x80000005);
  233. result = 1024 * (r.edx >> 24); // drop lower 24 bits, convert to bytes
  234. }
  235. return result;
  236. }
  237. static uint32 getL2cache()
  238. {
  239. Registers r;
  240. uint32 result = 0;
  241. r = cpuID(0x80000000);
  242. if (r.eax & 0x80000000 && r.eax >= 0x80000006) {
  243. r = cpuID(0x80000006);
  244. result = 1024 * (r.ecx >> 16); // drop lower 16 bits, convert to bytes
  245. }
  246. return result;
  247. }
  248. static uint32 getL3cache()
  249. {
  250. Registers r;
  251. uint32 result = 0;
  252. r = cpuID(0x80000000);
  253. if (r.eax & 0x80000000 && r.eax >= 0x80000006) {
  254. r = cpuID(0x80000006);
  255. result = 524288 * (r.edx >> 18); // drop lower 18 bits, convert from 512KB chunks to bytes
  256. }
  257. return result;
  258. }
  259. #endif
  260. std::string LinuxSystem::os()
  261. {
  262. std::ostringstream s;
  263. struct utsname name;
  264. uname(&name);
  265. s << name.sysname << " " << name.release << " " << name.machine;
  266. return s.str();
  267. }
  268. std::string LinuxSystem::model()
  269. {
  270. std::ostringstream s;
  271. #if defined(ARCH_PPC)
  272. if (m_model.length()) {
  273. return get_model_name(m_model);
  274. }
  275. #endif
  276. s << "Linux PC (" << cpu() << ")";
  277. return s.str();
  278. }
  279. std::string LinuxSystem::motherboard()
  280. {
  281. if (m_motherboard.length()) {
  282. return m_motherboard;
  283. }
  284. return "Unknown Motherboard";
  285. }
  286. std::string LinuxSystem::cpu()
  287. {
  288. if (m_cpu.length()) {
  289. return getX86Processor(m_cpu, cpuid());
  290. }
  291. return getX86Processor(::cpu(), cpuid());
  292. }
  293. std::string LinuxSystem::cpuid()
  294. {
  295. if (m_cpu.length()) {
  296. return m_cpu;
  297. }
  298. return ::cpuid();
  299. }
  300. uint64 LinuxSystem::cpu_logical_count()
  301. {
  302. return m_logicalCount;
  303. }
  304. uint64 LinuxSystem::cpu_core_count()
  305. {
  306. // TODO: Is this the correct thing to do for Linux?
  307. return m_logicalCount;
  308. }
  309. uint64 LinuxSystem::cpu_physical_count()
  310. {
  311. return m_physicalCount;
  312. }
  313. uint64 LinuxSystem::cpu_frequency()
  314. {
  315. return m_mhz * 1000000;
  316. }
  317. uint64 LinuxSystem::cpu_l1_inst_cache()
  318. {
  319. return (uint64)getL1Ccache();
  320. }
  321. uint64 LinuxSystem::cpu_l1_data_cache()
  322. {
  323. return (uint64)getL1Dcache();
  324. }
  325. uint64 LinuxSystem::cpu_l2_cache()
  326. {
  327. return (uint64)getL2cache();
  328. }
  329. uint64 LinuxSystem::cpu_l3_cache()
  330. {
  331. return (uint64)getL3cache();
  332. }
  333. uint64 LinuxSystem::bus_frequency()
  334. {
  335. return 0;
  336. }
  337. uint64 LinuxSystem::memory_size()
  338. {
  339. struct sysinfo si;
  340. if (!sysinfo(&si)) {
  341. return uint64(si.totalram) * si.mem_unit;
  342. }
  343. return 0;
  344. }
  345. uint64 LinuxSystem::memory_free()
  346. {
  347. struct sysinfo si;
  348. if (!sysinfo(&si)) {
  349. return uint64(si.freeram) * si.mem_unit;
  350. }
  351. return 0;
  352. }
  353. uint64 LinuxSystem::simd()
  354. {
  355. #if defined(ARCH_X86)
  356. return cpusse();
  357. #elif defined(ARCH_PPC)
  358. return m_altivec;
  359. #else
  360. #error Unsupported architecture.
  361. #endif
  362. }