Copyright 2013 -> 2014
[folly.git] / folly / test / PackedSyncPtrTest.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "folly/PackedSyncPtr.h"
18
19 #include <cinttypes>
20 #include <gtest/gtest.h>
21 #include <thread>
22 #include <unordered_map>
23 #include <utility>
24
25 using folly::PackedSyncPtr;
26
27 namespace {
28
29 // Compile time check for packability.  This requires that
30 // PackedSyncPtr is a POD struct on gcc.
31 struct ignore { PackedSyncPtr<int> foo; char c; } __attribute__((packed));
32 static_assert(sizeof(ignore) == 9, "PackedSyncPtr wasn't packable");
33
34 }
35
36 TEST(PackedSyncPtr, Basic) {
37   PackedSyncPtr<std::pair<int,int>> sp;
38   sp.init(new std::pair<int,int>[2]);
39   EXPECT_EQ(sizeof(sp), 8);
40   sp->first = 5;
41   EXPECT_EQ(sp[0].first, 5);
42   sp[1].second = 7;
43   EXPECT_EQ(sp[1].second, 7);
44   sp.lock();
45   EXPECT_EQ(sp[1].second, 7);
46   sp[0].first = 9;
47   EXPECT_EQ(sp->first, 9);
48   sp.unlock();
49   EXPECT_EQ((sp.get() + 1)->second, 7);
50
51   sp.lock();
52   EXPECT_EQ(sp.extra(), 0);
53   sp.setExtra(0x13);
54   EXPECT_EQ(sp.extra(), 0x13);
55   EXPECT_EQ((sp.get() + 1)->second, 7);
56   delete[] sp.get();
57   auto newP = new std::pair<int,int>();
58   sp.set(newP);
59   EXPECT_EQ(sp.extra(), 0x13);
60   EXPECT_EQ(sp.get(), newP);
61   sp.unlock();
62 }
63
64 // Here we use the PackedSyncPtr to lock the whole SyncVec (base, *base, and sz)
65 template<typename T>
66 struct SyncVec {
67   PackedSyncPtr<T> base;
68   SyncVec() { base.init(); }
69   void push_back(const T& t) {
70     base.set((T*) realloc(base.get(),
71       (base.extra() + 1) * sizeof(T)));
72     base[base.extra()] = t;
73     base.setExtra(base.extra() + 1);
74   }
75   void lock() {
76     base.lock();
77   }
78   void unlock() {
79     base.unlock();
80   }
81
82   T* begin() const { return base.get(); }
83   T* end() const { return base.get() + base.extra(); }
84 };
85 typedef SyncVec<intptr_t> VecT;
86 typedef std::unordered_map<int64_t, VecT> Map;
87 const int mapCap = 1317;
88 const int nthrs = 297;
89 static Map map(mapCap);
90
91 // Each app thread inserts it's ID into every vec in map
92 // map is read only, so doesn't need any additional locking
93 void appThread(intptr_t id) {
94   for (auto& kv : map) {
95     kv.second.lock();
96     kv.second.push_back(id);
97     kv.second.unlock();
98   }
99 }
100
101 TEST(PackedSyncPtr, Application) {
102   for (int64_t i = 0; i < mapCap / 2; ++i) {
103     map.insert(std::make_pair(i, VecT()));
104   }
105   std::vector<std::thread> thrs;
106   for (intptr_t i = 0; i < nthrs; i++) {
107     thrs.push_back(std::thread(appThread, i));
108   }
109   for (auto& t : thrs) {
110     t.join();
111   }
112
113   for (auto& kv : map) {
114     // Make sure every thread successfully inserted it's ID into every vec
115     std::set<intptr_t> idsFound;
116     for (auto& elem : kv.second) {
117       EXPECT_TRUE(idsFound.insert(elem).second);  // check for dups
118     }
119     EXPECT_EQ(idsFound.size(), nthrs); // check they are all there
120   }
121 }
122
123 TEST(PackedSyncPtr, extraData) {
124   PackedSyncPtr<int> p;
125   p.init();
126   int* unaligned = reinterpret_cast<int*>(0xf003);
127   p.lock();
128   p.set(unaligned);
129   uintptr_t* bytes = reinterpret_cast<uintptr_t*>(&p);
130   LOG(INFO) << "Bytes integer is: 0x" << std::hex << *bytes;
131   EXPECT_EQ(p.get(), unaligned);
132   p.unlock();
133 }