block/vpc: make calculate_geometry spec conform
[qemu/kevin.git] / page_cache.c
blobcf8878d1d7393f4587342cffd16596b93ec1e0c2
1 /*
2 * Page cache for QEMU
3 * The cache is base on a hash of the page address
5 * Copyright 2012 Red Hat, Inc. and/or its affiliates
7 * Authors:
8 * Orit Wasserman <owasserm@redhat.com>
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 <stdint.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <strings.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <stdbool.h>
23 #include <glib.h>
25 #include "qemu-common.h"
26 #include "migration/page_cache.h"
28 #ifdef DEBUG_CACHE
29 #define DPRINTF(fmt, ...) \
30 do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
31 #else
32 #define DPRINTF(fmt, ...) \
33 do { } while (0)
34 #endif
36 /* the page in cache will not be replaced in two cycles */
37 #define CACHED_PAGE_LIFETIME 2
39 typedef struct CacheItem CacheItem;
41 struct CacheItem {
42 uint64_t it_addr;
43 uint64_t it_age;
44 uint8_t *it_data;
47 struct PageCache {
48 CacheItem *page_cache;
49 unsigned int page_size;
50 int64_t max_num_items;
51 uint64_t max_item_age;
52 int64_t num_items;
55 PageCache *cache_init(int64_t num_pages, unsigned int page_size)
57 int64_t i;
59 PageCache *cache;
61 if (num_pages <= 0) {
62 DPRINTF("invalid number of pages\n");
63 return NULL;
66 /* We prefer not to abort if there is no memory */
67 cache = g_try_malloc(sizeof(*cache));
68 if (!cache) {
69 DPRINTF("Failed to allocate cache\n");
70 return NULL;
72 /* round down to the nearest power of 2 */
73 if (!is_power_of_2(num_pages)) {
74 num_pages = pow2floor(num_pages);
75 DPRINTF("rounding down to %" PRId64 "\n", num_pages);
77 cache->page_size = page_size;
78 cache->num_items = 0;
79 cache->max_item_age = 0;
80 cache->max_num_items = num_pages;
82 DPRINTF("Setting cache buckets to %" PRId64 "\n", cache->max_num_items);
84 /* We prefer not to abort if there is no memory */
85 cache->page_cache = g_try_malloc((cache->max_num_items) *
86 sizeof(*cache->page_cache));
87 if (!cache->page_cache) {
88 DPRINTF("Failed to allocate cache->page_cache\n");
89 g_free(cache);
90 return NULL;
93 for (i = 0; i < cache->max_num_items; i++) {
94 cache->page_cache[i].it_data = NULL;
95 cache->page_cache[i].it_age = 0;
96 cache->page_cache[i].it_addr = -1;
99 return cache;
102 void cache_fini(PageCache *cache)
104 int64_t i;
106 g_assert(cache);
107 g_assert(cache->page_cache);
109 for (i = 0; i < cache->max_num_items; i++) {
110 g_free(cache->page_cache[i].it_data);
113 g_free(cache->page_cache);
114 cache->page_cache = NULL;
115 g_free(cache);
118 static size_t cache_get_cache_pos(const PageCache *cache,
119 uint64_t address)
121 size_t pos;
123 g_assert(cache->max_num_items);
124 pos = (address / cache->page_size) & (cache->max_num_items - 1);
125 return pos;
128 static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr)
130 size_t pos;
132 g_assert(cache);
133 g_assert(cache->page_cache);
135 pos = cache_get_cache_pos(cache, addr);
137 return &cache->page_cache[pos];
140 uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
142 return cache_get_by_addr(cache, addr)->it_data;
145 bool cache_is_cached(const PageCache *cache, uint64_t addr,
146 uint64_t current_age)
148 CacheItem *it;
150 it = cache_get_by_addr(cache, addr);
152 if (it->it_addr == addr) {
153 /* update the it_age when the cache hit */
154 it->it_age = current_age;
155 return true;
157 return false;
160 int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata,
161 uint64_t current_age)
164 CacheItem *it;
166 /* actual update of entry */
167 it = cache_get_by_addr(cache, addr);
169 if (it->it_data && it->it_addr != addr &&
170 it->it_age + CACHED_PAGE_LIFETIME > current_age) {
171 /* the cache page is fresh, don't replace it */
172 return -1;
174 /* allocate page */
175 if (!it->it_data) {
176 it->it_data = g_try_malloc(cache->page_size);
177 if (!it->it_data) {
178 DPRINTF("Error allocating page\n");
179 return -1;
181 cache->num_items++;
184 memcpy(it->it_data, pdata, cache->page_size);
186 it->it_age = current_age;
187 it->it_addr = addr;
189 return 0;
192 int64_t cache_resize(PageCache *cache, int64_t new_num_pages)
194 PageCache *new_cache;
195 int64_t i;
197 CacheItem *old_it, *new_it;
199 g_assert(cache);
201 /* cache was not inited */
202 if (cache->page_cache == NULL) {
203 return -1;
206 /* same size */
207 if (pow2floor(new_num_pages) == cache->max_num_items) {
208 return cache->max_num_items;
211 new_cache = cache_init(new_num_pages, cache->page_size);
212 if (!(new_cache)) {
213 DPRINTF("Error creating new cache\n");
214 return -1;
217 /* move all data from old cache */
218 for (i = 0; i < cache->max_num_items; i++) {
219 old_it = &cache->page_cache[i];
220 if (old_it->it_addr != -1) {
221 /* check for collision, if there is, keep MRU page */
222 new_it = cache_get_by_addr(new_cache, old_it->it_addr);
223 if (new_it->it_data && new_it->it_age >= old_it->it_age) {
224 /* keep the MRU page */
225 g_free(old_it->it_data);
226 } else {
227 if (!new_it->it_data) {
228 new_cache->num_items++;
230 g_free(new_it->it_data);
231 new_it->it_data = old_it->it_data;
232 new_it->it_age = old_it->it_age;
233 new_it->it_addr = old_it->it_addr;
238 g_free(cache->page_cache);
239 cache->page_cache = new_cache->page_cache;
240 cache->max_num_items = new_cache->max_num_items;
241 cache->num_items = new_cache->num_items;
243 g_free(new_cache);
245 return cache->max_num_items;