Example #1
0
    def __init__(self, caches, weights=None, **kwargs):
        """Constructor

        Parameters
        ----------
        caches : array-like
            An array of caching nodes instances on the array
        weights : array-like
            Random weights according to which a cache of the array should be
            selected to process a given request
        """
        self._caches = caches
        self._len = sum(len(c) for c in caches)
        self._n_caches = len(caches)
        self._selected_cache = None
        if weights is not None:
            if np.abs(np.sum(weights) - 1) > 0.0001:
                raise ValueError("weights must sum up to 1")
            if len(weights) != self._n_caches:
                raise ValueError(
                    "weights must have as many elements as nr of caches")
            randvar = DiscreteDist(weights)
            self.select_cache = lambda: self._caches[randvar.rv() - 1]
        else:
            self.select_cache = lambda: random.choice(self._caches)
Example #2
0
def numeric_cache_hit_ratio_2_layers(pdf, l1_cache, l2_cache,
                                     warmup=None, measure=None, seed=None):
    """Numerically compute the cache hit ratio of a two-layer cache under IRM
    stationary demand with a given pdf.

    Differently from the numeric_cache_hit_ratio function, this function
    allows users to compute the hits at layer 1, layer 2 and overall.

    Parameters
    ----------
    pdf : array-like
        The probability density function of an item being requested
    cache : Cache
        The cache object (i.e. the instance of a class subclassing
        icarus.Cache)
    warmup : int, optional
        The number of warmup requests to generate. If not specified, it is set
        to 10 times the content population
    measure : int, optional
        The number of measured requests to generate. If not specified, it is
        set to 30 times the content population
    seed : int, optional
        The seed used to generate random numbers

    Returns
    -------
    cache_hit_ratio : dict
        Dictionary with keys "l1_hits", "l2_hits" and "total_hits"
    """
    if warmup is None:
        warmup = 10 * len(pdf)
    if measure is None:
        measure = 30 * len(pdf)
    z = DiscreteDist(pdf, seed)
    for _ in range(warmup):
        content = z.rv()
        if not l1_cache.get(content):
            if not l2_cache.get(content):
                l2_cache.put(content)
            l1_cache.put(content)
    l1_hits = 0
    l1_misses = 0
    l2_hits = 0
    for _ in range(measure):
        content = z.rv()
        if l1_cache.get(content):
            l1_hits += 1
        else:
            l1_misses += 1
            if l2_cache.get(content):
                l2_hits += 1
            else:
                l2_cache.put(content)
            l1_cache.put(content)
    return {
        'l1_hits': l1_hits / measure,
        'l2_hits': l2_hits / measure,
        'total_hits': (l1_hits + l2_hits) / measure
           }
def numeric_cache_hit_ratio_2_layers(pdf, l1_cache, l2_cache,
                                     warmup=None, measure=None, seed=None):
    """Numerically compute the cache hit ratio of a two-layer cache under IRM
    stationary demand with a given pdf.
    
    Differently from the numeric_cache_hit_ratio function, this function
    allows users to compute the hits at layer 1, layer 2 and overall.
    
    Parameters
    ----------
    pdf : array-like
        The probability density function of an item being requested
    cache : Cache
        The cache object (i.e. the instance of a class subclassing
        icarus.Cache)
    warmup : int, optional
        The number of warmup requests to generate. If not specified, it is set
        to 10 times the content population
    measure : int, optional
        The number of measured requests to generate. If not specified, it is
        set to 30 times the content population
    seed : int, optional
        The seed used to generate random numbers

    Returns
    -------
    cache_hit_ratio : dict
        Dictionary with keys "l1_hits", "l2_hits" and "total_hits" 
    """
    if warmup is None: warmup = 10*len(pdf)
    if measure is None: measure = 30*len(pdf)
    z = DiscreteDist(pdf, seed)
    for _ in range(warmup):
        content = z.rv()
        if not l1_cache.get(content):
            if not l2_cache.get(content):
                l2_cache.put(content)
            l1_cache.put(content)
    l1_hits = 0
    l1_misses = 0
    l2_hits = 0
    for _ in range(measure):
        content = z.rv()
        if l1_cache.get(content): 
            l1_hits += 1
        else:
            l1_misses += 1
            if l2_cache.get(content):
                l2_hits += 1
            else:
                l2_cache.put(content)
            l1_cache.put(content)
    return {
        'l1_hits': l1_hits/measure,
        'l2_hits': l2_hits/measure,
        'total_hits': (l1_hits+l2_hits)/measure
           }
Example #4
0
def numeric_per_content_cache_hit_ratio(pdf,
                                        cache,
                                        warmup=None,
                                        measure=None,
                                        seed=None,
                                        target=None):
    """Numerically compute the per-content cache hit ratio of a cache under IRM
    stationary demand with a given pdf.

    Parameters
    ----------
    pdf : array-like
        The probability density function of an item being requested
    cache : Cache
        The cache object (i.e. the instance of a class subclassing
        icarus.Cache)
    warmup : int, optional
        The number of warmup requests to generate. If not specified, it is set
        to 10 times the content population
    measure : int, optional
        The number of measured requests to generate. If not specified, it is
        set to 30 times the content population
    seed : int, optional
        The seed used to generate random numbers
    target : int, optional
        The item index [1, N] for which cache hit ratio is requested. If not
        specified, the function calculates the cache hit ratio of all the items
        in the population.

    Returns
    -------
    cache_hit_ratio : array of float or float
        If target is None, returns an array with the cache hit ratios of all
        items in the population. If a target is specified, then it returns
        the cache hit ratio of only the specified item.
    """
    if warmup is None:
        warmup = 10 * len(pdf)
    if measure is None:
        measure = 30 * len(pdf)
    z = DiscreteDist(pdf, seed)
    for _ in range(warmup):
        content = z.rv()
        if not cache.get(content):
            cache.put(content)
    cache_hits = np.zeros(len(pdf))
    requests = np.zeros(len(pdf))
    for _ in range(measure):
        content = z.rv()
        requests[content - 1] += 1
        if cache.get(content):
            cache_hits[content - 1] += 1
        else:
            cache.put(content)
    hit_ratio = np.where(requests > 0, cache_hits / requests, requests)
    return hit_ratio if target is None else hit_ratio[target - 1]
def numeric_per_content_cache_hit_ratio(pdf, cache, warmup=None, measure=None,
                                        seed=None, target=None):
    """Numerically compute the per-content cache hit ratio of a cache under IRM
    stationary demand with a given pdf.
    
    Parameters
    ----------
    pdf : array-like
        The probability density function of an item being requested
    cache : Cache
        The cache object (i.e. the instance of a class subclassing
        icarus.Cache)
    warmup : int, optional
        The number of warmup requests to generate. If not specified, it is set
        to 10 times the content population
    measure : int, optional
        The number of measured requests to generate. If not specified, it is
        set to 30 times the content population
    seed : int, optional
        The seed used to generate random numbers
    target : int, optional
        The item index [1, N] for which cache hit ratio is requested. If not
        specified, the function calculates the cache hit ratio of all the items
        in the population.
    
    Returns
    -------
    cache_hit_ratio : array of float or float
        If target is None, returns an array with the cache hit ratios of all
        items in the population. If a target is specified, then it returns
        the cache hit ratio of only the specified item.
    """
    if warmup is None: warmup = 10*len(pdf)
    if measure is None: measure = 30*len(pdf)
    z = DiscreteDist(pdf, seed)
    for _ in range(warmup):
        content = z.rv()
        if not cache.get(content):
            cache.put(content)
    cache_hits = np.zeros(len(pdf))
    requests = np.zeros(len(pdf))
    for _ in range(measure):
        content = z.rv()
        requests[content-1] += 1
        if cache.get(content): 
            cache_hits[content-1] += 1
        else:
            cache.put(content)
    hit_ratio = np.where(requests > 0, cache_hits/requests, requests) 
    return hit_ratio if target is None else hit_ratio[target-1]
Example #6
0
def numeric_cache_hit_ratio(pdf, cache, warmup=None, measure=None, seed=None):
    """Numerically compute the cache hit ratio of a cache under IRM
    stationary demand with a given pdf.

    Parameters
    ----------
    pdf : array-like
        The probability density function of an item being requested
    cache : Cache
        The cache object (i.e. the instance of a class subclassing
        icarus.Cache)
    warmup : int, optional
        The number of warmup requests to generate. If not specified, it is set
        to 10 times the content population
    measure : int, optional
        The number of measured requests to generate. If not specified, it is
        set to 30 times the content population
    seed : int, optional
        The seed used to generate random numbers

    Returns
    -------
    cache_hit_ratio : float
        The cache hit ratio
    """
    if warmup is None:
        warmup = 10 * len(pdf)
    if measure is None:
        measure = 30 * len(pdf)
    z = DiscreteDist(pdf, seed)
    for _ in range(warmup):
        content = z.rv()
        if not cache.get(content):
            cache.put(content)
    cache_hits = 0
    for _ in range(measure):
        content = z.rv()
        if cache.get(content):
            cache_hits += 1
        else:
            cache.put(content)
    return cache_hits / measure
def numeric_cache_hit_ratio(pdf, cache, warmup=None, measure=None, seed=None):
    """Numerically compute the cache hit ratio of a cache under IRM
    stationary demand with a given pdf.
    
    Parameters
    ----------
    pdf : array-like
        The probability density function of an item being requested
    cache : Cache
        The cache object (i.e. the instance of a class subclassing
        icarus.Cache)
    warmup : int, optional
        The number of warmup requests to generate. If not specified, it is set
        to 10 times the content population
    measure : int, optional
        The number of measured requests to generate. If not specified, it is
        set to 30 times the content population
    seed : int, optional
        The seed used to generate random numbers

    Returns
    -------
    cache_hit_ratio : float
        The cache hit ratio
    """
    if warmup is None: warmup = 10*len(pdf)
    if measure is None: measure = 30*len(pdf)
    z = DiscreteDist(pdf, seed)
    for _ in range(warmup):
        content = z.rv()
        if not cache.get(content):
            cache.put(content)
    cache_hits = 0
    for _ in range(measure):
        content = z.rv()
        if cache.get(content): 
            cache_hits += 1
        else:
            cache.put(content)
    return cache_hits/measure
Example #8
0
    def __init__(self, caches, weights=None, **kwargs):
        """Constructor

        Parameters
        ----------
        caches : array-like
            An array of caching nodes instances on the array
        weights : array-like
            Random weights according to which a cache of the array should be
            selected to process a given request
        """
        self._caches = caches
        self._len = sum(len(c) for c in caches)
        self._n_caches = len(caches)
        self._selected_cache = None
        if weights is not None:
            if np.abs(np.sum(weights) - 1) > 0.0001:
                raise ValueError("weights must sum up to 1")
            if len(weights) != self._n_caches:
                raise ValueError("weights must have as many elements as nr of caches")
            randvar = DiscreteDist(weights)
            self.select_cache = lambda: self._caches[randvar.rv() - 1]
        else:
            self.select_cache = lambda: random.choice(self._caches)