blob: 5f9e45f6aa8431ce1343d700d7b8eb90a2ac63c0 [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 {
Austin Schuhb4691e92020-12-31 12:37:18 -080031ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070032namespace base_internal {
33namespace {
34
35TEST(SysinfoTest, NumCPUs) {
36 EXPECT_NE(NumCPUs(), 0)
37 << "NumCPUs() should not have the default value of 0";
38}
39
Austin Schuhb4691e92020-12-31 12:37:18 -080040// 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__)))
Austin Schuh36244a12019-09-21 17:52:38 -070047TEST(SysinfoTest, NominalCPUFrequency) {
Austin Schuhb4691e92020-12-31 12:37:18 -080048 // 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__)
Austin Schuh36244a12019-09-21 17:52:38 -070054 EXPECT_EQ(NominalCPUFrequency(), 1.0)
55 << "CPU frequency detection was fixed! Please update unittest.";
Austin Schuhb4691e92020-12-31 12:37:18 -080056#else
57 EXPECT_GE(NominalCPUFrequency(), 1000.0)
58 << "NominalCPUFrequency() did not return a reasonable value";
Austin Schuh36244a12019-09-21 17:52:38 -070059#endif
60}
Austin Schuhb4691e92020-12-31 12:37:18 -080061#endif
Austin Schuh36244a12019-09-21 17:52:38 -070062
63TEST(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.
Austin Schuhb4691e92020-12-31 12:37:18 -080073 for (int i = 0; i < 10; ++i) {
74 constexpr int kNumThreads = 10;
Austin Schuh36244a12019-09-21 17:52:38 -070075 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__
102TEST(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
Austin Schuhb4691e92020-12-31 12:37:18 -0800110ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700111} // namespace absl