57361
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.jfr.consumer;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.nio.file.DirectoryStream;
|
|
30 |
import java.nio.file.Files;
|
|
31 |
import java.nio.file.Path;
|
|
32 |
import java.util.ArrayList;
|
|
33 |
import java.util.Collections;
|
|
34 |
import java.util.HashMap;
|
|
35 |
import java.util.HashSet;
|
|
36 |
import java.util.List;
|
|
37 |
import java.util.Map;
|
|
38 |
import java.util.Set;
|
|
39 |
import java.util.SortedMap;
|
|
40 |
import java.util.TreeMap;
|
|
41 |
|
|
42 |
import jdk.jfr.internal.LogLevel;
|
|
43 |
import jdk.jfr.internal.LogTag;
|
|
44 |
import jdk.jfr.internal.Logger;
|
|
45 |
import jdk.jfr.internal.Repository;
|
|
46 |
import jdk.jfr.internal.consumer.ChunkHeader;
|
|
47 |
|
|
48 |
/**
|
|
49 |
* This class corresponds to a disk repository.
|
|
50 |
* <p>
|
|
51 |
* Main purpose is to act as a cache if multiple {@code EventStream} want to
|
|
52 |
* access the same repository. An {@code EventSetLocation} should be released
|
|
53 |
* when it is no longer being used.
|
|
54 |
*
|
|
55 |
*/
|
|
56 |
final class EventSetLocation {
|
|
57 |
private static Map<Path, EventSetLocation> locations = new HashMap<>();
|
|
58 |
|
|
59 |
private final SortedMap<Long, EventSet> eventSets = new TreeMap<>();
|
|
60 |
private final Map<Path, Long> lastPaths = new HashMap<>();
|
|
61 |
|
|
62 |
final Path path;
|
|
63 |
private int count = 0;
|
|
64 |
private volatile boolean closed;
|
|
65 |
|
|
66 |
private EventSetLocation(Path path) {
|
|
67 |
this.path = path;
|
|
68 |
}
|
|
69 |
|
|
70 |
public static EventSetLocation get(Path absolutPath) {
|
|
71 |
synchronized (locations) {
|
|
72 |
EventSetLocation esl = locations.get(absolutPath);
|
|
73 |
if (esl == null) {
|
|
74 |
esl = new EventSetLocation(absolutPath);
|
|
75 |
locations.put(absolutPath, esl);
|
|
76 |
}
|
|
77 |
esl.count++;
|
|
78 |
return esl;
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
public static EventSetLocation current() throws IOException {
|
|
83 |
Repository.getRepository().ensureRepository();
|
|
84 |
return get(Repository.getRepository().getRepositoryPath().toPath());
|
|
85 |
}
|
|
86 |
|
|
87 |
public void release() {
|
|
88 |
synchronized (locations) {
|
|
89 |
count--;
|
|
90 |
if (count == 0) {
|
|
91 |
locations.remove(path);
|
|
92 |
closed = true;
|
|
93 |
}
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
public synchronized EventSet acquire(long startTimeNanos, EventSet previousEventSet) {
|
|
98 |
synchronized (eventSets) {
|
|
99 |
while (!closed) {
|
|
100 |
SortedMap<Long, EventSet> after = eventSets.tailMap(startTimeNanos);
|
|
101 |
if (!after.isEmpty()) {
|
|
102 |
EventSet es = after.get(after.firstKey());
|
|
103 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Acquired " + startTimeNanos + ", got " + es);
|
|
104 |
es.acquire();
|
|
105 |
return es;
|
|
106 |
}
|
|
107 |
try {
|
|
108 |
updateEventSets(previousEventSet);
|
|
109 |
} catch (IOException e) {
|
|
110 |
e.printStackTrace();
|
|
111 |
// This can happen if a chunk is being removed
|
|
112 |
// between the file was discovered and an instance
|
|
113 |
// of an EventSet was constructed. Just ignore,
|
|
114 |
// and retry later.
|
|
115 |
}
|
|
116 |
try {
|
|
117 |
eventSets.wait(1000);
|
|
118 |
} catch (InterruptedException e) {
|
|
119 |
// ignore
|
|
120 |
}
|
|
121 |
}
|
|
122 |
}
|
|
123 |
return null;
|
|
124 |
}
|
|
125 |
|
|
126 |
private void updateEventSets(EventSet previousEventSet) throws IOException {
|
|
127 |
List<Path> added = new ArrayList<>();
|
|
128 |
Set<Path> current = new HashSet<>();
|
|
129 |
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path, "*.jfr")) {
|
|
130 |
for (Path p : dirStream) {
|
|
131 |
if (!lastPaths.containsKey(p)) {
|
|
132 |
added.add(p);
|
|
133 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "New file found: " + p.toAbsolutePath());
|
|
134 |
}
|
|
135 |
current.add(p);
|
|
136 |
}
|
|
137 |
}
|
|
138 |
List<Path> removed = new ArrayList<>();
|
|
139 |
for (Path p : lastPaths.keySet()) {
|
|
140 |
if (!current.contains(p)) {
|
|
141 |
removed.add(p);
|
|
142 |
}
|
|
143 |
}
|
|
144 |
|
|
145 |
for (Path remove : removed) {
|
|
146 |
Long time = lastPaths.get(remove);
|
|
147 |
eventSets.remove(time);
|
|
148 |
lastPaths.remove(remove);
|
|
149 |
}
|
|
150 |
Collections.sort(added, (p1,p2) -> p1.compareTo(p2));
|
|
151 |
for (Path p : added) {
|
|
152 |
// Only add files that have a complete header
|
|
153 |
// as the JVM may be in progress writing the file
|
|
154 |
long size = Files.size(p);
|
|
155 |
if (size >= ChunkHeader.HEADER_SIZE) {
|
|
156 |
EventSet es = new EventSet(this, previousEventSet, p);
|
|
157 |
long startTime = es.getStartTimeNanos();
|
|
158 |
if (startTime == 0) {
|
|
159 |
String errorMsg = "Chunk header should always contain a valid start time";
|
|
160 |
System.err.println(errorMsg);
|
|
161 |
throw new InternalError(errorMsg);
|
|
162 |
}
|
|
163 |
EventSet previous = eventSets.get(startTime);
|
|
164 |
if (previous != null) {
|
|
165 |
String errorMsg = "Found chunk " + p + " with the same start time " + startTime + " as previous chunk " + previous.getPath();
|
|
166 |
System.err.println(errorMsg);
|
|
167 |
throw new InternalError(errorMsg);
|
|
168 |
}
|
|
169 |
eventSets.put(startTime, es);
|
|
170 |
lastPaths.put(p, startTime);
|
|
171 |
previousEventSet = es;
|
|
172 |
}
|
|
173 |
}
|
|
174 |
}
|
|
175 |
}
|