|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: sw=4 ts=4 et : |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "TestHarness.h" |
|
8 |
|
9 //#define OLD_API |
|
10 |
|
11 #define PASS() \ |
|
12 do { \ |
|
13 passed(__FUNCTION__); \ |
|
14 return NS_OK; \ |
|
15 } while (0) |
|
16 |
|
17 #define FAIL(why) \ |
|
18 do { \ |
|
19 fail("%s | %s - %s", __FILE__, __FUNCTION__, why); \ |
|
20 return NS_ERROR_FAILURE; \ |
|
21 } while (0) |
|
22 |
|
23 #ifdef OLD_API |
|
24 # include "nsAutoLock.h" |
|
25 typedef PRLock* moz_lock_t; |
|
26 # define NEWLOCK(n) nsAutoLock::NewLock(n) |
|
27 # define DELETELOCK(v) nsAutoLock::DestroyLock(v) |
|
28 # define AUTOLOCK(v, l) nsAutoLock v(l) |
|
29 #else |
|
30 # include "mozilla/Mutex.h" |
|
31 typedef mozilla::Mutex* moz_lock_t; |
|
32 # define NEWLOCK(n) new mozilla::Mutex(n) |
|
33 # define DELETELOCK(v) delete (v) |
|
34 # define AUTOLOCK(v, l) mozilla::MutexAutoLock v(*l) |
|
35 #endif |
|
36 |
|
37 // def/undef these to run particular tests. |
|
38 #undef DD_TEST1 |
|
39 #undef DD_TEST2 |
|
40 #undef DD_TEST3 |
|
41 |
|
42 //----------------------------------------------------------------------------- |
|
43 |
|
44 #ifdef DD_TEST1 |
|
45 |
|
46 static void |
|
47 AllocLockRecurseUnlockFree(int i) |
|
48 { |
|
49 if (0 == i) |
|
50 return; |
|
51 |
|
52 moz_lock_t lock = NEWLOCK("deadlockDetector.scalability.t1"); |
|
53 { |
|
54 AUTOLOCK(_, lock); |
|
55 AllocLockRecurseUnlockFree(i - 1); |
|
56 } |
|
57 DELETELOCK(lock); |
|
58 } |
|
59 |
|
60 // This test creates a resource dependency chain N elements long, then |
|
61 // frees all the resources in the chain. |
|
62 static nsresult |
|
63 LengthNDepChain(int N) |
|
64 { |
|
65 AllocLockRecurseUnlockFree(N); |
|
66 PASS(); |
|
67 } |
|
68 |
|
69 #endif |
|
70 |
|
71 //----------------------------------------------------------------------------- |
|
72 |
|
73 #ifdef DD_TEST2 |
|
74 |
|
75 // This test creates a single lock that is ordered < N resources, then |
|
76 // repeatedly exercises this order k times. |
|
77 static nsresult |
|
78 OneLockNDeps(const int N, const int K) |
|
79 { |
|
80 moz_lock_t lock = NEWLOCK("deadlockDetector.scalability.t2.master"); |
|
81 moz_lock_t* locks = new moz_lock_t[N]; |
|
82 if (!locks) |
|
83 NS_RUNTIMEABORT("couldn't allocate lock array"); |
|
84 |
|
85 for (int i = 0; i < N; ++i) |
|
86 locks[i] = |
|
87 NEWLOCK("deadlockDetector.scalability.t2.dep"); |
|
88 |
|
89 // establish orders |
|
90 {AUTOLOCK(m, lock); |
|
91 for (int i = 0; i < N; ++i) |
|
92 AUTOLOCK(s, locks[i]); |
|
93 } |
|
94 |
|
95 // exercise order check |
|
96 {AUTOLOCK(m, lock); |
|
97 for (int i = 0; i < K; ++i) |
|
98 for (int j = 0; j < N; ++j) |
|
99 AUTOLOCK(s, locks[i]); |
|
100 } |
|
101 |
|
102 for (int i = 0; i < N; ++i) |
|
103 DELETELOCK(locks[i]); |
|
104 delete[] locks; |
|
105 |
|
106 PASS(); |
|
107 } |
|
108 |
|
109 #endif |
|
110 |
|
111 //----------------------------------------------------------------------------- |
|
112 |
|
113 #ifdef DD_TEST3 |
|
114 |
|
115 // This test creates N resources and adds the theoretical maximum number |
|
116 // of dependencies, O(N^2). It then repeats that sequence of |
|
117 // acquisitions k times. Finally, all resources are freed. |
|
118 // |
|
119 // It's very difficult to perform well on this test. It's put forth as a |
|
120 // challenge problem. |
|
121 |
|
122 static nsresult |
|
123 MaxDepsNsq(const int N, const int K) |
|
124 { |
|
125 moz_lock_t* locks = new moz_lock_t[N]; |
|
126 if (!locks) |
|
127 NS_RUNTIMEABORT("couldn't allocate lock array"); |
|
128 |
|
129 for (int i = 0; i < N; ++i) |
|
130 locks[i] = NEWLOCK("deadlockDetector.scalability.t3"); |
|
131 |
|
132 for (int i = 0; i < N; ++i) { |
|
133 AUTOLOCK(al1, locks[i]); |
|
134 for (int j = i+1; j < N; ++j) |
|
135 AUTOLOCK(al2, locks[j]); |
|
136 } |
|
137 |
|
138 for (int i = 0; i < K; ++i) { |
|
139 for (int j = 0; j < N; ++j) { |
|
140 AUTOLOCK(al1, locks[j]); |
|
141 for (int k = j+1; k < N; ++k) |
|
142 AUTOLOCK(al2, locks[k]); |
|
143 } |
|
144 } |
|
145 |
|
146 for (int i = 0; i < N; ++i) |
|
147 DELETELOCK(locks[i]); |
|
148 delete[] locks; |
|
149 |
|
150 PASS(); |
|
151 } |
|
152 |
|
153 #endif |
|
154 |
|
155 //----------------------------------------------------------------------------- |
|
156 |
|
157 int |
|
158 main(int argc, char** argv) |
|
159 { |
|
160 ScopedXPCOM xpcom("Deadlock detector scalability (" __FILE__ ")"); |
|
161 if (xpcom.failed()) |
|
162 return 1; |
|
163 |
|
164 int rv = 0; |
|
165 |
|
166 // Uncomment these tests to run them. Not expected to be common. |
|
167 |
|
168 #ifndef DD_TEST1 |
|
169 puts("Skipping not-requested LengthNDepChain() test"); |
|
170 #else |
|
171 if (NS_FAILED(LengthNDepChain(1 << 14))) // 16K |
|
172 rv = 1; |
|
173 #endif |
|
174 |
|
175 #ifndef DD_TEST2 |
|
176 puts("Skipping not-requested OneLockNDeps() test"); |
|
177 #else |
|
178 if (NS_FAILED(OneLockNDeps(1 << 14, 100))) // 16k |
|
179 rv = 1; |
|
180 #endif |
|
181 |
|
182 #ifndef DD_TEST3 |
|
183 puts("Skipping not-requested MaxDepsNsq() test"); |
|
184 #else |
|
185 if (NS_FAILED(MaxDepsNsq(1 << 10, 10))) // 1k |
|
186 rv = 1; |
|
187 #endif |
|
188 |
|
189 return rv; |
|
190 } |