proxygen
CacheLocalityTest.cpp File Reference
#include <folly/concurrency/CacheLocality.h>
#include <folly/portability/GTest.h>
#include <glog/logging.h>
#include <memory>
#include <thread>
#include <unordered_map>

Go to the source code of this file.

Functions

 TEST (CacheLocality, FakeSysfs)
 
 TEST (ThreadId, SimplePthread)
 
 TEST (AccessSpreader, Simple)
 
 TEST (AccessSpreader, SimpleCached)
 
 TEST (AccessSpreader, ConcurrentAccessCached)
 

Variables

static std::unordered_map< std::string, std::stringfakeSysfsTree
 
static const CacheLocality nonUniformExampleLocality
 This is the expected CacheLocality structure for fakeSysfsTree. More...
 

Function Documentation

TEST ( CacheLocality  ,
FakeSysfs   
)

Definition at line 312 of file CacheLocalityTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, name, nonUniformExampleLocality, folly::Getcpu::resolveVdsoFunc(), string, and folly::TEST().

312  {
313  auto parsed = CacheLocality::readFromSysfsTree([](std::string name) {
314  auto iter = fakeSysfsTree.find(name);
315  return iter == fakeSysfsTree.end() ? std::string() : iter->second;
316  });
317 
318  auto& expected = nonUniformExampleLocality;
319  EXPECT_EQ(expected.numCpus, parsed.numCpus);
320  EXPECT_EQ(expected.numCachesByLevel, parsed.numCachesByLevel);
321  EXPECT_EQ(expected.localityIndexByCpu, parsed.localityIndexByCpu);
322 }
static std::unordered_map< std::string, std::string > fakeSysfsTree
static const CacheLocality nonUniformExampleLocality
This is the expected CacheLocality structure for fakeSysfsTree.
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const char * name
Definition: http_parser.c:437
const char * string
Definition: Conv.cpp:212
TEST ( ThreadId  ,
SimplePthread   
)

Definition at line 347 of file CacheLocalityTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, and folly::FallbackGetcpu< ThreadId >::getcpu().

347  {
348  unsigned cpu = 0;
349  auto rv =
350  folly::FallbackGetcpu<HashingThreadId>::getcpu(&cpu, nullptr, nullptr);
351  EXPECT_EQ(rv, 0);
352  EXPECT_TRUE(cpu > 0);
353  unsigned again;
354  folly::FallbackGetcpu<HashingThreadId>::getcpu(&again, nullptr, nullptr);
355  EXPECT_EQ(cpu, again);
356 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static int getcpu(unsigned *cpu, unsigned *node, void *)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( AccessSpreader  ,
Simple   
)

Definition at line 372 of file CacheLocalityTest.cpp.

References EXPECT_LT, and s.

372  {
373  for (size_t s = 1; s < 200; ++s) {
375  }
376 }
static set< string > s
#define EXPECT_LT(val1, val2)
Definition: gtest.h:1930
TEST ( AccessSpreader  ,
SimpleCached   
)

Definition at line 378 of file CacheLocalityTest.cpp.

References EXPECT_LT, and s.

378  {
379  for (size_t s = 1; s < 200; ++s) {
381  }
382 }
static set< string > s
#define EXPECT_LT(val1, val2)
Definition: gtest.h:1930
TEST ( AccessSpreader  ,
ConcurrentAccessCached   
)

Definition at line 384 of file CacheLocalityTest.cpp.

References a, c, folly::AccessSpreader< Atom >::current(), DECLARE_SPREADER_TAG, EXPECT_EQ, EXPECT_LT, EXPECT_TRUE, folly::CoreRawAllocator< Stripes >::get(), i, s, folly::TEST(), threads, folly::CacheLocality::uniform(), and folly::fibers::yield().

384  {
385  std::vector<std::thread> threads;
386  for (size_t i = 0; i < 4; ++i) {
387  threads.emplace_back([]() {
388  for (size_t s : {16, 32, 64}) {
389  for (size_t j = 1; j < 200; ++j) {
392  }
394  }
395  });
396  }
397  for (auto& thread : threads) {
398  thread.join();
399  }
400 }
std::vector< std::thread::id > threads
static set< string > s
#define EXPECT_LT(val1, val2)
Definition: gtest.h:1930

Variable Documentation

std::unordered_map<std::string, std::string> fakeSysfsTree
static

This is the relevant nodes from a production box's sysfs tree. If you think this map is ugly you should see the version of this test that used a real directory tree. To reduce the chance of testing error I haven't tried to remove the common prefix

Definition at line 32 of file CacheLocalityTest.cpp.

const CacheLocality nonUniformExampleLocality
static
Initial value:
= {
32,
{16, 16, 2},
{0, 2, 4, 6, 8, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28,
30, 1, 3, 5, 7, 9, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31}}

This is the expected CacheLocality structure for fakeSysfsTree.

Definition at line 306 of file CacheLocalityTest.cpp.

Referenced by TEST().