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