103 Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Acquired " + startTimeNanos + ", got " + es); |
103 Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Acquired " + startTimeNanos + ", got " + es); |
104 es.acquire(); |
104 es.acquire(); |
105 return es; |
105 return es; |
106 } |
106 } |
107 try { |
107 try { |
108 updateEventSets(previousEventSet); |
108 if (updateEventSets(previousEventSet)) { |
|
109 continue; |
|
110 } |
109 } catch (IOException e) { |
111 } catch (IOException e) { |
110 e.printStackTrace(); |
112 Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "IOException during event set update " + e.getMessage()); |
111 // This can happen if a chunk is being removed |
113 // This can happen if a chunk is being removed |
112 // between the file was discovered and an instance |
114 // between the file was discovered and an instance |
113 // of an EventSet was constructed. Just ignore, |
115 // of an EventSet was constructed. Just ignore, |
114 // and retry later. |
116 // and retry later. |
115 } |
117 } |
121 } |
123 } |
122 } |
124 } |
123 return null; |
125 return null; |
124 } |
126 } |
125 |
127 |
126 private void updateEventSets(EventSet previousEventSet) throws IOException { |
128 private boolean updateEventSets(EventSet previousEventSet) throws IOException { |
|
129 boolean foundNew = false; |
127 List<Path> added = new ArrayList<>(); |
130 List<Path> added = new ArrayList<>(); |
128 Set<Path> current = new HashSet<>(); |
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 } |
129 try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path, "*.jfr")) { |
137 try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path, "*.jfr")) { |
130 for (Path p : dirStream) { |
138 for (Path p : dirStream) { |
131 if (!lastPaths.containsKey(p)) { |
139 if (!lastPaths.containsKey(p)) { |
132 added.add(p); |
140 added.add(p); |
133 Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "New file found: " + p.toAbsolutePath()); |
141 Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "New file found: " + p.toAbsolutePath()); |
155 if (size >= ChunkHeader.HEADER_SIZE) { |
163 if (size >= ChunkHeader.HEADER_SIZE) { |
156 EventSet es = new EventSet(this, previousEventSet, p); |
164 EventSet es = new EventSet(this, previousEventSet, p); |
157 long startTime = es.getStartTimeNanos(); |
165 long startTime = es.getStartTimeNanos(); |
158 if (startTime == 0) { |
166 if (startTime == 0) { |
159 String errorMsg = "Chunk header should always contain a valid start time"; |
167 String errorMsg = "Chunk header should always contain a valid start time"; |
160 System.err.println(errorMsg); |
|
161 throw new InternalError(errorMsg); |
168 throw new InternalError(errorMsg); |
162 } |
169 } |
163 EventSet previous = eventSets.get(startTime); |
170 EventSet previous = eventSets.get(startTime); |
164 if (previous != null) { |
171 if (previous != null) { |
165 String errorMsg = "Found chunk " + p + " with the same start time " + startTime + " as previous chunk " + previous.getPath(); |
172 String errorMsg = "Found chunk " + p + " with the same start time " + startTime + " as previous chunk " + previous.getPath(); |
166 System.err.println(errorMsg); |
|
167 throw new InternalError(errorMsg); |
173 throw new InternalError(errorMsg); |
168 } |
174 } |
169 eventSets.put(startTime, es); |
175 eventSets.put(startTime, es); |
170 lastPaths.put(p, startTime); |
176 lastPaths.put(p, startTime); |
171 previousEventSet = es; |
177 previousEventSet = es; |
|
178 foundNew = true; |
172 } |
179 } |
173 } |
180 } |
|
181 return foundNew; |
174 } |
182 } |
175 } |
183 } |