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;
|
|
47 |
|
|
48 |
public final class RepositoryFiles {
|
|
49 |
private final Path repostory;
|
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;
|
|
53 |
|
|
54 |
public RepositoryFiles(Path repostory) {
|
|
55 |
this.repostory = repostory;
|
|
56 |
}
|
|
57 |
|
|
58 |
public long getTimestamp(Path p) {
|
|
59 |
return pathLookup.get(p);
|
|
60 |
}
|
|
61 |
|
57449
|
62 |
public Path lastPath() {
|
|
63 |
return nextPath(-1);
|
|
64 |
}
|
|
65 |
|
57433
|
66 |
public Path nextPath(long startTimeNanos) {
|
|
67 |
while (!closed) {
|
57449
|
68 |
if (startTimeNanos == -1) {
|
|
69 |
Entry<Long, Path> e = pathSet.lastEntry();
|
|
70 |
if (e != null) {
|
|
71 |
return e.getValue();
|
|
72 |
}
|
|
73 |
}
|
57433
|
74 |
SortedMap<Long, Path> after = pathSet.tailMap(startTimeNanos);
|
|
75 |
if (!after.isEmpty()) {
|
|
76 |
Path path = after.get(after.firstKey());
|
|
77 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Return path " + path + " for start time nanos " + startTimeNanos);
|
|
78 |
return path;
|
|
79 |
}
|
|
80 |
try {
|
|
81 |
if (updatePaths(repostory)) {
|
|
82 |
continue;
|
|
83 |
}
|
|
84 |
} catch (IOException e) {
|
|
85 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "IOException during repository file scan " + e.getMessage());
|
|
86 |
// This can happen if a chunk is being removed
|
|
87 |
// between the file was discovered and an instance
|
|
88 |
// of an EventSet was constructed. Just ignore,
|
|
89 |
// and retry later.
|
|
90 |
}
|
|
91 |
try {
|
|
92 |
synchronized (pathSet) {
|
|
93 |
pathSet.wait(1000);
|
|
94 |
}
|
|
95 |
} catch (InterruptedException e) {
|
|
96 |
// ignore
|
|
97 |
}
|
|
98 |
}
|
|
99 |
return null;
|
|
100 |
}
|
|
101 |
|
|
102 |
private boolean updatePaths(Path repo) throws IOException {
|
|
103 |
if (repo == null) {
|
|
104 |
repo = Repository.getRepository().getRepositoryPath().toPath();
|
|
105 |
}
|
|
106 |
boolean foundNew = false;
|
|
107 |
List<Path> added = new ArrayList<>();
|
|
108 |
Set<Path> current = new HashSet<>();
|
|
109 |
if (!Files.exists(repo)) {
|
|
110 |
// Repository removed, probably due to shutdown
|
|
111 |
return true;
|
|
112 |
}
|
|
113 |
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(repo, "*.jfr")) {
|
|
114 |
for (Path p : dirStream) {
|
|
115 |
if (!pathLookup.containsKey(p)) {
|
|
116 |
added.add(p);
|
|
117 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "New file found: " + p.toAbsolutePath());
|
|
118 |
}
|
|
119 |
current.add(p);
|
|
120 |
}
|
|
121 |
}
|
|
122 |
List<Path> removed = new ArrayList<>();
|
|
123 |
for (Path p : pathLookup.keySet()) {
|
|
124 |
if (!current.contains(p)) {
|
|
125 |
removed.add(p);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
for (Path remove : removed) {
|
|
130 |
Long time = pathLookup.get(remove);
|
|
131 |
pathSet.remove(time);
|
|
132 |
pathLookup.remove(remove);
|
|
133 |
}
|
|
134 |
Collections.sort(added, (p1, p2) -> p1.compareTo(p2));
|
|
135 |
for (Path p : added) {
|
|
136 |
// Only add files that have a complete header
|
|
137 |
// as the JVM may be in progress writing the file
|
|
138 |
long size = Files.size(p);
|
|
139 |
if (size >= ChunkHeader.HEADER_SIZE) {
|
|
140 |
long startNanos = readStartTime(p);
|
|
141 |
pathSet.put(startNanos, p);
|
|
142 |
pathLookup.put(p, startNanos);
|
|
143 |
foundNew = true;
|
|
144 |
}
|
|
145 |
}
|
|
146 |
return foundNew;
|
|
147 |
}
|
|
148 |
|
|
149 |
private long readStartTime(Path p) throws IOException {
|
|
150 |
try (RecordingInput in = new RecordingInput(p.toFile(), 100)) {
|
|
151 |
ChunkHeader c = new ChunkHeader(in);
|
|
152 |
return c.getStartNanos();
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
public void close() {
|
|
157 |
synchronized (pathSet) {
|
|
158 |
this.closed = true;
|
|
159 |
pathSet.notify();
|
|
160 |
}
|
|
161 |
}
|
57449
|
162 |
|
|
163 |
|
57433
|
164 |
} |