blob: 62a53f8c8969b515050d29ea184dc26a3c28f6de [file] [log] [blame]
Orit Wasserman9fb26642012-08-06 21:42:50 +03001/*
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 <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>
Orit Wasserman9fb26642012-08-06 21:42:50 +030024
25#include "qemu-common.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010026#include "migration/page_cache.h"
Orit Wasserman9fb26642012-08-06 21:42:50 +030027
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
35
36typedef struct CacheItem CacheItem;
37
38struct CacheItem {
39 uint64_t it_addr;
40 uint64_t it_age;
41 uint8_t *it_data;
42};
43
44struct PageCache {
45 CacheItem *page_cache;
46 unsigned int page_size;
47 int64_t max_num_items;
48 uint64_t max_item_age;
49 int64_t num_items;
50};
51
52PageCache *cache_init(int64_t num_pages, unsigned int page_size)
53{
54 int64_t i;
55
56 PageCache *cache;
57
58 if (num_pages <= 0) {
59 DPRINTF("invalid number of pages\n");
60 return NULL;
61 }
62
Orit Wassermana17b2fd2014-01-30 20:08:37 +020063 /* We prefer not to abort if there is no memory */
64 cache = g_try_malloc(sizeof(*cache));
65 if (!cache) {
66 DPRINTF("Failed to allocate cache\n");
67 return NULL;
68 }
Orit Wasserman9fb26642012-08-06 21:42:50 +030069 /* round down to the nearest power of 2 */
70 if (!is_power_of_2(num_pages)) {
71 num_pages = pow2floor(num_pages);
72 DPRINTF("rounding down to %" PRId64 "\n", num_pages);
73 }
74 cache->page_size = page_size;
75 cache->num_items = 0;
76 cache->max_item_age = 0;
77 cache->max_num_items = num_pages;
78
79 DPRINTF("Setting cache buckets to %" PRId64 "\n", cache->max_num_items);
80
Orit Wassermana17b2fd2014-01-30 20:08:37 +020081 /* We prefer not to abort if there is no memory */
82 cache->page_cache = g_try_malloc((cache->max_num_items) *
83 sizeof(*cache->page_cache));
84 if (!cache->page_cache) {
85 DPRINTF("Failed to allocate cache->page_cache\n");
86 g_free(cache);
87 return NULL;
88 }
Orit Wasserman9fb26642012-08-06 21:42:50 +030089
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
99void 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;
112}
113
114static size_t cache_get_cache_pos(const PageCache *cache,
115 uint64_t address)
116{
117 size_t pos;
118
119 g_assert(cache->max_num_items);
120 pos = (address / cache->page_size) & (cache->max_num_items - 1);
121 return pos;
122}
123
124bool cache_is_cached(const PageCache *cache, uint64_t addr)
125{
126 size_t pos;
127
128 g_assert(cache);
129 g_assert(cache->page_cache);
130
131 pos = cache_get_cache_pos(cache, addr);
132
133 return (cache->page_cache[pos].it_addr == addr);
134}
135
136static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr)
137{
138 size_t pos;
139
140 g_assert(cache);
141 g_assert(cache->page_cache);
142
143 pos = cache_get_cache_pos(cache, addr);
144
145 return &cache->page_cache[pos];
146}
147
148uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
149{
150 return cache_get_by_addr(cache, addr)->it_data;
151}
152
153void cache_insert(PageCache *cache, uint64_t addr, uint8_t *pdata)
154{
155
156 CacheItem *it = NULL;
157
158 g_assert(cache);
159 g_assert(cache->page_cache);
160
161 /* actual update of entry */
162 it = cache_get_by_addr(cache, addr);
163
Peter Lieven32a1c082013-02-25 19:12:03 +0200164 /* free old cached data if any */
165 g_free(it->it_data);
166
Orit Wasserman9fb26642012-08-06 21:42:50 +0300167 if (!it->it_data) {
168 cache->num_items++;
169 }
170
Peter Lievenee0b44a2013-02-25 19:12:04 +0200171 it->it_data = g_memdup(pdata, cache->page_size);
Orit Wasserman9fb26642012-08-06 21:42:50 +0300172 it->it_age = ++cache->max_item_age;
173 it->it_addr = addr;
174}
175
176int64_t cache_resize(PageCache *cache, int64_t new_num_pages)
177{
178 PageCache *new_cache;
179 int64_t i;
180
181 CacheItem *old_it, *new_it;
182
183 g_assert(cache);
184
185 /* cache was not inited */
186 if (cache->page_cache == NULL) {
187 return -1;
188 }
189
190 /* same size */
191 if (pow2floor(new_num_pages) == cache->max_num_items) {
192 return cache->max_num_items;
193 }
194
195 new_cache = cache_init(new_num_pages, cache->page_size);
196 if (!(new_cache)) {
197 DPRINTF("Error creating new cache\n");
198 return -1;
199 }
200
201 /* move all data from old cache */
202 for (i = 0; i < cache->max_num_items; i++) {
203 old_it = &cache->page_cache[i];
204 if (old_it->it_addr != -1) {
205 /* check for collision, if there is, keep MRU page */
206 new_it = cache_get_by_addr(new_cache, old_it->it_addr);
Orit Wassermana0ee2032013-02-25 19:12:02 +0200207 if (new_it->it_data && new_it->it_age >= old_it->it_age) {
Orit Wasserman9fb26642012-08-06 21:42:50 +0300208 /* keep the MRU page */
Orit Wassermana0ee2032013-02-25 19:12:02 +0200209 g_free(old_it->it_data);
Orit Wasserman9fb26642012-08-06 21:42:50 +0300210 } else {
Orit Wassermana0ee2032013-02-25 19:12:02 +0200211 if (!new_it->it_data) {
212 new_cache->num_items++;
213 }
214 g_free(new_it->it_data);
215 new_it->it_data = old_it->it_data;
216 new_it->it_age = old_it->it_age;
217 new_it->it_addr = old_it->it_addr;
Orit Wasserman9fb26642012-08-06 21:42:50 +0300218 }
219 }
220 }
221
Orit Wasserman0db65d62013-02-25 19:12:01 +0200222 g_free(cache->page_cache);
Orit Wasserman9fb26642012-08-06 21:42:50 +0300223 cache->page_cache = new_cache->page_cache;
224 cache->max_num_items = new_cache->max_num_items;
225 cache->num_items = new_cache->num_items;
226
227 g_free(new_cache);
228
229 return cache->max_num_items;
230}