POSIXCPUInfo.cpp 9.3 KB

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