-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyList.java
More file actions
337 lines (280 loc) · 7.27 KB
/
MyList.java
File metadata and controls
337 lines (280 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
class MyList {
public final static class HarrisList {
// retrieved from
// https://github.com/wala/MemSAT/blob/master/com.ibm.wala.memsat.testdata/source/data/concurrent/HarrisList.java
private final Node head, tail;
/**
* Constructs an empty Harris list.
*/
public HarrisList() {
head = new Node(Short.MIN_VALUE);
tail = new Node(Short.MAX_VALUE);
head.next.set(tail);
}
/**
* Adds the given key to this list if not already present.
* Returns true if the list was modified as a result of
* this operation; otherwise returns false.
* @return true if the list was modified as a result of
* this operation; otherwise returns false.
*/
public boolean insert(long key) {
Node newNode = new Node(key);
Node rightNode, leftNode;
do {
Pair pair = search(key);
leftNode = pair.fst;
rightNode = pair.snd;
if (rightNode != tail && rightNode.key==key) {
return false;
}
newNode.next = new AtomicReference/*<Node>*/(rightNode);
if (leftNode.next.compareAndSet(rightNode, newNode))
return true;
} while(true);
}
/**
* Removes the given key from this list, if present.
* Returns true if the list was modified as a result of
* this operation; otherwise returns false.
* @return true if the list was modified as a result of
* this operation; otherwise returns false.
*/
public boolean delete(long key) {
Node rightNode, rightNodeNext, leftNode;
do {
Pair pair = search(key);
leftNode = pair.fst;
rightNode = pair.snd;
if (rightNode==tail || rightNode.key!=key) {
return false;
}
rightNodeNext = rightNode.next();
if (!rightNode.isMarked()) {
if (rightNode.isMarked.compareAndSet(false, true)) {
break;
}
}
} while(true);
if (!leftNode.next.compareAndSet(rightNode, rightNodeNext)) {
search(rightNode.key);
}
return true;
}
public boolean find(long key) {
Pair pair = search(key);
Node right_node = pair.snd;
if (right_node == tail || right_node.key != key) {
return false;
}
return true;
}
/**
* Returns a pair of adjacent unmarked nodes
* such that the key of the left node is less
* than searchKey and the key of the right node
* is greater than or equal to searchKey.
* @return a pair of adjacent unmarked nodes
* such that the key of the left node is less
* than searchKey and the key of the right node
* is greater than or equal to searchKey.
*/
private Pair/*<Node, Node>*/ search(long searchKey) {
Node leftNode = null, leftNodeNext = null, rightNode = null;
search_again:
do {
Node t = head;
Node tNext = head.next();
do {
if (!t.isMarked.get()) {
leftNode = t;
leftNodeNext = t.next();
}
t = tNext;
if (t==tail) break;
tNext = t.next();
} while (t.isMarked() || t.key < searchKey);
rightNode = t;
if (leftNodeNext == rightNode) {
if (rightNode != tail && rightNode.isMarked())
continue search_again;
else
return new Pair(leftNode,rightNode);
}
if (leftNode.next.compareAndSet(leftNodeNext, rightNode)) {
if (rightNode != tail && rightNode.isMarked())
continue search_again;
else
return new Pair(leftNode,rightNode);
}
} while (true);
}
/**
* Node in the linked list.
*/
static final class Node {
long key;
AtomicReference/*<Node>*/ next;
AtomicBoolean isMarked;
Node(long key) {
this.key = key;
this.next = new AtomicReference/*<Node>*/();
this.isMarked = new AtomicBoolean(false);
}
boolean isMarked() { return isMarked.get(); }
Node next() { return (Node)next.get(); }
}
/**
* A pair of nodes.
* @author Emina Torlak
*/
static final class Pair {
final Node fst;
final Node snd;
Pair(Node fst, Node snd) {
this.fst = fst;
this.snd = snd;
}
}
}
static volatile int state = 0;
static class Random {
long seed;
public Random(long seed) {
this.seed = seed;
}
public long next() {
long x, hi, lo, t;
/*
* Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1).
* From "Random number generators: good ones are hard to find",
* Park and Miller, Communications of the ACM, vol. 31, no. 10,
* October 1988, p. 1195.
*/
x = seed;
hi = x / 127773;
lo = x % 127773;
t = 16807 * lo - 2836 * hi;
if (t <= 0) {
t += 0x7fffffff;
}
seed = t;
return t;
}
}
static class Agent implements Runnable {
static int id;
static int n_search = 100 - n_update;
static HarrisList list;
public Agent(int id, HarrisList list) {
this.id = id;
this.list = list;
}
@Override
public void run() {
Random rand = new Random(System.nanoTime());
int N = 100;
long r, key, action, ins_del;
long amask = (1<<10)-1;
while (state == 0) ;
while (state != 2) {
for (int i = 0; i < N; ++i) {
r = rand.next();
ins_del = r & 1;
r >>= 1;
action = ((r & amask) % 101);
r >>= 10;
key = r % n_keys;
if (action <= n_search) {
list.search(key);
} else if (ins_del == 0) {
list.delete(key);
} else {
list.insert(key);
}
}
MyList.ops[id] += N;
}
}
}
static Thread[] threads;
static int[] ops;
static int n_ms;
static int n_update;
static int n_elements;
static int n_threads;
static int n_keys;
public static void main(String[] args) throws Exception {
if (args.length != 4) {
System.out.println("Usage: nmilli nupdate nelements nthreads");
System.exit(-1);
}
n_ms = Integer.parseInt(args[0]);
n_update = Integer.parseInt(args[1]);
n_elements = Integer.parseInt(args[2]);
n_threads = Integer.parseInt(args[3]);
n_keys = n_elements * 2;
threads = new Thread[n_threads];
// warm up jvm
for (int j = 0; j < 5; ++j)
{
ops = new int[n_threads];
HarrisList list = new HarrisList();
for (int i = 0; i < n_elements; ++i) {
list.insert((long)i*2);
}
state = 0;
for (int i = 0; i < n_threads; ++i) {
threads[i] = new Thread(new Agent(i, list));
threads[i].start();
}
state = 1;
long start_time = System.nanoTime();
Thread.sleep(1000);
state = 2;
for (int i = 0; i < n_threads; ++i) {
threads[i].join();
}
long end_time = System.nanoTime();
long total_ops = 0;
for (int i = 0; i < n_threads; ++i) {
total_ops += ops[i];
}
int co = n_threads <= 64 ? n_threads : 1;
}
System.gc();
for (int j = 0; j < 1; ++j)
{
ops = new int[n_threads];
HarrisList list = new HarrisList();
for (int i = 0; i < n_elements; ++i) {
list.insert((long)i*2);
}
state = 0;
for (int i = 0; i < n_threads; ++i) {
threads[i] = new Thread(new Agent(i, list));
threads[i].start();
}
long start_time = System.nanoTime();
state = 1;
Thread.sleep(n_ms);
state = 2;
for (int i = 0; i < n_threads; ++i) {
threads[i].join();
}
long end_time = System.nanoTime();
long total_ops = 0;
for (int i = 0; i < n_threads; ++i) {
if (Long.MAX_VALUE - total_ops > ops[i]) {
total_ops += ops[i];
} else {
System.out.print("error");
}
}
int co = n_threads <= 64 ? n_threads : 1;
System.out.println(co * (end_time-start_time) / total_ops);
}
}
}