/* lru_cache.c This file is part of DRBD by Philipp Reisner and Lars Ellenberg. Copyright (C) 2003-2008, LINBIT Information Technologies GmbH. Copyright (C) 2003-2008, Philipp Reisner . Copyright (C) 2003-2008, Lars Ellenberg . drbd is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. drbd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with drbd; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include /* for memset */ #include /* for seq_printf */ #include MODULE_AUTHOR("Philipp Reisner , " "Lars Ellenberg "); MODULE_DESCRIPTION("lru_cache - Track sets of hot objects"); MODULE_LICENSE("GPL"); /* this is developers aid only. * it catches concurrent access (lack of locking on the users part) */ #define PARANOIA_ENTRY() do { \ BUG_ON(!lc); \ BUG_ON(!lc->nr_elements); \ BUG_ON(test_and_set_bit(__LC_PARANOIA, &lc->flags)); \ } while (0) #define RETURN(x...) do { \ clear_bit_unlock(__LC_PARANOIA, &lc->flags); \ return x ; } while (0) /* BUG() if e is not one of the elements tracked by lc */ #define PARANOIA_LC_ELEMENT(lc, e) do { \ struct lru_cache *lc_ = (lc); \ struct lc_element *e_ = (e); \ unsigned i = e_->lc_index; \ BUG_ON(i >= lc_->nr_elements); \ BUG_ON(lc_->lc_element[i] != e_); } while (0) /* We need to atomically * - try to grab the lock (set LC_LOCKED) * - only if there is no pending transaction * (neither LC_DIRTY nor LC_STARVING is set) * Because of PARANOIA_ENTRY() above abusing lc->flags as well, * it is not sufficient to just say * return 0 == cmpxchg(&lc->flags, 0, LC_LOCKED); */ int lc_try_lock(struct lru_cache *lc) { unsigned long val; do { val = cmpxchg(&lc->flags, 0, LC_LOCKED); } while (unlikely (val == LC_PARANOIA)); /* Spin until no-one is inside a PARANOIA_ENTRY()/RETURN() section. */ return 0 == val; #if 0 /* Alternative approach, spin in case someone enters or leaves a * PARANOIA_ENTRY()/RETURN() section. */ unsigned long old, new, val; do { old = lc->flags & LC_PARANOIA; new = old | LC_LOCKED; val = cmpxchg(&lc->flags, old, new); } while (unlikely (val == (old ^ LC_PARANOIA))); return old == val; #endif } /** * lc_create - prepares to track objects in an active set * @name: descriptive name only used in lc_seq_printf_stats and lc_seq_dump_details * @max_pending_changes: maximum changes to accumulate until a transaction is required * @e_count: number of elements allowed to be active simultaneously * @e_size: size of the tracked objects * @e_off: offset to the &struct lc_element member in a tracked object * * Returns a pointer to a newly initialized struct lru_cache on success, * or NULL on (allocation) failure. */ struct lru_cache *lc_create(const char *name, struct kmem_cache *cache, unsigned max_pending_changes, unsigned e_count, size_t e_size, size_t e_off) { struct hlist_head *slot = NULL; struct lc_element **element = NULL; struct lru_cache *lc; struct lc_element *e; unsigned cache_obj_size = kmem_cache_size(cache); unsigned i; WARN_ON(cache_obj_size < e_size); if (cache_obj_size < e_size) return NULL; /* e_count too big; would probably fail the allocation below anyways. * for typical use cases, e_count should be few thousand at most. */ if (e_count > LC_MAX_ACTIVE) return NULL; slot = kcalloc(e_count, sizeof(struct hlist_head), GFP_KERNEL); if (!slot) goto out_fail; element = kzalloc(e_count * sizeof(struct lc_element *), GFP_KERNEL); if (!element) goto out_fail; lc = kzalloc(sizeof(*lc), GFP_KERNEL); if (!lc) goto out_fail; INIT_LIST_HEAD(&lc->in_use); INIT_LIST_HEAD(&lc->lru); INIT_LIST_HEAD(&lc->free); INIT_LIST_HEAD(&lc->to_be_changed); lc->name = name; lc->element_size = e_size; lc->element_off = e_off; lc->nr_elements = e_count; lc->max_pending_changes = max_pending_changes; lc->lc_cache = cache; lc->lc_element = element; lc->lc_slot = slot; /* preallocate all objects */ for (i = 0; i < e_count; i++) { void *p = kmem_cache_alloc(cache, GFP_KERNEL); if (!p) break; memset(p, 0, lc->element_size); e = p + e_off; e->lc_index = i; e->lc_number = LC_FREE; e->lc_new_number = LC_FREE; list_add(&e->list, &lc->free); element[i] = e; } if (i == e_count) return lc; /* else: could not allocate all elements, give up */ for (i--; i; i--) { void *p = element[i]; kmem_cache_free(cache, p - e_off); } kfree(lc); out_fail: kfree(element); kfree(slot); return NULL; } static void lc_free_by_index(struct lru_cache *lc, unsigned i) { void *p = lc->lc_element[i]; WARN_ON(!p); if (
##############################################################################
# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
---
# Sample benchmark task config file
# measure network latency using ping

schema: "yardstick:task:0.1"

scenarios:
-
  type: Ping
  options:
    packetsize: 200
  host: athena.demo
  target: ares.demo

  runner:
    type: Iteration
    iterations: 60
    interval: 1

  sla:
    max_rtt: 10
    action: monitor

context:
  name: demo
  image: yardstick-image
  flavor: yardstick-flavor
  user: ubuntu

  placement_groups:
    pgrp1:
      policy: "availability"

  servers:
    athena:
      floating_ip: true
      placement: "pgrp1"
    ares:
      placement: "pgrp1"

  networks:
    test:
      cidr: '10.0.1.0/24'
RANOIA_ENTRY(); list_for_each_entry_safe(e, tmp, &lc->to_be_changed, list) { /* count number of changes, not number of transactions */ ++lc->changed; e->lc_number = e->lc_new_number; list_move(&e->list, &lc->in_use); } lc->pending_changes = 0; RETURN(); } /** * lc_put - give up refcnt of @e * @lc: the lru cache to operate on * @e: the element to put * * If refcnt reaches zero, the element is moved to the lru list, * and a %LC_STARVING (if set) is cleared. * Returns the new (post-decrement) refcnt. */ unsigned int lc_put(struct lru_cache *lc, struct lc_element *e) { PARANOIA_ENTRY(); PARANOIA_LC_ELEMENT(lc, e); BUG_ON(e->refcnt == 0); BUG_ON(e->lc_number != e->lc_new_number); if (--e->refcnt == 0) { /* move it to the front of LRU. */ list_move(&e->list, &lc->lru); lc->used--; clear_bit_unlock(__LC_STARVING, &lc->flags); } RETURN(e->refcnt); } /** * lc_element_by_index * @lc: the lru cache to operate on * @i: the index of the element to return */ struct lc_element *lc_element_by_index(struct lru_cache *lc, unsigned i) { BUG_ON(i >= lc->nr_elements); BUG_ON(lc->lc_element[i] == NULL); BUG_ON(lc->lc_element[i]->lc_index != i); return lc->lc_element[i]; } /** * lc_index_of * @lc: the lru cache to operate on * @e: the element to query for its index position in lc->element */ unsigned int lc_index_of(struct lru_cache *lc, struct lc_element *e) { PARANOIA_LC_ELEMENT(lc, e); return e->lc_index; } /** * lc_set - associate index with label * @lc: the lru cache to operate on * @enr: the label to set * @index: the element index to associate label with. * * Used to initialize the active set to some previously recorded state. */ void lc_set(struct lru_cache *lc, unsigned int enr, int index) { struct lc_element *e; struct list_head *lh; if (index < 0 || index >= lc->nr_elements) return; e = lc_element_by_index(lc, index); BUG_ON(e->lc_number != e->lc_new_number); BUG_ON(e->refcnt != 0); e->lc_number = e->lc_new_number = enr; hlist_del_init(&e->colision); if (enr == LC_FREE) lh = &lc->free; else { hlist_add_head(&e->colision, lc_hash_slot(lc, enr)); lh = &lc->lru; } list_move(&e->list, lh); } /** * lc_dump - Dump a complete LRU cache to seq in textual form. * @lc: the lru cache to operate on * @seq: the &struct seq_file pointer to seq_printf into * @utext: user supplied additional "heading" or other info * @detail: function pointer the user may provide to dump further details * of the object the lc_element is embedded in. May be NULL. * Note: a leading space ' ' and trailing newline '\n' is implied. */ void lc_seq_dump_details(struct seq_file *seq, struct lru_cache *lc, char *utext, void (*detail) (struct seq_file *, struct lc_element *)) { unsigned int nr_elements = lc->nr_elements; struct lc_element *e; int i; seq_printf(seq, "\tnn: lc_number (new nr) refcnt %s\n ", utext); for (i = 0; i < nr_elements; i++) { e = lc_element_by_index(lc, i); if (e->lc_number != e->lc_new_number) seq_printf(seq, "\t%5d: %6d %8d %6d ", i, e->lc_number, e->lc_new_number, e->refcnt); else seq_printf(seq, "\t%5d: %6d %-8s %6d ", i, e->lc_number, "-\"-", e->refcnt); if (detail) detail(seq, e); seq_putc(seq, '\n'); } } EXPORT_SYMBOL(lc_create); EXPORT_SYMBOL(lc_reset); EXPORT_SYMBOL(lc_destroy); EXPORT_SYMBOL(lc_set); EXPORT_SYMBOL(lc_del); EXPORT_SYMBOL(lc_try_get); EXPORT_SYMBOL(lc_find); EXPORT_SYMBOL(lc_get); EXPORT_SYMBOL(lc_put); EXPORT_SYMBOL(lc_committed); EXPORT_SYMBOL(lc_element_by_index); EXPORT_SYMBOL(lc_index_of); EXPORT_SYMBOL(lc_seq_printf_stats); EXPORT_SYMBOL(lc_seq_dump_details); EXPORT_SYMBOL(lc_try_lock); EXPORT_SYMBOL(lc_is_used); EXPORT_SYMBOL(lc_get_cumulative);