Skip to content

Commit

Permalink
enable nullaway on the simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Dec 22, 2024
1 parent 62f401c commit 88d58ca
Show file tree
Hide file tree
Showing 49 changed files with 390 additions and 182 deletions.
25 changes: 17 additions & 8 deletions caffeine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,32 @@ val generateNodes by tasks.registering(JavaExec::class) {
tasks.named<JavaCompile>("compileJava").configure {
dependsOn(generateLocalCaches, generateNodes)
finalizedBy(compileCodeGenJava)
options.errorprone {
disable("CheckReturnValue")
options.apply {
compilerArgs.addAll(listOf("-Xlint:-auxiliaryclass", "-Xlint:-exports"))
errorprone {
disable("CheckReturnValue")
}
}
}

tasks.named<JavaCompile>("compileTestJava").configure {
dependsOn(tasks.jar, compileCodeGenJava)
options.errorprone {
disable("Varifier")
disable("Var")
options.apply {
compilerArgs.add("-Xlint:-auxiliaryclass")
errorprone {
disable("Varifier")
disable("Var")
}
}
}

tasks.named<JavaCompile>("compileJmhJava").configure {
options.errorprone {
disable("Varifier")
disable("Var")
options.apply {
compilerArgs.add("-Xlint:-classfile")
errorprone {
disable("Varifier")
disable("Var")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,8 @@ public void evict_zeroWeight_victim(BoundedLocalCache<Int, Int> cache, CacheCont
@Test(dataProvider = "caches")
@CacheSpec(population = Population.EMPTY, maximumSize = Maximum.FULL)
public void evict_admit(BoundedLocalCache<Int, Int> cache, CacheContext context) {
// This test require that the JVM is run to allow the internal random fields can be overridden
// by adding the JVM argument --add-opens java.base/java.lang=ALL-UNNAMED
// This test modifies the thread's internal fields so that the random admission is predictable
// and therefore requires the JVM argument --add-opens java.base/java.lang=ALL-UNNAMED
Reset.resetThreadLocalRandom();

cache.frequencySketch().ensureCapacity(context.maximumSize());
Expand Down
1 change: 1 addition & 0 deletions gradle/config/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@
<Class name="com.github.benmanes.caffeine.cache.simulator.admission.bloom.MembershipTest"/>
<Bug pattern="
UC_USELESS_OBJECT,
CRLF_INJECTION_LOGS,
UPM_UNCALLED_PRIVATE_METHOD,
WOC_WRITE_ONLY_COLLECTION_LOCAL"/>
</Match>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ tasks.withType<JavaCompile>().configureEach {

options.apply {
javaModuleVersion = provider { version as String }
compilerArgs.addAll(listOf("-Xlint:all", "-Xlint:-auxiliaryclass", "-Xlint:-classfile",
"-Xlint:-exports", "-Xlint:-processing", "-Xlint:-requires-automatic", "-parameters",
compilerArgs.addAll(listOf("-Xlint:all", "-parameters",
"-Xmaxerrs", "500", "-Xmaxwarns", "500"))
if (isCI()) {
compilerArgs.add("-Werror")
Expand Down
4 changes: 4 additions & 0 deletions guava/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ dependencies {
testImplementation(libs.bundles.slf4j.nop)
}

tasks.named<JavaCompile>("compileJava").configure {
options.compilerArgs.add("-Xlint:-requires-automatic")
}

tasks.named<JavaCompile>("compileTestJava").configure {
options.errorprone {
disable("Varifier")
Expand Down
17 changes: 12 additions & 5 deletions simulator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ forbiddenApis {
"jdk-internal", "jdk-non-portable", "jdk-reflection", "jdk-unsafe"))
}

tasks.withType<JavaCompile>().configureEach {
options.errorprone {
disableWarningsInGeneratedCode = true
disable("SystemOut")
nullaway.disable()
tasks.named<JavaCompile>("compileJava").configure {
options.apply {
compilerArgs.addAll(listOf("-Xlint:-classfile", "-Xlint:-processing"))
errorprone {
disableWarningsInGeneratedCode = true
disable("SystemOut")

nullaway {
externalInitAnnotations.add("picocli.CommandLine.Command")
treatGeneratedAsUnannotated = true
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ public Config config() {
}

/** Gets the quoted integer at the given path, ignoring comma and underscore separators */
@SuppressWarnings("NullAway")
protected int getFormattedInt(String path) {
return parseFormattedNumber(path, config()::getInt, Ints::tryParse);
}

/** Gets the quoted long at the given path, ignoring comma and underscore separators */
@SuppressWarnings("NullAway")
protected long getFormattedLong(String path) {
return parseFormattedNumber(path, config()::getLong, Longs::tryParse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ private static void throwError(RuntimeException error, Iterable<PolicyActor> pol
try {
policy.completed().join();
} catch (CompletionException e) {
Throwables.throwIfUnchecked(e.getCause());
if (e.getCause() != null) {
Throwables.throwIfUnchecked(e.getCause());
}
e.addSuppressed(error);
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.ints.IntPriorityQueue;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMaps;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;

/**
Expand All @@ -47,11 +48,13 @@ public final class Clairvoyant implements KeyOnlyAdmittor {
private final PolicyStats policyStats;

public Clairvoyant(Config config, PolicyStats policyStats) {
if (snapshot.get() == null) {
snapshot.set(readAccessTimes(new BasicSettings(config)));
@Var var readOnlyAccessTimes = snapshot.get();
if (readOnlyAccessTimes == null) {
readOnlyAccessTimes = readAccessTimes(new BasicSettings(config));
snapshot.set(readOnlyAccessTimes);
}
accessTimes = new Long2ObjectOpenHashMap<>(snapshot.get().size());
for (var entry : snapshot.get().long2ObjectEntrySet()) {
accessTimes = new Long2ObjectOpenHashMap<>(readOnlyAccessTimes.size());
for (var entry : readOnlyAccessTimes.long2ObjectEntrySet()) {
var times = new IntArrayFIFOQueue(entry.getValue().size());
accessTimes.put(entry.getLongKey(), times);
entry.getValue().forEach(times::enqueue);
Expand Down Expand Up @@ -108,6 +111,6 @@ private static Long2ObjectMap<IntList> readAccessTimes(BasicSettings settings) {
times.add(++tick[0]);
});
}
return accessTimes;
return Long2ObjectMaps.unmodifiable(accessTimes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class CountMin4 implements Frequency {
* Creates a frequency sketch that can accurately estimate the popularity of elements given
* the maximum size of the cache.
*/
@SuppressWarnings({"this-escape", "Varifier"})
@SuppressWarnings({"NullAway.Init", "this-escape", "Varifier"})
protected CountMin4(Config config) {
var settings = new BasicSettings(config);
conservative = settings.tinyLfu().conservative();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class BloomFilter implements Membership {
* Creates a lazily initialized membership sketch, requiring {@link #ensureCapacity} be called
* when the expected number of insertions and the false positive probability have been determined.
*/
@SuppressWarnings("NullAway.Init")
public BloomFilter() {}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected BufferedInputStream readInput(InputStream input) {
try {
var archive = new ArchiveStreamFactory().createArchiveInputStream(input);
var entries = new AbstractIterator<InputStream>() {
@Override protected InputStream computeNext() {
@Override protected @Nullable InputStream computeNext() {
try {
return (archive.getNextEntry() == null)
? endOfData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.jspecify.annotations.Nullable;

import com.github.benmanes.caffeine.cache.simulator.policy.AccessEvent;
import com.google.common.io.Closeables;

Expand Down Expand Up @@ -57,7 +59,7 @@ public Stream<AccessEvent> events() {

private final class TraceIterator implements Iterator<AccessEvent> {
final DataInputStream input;
AccessEvent next;
@Nullable AccessEvent next;
boolean ready;

TraceIterator(DataInputStream input) {
Expand Down Expand Up @@ -85,6 +87,7 @@ public AccessEvent next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
requireNonNull(next);
ready = false;
return next;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Sets.toImmutableEnumSet;
import static java.util.Locale.US;
import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -127,7 +128,7 @@ private ImmutableList<TraceReader> readers() {
return filePaths.stream().map(path -> {
List<String> parts = Splitter.on(':').limit(2).splitToList(path);
TraceFormat format = (parts.size() == 1) ? TraceFormat.this : named(parts.get(0));
return format.factory.apply(Iterables.getLast(parts));
return format.factory.apply(requireNonNull(Iterables.getLast(parts)));
}).collect(toImmutableList());
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class PolicyActor {
* @param policy the cache policy being simulated
* @param settings the simulation settings
*/
@SuppressWarnings("NullAway")
public PolicyActor(Thread parent, Policy policy, BasicSettings settings) {
this.semaphore = new Semaphore(settings.actor().mailboxSize());
this.future = CompletableFuture.completedFuture(null);
Expand All @@ -70,6 +71,7 @@ public void finish() {
}

/** Submits the command to the mailbox and blocks until accepted. */
@SuppressWarnings("NullAway")
private void submit(Command command) {
try {
semaphore.acquire();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.github.benmanes.caffeine.cache.simulator.policy.adaptive;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;

import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -161,21 +162,21 @@ private void onMiss(long key) {
int sizeL2 = (sizeT2 + sizeB2);
if (sizeL1 == maximumSize) {
if (sizeT1 < maximumSize) {
Node victim = headB1.next;
Node victim = requireNonNull(headB1.next);
data.remove(victim.key);
victim.remove();
sizeB1--;

evict(node);
} else {
Node victim = headT1.next;
Node victim = requireNonNull(headT1.next);
data.remove(victim.key);
victim.remove();
sizeT1--;
}
} else if ((sizeL1 < maximumSize) && ((sizeL1 + sizeL2) >= maximumSize)) {
if ((sizeL1 + sizeL2) >= (2 * maximumSize)) {
Node victim = headB2.next;
Node victim = requireNonNull(headB2.next);
data.remove(victim.key);
victim.remove();
sizeB2--;
Expand All @@ -197,14 +198,14 @@ private void evict(Node candidate) {
// else move the LRU page in T2 to the top of B2 and remove it from the cache.

if ((sizeT1 >= 1) && (((candidate.type == QueueType.B2) && (sizeT1 == p)) || (sizeT1 > p))) {
Node victim = headT1.next;
Node victim = requireNonNull(headT1.next);
victim.remove();
victim.type = QueueType.B1;
victim.appendToTail(headB1);
sizeT1--;
sizeB1++;
} else {
Node victim = headT2.next;
Node victim = requireNonNull(headT2.next);
victim.remove();
victim.type = QueueType.B2;
victim.appendToTail(headB2);
Expand Down Expand Up @@ -255,7 +256,7 @@ static final class Node {

/** Appends the node to the tail of the list. */
public void appendToTail(Node head) {
Node tail = head.prev;
Node tail = requireNonNull(head.prev);
head.prev = this;
tail.next = this;
next = head;
Expand All @@ -264,6 +265,9 @@ public void appendToTail(Node head) {

/** Removes the node from the list. */
public void remove() {
requireNonNull(prev);
requireNonNull(next);

prev.next = next;
next.prev = prev;
prev = next = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.github.benmanes.caffeine.cache.simulator.policy.adaptive;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;

import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -124,13 +125,13 @@ private void onMiss(long key, @Var Node node) {
if (!isGhost(node)) {
if ((sizeT1 + sizeB1) == maximumSize) {
// Discard the LRU page in B1
Node victim = headB1.next;
Node victim = requireNonNull(headB1.next);
data.remove(victim.key);
victim.remove();
sizeB1--;
} else if ((sizeT1 + sizeT2 + sizeB1 + sizeB2) == (2 * maximumSize)) {
// Discard the LRU page in B2
Node victim = headB2.next;
Node victim = requireNonNull(headB2.next);
data.remove(victim.key);
victim.remove();
sizeB2--;
Expand Down Expand Up @@ -199,7 +200,7 @@ private void demote() {
for (;;) {
policyStats.recordOperation();
if (sizeT1 >= Math.max(1, p)) {
Node candidate = headT1.next;
Node candidate = requireNonNull(headT1.next);
if (!candidate.marked) {
candidate.remove();
sizeT1--;
Expand All @@ -216,7 +217,7 @@ private void demote() {
sizeT2++;
}
} else {
Node candidate = headT2.next;
Node candidate = requireNonNull(headT2.next);
if (!candidate.marked) {
candidate.remove();
sizeT2--;
Expand Down Expand Up @@ -275,7 +276,7 @@ static final class Node {

/** Appends the node to the tail of the list. */
public void appendToTail(Node head) {
Node tail = head.prev;
Node tail = requireNonNull(head.prev);
head.prev = this;
tail.next = this;
next = head;
Expand All @@ -284,6 +285,9 @@ public void appendToTail(Node head) {

/** Removes the node from the list. */
public void remove() {
requireNonNull(prev);
requireNonNull(next);

prev.next = next;
next.prev = prev;
prev = next = null;
Expand Down
Loading

0 comments on commit 88d58ca

Please sign in to comment.