| 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/host-utils.h" |
| 20 | #include "page_cache.h" |
| 21 | #include "trace.h" |
| 22 | |
| 23 | /* the page in cache will not be replaced in two cycles */ |
| 24 | #define CACHED_PAGE_LIFETIME 2 |
| 25 | |
| 26 | typedef struct CacheItem CacheItem; |
| 27 | |
| 28 | struct CacheItem { |
| 29 | uint64_t it_addr; |
| 30 | uint64_t it_age; |
| 31 | uint8_t *it_data; |
| 32 | }; |
| 33 | |
| 34 | struct PageCache { |
| 35 | CacheItem *page_cache; |
| 36 | size_t page_size; |
| 37 | size_t max_num_items; |
| 38 | size_t num_items; |
| 39 | }; |
| 40 | |
| 41 | PageCache *cache_init(uint64_t new_size, size_t page_size, Error **errp) |
| 42 | { |
| 43 | int64_t i; |
| 44 | size_t num_pages = new_size / page_size; |
| 45 | PageCache *cache; |
| 46 | |
| 47 | if (new_size < page_size) { |
| 48 | error_setg(errp, "cache size is smaller than target page size"); |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | /* round down to the nearest power of 2 */ |
| 53 | if (!is_power_of_2(num_pages)) { |
| 54 | error_setg(errp, "number of pages is not a power of two"); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | /* We prefer not to abort if there is no memory */ |
| 59 | cache = g_try_malloc(sizeof(*cache)); |
| 60 | if (!cache) { |
| 61 | error_setg(errp, "Failed to allocate cache"); |
| 62 | return NULL; |
| 63 | } |
| 64 | cache->page_size = page_size; |
| 65 | cache->num_items = 0; |
| 66 | cache->max_num_items = num_pages; |
| 67 | |
| 68 | trace_migration_pagecache_init(cache->max_num_items); |
| 69 | |
| 70 | /* We prefer not to abort if there is no memory */ |
| 71 | cache->page_cache = g_try_malloc((cache->max_num_items) * |
| 72 | sizeof(*cache->page_cache)); |
| 73 | if (!cache->page_cache) { |
| 74 | error_setg(errp, "Failed to allocate page cache"); |
| 75 | g_free(cache); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | for (i = 0; i < cache->max_num_items; i++) { |
| 80 | cache->page_cache[i].it_data = NULL; |
| 81 | cache->page_cache[i].it_age = 0; |
| 82 | cache->page_cache[i].it_addr = -1; |
| 83 | } |
| 84 | |
| 85 | return cache; |
| 86 | } |
| 87 | |
| 88 | void cache_fini(PageCache *cache) |
| 89 | { |
| 90 | int64_t i; |
| 91 | |
| 92 | g_assert(cache); |
| 93 | g_assert(cache->page_cache); |
| 94 | |
| 95 | for (i = 0; i < cache->max_num_items; i++) { |
| 96 | g_free(cache->page_cache[i].it_data); |
| 97 | } |
| 98 | |
| 99 | g_free(cache->page_cache); |
| 100 | cache->page_cache = NULL; |
| 101 | g_free(cache); |
| 102 | } |
| 103 | |
| 104 | static size_t cache_get_cache_pos(const PageCache *cache, |
| 105 | uint64_t address) |
| 106 | { |
| 107 | g_assert(cache->max_num_items); |
| 108 | return (address / cache->page_size) & (cache->max_num_items - 1); |
| 109 | } |
| 110 | |
| 111 | static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr) |
| 112 | { |
| 113 | size_t pos; |
| 114 | |
| 115 | g_assert(cache); |
| 116 | g_assert(cache->page_cache); |
| 117 | |
| 118 | pos = cache_get_cache_pos(cache, addr); |
| 119 | |
| 120 | return &cache->page_cache[pos]; |
| 121 | } |
| 122 | |
| 123 | uint8_t *get_cached_data(const PageCache *cache, uint64_t addr) |
| 124 | { |
| 125 | return cache_get_by_addr(cache, addr)->it_data; |
| 126 | } |
| 127 | |
| 128 | bool cache_is_cached(const PageCache *cache, uint64_t addr, |
| 129 | uint64_t current_age) |
| 130 | { |
| 131 | CacheItem *it; |
| 132 | |
| 133 | it = cache_get_by_addr(cache, addr); |
| 134 | |
| 135 | if (it->it_addr == addr) { |
| 136 | /* update the it_age when the cache hit */ |
| 137 | it->it_age = current_age; |
| 138 | return true; |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata, |
| 144 | uint64_t current_age) |
| 145 | { |
| 146 | |
| 147 | CacheItem *it; |
| 148 | |
| 149 | /* actual update of entry */ |
| 150 | it = cache_get_by_addr(cache, addr); |
| 151 | |
| 152 | if (it->it_data && it->it_addr != addr && |
| 153 | it->it_age + CACHED_PAGE_LIFETIME > current_age) { |
| 154 | /* the cache page is fresh, don't replace it */ |
| 155 | return -1; |
| 156 | } |
| 157 | /* allocate page */ |
| 158 | if (!it->it_data) { |
| 159 | it->it_data = g_try_malloc(cache->page_size); |
| 160 | if (!it->it_data) { |
| 161 | trace_migration_pagecache_insert(); |
| 162 | return -1; |
| 163 | } |
| 164 | cache->num_items++; |
| 165 | } |
| 166 | |
| 167 | memcpy(it->it_data, pdata, cache->page_size); |
| 168 | |
| 169 | it->it_age = current_age; |
| 170 | it->it_addr = addr; |
| 171 | |
| 172 | return 0; |
| 173 | } |