2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/test/DeterministicSchedule.h>
19 #include <folly/portability/GFlags.h>
20 #include <folly/portability/GTest.h>
22 using namespace folly::test;
24 TEST(DeterministicSchedule, uniform) {
25 auto p = DeterministicSchedule::uniform(0);
27 for (int i = 0; i < 100000; ++i) {
30 for (int i = 0; i < 10; ++i) {
31 EXPECT_TRUE(buckets[i] > 9000);
35 TEST(DeterministicSchedule, uniformSubset) {
36 auto ps = DeterministicSchedule::uniformSubset(0, 3, 100);
39 for (int i = 0; i < 100000; ++i) {
40 if (i > 0 && (i % 100) == 0) {
41 EXPECT_EQ(seen.size(), 3);
46 EXPECT_TRUE(seen.size() <= 3);
49 for (int i = 0; i < 10; ++i) {
50 EXPECT_TRUE(buckets[i] > 9000);
54 TEST(DeterministicSchedule, buggyAdd) {
55 for (bool bug : {false, true}) {
56 DeterministicSchedule sched(DeterministicSchedule::uniform(0));
58 FOLLY_TEST_DSCHED_VLOG("Test with race condition");
60 FOLLY_TEST_DSCHED_VLOG("Test without race condition");
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};
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
74 // Some threads use lock() others use try_lock()
82 int newval = test.load() + 1;
84 // Break the atomicity of the increment operation
94 for (auto& t : threads) {
95 DeterministicSchedule::join(t);
98 EXPECT_EQ(test.load(), baseline.load());
100 if (test.load() == baseline.load()) {
101 FOLLY_TEST_DSCHED_VLOG("Didn't catch the bug");
103 FOLLY_TEST_DSCHED_VLOG("Caught the bug");
109 /// Test support for auxiliary data and global invariants
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)
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
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
130 using DSched = DeterministicSchedule;
132 /** forward declaration of annotated shared class */
133 class AnnotatedAtomicCounter;
135 /** original shared class to be tested */
136 template <typename T, template <typename> class Atom = std::atomic>
137 class AtomicCounter {
138 friend AnnotatedAtomicCounter;
141 explicit AtomicCounter(T val) : counter_(val) {}
144 counter_.fetch_add(1);
148 int newval = counter_.load() + 1;
149 counter_.store(newval);
153 return counter_.load();
157 Atom<T> counter_ = {0};
160 /** auxiliary data */
162 explicit AuxData(int nthr) {
163 local_.resize(nthr, 0);
166 std::vector<int> local_;
169 /** aux update function(s) */
170 void auxUpdateAfterInc(int tid, AuxData& auxdata, bool success) {
172 auxdata.local_[tid]++;
176 /** annotated shared class */
177 class AnnotatedAtomicCounter {
179 explicit AnnotatedAtomicCounter(int val) : shared_(val) {}
181 void inc(AuxAct& auxfn) {
182 DSched::setAuxAct(auxfn);
183 /* calls the fine-grained original */
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);
196 return shared_.counter_.load_direct();
200 AtomicCounter<int, DeterministicAtomic> shared_;
203 using Annotated = AnnotatedAtomicCounter;
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 */
211 for (int v : auxdata.local_) {
215 VLOG(2) << "tid " << tid << " -- shared counter= " << val
216 << " -- sum increments= " << sum;
217 /* check invariant */
219 LOG(ERROR) << "counter=(" << val << ") expected(" << sum << ")";
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);
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");
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;
245 Annotated annotated(0);
246 AuxData auxdata(nthr);
247 DSched sched(DSched::uniform(seed));
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);
257 annotated.inc(auxfn);
262 for (auto& t : threads) {
265 EXPECT_EQ(annotated.load_direct(), nthr * niter);
268 int main(int argc, char** argv) {
269 testing::InitGoogleTest(&argc, argv);
270 gflags::ParseCommandLineFlags(&argc, &argv, true);
271 return RUN_ALL_TESTS();