-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RecordingPageCacheTracer.java
396 lines (338 loc) · 9.34 KB
/
RecordingPageCacheTracer.java
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.io.pagecache;
import org.hamcrest.Matcher;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import org.neo4j.io.pagecache.tracing.EvictionEvent;
import org.neo4j.io.pagecache.tracing.EvictionRunEvent;
import org.neo4j.io.pagecache.tracing.FlushEventOpportunity;
import org.neo4j.io.pagecache.tracing.MajorFlushEvent;
import org.neo4j.io.pagecache.tracing.PageCacheTracer;
import org.neo4j.io.pagecache.tracing.PageFaultEvent;
import org.neo4j.io.pagecache.tracing.PinEvent;
public class RecordingPageCacheTracer implements PageCacheTracer
{
private final Set<Class<? extends Event>> eventTypesToTrace = new HashSet<>();
private final BlockingQueue<Event> record = new LinkedBlockingQueue<>();
private CountDownLatch trapLatch;
private Matcher<? extends Event> trap;
public RecordingPageCacheTracer()
{
this( Evict.class, Fault.class );
}
@SafeVarargs
public RecordingPageCacheTracer( Class<? extends Event>... eventTypesToTrace )
{
Collections.addAll( this.eventTypesToTrace, eventTypesToTrace );
}
private void pageFaulted( long filePageId, PageSwapper swapper )
{
record( new Fault( swapper, filePageId ) );
}
private void evicted( long filePageId, PageSwapper swapper )
{
record( new Evict( swapper, filePageId ) );
}
private void pinned( long filePageId, PageSwapper swapper )
{
record( new Pin( swapper, filePageId ) );
}
private void record( Event event )
{
if ( eventTypesToTrace.contains( event.getClass() ) )
{
record.add( event );
trip( event );
}
}
@Override
public void mappedFile( File file )
{
// we currently do not record these
}
@Override
public void unmappedFile( File file )
{
// we currently do not record these
}
@Override
public EvictionRunEvent beginPageEvictions( int pageCountToEvict )
{
return new EvictionRunEvent()
{
@Override
public EvictionEvent beginEviction()
{
return new RecordingEvictionEvent();
}
@Override
public void close()
{
}
};
}
@Override
public PinEvent beginPin( boolean writeLock, final long filePageId, final PageSwapper swapper )
{
return new PinEvent()
{
@Override
public void setCachePageId( int cachePageId )
{
}
@Override
public PageFaultEvent beginPageFault()
{
return new PageFaultEvent()
{
@Override
public void addBytesRead( long bytes )
{
}
@Override
public void done()
{
pageFaulted( filePageId, swapper );
}
@Override
public void done( Throwable throwable )
{
}
@Override
public EvictionEvent beginEviction()
{
return new RecordingEvictionEvent();
}
@Override
public void setCachePageId( int cachePageId )
{
}
};
}
@Override
public void done()
{
pinned( filePageId, swapper );
}
};
}
@Override
public MajorFlushEvent beginFileFlush( PageSwapper swapper )
{
return MajorFlushEvent.NULL;
}
@Override
public MajorFlushEvent beginCacheFlush()
{
return MajorFlushEvent.NULL;
}
@Override
public long faults()
{
return 0;
}
@Override
public long evictions()
{
return 0;
}
@Override
public long pins()
{
return 0;
}
@Override
public long unpins()
{
return 0;
}
@Override
public long flushes()
{
return 0;
}
@Override
public long bytesRead()
{
return 0;
}
@Override
public long bytesWritten()
{
return 0;
}
@Override
public long filesMapped()
{
return 0;
}
@Override
public long filesUnmapped()
{
return 0;
}
@Override
public long evictionExceptions()
{
return 0;
}
public <T extends Event> T observe( Class<T> type ) throws InterruptedException
{
return type.cast( record.take() );
}
public <T extends Event> T tryObserve( Class<T> type )
{
return type.cast( record.poll() );
}
/**
* Set a trap for the eviction thread, and return a CountDownLatch with a counter set to 1.
* When the eviction thread performs the given trap-event, it will block on the latch after
* making the event observable.
*/
public synchronized CountDownLatch trap( Matcher<? extends Event> trap )
{
assert trap != null;
trapLatch = new CountDownLatch( 1 );
this.trap = trap;
return trapLatch;
}
private void trip( Event event )
{
Matcher<? extends Event> theTrap;
CountDownLatch theTrapLatch;
// The synchronized block is in here, so we don't risk calling await on
// the trapLatch while holding the monitor lock.
synchronized ( this )
{
theTrap = trap;
theTrapLatch = trapLatch;
}
if ( theTrap != null && theTrap.matches( event ) )
{
try
{
theTrapLatch.await();
}
catch ( InterruptedException e )
{
Thread.currentThread().interrupt();
throw new RuntimeException( "Unexpected interrupt in RecordingMonitor", e );
}
}
}
public static abstract class Event
{
public final PageSwapper io;
public final long pageId;
public Event( PageSwapper io, long pageId )
{
this.io = io;
this.pageId = pageId;
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
Event event = (Event) o;
return pageId == event.pageId && !(io != null ? !io.equals( event.io ) : event.io != null);
}
@Override
public int hashCode()
{
int result = io != null ? io.hashCode() : 0;
result = 31 * result + (int) (pageId ^ (pageId >>> 32));
return result;
}
@Override
public String toString()
{
return String.format( "%s{io=%s, pageId=%s}",
getClass().getSimpleName(), io, pageId );
}
}
public static class Fault extends Event
{
private Fault( PageSwapper io, long pageId )
{
super( io, pageId );
}
}
public static class Evict extends Event
{
private Evict( PageSwapper io, long pageId )
{
super( io, pageId );
}
}
public static class Pin extends Event
{
private Pin( PageSwapper io, long pageId )
{
super( io, pageId );
}
}
private class RecordingEvictionEvent implements EvictionEvent
{
private long filePageId;
private PageSwapper swapper;
@Override
public void setFilePageId( long filePageId )
{
this.filePageId = filePageId;
}
@Override
public void setSwapper( PageSwapper swapper )
{
this.swapper = swapper;
}
@Override
public FlushEventOpportunity flushEventOpportunity()
{
return FlushEventOpportunity.NULL;
}
@Override
public void threwException( IOException exception )
{
}
@Override
public void setCachePageId( int cachePageId )
{
}
@Override
public void close()
{
evicted( filePageId, swapper );
}
}
}