1 /* 2 * Page cache for QEMU 3 * The cache is base on a hash of the page address 4 * 5 * Copyright 2012 Red Hat, Inc. and/or its affiliates 6 * 7 * Authors: 8 * Orit Wasserman <owasserm@redhat.com> 9 * 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. 12 * 13 */ 14 15 #include "qemu/osdep.h" 16 17 #include "qapi/qmp/qerror.h" 18 #include "qapi/error.h" 19 #include "qemu-common.h" 20 #include "qemu/host-utils.h" 21 #include "migration/page_cache.h" 22 23 #ifdef DEBUG_CACHE 24 #define DPRINTF(fmt, ...) \ 25 do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0) 26 #else 27 #define DPRINTF(fmt, ...) \ 28 do { } while (0) 29 #endif 30 31 /* the page in cache will not be replaced in two cycles */ 32 #define CACHED_PAGE_LIFETIME 2 33 34 typedef struct CacheItem CacheItem; 35 36 struct CacheItem { 37 uint64_t it_addr; 38 uint64_t it_age; 39 uint8_t *it_data; 40 }; 41 42 struct PageCache { 43 CacheItem *page_cache; 44 size_t page_size; 45 size_t max_num_items; 46 size_t num_items; 47 }; 48 49 PageCache *cache_init(int64_t new_size, size_t page_size, Error **errp) 50 { 51 int64_t i; 52 size_t num_pages = new_size / page_size; 53 PageCache *cache; 54 55 if (new_size < page_size) { 56 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", 57 "is smaller than one target page size"); 58 return NULL; 59 } 60 61 /* We prefer not to abort if there is no memory */ 62 cache = g_try_malloc(sizeof(*cache)); 63 if (!cache) { 64 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", 65 "Failed to allocate cache"); 66 return NULL; 67 } 68 /* round down to the nearest power of 2 */ 69 if (!is_power_of_2(num_pages)) { 70 num_pages = pow2floor(num_pages); 71 DPRINTF("rounding down to %" PRId64 "\n", num_pages); 72 } 73 cache->page_size = page_size; 74 cache->num_items = 0; 75 cache->max_num_items = num_pages; 76 77 DPRINTF("Setting cache buckets to %" PRId64 "\n", cache->max_num_items); 78 79 /* We prefer not to abort if there is no memory */ 80 cache->page_cache = g_try_malloc((cache->max_num_items) * 81 sizeof(*cache->page_cache)); 82 if (!cache->page_cache) { 83 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", 84 "Failed to allocate page cache"); 85 g_free(cache); 86 return NULL; 87 } 88 89 for (i = 0; i < cache->max_num_items; i++) { 90 cache->page_cache[i].it_data = NULL; 91 cache->page_cache[i].it_age = 0; 92 cache->page_cache[i].it_addr = -1; 93 } 94 95 return cache; 96 } 97 98 void cache_fini(PageCache *cache) 99 { 100 int64_t i; 101 102 g_assert(cache); 103 g_assert(cache->page_cache); 104 105 for (i = 0; i < cache->max_num_items; i++) { 106 g_free(cache->page_cache[i].it_data); 107 } 108 109 g_free(cache->page_cache); 110 cache->page_cache = NULL; 111 g_free(cache); 112 } 113 114 static size_t cache_get_cache_pos(const PageCache *cache, 115 uint64_t address) 116 { 117 g_assert(cache->max_num_items); 118 return (address / cache->page_size) & (cache->max_num_items - 1); 119 } 120 121 static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr) 122 { 123 size_t pos; 124 125 g_assert(cache); 126 g_assert(cache->page_cache); 127 128 pos = cache_get_cache_pos(cache, addr); 129 130 return &cache->page_cache[pos]; 131 } 132 133 uint8_t *get_cached_data(const PageCache *cache, uint64_t addr) 134 { 135 return cache_get_by_addr(cache, addr)->it_data; 136 } 137 138 bool cache_is_cached(const PageCache *cache, uint64_t addr, 139 uint64_t current_age) 140 { 141 CacheItem *it; 142 143 it = cache_get_by_addr(cache, addr); 144 145 if (it->it_addr == addr) { 146 /* update the it_age when the cache hit */ 147 it->it_age = current_age; 148 return true; 149 } 150 return false; 151 } 152 153 int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata, 154 uint64_t current_age) 155 { 156 157 CacheItem *it; 158 159 /* actual update of entry */ 160 it = cache_get_by_addr(cache, addr); 161 162 if (it->it_data && it->it_addr != addr && 163 it->it_age + CACHED_PAGE_LIFETIME > current_age) { 164 /* the cache page is fresh, don't replace it */ 165 return -1; 166 } 167 /* allocate page */ 168 if (!it->it_data) { 169 it->it_data = g_try_malloc(cache->page_size); 170 if (!it->it_data) { 171 DPRINTF("Error allocating page\n"); 172 return -1; 173 } 174 cache->num_items++; 175 } 176 177 memcpy(it->it_data, pdata, cache->page_size); 178 179 it->it_age = current_age; 180 it->it_addr = addr; 181 182 return 0; 183 } 184