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