/
RemoteRecordingStream.java
660 lines (607 loc) · 22.5 KB
/
RemoteRecordingStream.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.management.jfr;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.security.AccessControlException;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import jdk.jfr.Configuration;
import jdk.jfr.EventSettings;
import jdk.jfr.EventType;
import jdk.jfr.Recording;
import jdk.jfr.RecordingState;
import jdk.jfr.consumer.EventStream;
import jdk.jfr.consumer.MetadataEvent;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordingStream;
import jdk.jfr.internal.management.EventSettingsModifier;
import jdk.jfr.internal.management.ManagementSupport;
import jdk.management.jfr.DiskRepository.DiskChunk;
import jdk.jfr.internal.management.EventByteStream;
/**
* An implementation of an {@link EventStream} that can serialize events over
* the network using an {@link MBeanServerConnection}.
* <p>
* The following example shows how to record garbage collection pauses and CPU
* usage on a remote host and print the events to standard out.
*
* <pre>
* {@literal
* String host = "com.example";
* int port = 4711;
*
* String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
*
* JMXServiceURL u = new JMXServiceURL(url);
* JMXConnector c = JMXConnectorFactory.connect(u);
* MBeanServerConnection conn = c.getMBeanServerConnection();
*
* try (var rs = new RemoteRecordingStream(conn)) {
* rs.enable("jdk.GCPhasePause").withoutThreshold();
* rs.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1));
* rs.onEvent("jdk.CPULoad", System.out::println);
* rs.onEvent("jdk.GCPhasePause", System.out::println);
* rs.start();
* }
* }
* </pre>
*
* @since 16
*/
public final class RemoteRecordingStream implements EventStream {
private static final String ENABLED = "enabled";
static final class RemoteSettings implements EventSettingsModifier {
private final FlightRecorderMXBean mbean;
private final long recordingId;
RemoteSettings(FlightRecorderMXBean mbean, long recordingId) {
this.mbean = mbean;
this.recordingId = recordingId;
}
@Override
public void with(String name, String value) {
Objects.requireNonNull(name);
Objects.requireNonNull(value);
// FlightRecorderMXBean implementation always returns
// new instance of Map so no need to create new here.
Map<String, String> newSettings = getEventSettings();
newSettings.put(name, value);
mbean.setRecordingSettings(recordingId, newSettings);
}
@Override
public Map<String, String> toMap() {
return getEventSettings();
}
private Map<String, String> getEventSettings() {
return mbean.getRecordingSettings(recordingId);
}
}
// Reference to stream is released when EventStream::close is called
static final class ChunkConsumer implements Consumer<Long> {
private final DiskRepository repository;
ChunkConsumer(DiskRepository repository) {
this.repository = repository;
}
@Override
public void accept(Long endNanos) {
repository.onChunkComplete(endNanos);
}
}
private static final ObjectName OBJECT_NAME = MBeanUtils.createObjectName();
final Path path;
final FlightRecorderMXBean mbean;
final long recordingId;
final EventStream stream;
@SuppressWarnings("removal")
final AccessControlContext accessControllerContext;
final DiskRepository repository;
final Instant creationTime;
final Object lock = new Object();
volatile Instant startTime;
volatile Instant endTime;
volatile boolean closed;
private boolean started; // always guarded by lock
/**
* Creates an event stream that operates against a {@link MBeanServerConnection}
* that has a registered {@link FlightRecorderMXBean}.
* <p>
* To configure event settings, use {@link #setSettings(Map)}.
*
* @param connection the {@code MBeanServerConnection} where the
* {@code FlightRecorderMXBean} is registered, not
* {@code null}
*
* @throws IOException if a stream can't be opened, an I/O error occurs
* when trying to access the repository or the
* {@code FlightRecorderMXBean}
*
* @throws SecurityException if a security manager exists and its
* {@code checkRead} method denies read access to the
* directory, or files in the directory.
*/
public RemoteRecordingStream(MBeanServerConnection connection) throws IOException {
this(connection, makeTempDirectory(), true);
}
/**
* Creates an event stream that operates against a {@link MBeanServerConnection}
* that has a registered {@link FlightRecorderMXBean}.
* <p>
* To configure event settings, use {@link #setSettings(Map)}.
*
* @param connection the {@code MBeanServerConnection} where the
* {@code FlightRecorderMXBean} is registered, not
* {@code null}
*
* @param directory the directory to store event data that is downloaded, not
* {@code null}
*
* @throws IOException if a stream can't be opened, an I/O error occurs
* when trying to access the repository or the
* {@code FlightRecorderMXBean}
*
* @throws SecurityException if a security manager exists and its
* {@code checkRead} method denies read access to the
* directory, or files in the directory.
*/
public RemoteRecordingStream(MBeanServerConnection connection, Path directory) throws IOException {
this(connection, directory, false);
}
@SuppressWarnings("removal")
private RemoteRecordingStream(MBeanServerConnection connection, Path dir, boolean delete) throws IOException {
Objects.requireNonNull(connection);
Objects.requireNonNull(dir);
accessControllerContext = AccessController.getContext();
// Make sure users can't implement malicious version of a Path object.
path = Paths.get(dir.toString());
if (!Files.exists(path)) {
throw new IOException("Download directory doesn't exist");
}
if (!Files.isDirectory(path)) {
throw new IOException("Download location must be a directory");
}
checkFileAccess(path);
creationTime = Instant.now();
mbean = createProxy(connection);
recordingId = createRecording();
stream = ManagementSupport.newEventDirectoryStream(accessControllerContext, path, configurations(mbean));
stream.setStartTime(Instant.MIN);
repository = new DiskRepository(path, delete);
ManagementSupport.setOnChunkCompleteHandler(stream, new ChunkConsumer(repository));
}
private List<Configuration> configurations(FlightRecorderMXBean mbean) {
List<ConfigurationInfo> cis = mbean.getConfigurations();
List<Configuration> confs = new ArrayList<>(cis.size());
for (ConfigurationInfo ci : cis) {
confs.add(ManagementSupport.newConfiguration(ci.getName(), ci.getLabel(), ci.getDescription(),
ci.getProvider(), ci.getSettings(), ci.getContents()));
}
return Collections.unmodifiableList(confs);
}
@Override
public void onMetadata(Consumer<MetadataEvent> action) {
stream.onMetadata(action);
}
private static void checkFileAccess(Path directory) throws IOException {
RandomAccessFile f = null;
try {
Path testFile = directory.resolve("test-access");
f = new RandomAccessFile(testFile.toFile(), "rw");
f.write(0);
f.seek(0);
f.read();
f.close();
Files.delete(testFile);
} catch (Exception e) {
closeSilently(f);
throw new IOException("Could not read/write/delete in directory" + directory + " :" + e.getMessage());
}
}
private static void closeSilently(RandomAccessFile f) {
if (f == null) {
return;
}
try {
f.close();
} catch (IOException ioe) {
// ignore
}
}
private static FlightRecorderMXBean createProxy(MBeanServerConnection connection) throws IOException {
try {
return JMX.newMXBeanProxy(connection, OBJECT_NAME, FlightRecorderMXBean.class);
} catch (Exception e) {
throw new IOException("Could not create proxy for FlightRecorderMXBean: " + e.getMessage(), e);
}
}
private long createRecording() throws IOException {
try {
long id = mbean.newRecording();
Map<String, String> options = new HashMap<>();
options.put("name", EventByteStream.NAME + ": " + creationTime);
mbean.setRecordingOptions(id, options);
return id;
} catch (Exception e) {
throw new IOException("Could not create new recording: " + e.getMessage(), e);
}
}
private Map<String, String> getRecordingOptions() throws IOException {
try {
return mbean.getRecordingOptions(recordingId);
} catch (Exception e) {
throw new IOException("Could not get recording options: " + e.getMessage(), e);
}
}
/**
* Replaces all settings for this recording stream.
* <p>
* The following example connects to a remote host and stream events using
* settings from the "default" configuration.
*
* <pre>
* {
* {@literal
*
* String host = "com.example";
* int port = 4711;
*
* String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
*
* JMXServiceURL u = new JMXServiceURL(url);
* JMXConnector c = JMXConnectorFactory.connect(u);
* MBeanServerConnection conn = c.getMBeanServerConnection();
*
* try (final var rs = new RemoteRecordingStream(conn)) {
* rs.onMetadata(e -> {
* for (Configuration c : e.getConfigurations()) {
* if (c.getName().equals("default")) {
* rs.setSettings(c.getSettings());
* }
* }
* });
* rs.onEvent(System.out::println);
* rs.start();
* }
*
* }
* </pre>
*
* @param settings the settings to set, not {@code null}
*
* @see Recording#setSettings(Map)
*/
public void setSettings(Map<String, String> settings) {
Objects.requireNonNull(settings);
try {
mbean.setRecordingSettings(recordingId, settings);
} catch (Exception e) {
ManagementSupport.logDebug(e.getMessage());
close();
}
};
/**
* Disables event with the specified name.
* <p>
* If multiple events with same name (for example, the same class is loaded in
* different class loaders), then all events that match the name are disabled.
*
* @param name the settings for the event, not {@code null}
*
* @return an event setting for further configuration, not {@code null}
*
*/
public EventSettings disable(String name) {
Objects.requireNonNull(name);
EventSettings s = ManagementSupport.newEventSettings(new RemoteSettings(mbean, recordingId));
try {
return s.with(name + "#" + ENABLED, "false");
} catch (Exception e) {
ManagementSupport.logDebug(e.getMessage());
close();
return s;
}
}
/**
* Enables the event with the specified name.
* <p>
* If multiple events have the same name (for example, the same class is loaded
* in different class loaders), then all events that match the name are enabled.
*
* @param name the settings for the event, not {@code null}
*
* @return an event setting for further configuration, not {@code null}
*
* @see EventType
*/
public EventSettings enable(String name) {
Objects.requireNonNull(name);
EventSettings s = ManagementSupport.newEventSettings(new RemoteSettings(mbean, recordingId));
try {
return s.with(name + "#" + ENABLED, "true");
} catch (Exception e) {
ManagementSupport.logDebug(e.getMessage());
close();
return s;
}
}
/**
* Determines how far back data is kept for the stream.
* <p>
* To control the amount of recording data stored on disk, the maximum length of
* time to retain the data can be specified. Data stored on disk that is older
* than the specified length of time is removed by the Java Virtual Machine
* (JVM).
* <p>
* If neither maximum limit or the maximum age is set, the size of the recording
* may grow indefinitely if events are not consumed.
*
* @param maxAge the length of time that data is kept, or {@code null} if
* infinite
*
* @throws IllegalArgumentException if {@code maxAge} is negative
*
* @throws IllegalStateException if the recording is in the {@code CLOSED}
* state
*/
public void setMaxAge(Duration maxAge) {
Objects.requireNonNull(maxAge);
repository.setMaxAge(maxAge);
}
/**
* Determines how much data is kept for the stream.
* <p>
* To control the amount of recording data that is stored on disk, the maximum
* amount of data to retain can be specified. When the maximum limit is
* exceeded, the Java Virtual Machine (JVM) removes the oldest chunk to make
* room for a more recent chunk.
* <p>
* If neither maximum limit or the maximum age is set, the size of the recording
* may grow indefinitely if events are not consumed.
* <p>
* The size is measured in bytes.
*
* @param maxSize the amount of data to retain, {@code 0} if infinite
*
* @throws IllegalArgumentException if {@code maxSize} is negative
*
* @throws IllegalStateException if the recording is in {@code CLOSED} state
*/
public void setMaxSize(long maxSize) {
if (maxSize < 0) {
throw new IllegalArgumentException("Max size of recording can't be negative");
}
repository.setMaxSize(maxSize);
}
@Override
public void onEvent(Consumer<RecordedEvent> action) {
stream.onEvent(action);
}
@Override
public void onEvent(String eventName, Consumer<RecordedEvent> action) {
stream.onEvent(eventName, action);
}
@Override
public void onFlush(Runnable action) {
stream.onFlush(action);
}
@Override
public void onError(Consumer<Throwable> action) {
stream.onError(action);
}
@Override
public void onClose(Runnable action) {
stream.onClose(action);
}
@Override
public void close() {
synchronized (lock) { // ensure one closer
if (closed) {
return;
}
closed = true;
}
ManagementSupport.setOnChunkCompleteHandler(stream, null);
stream.close();
try {
mbean.closeRecording(recordingId);
} catch (IOException e) {
ManagementSupport.logDebug(e.getMessage());
}
try {
repository.close();
} catch (IOException e) {
ManagementSupport.logDebug(e.getMessage());
}
}
@Override
public boolean remove(Object action) {
return stream.remove(action);
}
@Override
public void setReuse(boolean reuse) {
stream.setReuse(reuse);
}
@Override
public void setOrdered(boolean ordered) {
stream.setOrdered(ordered);
}
@Override
public void setStartTime(Instant startTime) {
stream.setStartTime(startTime);
this.startTime = startTime;
}
@Override
public void setEndTime(Instant endTime) {
stream.setEndTime(endTime);
this.endTime = endTime;
}
@Override
public void start() {
synchronized (lock) { // ensure one starter
ensureStartable();
try {
try {
mbean.startRecording(recordingId);
} catch (IllegalStateException ise) {
throw ise;
}
startDownload();
} catch (Exception e) {
ManagementSupport.logDebug(e.getMessage());
close();
return;
}
stream.start();
started = true;
}
}
@Override
public void startAsync() {
synchronized (lock) { // ensure one starter
ensureStartable();
stream.startAsync();
try {
mbean.startRecording(recordingId);
startDownload();
} catch (Exception e) {
ManagementSupport.logDebug(e.getMessage());
close();
}
started = true;
}
}
private void ensureStartable() {
if (closed) {
throw new IllegalStateException("Event stream is closed");
}
if (started) {
throw new IllegalStateException("Event stream can only be started once");
}
}
/**
* Writes recording data to a file.
* <p>
* The recording stream must be started, but not closed.
* <p>
* It's highly recommended that a max age or max size is set before
* starting the stream. Otherwise, the dump may not contain any events.
*
* @param destination the location where recording data is written, not
* {@code null}
*
* @throws IOException if the recording data can't be copied to the specified
* location, or if the stream is closed, or not started.
*
* @throws SecurityException if a security manager exists and the caller doesn't
* have {@code FilePermission} to write to the destination path
*
* @see RemoteRecordingStream#setMaxAge(Duration)
* @see RemoteRecordingStream#setMaxSize(long)
*
* @since 17
*/
public void dump(Path destination) throws IOException {
Objects.requireNonNull(destination);
long id = -1;
try {
FileDump fileDump;
synchronized (lock) { // ensure running state while preparing dump
if (closed) {
throw new IOException("Recording stream has been closed, no content to write");
}
if (!started) {
throw new IOException("Recording stream has not been started, no content to write");
}
// Take repository lock to prevent new data to be flushed
// client-side after clone has been created on the server.
synchronized (repository) {
id = mbean.cloneRecording(recordingId, true);
RecordingInfo ri = getRecordingInfo(mbean.getRecordings(), id);
fileDump = repository.newDump(ri.getStopTime());
}
}
// Write outside lock
fileDump.write(destination);
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
ManagementSupport.logDebug(e.getMessage());
close();
} finally {
if (id != -1) {
try {
mbean.closeRecording(id);
} catch (Exception e) {
ManagementSupport.logDebug(e.getMessage());
close();
}
}
}
}
private RecordingInfo getRecordingInfo(List<RecordingInfo> infos, long id) throws IOException {
for (RecordingInfo info : infos) {
if (info.getId() == id) {
return info;
}
}
throw new IOException("Unable to find id of dumped recording");
}
@Override
public void awaitTermination(Duration timeout) throws InterruptedException {
stream.awaitTermination(timeout);
}
@Override
public void awaitTermination() throws InterruptedException {
stream.awaitTermination();
}
private static Path makeTempDirectory() throws IOException {
return Files.createTempDirectory("jfr-streaming");
}
private void startDownload() {
String name = "JFR: Download Thread " + creationTime;
Thread downLoadThread = new DownLoadThread(this, name);
downLoadThread.start();
}
boolean isClosed() {
return closed;
}
}