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