Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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>
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
00045
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());
00053 #ifdef __native_client__
00054
00055
00056
00057 return;
00058 #endif
00059
00060
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
00078
00079
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
00092 EXPECT_EQ(GetTID(), getpid());
00093 }
00094 #endif
00095
00096 }
00097 }
00098 }