3 * The cache is base on a hash of the page address
5 * Copyright 2012 Red Hat, Inc. and/or its affiliates
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qapi/error.h"
19 #include "qemu-common.h"
20 #include "qemu/host-utils.h"
21 #include "page_cache.h"
24 #define DPRINTF(fmt, ...) \
25 do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
27 #define DPRINTF(fmt, ...) \
31 /* the page in cache will not be replaced in two cycles */
32 #define CACHED_PAGE_LIFETIME 2
34 typedef struct CacheItem CacheItem;
43 CacheItem *page_cache;
49 PageCache *cache_init(int64_t new_size, size_t page_size, Error **errp)
52 size_t num_pages = new_size / page_size;
55 if (new_size < page_size) {
56 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
57 "is smaller than one target page size");
61 /* round down to the nearest power of 2 */
62 if (!is_power_of_2(num_pages)) {
63 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
64 "is not a power of two number of pages");
68 /* We prefer not to abort if there is no memory */
69 cache = g_try_malloc(sizeof(*cache));
71 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
72 "Failed to allocate cache");
75 cache->page_size = page_size;
77 cache->max_num_items = num_pages;
79 DPRINTF("Setting cache buckets to %" PRId64 "\n", cache->max_num_items);
81 /* We prefer not to abort if there is no memory */
82 cache->page_cache = g_try_malloc((cache->max_num_items) *
83 sizeof(*cache->page_cache));
84 if (!cache->page_cache) {
85 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
86 "Failed to allocate page cache");
91 for (i = 0; i < cache->max_num_items; i++) {
92 cache->page_cache[i].it_data = NULL;
93 cache->page_cache[i].it_age = 0;
94 cache->page_cache[i].it_addr = -1;
100 void cache_fini(PageCache *cache)
105 g_assert(cache->page_cache);
107 for (i = 0; i < cache->max_num_items; i++) {
108 g_free(cache->page_cache[i].it_data);
111 g_free(cache->page_cache);
112 cache->page_cache = NULL;
116 static size_t cache_get_cache_pos(const PageCache *cache,
119 g_assert(cache->max_num_items);
120 return (address / cache->page_size) & (cache->max_num_items - 1);
123 static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr)
128 g_assert(cache->page_cache);
130 pos = cache_get_cache_pos(cache, addr);
132 return &cache->page_cache[pos];
135 uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
137 return cache_get_by_addr(cache, addr)->it_data;
140 bool cache_is_cached(const PageCache *cache, uint64_t addr,
141 uint64_t current_age)
145 it = cache_get_by_addr(cache, addr);
147 if (it->it_addr == addr) {
148 /* update the it_age when the cache hit */
149 it->it_age = current_age;
155 int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata,
156 uint64_t current_age)
161 /* actual update of entry */
162 it = cache_get_by_addr(cache, addr);
164 if (it->it_data && it->it_addr != addr &&
165 it->it_age + CACHED_PAGE_LIFETIME > current_age) {
166 /* the cache page is fresh, don't replace it */
171 it->it_data = g_try_malloc(cache->page_size);
173 DPRINTF("Error allocating page\n");
179 memcpy(it->it_data, pdata, cache->page_size);
181 it->it_age = current_age;