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.

72 lines
2.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "system_wrappers/include/cpu_info.h"
  11. #if defined(WEBRTC_WIN)
  12. #include <windows.h>
  13. #elif defined(WEBRTC_LINUX)
  14. #include <unistd.h>
  15. #elif defined(WEBRTC_MAC)
  16. #include <sys/sysctl.h>
  17. #elif defined(WEBRTC_FUCHSIA)
  18. #include <zircon/syscalls.h>
  19. #endif
  20. #include "rtc_base/logging.h"
  21. namespace internal {
  22. static int DetectNumberOfCores() {
  23. int number_of_cores;
  24. #if defined(WEBRTC_WIN)
  25. SYSTEM_INFO si;
  26. GetNativeSystemInfo(&si);
  27. number_of_cores = static_cast<int>(si.dwNumberOfProcessors);
  28. #elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)
  29. number_of_cores = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
  30. if (number_of_cores < 0) {
  31. RTC_LOG(LS_ERROR) << "Failed to get number of cores";
  32. number_of_cores = 1;
  33. }
  34. #elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
  35. int name[] = {CTL_HW, HW_AVAILCPU};
  36. size_t size = sizeof(number_of_cores);
  37. if (0 != sysctl(name, 2, &number_of_cores, &size, NULL, 0)) {
  38. RTC_LOG(LS_ERROR) << "Failed to get number of cores";
  39. number_of_cores = 1;
  40. }
  41. #elif defined(WEBRTC_FUCHSIA)
  42. number_of_cores = zx_system_get_num_cpus();
  43. #else
  44. RTC_LOG(LS_ERROR) << "No function to get number of cores";
  45. number_of_cores = 1;
  46. #endif
  47. RTC_LOG(LS_INFO) << "Available number of cores: " << number_of_cores;
  48. RTC_CHECK_GT(number_of_cores, 0);
  49. return number_of_cores;
  50. }
  51. } // namespace internal
  52. namespace webrtc {
  53. uint32_t CpuInfo::DetectNumberOfCores() {
  54. // Statically cache the number of system cores available since if the process
  55. // is running in a sandbox, we may only be able to read the value once (before
  56. // the sandbox is initialized) and not thereafter.
  57. // For more information see crbug.com/176522.
  58. static const uint32_t logical_cpus =
  59. static_cast<uint32_t>(internal::DetectNumberOfCores());
  60. return logical_cpus;
  61. }
  62. } // namespace webrtc