• 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 
TEST(SysinfoTest,NominalCPUFrequency)40 TEST(SysinfoTest, NominalCPUFrequency) {
41 #if !(defined(__aarch64__) && defined(__linux__)) && !defined(__EMSCRIPTEN__)
42   EXPECT_GE(NominalCPUFrequency(), 1000.0)
43       << "NominalCPUFrequency() did not return a reasonable value";
44 #else
45   // Aarch64 cannot read the CPU frequency from sysfs, so we get back 1.0.
46   // Emscripten does not have a sysfs to read from at all.
47   EXPECT_EQ(NominalCPUFrequency(), 1.0)
48       << "CPU frequency detection was fixed! Please update unittest.";
49 #endif
50 }
51 
TEST(SysinfoTest,GetTID)52 TEST(SysinfoTest, GetTID) {
53   EXPECT_EQ(GetTID(), GetTID());  // Basic compile and equality test.
54 #ifdef __native_client__
55   // Native Client has a race condition bug that leads to memory
56   // exaustion when repeatedly creating and joining threads.
57   // https://bugs.chromium.org/p/nativeclient/issues/detail?id=1027
58   return;
59 #endif
60   // Test that TIDs are unique to each thread.
61   // Uses a few loops to exercise implementations that reallocate IDs.
62   for (int i = 0; i < 10; ++i) {
63     constexpr int kNumThreads = 10;
64     Barrier all_threads_done(kNumThreads);
65     std::vector<std::thread> threads;
66 
67     Mutex mutex;
68     std::unordered_set<pid_t> tids;
69 
70     for (int j = 0; j < kNumThreads; ++j) {
71       threads.push_back(std::thread([&]() {
72         pid_t id = GetTID();
73         {
74           MutexLock lock(&mutex);
75           ASSERT_TRUE(tids.find(id) == tids.end());
76           tids.insert(id);
77         }
78         // We can't simply join the threads here. The threads need to
79         // be alive otherwise the TID might have been reallocated to
80         // another live thread.
81         all_threads_done.Block();
82       }));
83     }
84     for (auto& thread : threads) {
85       thread.join();
86     }
87   }
88 }
89 
90 #ifdef __linux__
TEST(SysinfoTest,LinuxGetTID)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 
97 }  // namespace
98 }  // namespace base_internal
99 ABSL_NAMESPACE_END
100 }  // namespace absl
101