author | egahlin |
Mon, 21 Oct 2019 18:25:56 +0200 | |
branch | JEP-349-branch |
changeset 58719 | be37d9dc96d1 |
parent 58200 | 2d147d680311 |
child 58806 | a7d850b47b19 |
permissions | -rw-r--r-- |
57434 | 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 |
*/ |
|
58200 | 25 |
|
57433 | 26 |
package jdk.jfr.internal.consumer; |
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.file.DirectoryStream; |
|
30 |
import java.nio.file.Path; |
|
31 |
import java.util.ArrayList; |
|
32 |
import java.util.Collections; |
|
33 |
import java.util.HashMap; |
|
34 |
import java.util.HashSet; |
|
35 |
import java.util.List; |
|
36 |
import java.util.Map; |
|
57449 | 37 |
import java.util.NavigableMap; |
57433 | 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; |
|
57628 | 46 |
import jdk.jfr.internal.SecuritySupport.SafePath; |
57433 | 47 |
|
48 |
public final class RepositoryFiles { |
|
57946 | 49 |
private static final Object WAIT_OBJECT = new Object(); |
50 |
public static void notifyNewFile() { |
|
51 |
synchronized (WAIT_OBJECT) { |
|
52 |
WAIT_OBJECT.notifyAll(); |
|
53 |
} |
|
54 |
} |
|
55 |
||
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57640
diff
changeset
|
56 |
private final FileAccess fileAccess; |
57449 | 57 |
private final NavigableMap<Long, Path> pathSet = new TreeMap<>(); |
57433 | 58 |
private final Map<Path, Long> pathLookup = new HashMap<>(); |
57754 | 59 |
private final Path repository; |
58200 | 60 |
private final Object waitObject; |
61 |
||
57433 | 62 |
private volatile boolean closed; |
63 |
||
58197 | 64 |
RepositoryFiles(FileAccess fileAccess, Path repository) { |
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
65 |
this.repository = repository; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57640
diff
changeset
|
66 |
this.fileAccess = fileAccess; |
57946 | 67 |
this.waitObject = repository == null ? WAIT_OBJECT : new Object(); |
57433 | 68 |
} |
69 |
||
58197 | 70 |
long getTimestamp(Path p) { |
57433 | 71 |
return pathLookup.get(p); |
72 |
} |
|
73 |
||
58197 | 74 |
Path lastPath() { |
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
75 |
if (waitForPaths()) { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
76 |
return pathSet.lastEntry().getValue(); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
77 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
78 |
return null; // closed |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
79 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
80 |
|
58197 | 81 |
Path firstPath(long startTimeNanos) { |
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
82 |
if (waitForPaths()) { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
83 |
// Pick closest chunk before timestamp |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
84 |
Long time = pathSet.floorKey(startTimeNanos); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
85 |
if (time != null) { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
86 |
startTimeNanos = time; |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
87 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
88 |
return path(startTimeNanos); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
89 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
90 |
return null; // closed |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
91 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
92 |
|
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
93 |
private boolean waitForPaths() { |
57640
46a77fccd251
Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents:
57638
diff
changeset
|
94 |
while (!closed) { |
46a77fccd251
Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents:
57638
diff
changeset
|
95 |
try { |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57640
diff
changeset
|
96 |
if (updatePaths()) { |
57640
46a77fccd251
Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents:
57638
diff
changeset
|
97 |
break; |
46a77fccd251
Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents:
57638
diff
changeset
|
98 |
} |
46a77fccd251
Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents:
57638
diff
changeset
|
99 |
} catch (IOException e) { |
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
100 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "IOException during repository file scan " + e.getMessage()); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
101 |
// This can happen if a chunk is being removed |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
102 |
// between the file was discovered and an instance |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
103 |
// was accessed, or if new file has been written yet |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
104 |
// Just ignore, and retry later. |
57640
46a77fccd251
Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents:
57638
diff
changeset
|
105 |
} |
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
106 |
nap(); |
57640
46a77fccd251
Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents:
57638
diff
changeset
|
107 |
} |
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
108 |
return !closed; |
57449 | 109 |
} |
110 |
||
58197 | 111 |
Path nextPath(long startTimeNanos) { |
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
112 |
return path(startTimeNanos); |
57640
46a77fccd251
Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents:
57638
diff
changeset
|
113 |
} |
46a77fccd251
Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents:
57638
diff
changeset
|
114 |
|
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
115 |
private Path path(long timestamp) { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
116 |
if (closed) { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
117 |
return null; |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
118 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
119 |
while (true) { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
120 |
SortedMap<Long, Path> after = pathSet.tailMap(timestamp); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
121 |
if (!after.isEmpty()) { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
122 |
Path path = after.get(after.firstKey()); |
57920 | 123 |
if (Logger.shouldLog(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE)) { |
124 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Return path " + path + " for start time nanos " + timestamp); |
|
125 |
} |
|
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
126 |
return path; |
57433 | 127 |
} |
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
128 |
if (!waitForPaths()) { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
129 |
return null; // closed |
57433 | 130 |
} |
131 |
} |
|
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
132 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
133 |
|
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
134 |
private void nap() { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
135 |
try { |
57946 | 136 |
synchronized (waitObject) { |
137 |
waitObject.wait(1000); |
|
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
138 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
139 |
} catch (InterruptedException e) { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
140 |
// ignore |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57690
diff
changeset
|
141 |
} |
57433 | 142 |
} |
143 |
||
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57640
diff
changeset
|
144 |
private boolean updatePaths() throws IOException { |
57754 | 145 |
boolean foundNew = false; |
146 |
Path repoPath = repository; |
|
147 |
if (repoPath == null) { |
|
148 |
// Always get the latest repository if 'jcmd JFR.configure |
|
149 |
// repositorypath=...' has been executed |
|
150 |
SafePath sf = Repository.getRepository().getRepositoryPath(); |
|
151 |
if (sf == null) { |
|
152 |
return false; // not initialized |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57640
diff
changeset
|
153 |
} |
57754 | 154 |
repoPath = sf.toPath(); |
57433 | 155 |
} |
57754 | 156 |
|
157 |
try (DirectoryStream<Path> dirStream = fileAccess.newDirectoryStream(repoPath)) { |
|
158 |
List<Path> added = new ArrayList<>(); |
|
159 |
Set<Path> current = new HashSet<>(); |
|
57433 | 160 |
for (Path p : dirStream) { |
161 |
if (!pathLookup.containsKey(p)) { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
162 |
String s = p.toString(); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
163 |
if (s.endsWith(".jfr")) { |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
164 |
added.add(p); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
165 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "New file found: " + p.toAbsolutePath()); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
166 |
} |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
167 |
current.add(p); |
57433 | 168 |
} |
169 |
} |
|
57754 | 170 |
List<Path> removed = new ArrayList<>(); |
171 |
for (Path p : pathLookup.keySet()) { |
|
172 |
if (!current.contains(p)) { |
|
173 |
removed.add(p); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
for (Path remove : removed) { |
|
178 |
Long time = pathLookup.get(remove); |
|
179 |
pathSet.remove(time); |
|
180 |
pathLookup.remove(remove); |
|
57433 | 181 |
} |
57754 | 182 |
Collections.sort(added, (p1, p2) -> p1.compareTo(p2)); |
183 |
for (Path p : added) { |
|
184 |
// Only add files that have a complete header |
|
185 |
// as the JVM may be in progress writing the file |
|
186 |
long size = fileAccess.fileSize(p); |
|
58197 | 187 |
if (size >= ChunkHeader.headerSize()) { |
57754 | 188 |
long startNanos = readStartTime(p); |
189 |
pathSet.put(startNanos, p); |
|
190 |
pathLookup.put(p, startNanos); |
|
191 |
foundNew = true; |
|
192 |
} |
|
193 |
} |
|
194 |
return foundNew; |
|
57433 | 195 |
} |
196 |
} |
|
197 |
||
198 |
private long readStartTime(Path p) throws IOException { |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57640
diff
changeset
|
199 |
try (RecordingInput in = new RecordingInput(p.toFile(), fileAccess, 100)) { |
58719 | 200 |
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Parsing header for chunk start time"); |
57433 | 201 |
ChunkHeader c = new ChunkHeader(in); |
202 |
return c.getStartNanos(); |
|
203 |
} |
|
204 |
} |
|
205 |
||
58197 | 206 |
void close() { |
57946 | 207 |
synchronized (waitObject) { |
57433 | 208 |
this.closed = true; |
57946 | 209 |
waitObject.notify(); |
57433 | 210 |
} |
211 |
} |
|
57946 | 212 |
} |