• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #include "absl/base/internal/sysinfo.h"
16 
17 #ifndef _WIN32
18 #include <sys/types.h>
19 #include <unistd.h>
20 #endif
21 
22 #include <thread>  // NOLINT(build/c++11)
23 #include <unordered_set>
24 #include <vector>
25 
26 #include "gtest/gtest.h"
27 #include "absl/synchronization/barrier.h"
28 #include "absl/synchronization/mutex.h"
29 
30 namespace absl {
31 ABSL_NAMESPACE_BEGIN
32 namespace base_internal {
33 namespace {
34 
TEST(SysinfoTest,NumCPUs)35 TEST(SysinfoTest, NumCPUs) {
36   EXPECT_NE(NumCPUs(), 0)
37       << "NumCPUs() should not have the default value of 0";
38 }
39 
40 // Ensure that NominalCPUFrequency returns a reasonable value, or 1.00 on
41 // platforms where the CPU frequency is not available through sysfs.
42 //
43 // POWER is particularly problematic here; some Linux kernels expose the CPU
44 // frequency, while others do not. Since we can't predict a priori what a given
45 // machine is going to do, just disable this test on POWER on Linux.
46 #if !(defined(__linux) && (defined(__ppc64__) || defined(__PPC64__)))
TEST(SysinfoTest,NominalCPUFrequency)47 TEST(SysinfoTest, NominalCPUFrequency) {
48   // Linux only exposes the CPU frequency on certain architectures, and
49   // Emscripten doesn't expose it at all.
50 #if defined(__linux__) &&                                                  \
51         (defined(__aarch64__) || defined(__hppa__) || defined(__mips__) || \
52          defined(__riscv) || defined(__s390x__)) ||                        \
53     defined(__EMSCRIPTEN__)
54   EXPECT_EQ(NominalCPUFrequency(), 1.0)
55       << "CPU frequency detection was fixed! Please update unittest.";
56 #else
57   EXPECT_GE(NominalCPUFrequency(), 1000.0)
58       << "NominalCPUFrequency() did not return a reasonable value";
59 #endif
60 }
61 #endif
62 
TEST(SysinfoTest,GetTID)63 TEST(SysinfoTest, GetTID) {
64   EXPECT_EQ(GetTID(), GetTID());  // Basic compile and equality test.
65 #ifdef __native_client__
66   // Native Client has a race condition bug that leads to memory
67   // exaustion when repeatedly creating and joining threads.
68   // https://bugs.chromium.org/p/nativeclient/issues/detail?id=1027
69   return;
70 #endif
71   // Test that TIDs are unique to each thread.
72   // Uses a few loops to exercise implementations that reallocate IDs.
73   for (int i = 0; i < 10; ++i) {
74     constexpr int kNumThreads = 10;
75     Barrier all_threads_done(kNumThreads);
76     std::vector<std::thread> threads;
77 
78     Mutex mutex;
79     std::unordered_set<pid_t> tids;
80 
81     for (int j = 0; j < kNumThreads; ++j) {
82       threads.push_back(std::thread([&]() {
83         pid_t id = GetTID();
84         {
85           MutexLock lock(&mutex);
86           ASSERT_TRUE(tids.find(id) == tids.end());
87           tids.insert(id);
88         }
89         // We can't simply join the threads here. The threads need to
90         // be alive otherwise the TID might have been reallocated to
91         // another live thread.
92         all_threads_done.Block();
93       }));
94     }
95     for (auto& thread : threads) {
96       thread.join();
97     }
98   }
99 }
100 
101 #ifdef __linux__
TEST(SysinfoTest,LinuxGetTID)102 TEST(SysinfoTest, LinuxGetTID) {
103   // On Linux, for the main thread, GetTID()==getpid() is guaranteed by the API.
104   EXPECT_EQ(GetTID(), getpid());
105 }
106 #endif
107 
108 }  // namespace
109 }  // namespace base_internal
110 ABSL_NAMESPACE_END
111 }  // namespace absl
112