Use the GTest portability headers
[folly.git] / folly / test / DeterministicScheduleTest.cpp
1 /*
2  * Copyright 2016 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/test/DeterministicSchedule.h>
18
19 #include <folly/portability/GFlags.h>
20 #include <folly/portability/GTest.h>
21
22 using namespace folly::test;
23
24 TEST(DeterministicSchedule, uniform) {
25   auto p = DeterministicSchedule::uniform(0);
26   int buckets[10] = {};
27   for (int i = 0; i < 100000; ++i) {
28     buckets[p(10)]++;
29   }
30   for (int i = 0; i < 10; ++i) {
31     EXPECT_TRUE(buckets[i] > 9000);
32   }
33 }
34
35 TEST(DeterministicSchedule, uniformSubset) {
36   auto ps = DeterministicSchedule::uniformSubset(0, 3, 100);
37   int buckets[10] = {};
38   std::set<int> seen;
39   for (int i = 0; i < 100000; ++i) {
40     if (i > 0 && (i % 100) == 0) {
41       EXPECT_EQ(seen.size(), 3);
42       seen.clear();
43     }
44     int x = ps(10);
45     seen.insert(x);
46     EXPECT_TRUE(seen.size() <= 3);
47     buckets[x]++;
48   }
49   for (int i = 0; i < 10; ++i) {
50     EXPECT_TRUE(buckets[i] > 9000);
51   }
52 }
53
54 TEST(DeterministicSchedule, buggyAdd) {
55   for (bool bug : {false, true}) {
56     DeterministicSchedule sched(DeterministicSchedule::uniform(0));
57     if (bug) {
58       FOLLY_TEST_DSCHED_VLOG("Test with race condition");
59     } else {
60       FOLLY_TEST_DSCHED_VLOG("Test without race condition");
61     }
62     DeterministicMutex m;
63     // The use of DeterinisticAtomic is not needed here, but it makes
64     // it easier to understand the sequence of events in logs.
65     DeterministicAtomic<int> test{0};
66     DeterministicAtomic<int> baseline{0};
67     int numThreads = 10;
68     std::vector<std::thread> threads(numThreads);
69     for (int t = 0; t < numThreads; ++t) {
70       threads[t] = DeterministicSchedule::thread([&, t] {
71         baseline.fetch_add(1);
72         // Atomic increment of test protected by mutex m
73         do {
74           // Some threads use lock() others use try_lock()
75           if ((t & 1) == 0) {
76             m.lock();
77           } else {
78             if (!m.try_lock()) {
79               continue;
80             }
81           }
82           int newval = test.load() + 1;
83           if (bug) {
84             // Break the atomicity of the increment operation
85             m.unlock();
86             m.lock();
87           }
88           test.store(newval);
89           m.unlock();
90           break;
91         } while (true);
92       }); // thread lambda
93     } // for t
94     for (auto& t : threads) {
95       DeterministicSchedule::join(t);
96     }
97     if (!bug) {
98       EXPECT_EQ(test.load(), baseline.load());
99     } else {
100       if (test.load() == baseline.load()) {
101         FOLLY_TEST_DSCHED_VLOG("Didn't catch the bug");
102       } else {
103         FOLLY_TEST_DSCHED_VLOG("Caught the bug");
104       }
105     }
106   } // for bug
107 } // TEST
108
109 /// Test support for auxiliary data and global invariants
110 ///
111
112 /// How to use DSched support for auxiliary data and global invariants:
113 ///   1. Forward declare an annotated shared class
114 ///   2. Add the annotated shared class as a friend of the original class(es)
115 ///      to be tested
116 ///   3. Define auxiliary data
117 ///   4. Define function(s) for updating auxiliary data to match shared updates
118 ///   5. Define the annotated shared class
119 ///      It supports an interface to the original class along with auxiliary
120 ///        functions for updating aux data, checking invariants, and/or logging
121 ///      It may have to duplicate the steps of multi-step operations in the
122 ////       original code in order to manage aux data and check invariants after
123 ///        shared accesses other than the first access in an opeeration
124 ///   6. Define function for checking global invariants and/or logging global
125 ///      state
126 ///   7. Define function generator(s) for function object(s) that update aux
127 ///      data, check invariants, and/or log state
128 ///   8. Define TEST using anotated shared data, aux data, and aux functions
129
130 using DSched = DeterministicSchedule;
131
132 /** forward declaration of annotated shared class */
133 class AnnotatedAtomicCounter;
134
135 /** original shared class to be tested */
136 template <typename T, template <typename> class Atom = std::atomic>
137 class AtomicCounter {
138   friend AnnotatedAtomicCounter;
139
140  public:
141   explicit AtomicCounter(T val) : counter_(val) {}
142
143   void inc() {
144     counter_.fetch_add(1);
145   }
146
147   void inc_bug() {
148     int newval = counter_.load() + 1;
149     counter_.store(newval);
150   }
151
152   T load() {
153     return counter_.load();
154   }
155
156  private:
157   Atom<T> counter_ = {0};
158 };
159
160 /** auxiliary data */
161 struct AuxData {
162   explicit AuxData(int nthr) {
163     local_.resize(nthr, 0);
164   }
165
166   std::vector<int> local_;
167 };
168
169 /** aux update function(s) */
170 void auxUpdateAfterInc(int tid, AuxData& auxdata, bool success) {
171   if (success) {
172     auxdata.local_[tid]++;
173   }
174 }
175
176 /** annotated shared class */
177 class AnnotatedAtomicCounter {
178  public:
179   explicit AnnotatedAtomicCounter(int val) : shared_(val) {}
180
181   void inc(AuxAct& auxfn) {
182     DSched::setAuxAct(auxfn);
183     /* calls the fine-grained original */
184     shared_.inc();
185   }
186
187   void inc_bug(AuxAct auxfn) {
188     /* duplicates the steps of the multi-access original in order to
189      * annotate the second access */
190     int newval = shared_.counter_.load() + 1;
191     DSched::setAuxAct(auxfn);
192     shared_.counter_.store(newval);
193   }
194
195   int load_direct() {
196     return shared_.counter_.load_direct();
197   }
198
199  private:
200   AtomicCounter<int, DeterministicAtomic> shared_;
201 };
202
203 using Annotated = AnnotatedAtomicCounter;
204
205 /** aux log & check function */
206 void auxCheck(int tid, Annotated& annotated, AuxData& auxdata) {
207   /* read shared data */
208   int val = annotated.load_direct();
209   /* read auxiliary data */
210   int sum = 0;
211   for (int v : auxdata.local_) {
212     sum += v;
213   }
214   /* log state */
215   VLOG(2) << "tid " << tid << " -- shared counter= " << val
216           << " -- sum increments= " << sum;
217   /* check invariant */
218   if (val != sum) {
219     LOG(ERROR) << "counter=(" << val << ") expected(" << sum << ")";
220     CHECK(false);
221   }
222 }
223
224 /** function generator(s) */
225 AuxAct auxAfterInc(int tid, Annotated& annotated, AuxData& auxdata) {
226   return [&annotated, &auxdata, tid](bool success) {
227     auxUpdateAfterInc(tid, auxdata, success);
228     auxCheck(tid, annotated, auxdata);
229   };
230 }
231
232 DEFINE_bool(bug, false, "Introduce bug");
233 DEFINE_int64(seed, 0, "Seed for random number generator");
234 DEFINE_int32(num_threads, 2, "Number of threads");
235 DEFINE_int32(num_iterations, 10, "Number of iterations");
236
237 TEST(DSchedCustom, atomic_add) {
238   bool bug = FLAGS_bug;
239   long seed = FLAGS_seed;
240   int nthr = FLAGS_num_threads;
241   int niter = FLAGS_num_iterations;
242
243   CHECK_GT(nthr, 0);
244
245   Annotated annotated(0);
246   AuxData auxdata(nthr);
247   DSched sched(DSched::uniform(seed));
248
249   std::vector<std::thread> threads(nthr);
250   for (int tid = 0; tid < nthr; ++tid) {
251     threads[tid] = DSched::thread([&, tid]() {
252       AuxAct auxfn = auxAfterInc(tid, annotated, auxdata);
253       for (int i = 0; i < niter; ++i) {
254         if (bug && (tid == 0) && (i % 10 == 0)) {
255           annotated.inc_bug(auxfn);
256         } else {
257           annotated.inc(auxfn);
258         }
259       }
260     });
261   }
262   for (auto& t : threads) {
263     DSched::join(t);
264   }
265   EXPECT_EQ(annotated.load_direct(), nthr * niter);
266 }
267
268 int main(int argc, char** argv) {
269   testing::InitGoogleTest(&argc, argv);
270   gflags::ParseCommandLineFlags(&argc, &argv, true);
271   return RUN_ALL_TESTS();
272 }