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