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 */ |
|
25 |
|
26 package jdk.jfr.consumer; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.nio.file.DirectoryStream; |
|
30 import java.nio.file.Files; |
|
31 import java.nio.file.Path; |
|
32 import java.util.ArrayList; |
|
33 import java.util.Collections; |
|
34 import java.util.HashMap; |
|
35 import java.util.HashSet; |
|
36 import java.util.List; |
|
37 import java.util.Map; |
|
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; |
|
46 import jdk.jfr.internal.consumer.ChunkHeader; |
|
47 |
|
48 /** |
|
49 * This class corresponds to a disk repository. |
|
50 * <p> |
|
51 * Main purpose is to act as a cache if multiple {@code EventStream} want to |
|
52 * access the same repository. An {@code EventSetLocation} should be released |
|
53 * when it is no longer being used. |
|
54 * |
|
55 */ |
|
56 final class EventSetLocation { |
|
57 private static Map<Path, EventSetLocation> locations = new HashMap<>(); |
|
58 |
|
59 private final SortedMap<Long, EventSet> eventSets = new TreeMap<>(); |
|
60 private final Map<Path, Long> lastPaths = new HashMap<>(); |
|
61 |
|
62 final Path path; |
|
63 private int count = 0; |
|
64 private volatile boolean closed; |
|
65 |
|
66 private EventSetLocation(Path path) { |
|
67 this.path = path; |
|
68 } |
|
69 |
|
70 public static EventSetLocation get(Path absolutPath) { |
|
71 synchronized (locations) { |
|
72 EventSetLocation esl = locations.get(absolutPath); |
|
73 if (esl == null) { |
|
74 esl = new EventSetLocation(absolutPath); |
|
75 locations.put(absolutPath, esl); |
|
76 } |
|
77 esl.count++; |
|
78 return esl; |
|
79 } |
|
80 } |
|
81 |
|
82 public static EventSetLocation current() throws IOException { |
|
83 Repository.getRepository().ensureRepository(); |
|
84 return get(Repository.getRepository().getRepositoryPath().toPath()); |
|
85 } |
|
86 |
|
87 public void release() { |
|
88 synchronized (locations) { |
|
89 count--; |
|
90 if (count == 0) { |
|
91 locations.remove(path); |
|
92 closed = true; |
|
93 } |
|
94 } |
|
95 } |
|
96 |
|
97 public synchronized EventSet acquire(long startTimeNanos, EventSet previousEventSet) { |
|
98 synchronized (eventSets) { |
|
99 while (!closed) { |
|
100 SortedMap<Long, EventSet> after = eventSets.tailMap(startTimeNanos); |
|
101 if (!after.isEmpty()) { |
|
102 EventSet es = after.get(after.firstKey()); |
|
103 Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Acquired " + startTimeNanos + ", got " + es); |
|
104 es.acquire(); |
|
105 return es; |
|
106 } |
|
107 try { |
|
108 if (updateEventSets(previousEventSet)) { |
|
109 continue; |
|
110 } |
|
111 } catch (IOException e) { |
|
112 Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "IOException during event set update " + e.getMessage()); |
|
113 // This can happen if a chunk is being removed |
|
114 // between the file was discovered and an instance |
|
115 // of an EventSet was constructed. Just ignore, |
|
116 // and retry later. |
|
117 } |
|
118 try { |
|
119 eventSets.wait(1000); |
|
120 } catch (InterruptedException e) { |
|
121 // ignore |
|
122 } |
|
123 } |
|
124 } |
|
125 return null; |
|
126 } |
|
127 |
|
128 private boolean updateEventSets(EventSet previousEventSet) throws IOException { |
|
129 boolean foundNew = false; |
|
130 List<Path> added = new ArrayList<>(); |
|
131 Set<Path> current = new HashSet<>(); |
|
132 if (!Files.exists(path)) { |
|
133 // Repository removed, probably due to shutdown |
|
134 closed = true; |
|
135 return true; |
|
136 } |
|
137 try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path, "*.jfr")) { |
|
138 for (Path p : dirStream) { |
|
139 if (!lastPaths.containsKey(p)) { |
|
140 added.add(p); |
|
141 Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "New file found: " + p.toAbsolutePath()); |
|
142 } |
|
143 current.add(p); |
|
144 } |
|
145 } |
|
146 List<Path> removed = new ArrayList<>(); |
|
147 for (Path p : lastPaths.keySet()) { |
|
148 if (!current.contains(p)) { |
|
149 removed.add(p); |
|
150 } |
|
151 } |
|
152 |
|
153 for (Path remove : removed) { |
|
154 Long time = lastPaths.get(remove); |
|
155 eventSets.remove(time); |
|
156 lastPaths.remove(remove); |
|
157 } |
|
158 Collections.sort(added, (p1,p2) -> p1.compareTo(p2)); |
|
159 for (Path p : added) { |
|
160 // Only add files that have a complete header |
|
161 // as the JVM may be in progress writing the file |
|
162 long size = Files.size(p); |
|
163 if (size >= ChunkHeader.HEADER_SIZE) { |
|
164 EventSet es = new EventSet(this, previousEventSet, p); |
|
165 long startTime = es.getStartTimeNanos(); |
|
166 if (startTime == 0) { |
|
167 String errorMsg = "Chunk header should always contain a valid start time"; |
|
168 throw new InternalError(errorMsg); |
|
169 } |
|
170 EventSet previous = eventSets.get(startTime); |
|
171 if (previous != null) { |
|
172 String errorMsg = "Found chunk " + p + " with the same start time " + startTime + " as previous chunk " + previous.getPath(); |
|
173 throw new InternalError(errorMsg); |
|
174 } |
|
175 eventSets.put(startTime, es); |
|
176 lastPaths.put(p, startTime); |
|
177 previousEventSet = es; |
|
178 foundNew = true; |
|
179 } |
|
180 } |
|
181 return foundNew; |
|
182 } |
|
183 } |
|