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