POSIXCPUInfo.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #ifndef __APPLE__
  23. #include <fstream>
  24. #include <iostream>
  25. #include <string>
  26. #include <sstream>
  27. #include <vector>
  28. #include "platform/platform.h"
  29. #include "platformPOSIX/platformPOSIX.h"
  30. #include "platform/platformCPUCount.h"
  31. #include "console/console.h"
  32. #include <unistd.h>
  33. Platform::SystemInfo_struct Platform::SystemInfo;
  34. static inline void rtrim(std::string &s)
  35. {
  36. s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
  37. }
  38. static inline void ltrim(std::string &s)
  39. {
  40. s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
  41. }
  42. static void getCPUInformation()
  43. {
  44. std::string vendorString;
  45. std::string brandString;
  46. std::ifstream cpuInfo("/proc/cpuinfo");
  47. U32 logicalCoreCount = 0;
  48. U32 physicalCoreCount = 1;
  49. if (cpuInfo.is_open())
  50. {
  51. // Load every line of the CPU Info
  52. std::string line;
  53. while (std::getline(cpuInfo, line))
  54. {
  55. std::string fieldName = line.substr(0, line.find(":"));
  56. rtrim(fieldName);
  57. // Entries are newline separated
  58. if (fieldName == "")
  59. {
  60. ++logicalCoreCount;
  61. continue;
  62. }
  63. std::string fieldValue = line.substr(line.find(":") + 1, line.length());
  64. ltrim(fieldValue);
  65. rtrim(fieldValue);
  66. // Load fields
  67. if (fieldName == "vendor_id")
  68. {
  69. vendorString = fieldValue.c_str();
  70. }
  71. else if (fieldName == "model name")
  72. {
  73. brandString = fieldValue.c_str();
  74. }
  75. else if (fieldName == "cpu cores")
  76. {
  77. physicalCoreCount = dAtoui(fieldValue.c_str());
  78. }
  79. else if (fieldName == "flags")
  80. {
  81. std::vector<std::string> flags;
  82. std::istringstream flagStream(fieldValue);
  83. std::string currentFlag;
  84. while (std::getline(flagStream, currentFlag, ' '))
  85. {
  86. flags.push_back(currentFlag);
  87. }
  88. // Set CPU flags
  89. if (std::find(flags.begin(), flags.end(), "fpu") != flags.end())
  90. {
  91. Platform::SystemInfo.processor.properties |= CPU_PROP_FPU;
  92. }
  93. if (std::find(flags.begin(), flags.end(), "sse3") != flags.end())
  94. {
  95. Platform::SystemInfo.processor.properties |= CPU_PROP_SSE3;
  96. }
  97. if (std::find(flags.begin(), flags.end(), "avx") != flags.end())
  98. {
  99. Platform::SystemInfo.processor.properties |= CPU_PROP_AVX;
  100. }
  101. if (std::find(flags.begin(), flags.end(), "ssse3") != flags.end())
  102. {
  103. Platform::SystemInfo.processor.properties |= CPU_PROP_SSE3ex;
  104. }
  105. if (std::find(flags.begin(), flags.end(), "sse") != flags.end())
  106. {
  107. Platform::SystemInfo.processor.properties |= CPU_PROP_SSE;
  108. }
  109. if (std::find(flags.begin(), flags.end(), "sse2") != flags.end())
  110. {
  111. Platform::SystemInfo.processor.properties |= CPU_PROP_SSE2;
  112. }
  113. if (std::find(flags.begin(), flags.end(), "sse4_1") != flags.end())
  114. {
  115. Platform::SystemInfo.processor.properties |= CPU_PROP_SSE4_1;
  116. }
  117. if (std::find(flags.begin(), flags.end(), "sse4_2") != flags.end())
  118. {
  119. Platform::SystemInfo.processor.properties |= CPU_PROP_SSE4_2;
  120. }
  121. if (std::find(flags.begin(), flags.end(), "mmx") != flags.end())
  122. {
  123. Platform::SystemInfo.processor.properties |= CPU_PROP_MMX;
  124. }
  125. }
  126. }
  127. cpuInfo.close();
  128. }
  129. else
  130. {
  131. logicalCoreCount = 1;
  132. }
  133. Platform::SystemInfo.processor.numLogicalProcessors = logicalCoreCount;
  134. Platform::SystemInfo.processor.numPhysicalProcessors = physicalCoreCount;
  135. Platform::SystemInfo.processor.isHyperThreaded = logicalCoreCount != physicalCoreCount;
  136. Platform::SystemInfo.processor.isMultiCore = physicalCoreCount != 1;
  137. Platform::SystemInfo.processor.numLogicalProcessors = logicalCoreCount;
  138. Platform::SystemInfo.processor.numPhysicalProcessors = physicalCoreCount;
  139. if (Platform::SystemInfo.processor.isMultiCore)
  140. {
  141. Platform::SystemInfo.processor.properties |= CPU_PROP_MP;
  142. }
  143. // Load processor base frequency
  144. std::ifstream baseFrequencyStream("/sys/devices/system/cpu/cpu0/cpufreq/base_frequency");
  145. if (baseFrequencyStream.is_open())
  146. {
  147. U32 baseFrequencyKHz = 0;
  148. baseFrequencyStream >> baseFrequencyKHz;
  149. Platform::SystemInfo.processor.mhz = baseFrequencyKHz / 1000;
  150. baseFrequencyStream.close();
  151. }
  152. SetProcessorInfo(Platform::SystemInfo.processor, vendorString.c_str(), brandString.c_str());
  153. }
  154. void Processor::init()
  155. {
  156. getCPUInformation();
  157. #if defined(TORQUE_CPU_X64) || defined(TORQUE_CPU_X32)
  158. // Set sane default information
  159. Platform::SystemInfo.processor.properties |= CPU_PROP_C | CPU_PROP_FPU | CPU_PROP_LE ;
  160. #elif defined(TORQUE_CPU_ARM32) || defined(TORQUE_CPU_ARM64)
  161. Platform::SystemInfo.processor.type = CPU_ArmCompatible;
  162. Platform::SystemInfo.processor.name = StringTable->insert("Unknown ARM Processor");
  163. Platform::SystemInfo.processor.properties = CPU_PROP_C;
  164. #else
  165. #warning Unsupported CPU
  166. #endif
  167. // Set 64bit flag
  168. #if defined(TORQUE_CPU_X64) || defined(TORQUE_CPU_ARM64)
  169. Platform::SystemInfo.processor.properties |= CPU_PROP_64bit;
  170. #endif
  171. // Once CPU information is resolved, produce an output like Windows does
  172. Con::printf("Processor Init:");
  173. Con::printf(" Processor: %s", Platform::SystemInfo.processor.name);
  174. if (Platform::SystemInfo.processor.properties & CPU_PROP_MMX)
  175. Con::printf(" MMX detected" );
  176. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE)
  177. Con::printf(" SSE detected" );
  178. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE2)
  179. Con::printf(" SSE2 detected" );
  180. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE3)
  181. Con::printf(" SSE3 detected" );
  182. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE4_1)
  183. Con::printf(" SSE4.1 detected" );
  184. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE4_2)
  185. Con::printf(" SSE4.2 detected" );
  186. if (Platform::SystemInfo.processor.properties & CPU_PROP_AVX)
  187. Con::printf(" AVX detected" );
  188. if (Platform::SystemInfo.processor.properties & CPU_PROP_SSE3ex)
  189. Con::printf(" SSE3ex detected" );
  190. if (Platform::SystemInfo.processor.properties & CPU_PROP_MP)
  191. Con::printf(" MultiCore CPU detected [%i cores, %i logical]", Platform::SystemInfo.processor.numPhysicalProcessors, Platform::SystemInfo.processor.numLogicalProcessors);
  192. Con::printf(" ");
  193. }
  194. namespace CPUInfo
  195. {
  196. EConfig CPUCount(U32 &logical, U32 &physical)
  197. {
  198. // We don't set logical or physical here because it's already been determined by this point
  199. if (Platform::SystemInfo.processor.isHyperThreaded && Platform::SystemInfo.processor.numPhysicalProcessors == 1)
  200. {
  201. return CONFIG_SingleCoreHTEnabled;
  202. }
  203. else if (!Platform::SystemInfo.processor.isHyperThreaded && Platform::SystemInfo.processor.numPhysicalProcessors > 1)
  204. {
  205. return CONFIG_MultiCoreAndHTNotCapable;
  206. }
  207. else if (!Platform::SystemInfo.processor.isHyperThreaded && Platform::SystemInfo.processor.numPhysicalProcessors == 1)
  208. {
  209. return CONFIG_SingleCoreAndHTNotCapable;
  210. }
  211. return CONFIG_MultiCoreAndHTEnabled;
  212. }
  213. }; // namespace CPUInfo
  214. #endif