Skip to content

Commit

Permalink
Add Sieve and S3-FIFO to the simulator (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Dec 29, 2023
1 parent c79bcfc commit 1796be4
Show file tree
Hide file tree
Showing 14 changed files with 716 additions and 299 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ univocity-parsers = "2.9.1"
versions = "0.50.0"
xz = "1.9"
ycsb = "0.17.0"
zero-allocation-hashing = "0.16"
zstd = "1.5.5-11"

[libraries]
Expand Down Expand Up @@ -206,6 +207,7 @@ truth-java8 = { module = "com.google.truth.extensions:truth-java8-extension", ve
univocity-parsers = { module = "com.univocity:univocity-parsers", version.ref = "univocity-parsers" }
xz = { module = "org.tukaani:xz", version.ref = "xz" }
ycsb = { module = "site.ycsb:core", version.ref = "ycsb" }
zero-allocation-hashing = { module = "net.openhft:zero-allocation-hashing", version.ref = "zero-allocation-hashing" }
zstd = { module = "com.github.luben:zstd-jni", version.ref = "zstd" }

[bundles]
Expand Down
1 change: 1 addition & 0 deletions simulator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
implementation(libs.bundles.coherence)
implementation(libs.bundles.slf4j.jdk)
implementation(libs.univocity.parsers)
implementation(libs.zero.allocation.hashing)
}

application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@

import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;

import org.checkerframework.checker.nullness.qual.Nullable;

import com.github.benmanes.caffeine.cache.simulator.admission.Admission;
import com.github.benmanes.caffeine.cache.simulator.membership.FilterType;
import com.github.benmanes.caffeine.cache.simulator.parser.TraceFormat;
import com.github.benmanes.caffeine.cache.simulator.report.ReportFormat;
import com.google.common.base.CaseFormat;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigException;

/**
* The simulator's configuration. A policy can extend this class as a convenient way to extract
Expand All @@ -38,6 +45,8 @@
* @author [email protected] (Ben Manes)
*/
public class BasicSettings {
private static final Pattern NUMERIC_SEPARATOR = Pattern.compile("[_,]");

private final Config config;

public BasicSettings(Config config) {
Expand All @@ -53,7 +62,7 @@ public ReportSettings report() {
}

public int randomSeed() {
return config().getInt("random-seed");
return getFormattedInt("random-seed");
}

public Set<String> policies() {
Expand All @@ -78,7 +87,7 @@ public TinyLfuSettings tinyLfu() {
}

public long maximumSize() {
return config().getLong("maximum-size");
return getFormattedLong("maximum-size");
}

public TraceSettings trace() {
Expand All @@ -90,6 +99,30 @@ public Config config() {
return config;
}

/** Gets the quoted integer at the given path, ignoring comma and underscore separators */
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 */
protected long getFormattedLong(String path) {
return parseFormattedNumber(path, config()::getLong, Longs::tryParse);
}

private <T extends Number> T parseFormattedNumber(String path,
Function<String, T> getter, Function<String, @Nullable T> tryParse) {
try {
return getter.apply(path);
} catch (ConfigException.Parse | ConfigException.WrongType e) {
var matcher = NUMERIC_SEPARATOR.matcher(config().getString(path));
var value = tryParse.apply(matcher.replaceAll(""));
if (value == null) {
throw e;
}
return value;
}
}

public final class ActorSettings {
public int mailboxSize() {
return config().getInt("actor.mailbox-size");
Expand Down Expand Up @@ -183,10 +216,10 @@ public boolean enabled() {

public final class TraceSettings {
public long skip() {
return config().getLong("trace.skip");
return getFormattedLong("trace.skip");
}
public long limit() {
return config().getIsNull("trace.limit") ? Long.MAX_VALUE : config().getLong("trace.limit");
return config().getIsNull("trace.limit") ? Long.MAX_VALUE : getFormattedLong("trace.limit");
}
public boolean isFiles() {
return config().getString("trace.source").equals("files");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import com.github.benmanes.caffeine.cache.simulator.parser.glcache.GLCacheTraceReader;
import com.github.benmanes.caffeine.cache.simulator.parser.gradle.GradleTraceReader;
import com.github.benmanes.caffeine.cache.simulator.parser.kaggle.OutbrainTraceReader;
import com.github.benmanes.caffeine.cache.simulator.parser.libcachesim.csv.LibCacheSimCsvTraceReader;
import com.github.benmanes.caffeine.cache.simulator.parser.libcachesim.twitter.LibCacheSimTwitterTraceReader;
import com.github.benmanes.caffeine.cache.simulator.parser.lirs.LirsTraceReader;
import com.github.benmanes.caffeine.cache.simulator.parser.lrb.LrbTraceReader;
import com.github.benmanes.caffeine.cache.simulator.parser.scarab.ScarabTraceReader;
Expand Down Expand Up @@ -74,6 +76,8 @@ public enum TraceFormat {
CORDA(CordaTraceReader::new),
GL_CACHE(GLCacheTraceReader::new),
GRADLE(GradleTraceReader::new),
LCS_TRACE(LibCacheSimCsvTraceReader::new),
LCS_TWITTER(LibCacheSimTwitterTraceReader::new),
LIRS(LirsTraceReader::new),
LRB(LrbTraceReader::new),
OUTBRAIN(OutbrainTraceReader::new),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2023 Ben Manes. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.cache.simulator.parser.libcachesim.csv;

import static com.github.benmanes.caffeine.cache.simulator.policy.Policy.Characteristic.WEIGHTED;

import java.util.Set;
import java.util.stream.Stream;

import com.github.benmanes.caffeine.cache.simulator.parser.TextTraceReader;
import com.github.benmanes.caffeine.cache.simulator.policy.AccessEvent;
import com.github.benmanes.caffeine.cache.simulator.policy.Policy.Characteristic;

/**
* A reader for the <b>data/trace.csv</b> file provided by the authors of
* <a href="https://github.com/1a1a11a/libCacheSim">libCacheSim</a>.
*
* @author [email protected] (Ben Manes)
*/
public final class LibCacheSimCsvTraceReader extends TextTraceReader {

public LibCacheSimCsvTraceReader(String filePath) {
super(filePath);
}

@Override
public Set<Characteristic> characteristics() {
return Set.of(WEIGHTED);
}

@Override
public Stream<AccessEvent> events() {
return lines()
.skip(1)
.map(line -> line.split(","))
.map(array -> {
long key = Long.parseLong(array[4]);
int weight = Integer.parseInt(array[3]);
return AccessEvent.forKeyAndWeight(key, weight);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@CheckReturnValue
package com.github.benmanes.caffeine.cache.simulator.parser.libcachesim.csv;

import com.google.errorprone.annotations.CheckReturnValue;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023 Ben Manes. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.cache.simulator.parser.libcachesim.twitter;

import static com.github.benmanes.caffeine.cache.simulator.policy.Policy.Characteristic.WEIGHTED;

import java.util.Set;
import java.util.stream.Stream;

import com.github.benmanes.caffeine.cache.simulator.parser.TextTraceReader;
import com.github.benmanes.caffeine.cache.simulator.policy.AccessEvent;
import com.github.benmanes.caffeine.cache.simulator.policy.Policy.Characteristic;

import net.openhft.hashing.LongHashFunction;

/**
* A reader for the <b>data/twitter_cluster52.csv</b> file provided by the authors of
* <a href="https://github.com/1a1a11a/libCacheSim">libCacheSim</a>.
*
* @author [email protected] (Ben Manes)
*/
public final class LibCacheSimTwitterTraceReader extends TextTraceReader {

public LibCacheSimTwitterTraceReader(String filePath) {
super(filePath);
}

@Override
public Set<Characteristic> characteristics() {
return Set.of(WEIGHTED);
}

@Override
public Stream<AccessEvent> events() {
var hasher = LongHashFunction.xx3();
return lines()
.skip(1)
.map(line -> line.split(", "))
.map(array -> {
long key = hasher.hashChars(array[1]);
int weight = Integer.parseInt(array[2]);
return AccessEvent.forKeyAndWeight(key, weight);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@CheckReturnValue
package com.github.benmanes.caffeine.cache.simulator.parser.libcachesim.twitter;

import com.google.errorprone.annotations.CheckReturnValue;
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public PolicyStats(String format, Object... args) {

addMetric(Metric.of("Policy", (Supplier<String>) this::name, OBJECT, true));
addMetric(Metric.of("Hit Rate", (DoubleSupplier) this::hitRate, PERCENT, true));
addMetric(Metric.of("Miss Rate", (DoubleSupplier) this::missRate, PERCENT, true));
addMetric(Metric.of("Hits", (LongSupplier) this::hitCount, NUMBER, true));
addMetric(Metric.of("Misses", (LongSupplier) this::missCount, NUMBER, true));
addMetric(Metric.of("Requests", (LongSupplier) this::requestCount, NUMBER, true));
Expand All @@ -87,6 +88,12 @@ public PolicyStats(String format, Object... args) {
.name("Weighted Hit Rate")
.type(PERCENT)
.build());
addMetric(Metric.builder()
.value((DoubleSupplier) this::weightedMissRate)
.addCharacteristic(WEIGHTED)
.name("Weighted Miss Rate")
.type(PERCENT)
.build());
addPercentMetric("Adaption", this::percentAdaption);
addMetric("Average Miss Penalty", this::averageMissPenalty);
addMetric("Average Penalty", this::avergePenalty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.github.benmanes.caffeine.cache.simulator.policy.linked.MultiQueuePolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.linked.S4LruPolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.linked.SegmentedLruPolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.linked.SievePolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.opt.ClairvoyantPolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.opt.UnboundedPolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.product.Cache2kPolicy;
Expand All @@ -71,7 +72,7 @@
import com.github.benmanes.caffeine.cache.simulator.policy.sketch.tinycache.TinyCachePolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.sketch.tinycache.TinyCacheWithGhostCachePolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.sketch.tinycache.WindowTinyCachePolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.two_queue.QdlpPolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.two_queue.S3FifoPolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.two_queue.TuQueuePolicy;
import com.github.benmanes.caffeine.cache.simulator.policy.two_queue.TwoQueuePolicy;
import com.google.auto.value.AutoValue;
Expand Down Expand Up @@ -158,6 +159,7 @@ private void registerLinked() {
registerMany(policy.label(), FrequentlyUsedPolicy.class,
config -> FrequentlyUsedPolicy.policies(config, policy));
}
register(SievePolicy.class, SievePolicy::new);
registerMany(S4LruPolicy.class, S4LruPolicy::policies);
register(MultiQueuePolicy.class, MultiQueuePolicy::new);
registerMany(SegmentedLruPolicy.class, SegmentedLruPolicy::policies);
Expand All @@ -171,7 +173,7 @@ private void registerSampled() {
}

private void registerTwoQueue() {
register(QdlpPolicy.class, QdlpPolicy::new);
register(S3FifoPolicy.class, S3FifoPolicy::new);
register(TuQueuePolicy.class, TuQueuePolicy::new);
register(TwoQueuePolicy.class, TwoQueuePolicy::new);
}
Expand Down
Loading

0 comments on commit 1796be4

Please sign in to comment.