Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ static class AnnotatedRegion {
this.smapsPath = smapsPath;
}

// @VisibleForTesting
void invalidate() {
UPDATER.getAndSet(this, System.nanoTime() - (2 * ttl));
}

@SuppressWarnings("unchecked")
public List<SmapEntryEvent> getEvents() {
long prevTimestamp = lastTimestamp;
Expand Down Expand Up @@ -296,9 +301,9 @@ private static Map<Long, String> getAnnotatedRegions() {
return Collections.emptyMap();
}

private static void collectEvents(List<SmapEntryEvent> events) {
private void collectEvents(List<SmapEntryEvent> events) {
try (BufferedReader br =
new BufferedReader(new InputStreamReader(Files.newInputStream(SMAPS_PATH)), 64 * 1024)) {
new BufferedReader(new InputStreamReader(Files.newInputStream(smapsPath)), 64 * 1024)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

readEvents(br, events);
Map<Long, String> regions = getAnnotatedRegions();
for (SmapEntryEvent e : events) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ void getEvents() throws Exception {
assumeTrue(OperatingSystem.isLinux());
// We need at least Java 22 for the annotated regions
assumeTrue(JavaVirtualMachine.isJavaVersionAtLeast(22));
SmapEntryCache smapEntryCache = new SmapEntryCache(Duration.ofMillis(100));
SmapEntryCache smapEntryCache =
new SmapEntryCache(Duration.ofHours(1)); // set up a really long expiration duration
List<SmapEntryEvent> events1 = smapEntryCache.getEvents();
List<SmapEntryEvent> events2 = smapEntryCache.getEvents();
// the cache is using double buffered event list so we can use identity comparison
assertSame(events1, events2);

long ts = System.nanoTime();
while (System.nanoTime() - ts < 150_000_000L) { // make sure the cache is expired
Thread.sleep(200);
}
smapEntryCache.invalidate(); // pretend expiring the cache
events1 = smapEntryCache.getEvents();
assertNotSame(events1, events2);
}
Expand Down