xref: /openbmc/qemu/contrib/plugins/cache.c (revision 40a770ea8b9478aefa3a60049bc67cc04ace569c)
1e2c5557cSMahmoud Mandour /*
2e2c5557cSMahmoud Mandour  * Copyright (C) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
3e2c5557cSMahmoud Mandour  *
4e2c5557cSMahmoud Mandour  * License: GNU GPL, version 2 or later.
5e2c5557cSMahmoud Mandour  *   See the COPYING file in the top-level directory.
6e2c5557cSMahmoud Mandour  */
7e2c5557cSMahmoud Mandour 
8e2c5557cSMahmoud Mandour #include <inttypes.h>
9e2c5557cSMahmoud Mandour #include <stdio.h>
10e2c5557cSMahmoud Mandour #include <glib.h>
11e2c5557cSMahmoud Mandour 
12e2c5557cSMahmoud Mandour #include <qemu-plugin.h>
13e2c5557cSMahmoud Mandour 
1453366adfSMahmoud Mandour #define STRTOLL(x) g_ascii_strtoll(x, NULL, 10)
1553366adfSMahmoud Mandour 
16e2c5557cSMahmoud Mandour QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
17e2c5557cSMahmoud Mandour 
18e2c5557cSMahmoud Mandour static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
19e2c5557cSMahmoud Mandour 
20e2c5557cSMahmoud Mandour static GHashTable *miss_ht;
21e2c5557cSMahmoud Mandour 
22c79a2116SMahmoud Mandour static GMutex hashtable_lock;
23e2c5557cSMahmoud Mandour static GRand *rng;
24e2c5557cSMahmoud Mandour 
25e2c5557cSMahmoud Mandour static int limit;
26e2c5557cSMahmoud Mandour static bool sys;
27e2c5557cSMahmoud Mandour 
2840c4a553SMahmoud Mandour enum EvictionPolicy {
2940c4a553SMahmoud Mandour     LRU,
3040c4a553SMahmoud Mandour     FIFO,
3140c4a553SMahmoud Mandour     RAND,
3240c4a553SMahmoud Mandour };
3340c4a553SMahmoud Mandour 
3440c4a553SMahmoud Mandour enum EvictionPolicy policy;
3540c4a553SMahmoud Mandour 
36e2c5557cSMahmoud Mandour /*
37e2c5557cSMahmoud Mandour  * A CacheSet is a set of cache blocks. A memory block that maps to a set can be
38e2c5557cSMahmoud Mandour  * put in any of the blocks inside the set. The number of block per set is
39e2c5557cSMahmoud Mandour  * called the associativity (assoc).
40e2c5557cSMahmoud Mandour  *
417a21bee2SDaniel P. Berrangé  * Each block contains the stored tag and a valid bit. Since this is not
42e2c5557cSMahmoud Mandour  * a functional simulator, the data itself is not stored. We only identify
43e2c5557cSMahmoud Mandour  * whether a block is in the cache or not by searching for its tag.
44e2c5557cSMahmoud Mandour  *
45e2c5557cSMahmoud Mandour  * In order to search for memory data in the cache, the set identifier and tag
46e2c5557cSMahmoud Mandour  * are extracted from the address and the set is probed to see whether a tag
47e2c5557cSMahmoud Mandour  * match occur.
48e2c5557cSMahmoud Mandour  *
49e2c5557cSMahmoud Mandour  * An address is logically divided into three portions: The block offset,
50e2c5557cSMahmoud Mandour  * the set number, and the tag.
51e2c5557cSMahmoud Mandour  *
52e2c5557cSMahmoud Mandour  * The set number is used to identify the set in which the block may exist.
53e2c5557cSMahmoud Mandour  * The tag is compared against all the tags of a set to search for a match. If a
54e2c5557cSMahmoud Mandour  * match is found, then the access is a hit.
5540c4a553SMahmoud Mandour  *
5640c4a553SMahmoud Mandour  * The CacheSet also contains bookkeaping information about eviction details.
57e2c5557cSMahmoud Mandour  */
58e2c5557cSMahmoud Mandour 
59e2c5557cSMahmoud Mandour typedef struct {
60e2c5557cSMahmoud Mandour     uint64_t tag;
61e2c5557cSMahmoud Mandour     bool valid;
62e2c5557cSMahmoud Mandour } CacheBlock;
63e2c5557cSMahmoud Mandour 
64e2c5557cSMahmoud Mandour typedef struct {
65e2c5557cSMahmoud Mandour     CacheBlock *blocks;
6640c4a553SMahmoud Mandour     uint64_t *lru_priorities;
6740c4a553SMahmoud Mandour     uint64_t lru_gen_counter;
6840c4a553SMahmoud Mandour     GQueue *fifo_queue;
69e2c5557cSMahmoud Mandour } CacheSet;
70e2c5557cSMahmoud Mandour 
71e2c5557cSMahmoud Mandour typedef struct {
72e2c5557cSMahmoud Mandour     CacheSet *sets;
73e2c5557cSMahmoud Mandour     int num_sets;
74e2c5557cSMahmoud Mandour     int cachesize;
75e2c5557cSMahmoud Mandour     int assoc;
76e2c5557cSMahmoud Mandour     int blksize_shift;
77e2c5557cSMahmoud Mandour     uint64_t set_mask;
78e2c5557cSMahmoud Mandour     uint64_t tag_mask;
79c79a2116SMahmoud Mandour     uint64_t accesses;
80c79a2116SMahmoud Mandour     uint64_t misses;
81e2c5557cSMahmoud Mandour } Cache;
82e2c5557cSMahmoud Mandour 
83e2c5557cSMahmoud Mandour typedef struct {
84e2c5557cSMahmoud Mandour     char *disas_str;
85e2c5557cSMahmoud Mandour     const char *symbol;
86e2c5557cSMahmoud Mandour     uint64_t addr;
8714f3110aSMahmoud Mandour     uint64_t l1_dmisses;
8814f3110aSMahmoud Mandour     uint64_t l1_imisses;
8914f3110aSMahmoud Mandour     uint64_t l2_misses;
90e2c5557cSMahmoud Mandour } InsnData;
91e2c5557cSMahmoud Mandour 
9240c4a553SMahmoud Mandour void (*update_hit)(Cache *cache, int set, int blk);
9340c4a553SMahmoud Mandour void (*update_miss)(Cache *cache, int set, int blk);
9440c4a553SMahmoud Mandour 
9540c4a553SMahmoud Mandour void (*metadata_init)(Cache *cache);
9640c4a553SMahmoud Mandour void (*metadata_destroy)(Cache *cache);
9740c4a553SMahmoud Mandour 
98c79a2116SMahmoud Mandour static int cores;
9914f3110aSMahmoud Mandour static Cache **l1_dcaches, **l1_icaches;
100447f9356SMahmoud Mandour 
101447f9356SMahmoud Mandour static bool use_l2;
10214f3110aSMahmoud Mandour static Cache **l2_ucaches;
103c79a2116SMahmoud Mandour 
10414f3110aSMahmoud Mandour static GMutex *l1_dcache_locks;
10514f3110aSMahmoud Mandour static GMutex *l1_icache_locks;
10614f3110aSMahmoud Mandour static GMutex *l2_ucache_locks;
107c79a2116SMahmoud Mandour 
10814f3110aSMahmoud Mandour static uint64_t l1_dmem_accesses;
10914f3110aSMahmoud Mandour static uint64_t l1_imem_accesses;
11014f3110aSMahmoud Mandour static uint64_t l1_imisses;
11114f3110aSMahmoud Mandour static uint64_t l1_dmisses;
11214f3110aSMahmoud Mandour 
11314f3110aSMahmoud Mandour static uint64_t l2_mem_accesses;
11414f3110aSMahmoud Mandour static uint64_t l2_misses;
115e2c5557cSMahmoud Mandour 
pow_of_two(int num)116e2c5557cSMahmoud Mandour static int pow_of_two(int num)
117e2c5557cSMahmoud Mandour {
118e2c5557cSMahmoud Mandour     g_assert((num & (num - 1)) == 0);
119e2c5557cSMahmoud Mandour     int ret = 0;
120e2c5557cSMahmoud Mandour     while (num /= 2) {
121e2c5557cSMahmoud Mandour         ret++;
122e2c5557cSMahmoud Mandour     }
123e2c5557cSMahmoud Mandour     return ret;
124e2c5557cSMahmoud Mandour }
125e2c5557cSMahmoud Mandour 
12640c4a553SMahmoud Mandour /*
12740c4a553SMahmoud Mandour  * LRU evection policy: For each set, a generation counter is maintained
12840c4a553SMahmoud Mandour  * alongside a priority array.
12940c4a553SMahmoud Mandour  *
13040c4a553SMahmoud Mandour  * On each set access, the generation counter is incremented.
13140c4a553SMahmoud Mandour  *
13240c4a553SMahmoud Mandour  * On a cache hit: The hit-block is assigned the current generation counter,
13340c4a553SMahmoud Mandour  * indicating that it is the most recently used block.
13440c4a553SMahmoud Mandour  *
13540c4a553SMahmoud Mandour  * On a cache miss: The block with the least priority is searched and replaced
13640c4a553SMahmoud Mandour  * with the newly-cached block, of which the priority is set to the current
13740c4a553SMahmoud Mandour  * generation number.
13840c4a553SMahmoud Mandour  */
13940c4a553SMahmoud Mandour 
lru_priorities_init(Cache * cache)14040c4a553SMahmoud Mandour static void lru_priorities_init(Cache *cache)
14140c4a553SMahmoud Mandour {
14240c4a553SMahmoud Mandour     int i;
14340c4a553SMahmoud Mandour 
14440c4a553SMahmoud Mandour     for (i = 0; i < cache->num_sets; i++) {
14540c4a553SMahmoud Mandour         cache->sets[i].lru_priorities = g_new0(uint64_t, cache->assoc);
14640c4a553SMahmoud Mandour         cache->sets[i].lru_gen_counter = 0;
14740c4a553SMahmoud Mandour     }
14840c4a553SMahmoud Mandour }
14940c4a553SMahmoud Mandour 
lru_update_blk(Cache * cache,int set_idx,int blk_idx)15040c4a553SMahmoud Mandour static void lru_update_blk(Cache *cache, int set_idx, int blk_idx)
15140c4a553SMahmoud Mandour {
15240c4a553SMahmoud Mandour     CacheSet *set = &cache->sets[set_idx];
15340c4a553SMahmoud Mandour     set->lru_priorities[blk_idx] = cache->sets[set_idx].lru_gen_counter;
15440c4a553SMahmoud Mandour     set->lru_gen_counter++;
15540c4a553SMahmoud Mandour }
15640c4a553SMahmoud Mandour 
lru_get_lru_block(Cache * cache,int set_idx)15740c4a553SMahmoud Mandour static int lru_get_lru_block(Cache *cache, int set_idx)
15840c4a553SMahmoud Mandour {
15940c4a553SMahmoud Mandour     int i, min_idx, min_priority;
16040c4a553SMahmoud Mandour 
16140c4a553SMahmoud Mandour     min_priority = cache->sets[set_idx].lru_priorities[0];
16240c4a553SMahmoud Mandour     min_idx = 0;
16340c4a553SMahmoud Mandour 
16440c4a553SMahmoud Mandour     for (i = 1; i < cache->assoc; i++) {
16540c4a553SMahmoud Mandour         if (cache->sets[set_idx].lru_priorities[i] < min_priority) {
16640c4a553SMahmoud Mandour             min_priority = cache->sets[set_idx].lru_priorities[i];
16740c4a553SMahmoud Mandour             min_idx = i;
16840c4a553SMahmoud Mandour         }
16940c4a553SMahmoud Mandour     }
17040c4a553SMahmoud Mandour     return min_idx;
17140c4a553SMahmoud Mandour }
17240c4a553SMahmoud Mandour 
lru_priorities_destroy(Cache * cache)17340c4a553SMahmoud Mandour static void lru_priorities_destroy(Cache *cache)
17440c4a553SMahmoud Mandour {
17540c4a553SMahmoud Mandour     int i;
17640c4a553SMahmoud Mandour 
17740c4a553SMahmoud Mandour     for (i = 0; i < cache->num_sets; i++) {
17840c4a553SMahmoud Mandour         g_free(cache->sets[i].lru_priorities);
17940c4a553SMahmoud Mandour     }
18040c4a553SMahmoud Mandour }
18140c4a553SMahmoud Mandour 
18240c4a553SMahmoud Mandour /*
18340c4a553SMahmoud Mandour  * FIFO eviction policy: a FIFO queue is maintained for each CacheSet that
18440c4a553SMahmoud Mandour  * stores accesses to the cache.
18540c4a553SMahmoud Mandour  *
18640c4a553SMahmoud Mandour  * On a compulsory miss: The block index is enqueued to the fifo_queue to
18740c4a553SMahmoud Mandour  * indicate that it's the latest cached block.
18840c4a553SMahmoud Mandour  *
18940c4a553SMahmoud Mandour  * On a conflict miss: The first-in block is removed from the cache and the new
19040c4a553SMahmoud Mandour  * block is put in its place and enqueued to the FIFO queue.
19140c4a553SMahmoud Mandour  */
19240c4a553SMahmoud Mandour 
fifo_init(Cache * cache)19340c4a553SMahmoud Mandour static void fifo_init(Cache *cache)
19440c4a553SMahmoud Mandour {
19540c4a553SMahmoud Mandour     int i;
19640c4a553SMahmoud Mandour 
19740c4a553SMahmoud Mandour     for (i = 0; i < cache->num_sets; i++) {
19840c4a553SMahmoud Mandour         cache->sets[i].fifo_queue = g_queue_new();
19940c4a553SMahmoud Mandour     }
20040c4a553SMahmoud Mandour }
20140c4a553SMahmoud Mandour 
fifo_get_first_block(Cache * cache,int set)20240c4a553SMahmoud Mandour static int fifo_get_first_block(Cache *cache, int set)
20340c4a553SMahmoud Mandour {
20440c4a553SMahmoud Mandour     GQueue *q = cache->sets[set].fifo_queue;
20540c4a553SMahmoud Mandour     return GPOINTER_TO_INT(g_queue_pop_tail(q));
20640c4a553SMahmoud Mandour }
20740c4a553SMahmoud Mandour 
fifo_update_on_miss(Cache * cache,int set,int blk_idx)20840c4a553SMahmoud Mandour static void fifo_update_on_miss(Cache *cache, int set, int blk_idx)
20940c4a553SMahmoud Mandour {
21040c4a553SMahmoud Mandour     GQueue *q = cache->sets[set].fifo_queue;
21140c4a553SMahmoud Mandour     g_queue_push_head(q, GINT_TO_POINTER(blk_idx));
21240c4a553SMahmoud Mandour }
21340c4a553SMahmoud Mandour 
fifo_destroy(Cache * cache)21440c4a553SMahmoud Mandour static void fifo_destroy(Cache *cache)
21540c4a553SMahmoud Mandour {
21640c4a553SMahmoud Mandour     int i;
21740c4a553SMahmoud Mandour 
218de9fc40fSMahmoud Mandour     for (i = 0; i < cache->num_sets; i++) {
21940c4a553SMahmoud Mandour         g_queue_free(cache->sets[i].fifo_queue);
22040c4a553SMahmoud Mandour     }
22140c4a553SMahmoud Mandour }
22240c4a553SMahmoud Mandour 
extract_tag(Cache * cache,uint64_t addr)223e2c5557cSMahmoud Mandour static inline uint64_t extract_tag(Cache *cache, uint64_t addr)
224e2c5557cSMahmoud Mandour {
225e2c5557cSMahmoud Mandour     return addr & cache->tag_mask;
226e2c5557cSMahmoud Mandour }
227e2c5557cSMahmoud Mandour 
extract_set(Cache * cache,uint64_t addr)228e2c5557cSMahmoud Mandour static inline uint64_t extract_set(Cache *cache, uint64_t addr)
229e2c5557cSMahmoud Mandour {
230e2c5557cSMahmoud Mandour     return (addr & cache->set_mask) >> cache->blksize_shift;
231e2c5557cSMahmoud Mandour }
232e2c5557cSMahmoud Mandour 
cache_config_error(int blksize,int assoc,int cachesize)23386ae3a1dSMahmoud Mandour static const char *cache_config_error(int blksize, int assoc, int cachesize)
23486ae3a1dSMahmoud Mandour {
23586ae3a1dSMahmoud Mandour     if (cachesize % blksize != 0) {
23686ae3a1dSMahmoud Mandour         return "cache size must be divisible by block size";
23786ae3a1dSMahmoud Mandour     } else if (cachesize % (blksize * assoc) != 0) {
23886ae3a1dSMahmoud Mandour         return "cache size must be divisible by set size (assoc * block size)";
23986ae3a1dSMahmoud Mandour     } else {
24086ae3a1dSMahmoud Mandour         return NULL;
24186ae3a1dSMahmoud Mandour     }
24286ae3a1dSMahmoud Mandour }
24386ae3a1dSMahmoud Mandour 
bad_cache_params(int blksize,int assoc,int cachesize)24486ae3a1dSMahmoud Mandour static bool bad_cache_params(int blksize, int assoc, int cachesize)
24586ae3a1dSMahmoud Mandour {
24686ae3a1dSMahmoud Mandour     return (cachesize % blksize) != 0 || (cachesize % (blksize * assoc) != 0);
24786ae3a1dSMahmoud Mandour }
24886ae3a1dSMahmoud Mandour 
cache_init(int blksize,int assoc,int cachesize)249e2c5557cSMahmoud Mandour static Cache *cache_init(int blksize, int assoc, int cachesize)
250e2c5557cSMahmoud Mandour {
251e2c5557cSMahmoud Mandour     Cache *cache;
252e2c5557cSMahmoud Mandour     int i;
253e2c5557cSMahmoud Mandour     uint64_t blk_mask;
254e2c5557cSMahmoud Mandour 
255c79a2116SMahmoud Mandour     /*
256c79a2116SMahmoud Mandour      * This function shall not be called directly, and hence expects suitable
257c79a2116SMahmoud Mandour      * parameters.
258c79a2116SMahmoud Mandour      */
259c79a2116SMahmoud Mandour     g_assert(!bad_cache_params(blksize, assoc, cachesize));
260c79a2116SMahmoud Mandour 
261e2c5557cSMahmoud Mandour     cache = g_new(Cache, 1);
262e2c5557cSMahmoud Mandour     cache->assoc = assoc;
263e2c5557cSMahmoud Mandour     cache->cachesize = cachesize;
264e2c5557cSMahmoud Mandour     cache->num_sets = cachesize / (blksize * assoc);
265e2c5557cSMahmoud Mandour     cache->sets = g_new(CacheSet, cache->num_sets);
266e2c5557cSMahmoud Mandour     cache->blksize_shift = pow_of_two(blksize);
267c79a2116SMahmoud Mandour     cache->accesses = 0;
268c79a2116SMahmoud Mandour     cache->misses = 0;
269e2c5557cSMahmoud Mandour 
270e2c5557cSMahmoud Mandour     for (i = 0; i < cache->num_sets; i++) {
271e2c5557cSMahmoud Mandour         cache->sets[i].blocks = g_new0(CacheBlock, assoc);
272e2c5557cSMahmoud Mandour     }
273e2c5557cSMahmoud Mandour 
274e2c5557cSMahmoud Mandour     blk_mask = blksize - 1;
275e2c5557cSMahmoud Mandour     cache->set_mask = ((cache->num_sets - 1) << cache->blksize_shift);
276e2c5557cSMahmoud Mandour     cache->tag_mask = ~(cache->set_mask | blk_mask);
27740c4a553SMahmoud Mandour 
27840c4a553SMahmoud Mandour     if (metadata_init) {
27940c4a553SMahmoud Mandour         metadata_init(cache);
28040c4a553SMahmoud Mandour     }
28140c4a553SMahmoud Mandour 
282e2c5557cSMahmoud Mandour     return cache;
283e2c5557cSMahmoud Mandour }
284e2c5557cSMahmoud Mandour 
caches_init(int blksize,int assoc,int cachesize)285c79a2116SMahmoud Mandour static Cache **caches_init(int blksize, int assoc, int cachesize)
286c79a2116SMahmoud Mandour {
287c79a2116SMahmoud Mandour     Cache **caches;
288c79a2116SMahmoud Mandour     int i;
289c79a2116SMahmoud Mandour 
290c79a2116SMahmoud Mandour     if (bad_cache_params(blksize, assoc, cachesize)) {
291c79a2116SMahmoud Mandour         return NULL;
292c79a2116SMahmoud Mandour     }
293c79a2116SMahmoud Mandour 
294c79a2116SMahmoud Mandour     caches = g_new(Cache *, cores);
295c79a2116SMahmoud Mandour 
296c79a2116SMahmoud Mandour     for (i = 0; i < cores; i++) {
297c79a2116SMahmoud Mandour         caches[i] = cache_init(blksize, assoc, cachesize);
298c79a2116SMahmoud Mandour     }
299c79a2116SMahmoud Mandour 
300c79a2116SMahmoud Mandour     return caches;
301c79a2116SMahmoud Mandour }
302c79a2116SMahmoud Mandour 
get_invalid_block(Cache * cache,uint64_t set)303e2c5557cSMahmoud Mandour static int get_invalid_block(Cache *cache, uint64_t set)
304e2c5557cSMahmoud Mandour {
305e2c5557cSMahmoud Mandour     int i;
306e2c5557cSMahmoud Mandour 
307e2c5557cSMahmoud Mandour     for (i = 0; i < cache->assoc; i++) {
308e2c5557cSMahmoud Mandour         if (!cache->sets[set].blocks[i].valid) {
309e2c5557cSMahmoud Mandour             return i;
310e2c5557cSMahmoud Mandour         }
311e2c5557cSMahmoud Mandour     }
312e2c5557cSMahmoud Mandour 
313e2c5557cSMahmoud Mandour     return -1;
314e2c5557cSMahmoud Mandour }
315e2c5557cSMahmoud Mandour 
get_replaced_block(Cache * cache,int set)31640c4a553SMahmoud Mandour static int get_replaced_block(Cache *cache, int set)
317e2c5557cSMahmoud Mandour {
31840c4a553SMahmoud Mandour     switch (policy) {
31940c4a553SMahmoud Mandour     case RAND:
320e2c5557cSMahmoud Mandour         return g_rand_int_range(rng, 0, cache->assoc);
32140c4a553SMahmoud Mandour     case LRU:
32240c4a553SMahmoud Mandour         return lru_get_lru_block(cache, set);
32340c4a553SMahmoud Mandour     case FIFO:
32440c4a553SMahmoud Mandour         return fifo_get_first_block(cache, set);
32540c4a553SMahmoud Mandour     default:
32640c4a553SMahmoud Mandour         g_assert_not_reached();
32740c4a553SMahmoud Mandour     }
328e2c5557cSMahmoud Mandour }
329e2c5557cSMahmoud Mandour 
in_cache(Cache * cache,uint64_t addr)33040c4a553SMahmoud Mandour static int in_cache(Cache *cache, uint64_t addr)
331e2c5557cSMahmoud Mandour {
332e2c5557cSMahmoud Mandour     int i;
333e2c5557cSMahmoud Mandour     uint64_t tag, set;
334e2c5557cSMahmoud Mandour 
335e2c5557cSMahmoud Mandour     tag = extract_tag(cache, addr);
336e2c5557cSMahmoud Mandour     set = extract_set(cache, addr);
337e2c5557cSMahmoud Mandour 
338e2c5557cSMahmoud Mandour     for (i = 0; i < cache->assoc; i++) {
339e2c5557cSMahmoud Mandour         if (cache->sets[set].blocks[i].tag == tag &&
340e2c5557cSMahmoud Mandour                 cache->sets[set].blocks[i].valid) {
34140c4a553SMahmoud Mandour             return i;
342e2c5557cSMahmoud Mandour         }
343e2c5557cSMahmoud Mandour     }
344e2c5557cSMahmoud Mandour 
34540c4a553SMahmoud Mandour     return -1;
346e2c5557cSMahmoud Mandour }
347e2c5557cSMahmoud Mandour 
348e2c5557cSMahmoud Mandour /**
349e2c5557cSMahmoud Mandour  * access_cache(): Simulate a cache access
350e2c5557cSMahmoud Mandour  * @cache: The cache under simulation
351e2c5557cSMahmoud Mandour  * @addr: The address of the requested memory location
352e2c5557cSMahmoud Mandour  *
353669dcb60SMichael Tokarev  * Returns true if the requested data is hit in the cache and false when missed.
354e2c5557cSMahmoud Mandour  * The cache is updated on miss for the next access.
355e2c5557cSMahmoud Mandour  */
access_cache(Cache * cache,uint64_t addr)356e2c5557cSMahmoud Mandour static bool access_cache(Cache *cache, uint64_t addr)
357e2c5557cSMahmoud Mandour {
35840c4a553SMahmoud Mandour     int hit_blk, replaced_blk;
359e2c5557cSMahmoud Mandour     uint64_t tag, set;
360e2c5557cSMahmoud Mandour 
361e2c5557cSMahmoud Mandour     tag = extract_tag(cache, addr);
362e2c5557cSMahmoud Mandour     set = extract_set(cache, addr);
363e2c5557cSMahmoud Mandour 
36440c4a553SMahmoud Mandour     hit_blk = in_cache(cache, addr);
36540c4a553SMahmoud Mandour     if (hit_blk != -1) {
36640c4a553SMahmoud Mandour         if (update_hit) {
36740c4a553SMahmoud Mandour             update_hit(cache, set, hit_blk);
36840c4a553SMahmoud Mandour         }
36940c4a553SMahmoud Mandour         return true;
37040c4a553SMahmoud Mandour     }
37140c4a553SMahmoud Mandour 
372e2c5557cSMahmoud Mandour     replaced_blk = get_invalid_block(cache, set);
373e2c5557cSMahmoud Mandour 
374e2c5557cSMahmoud Mandour     if (replaced_blk == -1) {
37540c4a553SMahmoud Mandour         replaced_blk = get_replaced_block(cache, set);
37640c4a553SMahmoud Mandour     }
37740c4a553SMahmoud Mandour 
37840c4a553SMahmoud Mandour     if (update_miss) {
37940c4a553SMahmoud Mandour         update_miss(cache, set, replaced_blk);
380e2c5557cSMahmoud Mandour     }
381e2c5557cSMahmoud Mandour 
382e2c5557cSMahmoud Mandour     cache->sets[set].blocks[replaced_blk].tag = tag;
383e2c5557cSMahmoud Mandour     cache->sets[set].blocks[replaced_blk].valid = true;
384e2c5557cSMahmoud Mandour 
385e2c5557cSMahmoud Mandour     return false;
386e2c5557cSMahmoud Mandour }
387e2c5557cSMahmoud Mandour 
vcpu_mem_access(unsigned int vcpu_index,qemu_plugin_meminfo_t info,uint64_t vaddr,void * userdata)388e2c5557cSMahmoud Mandour static void vcpu_mem_access(unsigned int vcpu_index, qemu_plugin_meminfo_t info,
389e2c5557cSMahmoud Mandour                             uint64_t vaddr, void *userdata)
390e2c5557cSMahmoud Mandour {
391e2c5557cSMahmoud Mandour     uint64_t effective_addr;
392e2c5557cSMahmoud Mandour     struct qemu_plugin_hwaddr *hwaddr;
393c79a2116SMahmoud Mandour     int cache_idx;
394e2c5557cSMahmoud Mandour     InsnData *insn;
39514f3110aSMahmoud Mandour     bool hit_in_l1;
396e2c5557cSMahmoud Mandour 
397e2c5557cSMahmoud Mandour     hwaddr = qemu_plugin_get_hwaddr(info, vaddr);
398e2c5557cSMahmoud Mandour     if (hwaddr && qemu_plugin_hwaddr_is_io(hwaddr)) {
399e2c5557cSMahmoud Mandour         return;
400e2c5557cSMahmoud Mandour     }
401e2c5557cSMahmoud Mandour 
402e2c5557cSMahmoud Mandour     effective_addr = hwaddr ? qemu_plugin_hwaddr_phys_addr(hwaddr) : vaddr;
403c79a2116SMahmoud Mandour     cache_idx = vcpu_index % cores;
404e2c5557cSMahmoud Mandour 
40514f3110aSMahmoud Mandour     g_mutex_lock(&l1_dcache_locks[cache_idx]);
40614f3110aSMahmoud Mandour     hit_in_l1 = access_cache(l1_dcaches[cache_idx], effective_addr);
40714f3110aSMahmoud Mandour     if (!hit_in_l1) {
4083d558330SMarkus Armbruster         insn = userdata;
40914f3110aSMahmoud Mandour         __atomic_fetch_add(&insn->l1_dmisses, 1, __ATOMIC_SEQ_CST);
41014f3110aSMahmoud Mandour         l1_dcaches[cache_idx]->misses++;
411e2c5557cSMahmoud Mandour     }
41214f3110aSMahmoud Mandour     l1_dcaches[cache_idx]->accesses++;
41314f3110aSMahmoud Mandour     g_mutex_unlock(&l1_dcache_locks[cache_idx]);
41414f3110aSMahmoud Mandour 
415447f9356SMahmoud Mandour     if (hit_in_l1 || !use_l2) {
41614f3110aSMahmoud Mandour         /* No need to access L2 */
41714f3110aSMahmoud Mandour         return;
41814f3110aSMahmoud Mandour     }
41914f3110aSMahmoud Mandour 
42014f3110aSMahmoud Mandour     g_mutex_lock(&l2_ucache_locks[cache_idx]);
42114f3110aSMahmoud Mandour     if (!access_cache(l2_ucaches[cache_idx], effective_addr)) {
4223d558330SMarkus Armbruster         insn = userdata;
42314f3110aSMahmoud Mandour         __atomic_fetch_add(&insn->l2_misses, 1, __ATOMIC_SEQ_CST);
42414f3110aSMahmoud Mandour         l2_ucaches[cache_idx]->misses++;
42514f3110aSMahmoud Mandour     }
42614f3110aSMahmoud Mandour     l2_ucaches[cache_idx]->accesses++;
42714f3110aSMahmoud Mandour     g_mutex_unlock(&l2_ucache_locks[cache_idx]);
428e2c5557cSMahmoud Mandour }
429e2c5557cSMahmoud Mandour 
vcpu_insn_exec(unsigned int vcpu_index,void * userdata)430e2c5557cSMahmoud Mandour static void vcpu_insn_exec(unsigned int vcpu_index, void *userdata)
431e2c5557cSMahmoud Mandour {
432e2c5557cSMahmoud Mandour     uint64_t insn_addr;
433e2c5557cSMahmoud Mandour     InsnData *insn;
434c79a2116SMahmoud Mandour     int cache_idx;
43514f3110aSMahmoud Mandour     bool hit_in_l1;
436e2c5557cSMahmoud Mandour 
437e2c5557cSMahmoud Mandour     insn_addr = ((InsnData *) userdata)->addr;
438e2c5557cSMahmoud Mandour 
439c79a2116SMahmoud Mandour     cache_idx = vcpu_index % cores;
44014f3110aSMahmoud Mandour     g_mutex_lock(&l1_icache_locks[cache_idx]);
44114f3110aSMahmoud Mandour     hit_in_l1 = access_cache(l1_icaches[cache_idx], insn_addr);
44214f3110aSMahmoud Mandour     if (!hit_in_l1) {
4433d558330SMarkus Armbruster         insn = userdata;
44414f3110aSMahmoud Mandour         __atomic_fetch_add(&insn->l1_imisses, 1, __ATOMIC_SEQ_CST);
44514f3110aSMahmoud Mandour         l1_icaches[cache_idx]->misses++;
446e2c5557cSMahmoud Mandour     }
44714f3110aSMahmoud Mandour     l1_icaches[cache_idx]->accesses++;
44814f3110aSMahmoud Mandour     g_mutex_unlock(&l1_icache_locks[cache_idx]);
44914f3110aSMahmoud Mandour 
450447f9356SMahmoud Mandour     if (hit_in_l1 || !use_l2) {
45114f3110aSMahmoud Mandour         /* No need to access L2 */
45214f3110aSMahmoud Mandour         return;
45314f3110aSMahmoud Mandour     }
45414f3110aSMahmoud Mandour 
45514f3110aSMahmoud Mandour     g_mutex_lock(&l2_ucache_locks[cache_idx]);
45614f3110aSMahmoud Mandour     if (!access_cache(l2_ucaches[cache_idx], insn_addr)) {
4573d558330SMarkus Armbruster         insn = userdata;
45814f3110aSMahmoud Mandour         __atomic_fetch_add(&insn->l2_misses, 1, __ATOMIC_SEQ_CST);
45914f3110aSMahmoud Mandour         l2_ucaches[cache_idx]->misses++;
46014f3110aSMahmoud Mandour     }
46114f3110aSMahmoud Mandour     l2_ucaches[cache_idx]->accesses++;
46214f3110aSMahmoud Mandour     g_mutex_unlock(&l2_ucache_locks[cache_idx]);
463e2c5557cSMahmoud Mandour }
464e2c5557cSMahmoud Mandour 
vcpu_tb_trans(qemu_plugin_id_t id,struct qemu_plugin_tb * tb)465e2c5557cSMahmoud Mandour static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
466e2c5557cSMahmoud Mandour {
467e2c5557cSMahmoud Mandour     size_t n_insns;
468e2c5557cSMahmoud Mandour     size_t i;
469e2c5557cSMahmoud Mandour     InsnData *data;
470e2c5557cSMahmoud Mandour 
471e2c5557cSMahmoud Mandour     n_insns = qemu_plugin_tb_n_insns(tb);
472e2c5557cSMahmoud Mandour     for (i = 0; i < n_insns; i++) {
473e2c5557cSMahmoud Mandour         struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
474e2c5557cSMahmoud Mandour         uint64_t effective_addr;
475e2c5557cSMahmoud Mandour 
476e2c5557cSMahmoud Mandour         if (sys) {
477e2c5557cSMahmoud Mandour             effective_addr = (uint64_t) qemu_plugin_insn_haddr(insn);
478e2c5557cSMahmoud Mandour         } else {
479e2c5557cSMahmoud Mandour             effective_addr = (uint64_t) qemu_plugin_insn_vaddr(insn);
480e2c5557cSMahmoud Mandour         }
481e2c5557cSMahmoud Mandour 
482e2c5557cSMahmoud Mandour         /*
483e2c5557cSMahmoud Mandour          * Instructions might get translated multiple times, we do not create
484e2c5557cSMahmoud Mandour          * new entries for those instructions. Instead, we fetch the same
485e2c5557cSMahmoud Mandour          * entry from the hash table and register it for the callback again.
486e2c5557cSMahmoud Mandour          */
487c79a2116SMahmoud Mandour         g_mutex_lock(&hashtable_lock);
488e2c5557cSMahmoud Mandour         data = g_hash_table_lookup(miss_ht, GUINT_TO_POINTER(effective_addr));
489e2c5557cSMahmoud Mandour         if (data == NULL) {
490e2c5557cSMahmoud Mandour             data = g_new0(InsnData, 1);
491e2c5557cSMahmoud Mandour             data->disas_str = qemu_plugin_insn_disas(insn);
492e2c5557cSMahmoud Mandour             data->symbol = qemu_plugin_insn_symbol(insn);
493e2c5557cSMahmoud Mandour             data->addr = effective_addr;
494e2c5557cSMahmoud Mandour             g_hash_table_insert(miss_ht, GUINT_TO_POINTER(effective_addr),
495e2c5557cSMahmoud Mandour                                (gpointer) data);
496e2c5557cSMahmoud Mandour         }
497c79a2116SMahmoud Mandour         g_mutex_unlock(&hashtable_lock);
498e2c5557cSMahmoud Mandour 
499e2c5557cSMahmoud Mandour         qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem_access,
500e2c5557cSMahmoud Mandour                                          QEMU_PLUGIN_CB_NO_REGS,
501e2c5557cSMahmoud Mandour                                          rw, data);
502e2c5557cSMahmoud Mandour 
503e2c5557cSMahmoud Mandour         qemu_plugin_register_vcpu_insn_exec_cb(insn, vcpu_insn_exec,
504e2c5557cSMahmoud Mandour                                                QEMU_PLUGIN_CB_NO_REGS, data);
505e2c5557cSMahmoud Mandour     }
506e2c5557cSMahmoud Mandour }
507e2c5557cSMahmoud Mandour 
insn_free(gpointer data)508e2c5557cSMahmoud Mandour static void insn_free(gpointer data)
509e2c5557cSMahmoud Mandour {
510e2c5557cSMahmoud Mandour     InsnData *insn = (InsnData *) data;
511e2c5557cSMahmoud Mandour     g_free(insn->disas_str);
512e2c5557cSMahmoud Mandour     g_free(insn);
513e2c5557cSMahmoud Mandour }
514e2c5557cSMahmoud Mandour 
cache_free(Cache * cache)515e2c5557cSMahmoud Mandour static void cache_free(Cache *cache)
516e2c5557cSMahmoud Mandour {
517e2c5557cSMahmoud Mandour     for (int i = 0; i < cache->num_sets; i++) {
518e2c5557cSMahmoud Mandour         g_free(cache->sets[i].blocks);
519e2c5557cSMahmoud Mandour     }
520e2c5557cSMahmoud Mandour 
52140c4a553SMahmoud Mandour     if (metadata_destroy) {
52240c4a553SMahmoud Mandour         metadata_destroy(cache);
52340c4a553SMahmoud Mandour     }
52440c4a553SMahmoud Mandour 
525e2c5557cSMahmoud Mandour     g_free(cache->sets);
526e2c5557cSMahmoud Mandour     g_free(cache);
527e2c5557cSMahmoud Mandour }
528e2c5557cSMahmoud Mandour 
caches_free(Cache ** caches)529c79a2116SMahmoud Mandour static void caches_free(Cache **caches)
530c79a2116SMahmoud Mandour {
531c79a2116SMahmoud Mandour     int i;
532c79a2116SMahmoud Mandour 
533c79a2116SMahmoud Mandour     for (i = 0; i < cores; i++) {
534c79a2116SMahmoud Mandour         cache_free(caches[i]);
535c79a2116SMahmoud Mandour     }
536c79a2116SMahmoud Mandour }
537c79a2116SMahmoud Mandour 
append_stats_line(GString * line,uint64_t l1_daccess,uint64_t l1_dmisses,uint64_t l1_iaccess,uint64_t l1_imisses,uint64_t l2_access,uint64_t l2_misses)53860cb16c0SAlex Bennée static void append_stats_line(GString *line,
53960cb16c0SAlex Bennée                               uint64_t l1_daccess, uint64_t l1_dmisses,
54060cb16c0SAlex Bennée                               uint64_t l1_iaccess, uint64_t l1_imisses,
54160cb16c0SAlex Bennée                               uint64_t l2_access, uint64_t l2_misses)
542e2c5557cSMahmoud Mandour {
54360cb16c0SAlex Bennée     double l1_dmiss_rate = ((double) l1_dmisses) / (l1_daccess) * 100.0;
54460cb16c0SAlex Bennée     double l1_imiss_rate = ((double) l1_imisses) / (l1_iaccess) * 100.0;
545c79a2116SMahmoud Mandour 
54686e49b2eSPhilippe Mathieu-Daudé     g_string_append_printf(line, "%-14" PRIu64 " %-12" PRIu64 " %9.4lf%%"
54786e49b2eSPhilippe Mathieu-Daudé                            "  %-14" PRIu64 " %-12" PRIu64 " %9.4lf%%",
54814f3110aSMahmoud Mandour                            l1_daccess,
54914f3110aSMahmoud Mandour                            l1_dmisses,
55014f3110aSMahmoud Mandour                            l1_daccess ? l1_dmiss_rate : 0.0,
55114f3110aSMahmoud Mandour                            l1_iaccess,
55214f3110aSMahmoud Mandour                            l1_imisses,
553447f9356SMahmoud Mandour                            l1_iaccess ? l1_imiss_rate : 0.0);
554447f9356SMahmoud Mandour 
55560cb16c0SAlex Bennée     if (l2_access && l2_misses) {
55660cb16c0SAlex Bennée         double l2_miss_rate =  ((double) l2_misses) / (l2_access) * 100.0;
55786e49b2eSPhilippe Mathieu-Daudé         g_string_append_printf(line,
55886e49b2eSPhilippe Mathieu-Daudé                                "  %-12" PRIu64 " %-11" PRIu64 " %10.4lf%%",
55914f3110aSMahmoud Mandour                                l2_access,
56014f3110aSMahmoud Mandour                                l2_misses,
561*33ef9cdcSPeter Maydell                                l2_miss_rate);
562c79a2116SMahmoud Mandour     }
563c79a2116SMahmoud Mandour 
564447f9356SMahmoud Mandour     g_string_append(line, "\n");
565447f9356SMahmoud Mandour }
566447f9356SMahmoud Mandour 
sum_stats(void)567c79a2116SMahmoud Mandour static void sum_stats(void)
568c79a2116SMahmoud Mandour {
569c79a2116SMahmoud Mandour     int i;
570c79a2116SMahmoud Mandour 
571c79a2116SMahmoud Mandour     g_assert(cores > 1);
572c79a2116SMahmoud Mandour     for (i = 0; i < cores; i++) {
57314f3110aSMahmoud Mandour         l1_imisses += l1_icaches[i]->misses;
57414f3110aSMahmoud Mandour         l1_dmisses += l1_dcaches[i]->misses;
57514f3110aSMahmoud Mandour         l1_imem_accesses += l1_icaches[i]->accesses;
57614f3110aSMahmoud Mandour         l1_dmem_accesses += l1_dcaches[i]->accesses;
57714f3110aSMahmoud Mandour 
578447f9356SMahmoud Mandour         if (use_l2) {
57914f3110aSMahmoud Mandour             l2_misses += l2_ucaches[i]->misses;
58014f3110aSMahmoud Mandour             l2_mem_accesses += l2_ucaches[i]->accesses;
581c79a2116SMahmoud Mandour         }
582c79a2116SMahmoud Mandour     }
583447f9356SMahmoud Mandour }
584c79a2116SMahmoud Mandour 
dcmp(gconstpointer a,gconstpointer b)58514f3110aSMahmoud Mandour static int dcmp(gconstpointer a, gconstpointer b)
58614f3110aSMahmoud Mandour {
58714f3110aSMahmoud Mandour     InsnData *insn_a = (InsnData *) a;
58814f3110aSMahmoud Mandour     InsnData *insn_b = (InsnData *) b;
58914f3110aSMahmoud Mandour 
59014f3110aSMahmoud Mandour     return insn_a->l1_dmisses < insn_b->l1_dmisses ? 1 : -1;
59114f3110aSMahmoud Mandour }
59214f3110aSMahmoud Mandour 
icmp(gconstpointer a,gconstpointer b)593e2c5557cSMahmoud Mandour static int icmp(gconstpointer a, gconstpointer b)
594e2c5557cSMahmoud Mandour {
595e2c5557cSMahmoud Mandour     InsnData *insn_a = (InsnData *) a;
596e2c5557cSMahmoud Mandour     InsnData *insn_b = (InsnData *) b;
597e2c5557cSMahmoud Mandour 
59814f3110aSMahmoud Mandour     return insn_a->l1_imisses < insn_b->l1_imisses ? 1 : -1;
59914f3110aSMahmoud Mandour }
60014f3110aSMahmoud Mandour 
l2_cmp(gconstpointer a,gconstpointer b)60114f3110aSMahmoud Mandour static int l2_cmp(gconstpointer a, gconstpointer b)
60214f3110aSMahmoud Mandour {
60314f3110aSMahmoud Mandour     InsnData *insn_a = (InsnData *) a;
60414f3110aSMahmoud Mandour     InsnData *insn_b = (InsnData *) b;
60514f3110aSMahmoud Mandour 
60614f3110aSMahmoud Mandour     return insn_a->l2_misses < insn_b->l2_misses ? 1 : -1;
607e2c5557cSMahmoud Mandour }
608e2c5557cSMahmoud Mandour 
log_stats(void)609c2888a67SMahmoud Mandour static void log_stats(void)
610e2c5557cSMahmoud Mandour {
611c79a2116SMahmoud Mandour     int i;
61214f3110aSMahmoud Mandour     Cache *icache, *dcache, *l2_cache;
613e2c5557cSMahmoud Mandour 
614c79a2116SMahmoud Mandour     g_autoptr(GString) rep = g_string_new("core #, data accesses, data misses,"
615c79a2116SMahmoud Mandour                                           " dmiss rate, insn accesses,"
616447f9356SMahmoud Mandour                                           " insn misses, imiss rate");
617447f9356SMahmoud Mandour 
618447f9356SMahmoud Mandour     if (use_l2) {
619447f9356SMahmoud Mandour         g_string_append(rep, ", l2 accesses, l2 misses, l2 miss rate");
620447f9356SMahmoud Mandour     }
621447f9356SMahmoud Mandour 
622447f9356SMahmoud Mandour     g_string_append(rep, "\n");
623e2c5557cSMahmoud Mandour 
624c79a2116SMahmoud Mandour     for (i = 0; i < cores; i++) {
625c79a2116SMahmoud Mandour         g_string_append_printf(rep, "%-8d", i);
62614f3110aSMahmoud Mandour         dcache = l1_dcaches[i];
62714f3110aSMahmoud Mandour         icache = l1_icaches[i];
628447f9356SMahmoud Mandour         l2_cache = use_l2 ? l2_ucaches[i] : NULL;
629c79a2116SMahmoud Mandour         append_stats_line(rep, dcache->accesses, dcache->misses,
630447f9356SMahmoud Mandour                 icache->accesses, icache->misses,
631447f9356SMahmoud Mandour                 l2_cache ? l2_cache->accesses : 0,
632447f9356SMahmoud Mandour                 l2_cache ? l2_cache->misses : 0);
633c79a2116SMahmoud Mandour     }
634c79a2116SMahmoud Mandour 
635c79a2116SMahmoud Mandour     if (cores > 1) {
636c79a2116SMahmoud Mandour         sum_stats();
637c79a2116SMahmoud Mandour         g_string_append_printf(rep, "%-8s", "sum");
63814f3110aSMahmoud Mandour         append_stats_line(rep, l1_dmem_accesses, l1_dmisses,
639447f9356SMahmoud Mandour                 l1_imem_accesses, l1_imisses,
640447f9356SMahmoud Mandour                 l2_cache ? l2_mem_accesses : 0, l2_cache ? l2_misses : 0);
641c79a2116SMahmoud Mandour     }
642c79a2116SMahmoud Mandour 
643c79a2116SMahmoud Mandour     g_string_append(rep, "\n");
644e2c5557cSMahmoud Mandour     qemu_plugin_outs(rep->str);
645e2c5557cSMahmoud Mandour }
646e2c5557cSMahmoud Mandour 
log_top_insns(void)647c2888a67SMahmoud Mandour static void log_top_insns(void)
648e2c5557cSMahmoud Mandour {
649e2c5557cSMahmoud Mandour     int i;
650e2c5557cSMahmoud Mandour     GList *curr, *miss_insns;
651e2c5557cSMahmoud Mandour     InsnData *insn;
652e2c5557cSMahmoud Mandour 
653e2c5557cSMahmoud Mandour     miss_insns = g_hash_table_get_values(miss_ht);
654e2c5557cSMahmoud Mandour     miss_insns = g_list_sort(miss_insns, dcmp);
655e2c5557cSMahmoud Mandour     g_autoptr(GString) rep = g_string_new("");
656e2c5557cSMahmoud Mandour     g_string_append_printf(rep, "%s", "address, data misses, instruction\n");
657e2c5557cSMahmoud Mandour 
658e2c5557cSMahmoud Mandour     for (curr = miss_insns, i = 0; curr && i < limit; i++, curr = curr->next) {
659e2c5557cSMahmoud Mandour         insn = (InsnData *) curr->data;
660e2c5557cSMahmoud Mandour         g_string_append_printf(rep, "0x%" PRIx64, insn->addr);
661e2c5557cSMahmoud Mandour         if (insn->symbol) {
662e2c5557cSMahmoud Mandour             g_string_append_printf(rep, " (%s)", insn->symbol);
663e2c5557cSMahmoud Mandour         }
66486e49b2eSPhilippe Mathieu-Daudé         g_string_append_printf(rep, ", %" PRId64 ", %s\n",
66586e49b2eSPhilippe Mathieu-Daudé                                insn->l1_dmisses, insn->disas_str);
666e2c5557cSMahmoud Mandour     }
667e2c5557cSMahmoud Mandour 
668e2c5557cSMahmoud Mandour     miss_insns = g_list_sort(miss_insns, icmp);
669e2c5557cSMahmoud Mandour     g_string_append_printf(rep, "%s", "\naddress, fetch misses, instruction\n");
670e2c5557cSMahmoud Mandour 
671e2c5557cSMahmoud Mandour     for (curr = miss_insns, i = 0; curr && i < limit; i++, curr = curr->next) {
672e2c5557cSMahmoud Mandour         insn = (InsnData *) curr->data;
673e2c5557cSMahmoud Mandour         g_string_append_printf(rep, "0x%" PRIx64, insn->addr);
674e2c5557cSMahmoud Mandour         if (insn->symbol) {
675e2c5557cSMahmoud Mandour             g_string_append_printf(rep, " (%s)", insn->symbol);
676e2c5557cSMahmoud Mandour         }
67786e49b2eSPhilippe Mathieu-Daudé         g_string_append_printf(rep, ", %" PRId64 ", %s\n",
67886e49b2eSPhilippe Mathieu-Daudé                                insn->l1_imisses, insn->disas_str);
67914f3110aSMahmoud Mandour     }
68014f3110aSMahmoud Mandour 
681447f9356SMahmoud Mandour     if (!use_l2) {
682447f9356SMahmoud Mandour         goto finish;
683447f9356SMahmoud Mandour     }
684447f9356SMahmoud Mandour 
68514f3110aSMahmoud Mandour     miss_insns = g_list_sort(miss_insns, l2_cmp);
68614f3110aSMahmoud Mandour     g_string_append_printf(rep, "%s", "\naddress, L2 misses, instruction\n");
68714f3110aSMahmoud Mandour 
68814f3110aSMahmoud Mandour     for (curr = miss_insns, i = 0; curr && i < limit; i++, curr = curr->next) {
68914f3110aSMahmoud Mandour         insn = (InsnData *) curr->data;
69014f3110aSMahmoud Mandour         g_string_append_printf(rep, "0x%" PRIx64, insn->addr);
69114f3110aSMahmoud Mandour         if (insn->symbol) {
69214f3110aSMahmoud Mandour             g_string_append_printf(rep, " (%s)", insn->symbol);
69314f3110aSMahmoud Mandour         }
69486e49b2eSPhilippe Mathieu-Daudé         g_string_append_printf(rep, ", %" PRId64 ", %s\n",
69586e49b2eSPhilippe Mathieu-Daudé                                insn->l2_misses, insn->disas_str);
696e2c5557cSMahmoud Mandour     }
697e2c5557cSMahmoud Mandour 
698447f9356SMahmoud Mandour finish:
699e2c5557cSMahmoud Mandour     qemu_plugin_outs(rep->str);
700e2c5557cSMahmoud Mandour     g_list_free(miss_insns);
701e2c5557cSMahmoud Mandour }
702e2c5557cSMahmoud Mandour 
plugin_exit(qemu_plugin_id_t id,void * p)703e2c5557cSMahmoud Mandour static void plugin_exit(qemu_plugin_id_t id, void *p)
704e2c5557cSMahmoud Mandour {
705e2c5557cSMahmoud Mandour     log_stats();
706e2c5557cSMahmoud Mandour     log_top_insns();
707e2c5557cSMahmoud Mandour 
70814f3110aSMahmoud Mandour     caches_free(l1_dcaches);
70914f3110aSMahmoud Mandour     caches_free(l1_icaches);
710e2c5557cSMahmoud Mandour 
71114f3110aSMahmoud Mandour     g_free(l1_dcache_locks);
71214f3110aSMahmoud Mandour     g_free(l1_icache_locks);
713447f9356SMahmoud Mandour 
714447f9356SMahmoud Mandour     if (use_l2) {
715447f9356SMahmoud Mandour         caches_free(l2_ucaches);
71614f3110aSMahmoud Mandour         g_free(l2_ucache_locks);
717447f9356SMahmoud Mandour     }
718ad039c50SMahmoud Mandour 
719e2c5557cSMahmoud Mandour     g_hash_table_destroy(miss_ht);
720e2c5557cSMahmoud Mandour }
721e2c5557cSMahmoud Mandour 
policy_init(void)722c2888a67SMahmoud Mandour static void policy_init(void)
72340c4a553SMahmoud Mandour {
72440c4a553SMahmoud Mandour     switch (policy) {
72540c4a553SMahmoud Mandour     case LRU:
72640c4a553SMahmoud Mandour         update_hit = lru_update_blk;
72740c4a553SMahmoud Mandour         update_miss = lru_update_blk;
72840c4a553SMahmoud Mandour         metadata_init = lru_priorities_init;
72940c4a553SMahmoud Mandour         metadata_destroy = lru_priorities_destroy;
73040c4a553SMahmoud Mandour         break;
73140c4a553SMahmoud Mandour     case FIFO:
73240c4a553SMahmoud Mandour         update_miss = fifo_update_on_miss;
73340c4a553SMahmoud Mandour         metadata_init = fifo_init;
73440c4a553SMahmoud Mandour         metadata_destroy = fifo_destroy;
73540c4a553SMahmoud Mandour         break;
73640c4a553SMahmoud Mandour     case RAND:
73740c4a553SMahmoud Mandour         rng = g_rand_new();
73840c4a553SMahmoud Mandour         break;
73940c4a553SMahmoud Mandour     default:
74040c4a553SMahmoud Mandour         g_assert_not_reached();
74140c4a553SMahmoud Mandour     }
74240c4a553SMahmoud Mandour }
74340c4a553SMahmoud Mandour 
744e2c5557cSMahmoud Mandour QEMU_PLUGIN_EXPORT
qemu_plugin_install(qemu_plugin_id_t id,const qemu_info_t * info,int argc,char ** argv)745e2c5557cSMahmoud Mandour int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
746e2c5557cSMahmoud Mandour                         int argc, char **argv)
747e2c5557cSMahmoud Mandour {
748e2c5557cSMahmoud Mandour     int i;
74914f3110aSMahmoud Mandour     int l1_iassoc, l1_iblksize, l1_icachesize;
75014f3110aSMahmoud Mandour     int l1_dassoc, l1_dblksize, l1_dcachesize;
75114f3110aSMahmoud Mandour     int l2_assoc, l2_blksize, l2_cachesize;
752e2c5557cSMahmoud Mandour 
753e2c5557cSMahmoud Mandour     limit = 32;
754e2c5557cSMahmoud Mandour     sys = info->system_emulation;
755e2c5557cSMahmoud Mandour 
75614f3110aSMahmoud Mandour     l1_dassoc = 8;
75714f3110aSMahmoud Mandour     l1_dblksize = 64;
75814f3110aSMahmoud Mandour     l1_dcachesize = l1_dblksize * l1_dassoc * 32;
759e2c5557cSMahmoud Mandour 
76014f3110aSMahmoud Mandour     l1_iassoc = 8;
76114f3110aSMahmoud Mandour     l1_iblksize = 64;
76214f3110aSMahmoud Mandour     l1_icachesize = l1_iblksize * l1_iassoc * 32;
76314f3110aSMahmoud Mandour 
76414f3110aSMahmoud Mandour     l2_assoc = 16;
76514f3110aSMahmoud Mandour     l2_blksize = 64;
76614f3110aSMahmoud Mandour     l2_cachesize = l2_assoc * l2_blksize * 2048;
767e2c5557cSMahmoud Mandour 
76840c4a553SMahmoud Mandour     policy = LRU;
769e2c5557cSMahmoud Mandour 
770926e146eSPierrick Bouvier     cores = sys ? info->system.smp_vcpus : 1;
771c79a2116SMahmoud Mandour 
772e2c5557cSMahmoud Mandour     for (i = 0; i < argc; i++) {
773e2c5557cSMahmoud Mandour         char *opt = argv[i];
77440258741SAlex Bennée         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
77553366adfSMahmoud Mandour 
77653366adfSMahmoud Mandour         if (g_strcmp0(tokens[0], "iblksize") == 0) {
77753366adfSMahmoud Mandour             l1_iblksize = STRTOLL(tokens[1]);
77853366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "iassoc") == 0) {
77953366adfSMahmoud Mandour             l1_iassoc = STRTOLL(tokens[1]);
78053366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "icachesize") == 0) {
78153366adfSMahmoud Mandour             l1_icachesize = STRTOLL(tokens[1]);
78253366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "dblksize") == 0) {
78353366adfSMahmoud Mandour             l1_dblksize = STRTOLL(tokens[1]);
78453366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "dassoc") == 0) {
78553366adfSMahmoud Mandour             l1_dassoc = STRTOLL(tokens[1]);
78653366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "dcachesize") == 0) {
78753366adfSMahmoud Mandour             l1_dcachesize = STRTOLL(tokens[1]);
78853366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "limit") == 0) {
78953366adfSMahmoud Mandour             limit = STRTOLL(tokens[1]);
79053366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "cores") == 0) {
79153366adfSMahmoud Mandour             cores = STRTOLL(tokens[1]);
79253366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "l2cachesize") == 0) {
793447f9356SMahmoud Mandour             use_l2 = true;
79453366adfSMahmoud Mandour             l2_cachesize = STRTOLL(tokens[1]);
79553366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "l2blksize") == 0) {
796447f9356SMahmoud Mandour             use_l2 = true;
79753366adfSMahmoud Mandour             l2_blksize = STRTOLL(tokens[1]);
79853366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "l2assoc") == 0) {
799447f9356SMahmoud Mandour             use_l2 = true;
80053366adfSMahmoud Mandour             l2_assoc = STRTOLL(tokens[1]);
801447f9356SMahmoud Mandour         } else if (g_strcmp0(tokens[0], "l2") == 0) {
802447f9356SMahmoud Mandour             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &use_l2)) {
803447f9356SMahmoud Mandour                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
804447f9356SMahmoud Mandour                 return -1;
805447f9356SMahmoud Mandour             }
80653366adfSMahmoud Mandour         } else if (g_strcmp0(tokens[0], "evict") == 0) {
80753366adfSMahmoud Mandour             if (g_strcmp0(tokens[1], "rand") == 0) {
80840c4a553SMahmoud Mandour                 policy = RAND;
80953366adfSMahmoud Mandour             } else if (g_strcmp0(tokens[1], "lru") == 0) {
81040c4a553SMahmoud Mandour                 policy = LRU;
81153366adfSMahmoud Mandour             } else if (g_strcmp0(tokens[1], "fifo") == 0) {
81240c4a553SMahmoud Mandour                 policy = FIFO;
81340c4a553SMahmoud Mandour             } else {
81440c4a553SMahmoud Mandour                 fprintf(stderr, "invalid eviction policy: %s\n", opt);
81540c4a553SMahmoud Mandour                 return -1;
81640c4a553SMahmoud Mandour             }
817e2c5557cSMahmoud Mandour         } else {
818e2c5557cSMahmoud Mandour             fprintf(stderr, "option parsing failed: %s\n", opt);
819e2c5557cSMahmoud Mandour             return -1;
820e2c5557cSMahmoud Mandour         }
821e2c5557cSMahmoud Mandour     }
822e2c5557cSMahmoud Mandour 
82340c4a553SMahmoud Mandour     policy_init();
82440c4a553SMahmoud Mandour 
82514f3110aSMahmoud Mandour     l1_dcaches = caches_init(l1_dblksize, l1_dassoc, l1_dcachesize);
82614f3110aSMahmoud Mandour     if (!l1_dcaches) {
82714f3110aSMahmoud Mandour         const char *err = cache_config_error(l1_dblksize, l1_dassoc, l1_dcachesize);
82886ae3a1dSMahmoud Mandour         fprintf(stderr, "dcache cannot be constructed from given parameters\n");
82986ae3a1dSMahmoud Mandour         fprintf(stderr, "%s\n", err);
83086ae3a1dSMahmoud Mandour         return -1;
83186ae3a1dSMahmoud Mandour     }
83286ae3a1dSMahmoud Mandour 
83314f3110aSMahmoud Mandour     l1_icaches = caches_init(l1_iblksize, l1_iassoc, l1_icachesize);
83414f3110aSMahmoud Mandour     if (!l1_icaches) {
83514f3110aSMahmoud Mandour         const char *err = cache_config_error(l1_iblksize, l1_iassoc, l1_icachesize);
83686ae3a1dSMahmoud Mandour         fprintf(stderr, "icache cannot be constructed from given parameters\n");
83786ae3a1dSMahmoud Mandour         fprintf(stderr, "%s\n", err);
83886ae3a1dSMahmoud Mandour         return -1;
83986ae3a1dSMahmoud Mandour     }
840e2c5557cSMahmoud Mandour 
841447f9356SMahmoud Mandour     l2_ucaches = use_l2 ? caches_init(l2_blksize, l2_assoc, l2_cachesize) : NULL;
842447f9356SMahmoud Mandour     if (!l2_ucaches && use_l2) {
84314f3110aSMahmoud Mandour         const char *err = cache_config_error(l2_blksize, l2_assoc, l2_cachesize);
84414f3110aSMahmoud Mandour         fprintf(stderr, "L2 cache cannot be constructed from given parameters\n");
84514f3110aSMahmoud Mandour         fprintf(stderr, "%s\n", err);
84614f3110aSMahmoud Mandour         return -1;
84714f3110aSMahmoud Mandour     }
84814f3110aSMahmoud Mandour 
84914f3110aSMahmoud Mandour     l1_dcache_locks = g_new0(GMutex, cores);
85014f3110aSMahmoud Mandour     l1_icache_locks = g_new0(GMutex, cores);
851447f9356SMahmoud Mandour     l2_ucache_locks = use_l2 ? g_new0(GMutex, cores) : NULL;
852c79a2116SMahmoud Mandour 
853e2c5557cSMahmoud Mandour     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
854e2c5557cSMahmoud Mandour     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
855e2c5557cSMahmoud Mandour 
856e2c5557cSMahmoud Mandour     miss_ht = g_hash_table_new_full(NULL, g_direct_equal, NULL, insn_free);
857e2c5557cSMahmoud Mandour 
858e2c5557cSMahmoud Mandour     return 0;
859e2c5557cSMahmoud Mandour }
860