author | egahlin |
Fri, 02 Aug 2019 20:05:23 +0200 | |
branch | JEP-349-branch |
changeset 57628 | f5f590eaecf5 |
parent 57604 | 838f9a7635b6 |
child 57638 | 3b41affae2d2 |
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.Map.Entry; |
38 |
import java.util.NavigableMap; |
|
57433 | 39 |
import java.util.Set; |
40 |
import java.util.SortedMap; |
|
41 |
import java.util.TreeMap; |
|
42 |
||
43 |
import jdk.jfr.internal.LogLevel; |
|
44 |
import jdk.jfr.internal.LogTag; |
|
45 |
import jdk.jfr.internal.Logger; |
|
46 |
import jdk.jfr.internal.Repository; |
|
57628 | 47 |
import jdk.jfr.internal.SecuritySupport.SafePath; |
57433 | 48 |
|
49 |
public final class RepositoryFiles { |
|
57628 | 50 |
private final Path repository; |
57449 | 51 |
private final NavigableMap<Long, Path> pathSet = new TreeMap<>(); |
57433 | 52 |
private final Map<Path, Long> pathLookup = new HashMap<>(); |
53 |
private volatile boolean closed; |
|
54 |
||
57628 | 55 |
public RepositoryFiles(SafePath repository) { |
56 |
this.repository = repository.toPath(); |
|
57433 | 57 |
} |
58 |
||
59 |
public long getTimestamp(Path p) { |
|
60 |
return pathLookup.get(p); |
|
61 |
} |
|
62 |
||
57449 | 63 |
public Path lastPath() { |
64 |
return nextPath(-1); |
|
65 |
} |
|
66 |
||
57433 | 67 |
public Path nextPath(long startTimeNanos) { |
68 |
while (!closed) { |
|
57449 | 69 |
if (startTimeNanos == -1) { |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
70 |
Entry<Long, Path> e = pathSet.lastEntry(); |
57449 | 71 |
if (e != null) { |
72 |
return e.getValue(); |
|
73 |
} |
|
74 |
} |
|
57628 | 75 |
Long f = pathSet.floorKey(startTimeNanos); |
76 |
if (f != null) { |
|
77 |
SortedMap<Long, Path> after = pathSet.tailMap(f); |
|
78 |
if (!after.isEmpty()) { |
|
79 |
Path path = after.get(after.firstKey()); |
|
80 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Return path " + path + " for start time nanos " + startTimeNanos); |
|
81 |
return path; |
|
82 |
} |
|
57433 | 83 |
} |
84 |
try { |
|
57628 | 85 |
if (updatePaths(repository)) { |
57433 | 86 |
continue; |
87 |
} |
|
88 |
} catch (IOException e) { |
|
89 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "IOException during repository file scan " + e.getMessage()); |
|
90 |
// This can happen if a chunk is being removed |
|
91 |
// between the file was discovered and an instance |
|
92 |
// of an EventSet was constructed. Just ignore, |
|
93 |
// and retry later. |
|
94 |
} |
|
95 |
try { |
|
96 |
synchronized (pathSet) { |
|
97 |
pathSet.wait(1000); |
|
98 |
} |
|
99 |
} catch (InterruptedException e) { |
|
100 |
// ignore |
|
101 |
} |
|
102 |
} |
|
103 |
return null; |
|
104 |
} |
|
105 |
||
106 |
private boolean updatePaths(Path repo) throws IOException { |
|
107 |
if (repo == null) { |
|
108 |
repo = Repository.getRepository().getRepositoryPath().toPath(); |
|
109 |
} |
|
110 |
boolean foundNew = false; |
|
111 |
List<Path> added = new ArrayList<>(); |
|
112 |
Set<Path> current = new HashSet<>(); |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
113 |
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(repo)) { |
57433 | 114 |
for (Path p : dirStream) { |
115 |
if (!pathLookup.containsKey(p)) { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
116 |
String s = p.toString(); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
117 |
if (s.endsWith(".jfr")) { |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
118 |
added.add(p); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
119 |
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
|
120 |
} |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57449
diff
changeset
|
121 |
current.add(p); |
57433 | 122 |
} |
123 |
} |
|
124 |
} |
|
125 |
List<Path> removed = new ArrayList<>(); |
|
126 |
for (Path p : pathLookup.keySet()) { |
|
127 |
if (!current.contains(p)) { |
|
128 |
removed.add(p); |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
for (Path remove : removed) { |
|
133 |
Long time = pathLookup.get(remove); |
|
134 |
pathSet.remove(time); |
|
135 |
pathLookup.remove(remove); |
|
136 |
} |
|
137 |
Collections.sort(added, (p1, p2) -> p1.compareTo(p2)); |
|
138 |
for (Path p : added) { |
|
139 |
// Only add files that have a complete header |
|
140 |
// as the JVM may be in progress writing the file |
|
141 |
long size = Files.size(p); |
|
142 |
if (size >= ChunkHeader.HEADER_SIZE) { |
|
143 |
long startNanos = readStartTime(p); |
|
144 |
pathSet.put(startNanos, p); |
|
145 |
pathLookup.put(p, startNanos); |
|
146 |
foundNew = true; |
|
147 |
} |
|
148 |
} |
|
149 |
return foundNew; |
|
150 |
} |
|
151 |
||
152 |
private long readStartTime(Path p) throws IOException { |
|
153 |
try (RecordingInput in = new RecordingInput(p.toFile(), 100)) { |
|
154 |
ChunkHeader c = new ChunkHeader(in); |
|
155 |
return c.getStartNanos(); |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
public void close() { |
|
160 |
synchronized (pathSet) { |
|
161 |
this.closed = true; |
|
162 |
pathSet.notify(); |
|
163 |
} |
|
164 |
} |
|
57449 | 165 |
|
57433 | 166 |
} |