-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sample_07_Periodic.java
80 lines (73 loc) · 2.18 KB
/
Sample_07_Periodic.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
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import jdk.jfr.DataAmount;
import jdk.jfr.Event;
import jdk.jfr.FlightRecorder;
import jdk.jfr.Label;
import jdk.jfr.Name;
import jdk.jfr.Period;
// Description
// -----------
//
// The sample shows how to create an event that is emitted periodically and
// how the period can be set using the Period annotation.
//
// Valid units for a period are: ns, us, ms, s, m, h, and d.
//
// It is also possible to specify:
//
// - "everyChunk"
// A periodic event will be emitted at least once in the recording
//
// - "beginChunk"
// A periodic event will be emitted in the beginning of a recording
//
// - "endChunk",
// A periodic event will be emitted in the end of a recording
//
// - "infinity",
// A periodic event will never be emitted
//
// How to run
// ----------
//
// $ java -XX:StartFlightRecording:filename=07.jfr Sample_07_Periodic.java
// $ jfr print --events FileStore 07.jfr
//
//
public class Sample_07_Periodic {
@Period("2 s")
@Label("File Store")
@Name("com.sample.FileStore")
static class FileStoreEvent extends Event {
@Label("Total")
@DataAmount(DataAmount.BYTES)
long total;
@Label("Used")
@DataAmount(DataAmount.BYTES)
long used;
@Label("Available")
@DataAmount(DataAmount.BYTES)
long available;
}
public static void main(String... args) throws Exception {
Runnable r = () -> {
for (FileStore s : FileSystems.getDefault().getFileStores()) {
try {
FileStoreEvent event = new FileStoreEvent();
event.total = s.getTotalSpace();
event.used = s.getTotalSpace() - s.getUnallocatedSpace();
event.available = s.getUsableSpace();
event.commit();
} catch (IOException ioe) {
// ignore
}
}
};
FlightRecorder.addPeriodicEvent(FileStoreEvent.class, r);
System.out.println("Wait for ten seconds...");
Thread.sleep(10_000);
FlightRecorder.removePeriodicEvent(r);
}
}