sysinfo_test.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/base/internal/sysinfo.h"
  15. #ifndef _WIN32
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. #endif
  19. #include <thread> // NOLINT(build/c++11)
  20. #include <unordered_set>
  21. #include <vector>
  22. #include "gtest/gtest.h"
  23. #include "absl/synchronization/barrier.h"
  24. #include "absl/synchronization/mutex.h"
  25. namespace absl {
  26. ABSL_NAMESPACE_BEGIN
  27. namespace base_internal {
  28. namespace {
  29. TEST(SysinfoTest, NumCPUs) {
  30. EXPECT_NE(NumCPUs(), 0)
  31. << "NumCPUs() should not have the default value of 0";
  32. }
  33. // Ensure that NominalCPUFrequency returns a reasonable value, or 1.00 on
  34. // platforms where the CPU frequency is not available through sysfs.
  35. //
  36. // POWER is particularly problematic here; some Linux kernels expose the CPU
  37. // frequency, while others do not. Since we can't predict a priori what a given
  38. // machine is going to do, just disable this test on POWER on Linux.
  39. #if !(defined(__linux) && (defined(__ppc64__) || defined(__PPC64__)))
  40. TEST(SysinfoTest, NominalCPUFrequency) {
  41. // Linux only exposes the CPU frequency on certain architectures, and
  42. // Emscripten doesn't expose it at all.
  43. #if defined(__linux__) && \
  44. (defined(__aarch64__) || defined(__hppa__) || defined(__mips__) || \
  45. defined(__riscv) || defined(__s390x__)) || \
  46. defined(__EMSCRIPTEN__)
  47. EXPECT_EQ(NominalCPUFrequency(), 1.0)
  48. << "CPU frequency detection was fixed! Please update unittest.";
  49. #else
  50. EXPECT_GE(NominalCPUFrequency(), 1000.0)
  51. << "NominalCPUFrequency() did not return a reasonable value";
  52. #endif
  53. }
  54. #endif
  55. TEST(SysinfoTest, GetTID) {
  56. EXPECT_EQ(GetTID(), GetTID()); // Basic compile and equality test.
  57. #ifdef __native_client__
  58. // Native Client has a race condition bug that leads to memory
  59. // exaustion when repeatedly creating and joining threads.
  60. // https://bugs.chromium.org/p/nativeclient/issues/detail?id=1027
  61. return;
  62. #endif
  63. // Test that TIDs are unique to each thread.
  64. // Uses a few loops to exercise implementations that reallocate IDs.
  65. for (int i = 0; i < 10; ++i) {
  66. constexpr int kNumThreads = 10;
  67. Barrier all_threads_done(kNumThreads);
  68. std::vector<std::thread> threads;
  69. Mutex mutex;
  70. std::unordered_set<pid_t> tids;
  71. for (int j = 0; j < kNumThreads; ++j) {
  72. threads.push_back(std::thread([&]() {
  73. pid_t id = GetTID();
  74. {
  75. MutexLock lock(&mutex);
  76. ASSERT_TRUE(tids.find(id) == tids.end());
  77. tids.insert(id);
  78. }
  79. // We can't simply join the threads here. The threads need to
  80. // be alive otherwise the TID might have been reallocated to
  81. // another live thread.
  82. all_threads_done.Block();
  83. }));
  84. }
  85. for (auto& thread : threads) {
  86. thread.join();
  87. }
  88. }
  89. }
  90. #ifdef __linux__
  91. TEST(SysinfoTest, LinuxGetTID) {
  92. // On Linux, for the main thread, GetTID()==getpid() is guaranteed by the API.
  93. EXPECT_EQ(GetTID(), getpid());
  94. }
  95. #endif
  96. } // namespace
  97. } // namespace base_internal
  98. ABSL_NAMESPACE_END
  99. } // namespace absl