sysinfo_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/base/internal/sysinfo.h"
00016 
00017 #ifndef _WIN32
00018 #include <sys/types.h>
00019 #include <unistd.h>
00020 #endif
00021 
00022 #include <thread>  // NOLINT(build/c++11)
00023 #include <unordered_set>
00024 #include <vector>
00025 
00026 #include "gtest/gtest.h"
00027 #include "absl/synchronization/barrier.h"
00028 #include "absl/synchronization/mutex.h"
00029 
00030 namespace absl {
00031 namespace base_internal {
00032 namespace {
00033 
00034 TEST(SysinfoTest, NumCPUs) {
00035   EXPECT_NE(NumCPUs(), 0)
00036       << "NumCPUs() should not have the default value of 0";
00037 }
00038 
00039 TEST(SysinfoTest, NominalCPUFrequency) {
00040 #if !(defined(__aarch64__) && defined(__linux__)) && !defined(__EMSCRIPTEN__)
00041   EXPECT_GE(NominalCPUFrequency(), 1000.0)
00042       << "NominalCPUFrequency() did not return a reasonable value";
00043 #else
00044   // Aarch64 cannot read the CPU frequency from sysfs, so we get back 1.0.
00045   // Emscripten does not have a sysfs to read from at all.
00046   EXPECT_EQ(NominalCPUFrequency(), 1.0)
00047       << "CPU frequency detection was fixed! Please update unittest.";
00048 #endif
00049 }
00050 
00051 TEST(SysinfoTest, GetTID) {
00052   EXPECT_EQ(GetTID(), GetTID());  // Basic compile and equality test.
00053 #ifdef __native_client__
00054   // Native Client has a race condition bug that leads to memory
00055   // exaustion when repeatedly creating and joining threads.
00056   // https://bugs.chromium.org/p/nativeclient/issues/detail?id=1027
00057   return;
00058 #endif
00059   // Test that TIDs are unique to each thread.
00060   // Uses a few loops to exercise implementations that reallocate IDs.
00061   for (int i = 0; i < 32; ++i) {
00062     constexpr int kNumThreads = 64;
00063     Barrier all_threads_done(kNumThreads);
00064     std::vector<std::thread> threads;
00065 
00066     Mutex mutex;
00067     std::unordered_set<pid_t> tids;
00068 
00069     for (int j = 0; j < kNumThreads; ++j) {
00070       threads.push_back(std::thread([&]() {
00071         pid_t id = GetTID();
00072         {
00073           MutexLock lock(&mutex);
00074           ASSERT_TRUE(tids.find(id) == tids.end());
00075           tids.insert(id);
00076         }
00077         // We can't simply join the threads here. The threads need to
00078         // be alive otherwise the TID might have been reallocated to
00079         // another live thread.
00080         all_threads_done.Block();
00081       }));
00082     }
00083     for (auto& thread : threads) {
00084       thread.join();
00085     }
00086   }
00087 }
00088 
00089 #ifdef __linux__
00090 TEST(SysinfoTest, LinuxGetTID) {
00091   // On Linux, for the main thread, GetTID()==getpid() is guaranteed by the API.
00092   EXPECT_EQ(GetTID(), getpid());
00093 }
00094 #endif
00095 
00096 }  // namespace
00097 }  // namespace base_internal
00098 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15