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