Giter Club home page Giter Club logo

cache-sim's Introduction

A cache simulator.

Basic Usage

use std::collections::HashSet;

use cache_sim::{Cache, Lru};

let mut c = Cache::<Lru>::new(3);

c.access(0);
c.access(1);
c.access(2);
c.access(0);
c.access(3);

assert_eq!(c.set(), &HashSet::from([0, 2, 3]));

Configuring the Cache

Items

Currently, the cache abstractly caches u32s, each of which should be read to represent a different cacheable item, e.x. a block from memory. The cache will work with any type which implements the Item marker trait. For forwards-compatibliity, this should not be done frivolously, because in the future this trait will represent properties of more abstract caching models, like the cost and size of the item.

Capacity

The Cache::new function takes a capacity as a parameter; this allows you to experiment on arbitrarily-sized caches.

Replacement Policies

The library contains implementations of several replacement policies, represented by the first generic type of Cache:

use std::collections::HashSet;

use cache_sim::{Cache, Fifo};

let mut c = Cache::<Fifo>::new(3);

c.access(0);
c.access(1);
c.access(2);
c.access(0);
c.access(3);

assert_eq!(c.set(), &HashSet::from([1, 2, 3]));

Statistics

You can attach statistics to the cache using its second generic type (default ()), like so:

use cache_sim::{Cache, Lru};
use cache_sim::stats::HitCount;

let mut c = Cache::<Lru, HitCount>::new(3);
c.access(0); // miss
c.access(1); // miss
c.access(2); // miss
c.access(0); // hit
c.access(3); // miss
c.access(0); // hit

assert_eq!(c.stat().0, 2);

You can track multiple statistics using a tuple:

use cache_sim::{Cache, Lru};
use cache_sim::stats::{HitCount, MissCount};

let mut c = Cache::<Lru, (HitCount, MissCount)>::new(3);
c.access(0); // miss
c.access(1); // miss
c.access(2); // miss
c.access(0); // hit
c.access(3); // miss
c.access(0); // hit

assert_eq!(c.stat().0.0, 2);
assert_eq!(c.stat().1.0, 4);

Traces

There are also tools available for analyzing abstracted traces, like so:

use std::collections::HashMap;

use cache_sim::Trace;
use cache_sim::condition::NoCondition;

let trace = Trace::from(vec![0, 0, 1, 0, 3, 1]);
let frequencies = trace.frequency_histogram(&NoCondition);
assert_eq!(frequencies.get(&0), Some(&3));

cache-sim's People

Contributors

rileyshahar avatar brmorrell avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.