cpu.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_CPU_H_
  5. #define BASE_CPU_H_
  6. #include <string>
  7. #include <tuple>
  8. #include <vector>
  9. #include "base/base_export.h"
  10. #include "base/time/time.h"
  11. #include "build/build_config.h"
  12. namespace base {
  13. #if defined(ARCH_CPU_X86_FAMILY)
  14. namespace internal {
  15. // Compute the CPU family and model based on the vendor and CPUID signature.
  16. // Returns in order: family, model, extended family, extended model.
  17. BASE_EXPORT std::tuple<int, int, int, int> ComputeX86FamilyAndModel(
  18. const std::string& vendor,
  19. int signature);
  20. } // namespace internal
  21. #endif // defined(ARCH_CPU_X86_FAMILY)
  22. // Query information about the processor.
  23. class BASE_EXPORT CPU final {
  24. public:
  25. CPU();
  26. enum IntelMicroArchitecture {
  27. PENTIUM,
  28. SSE,
  29. SSE2,
  30. SSE3,
  31. SSSE3,
  32. SSE41,
  33. SSE42,
  34. AVX,
  35. AVX2,
  36. MAX_INTEL_MICRO_ARCHITECTURE
  37. };
  38. // Accessors for CPU information.
  39. const std::string& vendor_name() const { return cpu_vendor_; }
  40. int signature() const { return signature_; }
  41. int stepping() const { return stepping_; }
  42. int model() const { return model_; }
  43. int family() const { return family_; }
  44. int type() const { return type_; }
  45. int extended_model() const { return ext_model_; }
  46. int extended_family() const { return ext_family_; }
  47. bool has_mmx() const { return has_mmx_; }
  48. bool has_sse() const { return has_sse_; }
  49. bool has_sse2() const { return has_sse2_; }
  50. bool has_sse3() const { return has_sse3_; }
  51. bool has_ssse3() const { return has_ssse3_; }
  52. bool has_sse41() const { return has_sse41_; }
  53. bool has_sse42() const { return has_sse42_; }
  54. bool has_popcnt() const { return has_popcnt_; }
  55. bool has_avx() const { return has_avx_; }
  56. bool has_avx2() const { return has_avx2_; }
  57. bool has_aesni() const { return has_aesni_; }
  58. bool has_non_stop_time_stamp_counter() const {
  59. return has_non_stop_time_stamp_counter_;
  60. }
  61. bool is_running_in_vm() const { return is_running_in_vm_; }
  62. IntelMicroArchitecture GetIntelMicroArchitecture() const;
  63. const std::string& cpu_brand() const { return cpu_brand_; }
  64. #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_ANDROID) || \
  65. defined(OS_AIX)
  66. enum class CoreType {
  67. kUnknown = 0,
  68. kOther,
  69. kSymmetric,
  70. kBigLittle_Little,
  71. kBigLittle_Big,
  72. kBigLittleBigger_Little,
  73. kBigLittleBigger_Big,
  74. kBigLittleBigger_Bigger,
  75. kMaxValue = kBigLittleBigger_Bigger
  76. };
  77. // Attempts to guess the core types of individual CPU cores based on frequency
  78. // information from /sys/devices/system/cpu/cpuN/cpufreq/cpuinfo_max_freq.
  79. // Beware that it is kernel/hardware dependent whether the information from
  80. // sys is accurate.
  81. //
  82. // Returns a vector with the guessed type for core N at index N.
  83. static std::vector<CoreType> GuessCoreTypes();
  84. struct TimeInStateEntry {
  85. CPU::CoreType core_type; // type of the cores in this cluster.
  86. uint32_t cluster_core_index; // index of the first core in the cluster.
  87. uint64_t core_frequency_khz;
  88. TimeDelta cumulative_cpu_time;
  89. };
  90. using TimeInState = std::vector<TimeInStateEntry>;
  91. // Emits the device's cumulative CPU usage split by CPU cluster frequency
  92. // states into the output parameter (replacing its current contents). One
  93. // entry in the output parameter is added for each cluster core index
  94. // + frequency state combination with a non-zero CPU time value. Returns false
  95. // on failure. We return the usage via an output parameter to allow reuse of
  96. // TimeInState's std::vector by the caller, e.g. to avoid allocations between
  97. // repeated calls to this method.
  98. //
  99. // NOTE: Currently only supported on Linux/Android, and only on kernels with
  100. // cpufreq-stats driver.
  101. static bool GetTimeInState(TimeInState&);
  102. #endif // defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_ANDROID) ||
  103. // defined(OS_AIX)
  104. private:
  105. // Query the processor for CPUID information.
  106. void Initialize();
  107. int signature_; // raw form of type, family, model, and stepping
  108. int type_; // process type
  109. int family_; // family of the processor
  110. int model_; // model of processor
  111. int stepping_; // processor revision number
  112. int ext_model_;
  113. int ext_family_;
  114. bool has_mmx_;
  115. bool has_sse_;
  116. bool has_sse2_;
  117. bool has_sse3_;
  118. bool has_ssse3_;
  119. bool has_sse41_;
  120. bool has_sse42_;
  121. bool has_popcnt_;
  122. bool has_avx_;
  123. bool has_avx2_;
  124. bool has_aesni_;
  125. bool has_non_stop_time_stamp_counter_;
  126. bool is_running_in_vm_;
  127. std::string cpu_vendor_;
  128. std::string cpu_brand_;
  129. };
  130. } // namespace base
  131. #endif // BASE_CPU_H_