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