1
2
3
4
5
6
7
8
9
10
11
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>
24
25#include "qemu-common.h"
26#include "migration/page_cache.h"
27
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
36
37#define CACHED_PAGE_LIFETIME 2
38
39typedef struct CacheItem CacheItem;
40
41struct CacheItem {
42 uint64_t it_addr;
43 uint64_t it_age;
44 uint8_t *it_data;
45};
46
47struct 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;
53};
54
55PageCache *cache_init(int64_t num_pages, unsigned int page_size)
56{
57 int64_t i;
58
59 PageCache *cache;
60
61 if (num_pages <= 0) {
62 DPRINTF("invalid number of pages\n");
63 return NULL;
64 }
65
66
67 cache = g_try_malloc(sizeof(*cache));
68 if (!cache) {
69 DPRINTF("Failed to allocate cache\n");
70 return NULL;
71 }
72
73 if (!is_power_of_2(num_pages)) {
74 num_pages = pow2floor(num_pages);
75 DPRINTF("rounding down to %" PRId64 "\n", num_pages);
76 }
77 cache->page_size = page_size;
78 cache->num_items = 0;
79 cache->max_item_age = 0;
80 cache->max_num_items = num_pages;
81
82 DPRINTF("Setting cache buckets to %" PRId64 "\n", cache->max_num_items);
83
84
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;
91 }
92
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;
97 }
98
99 return cache;
100}
101
102void cache_fini(PageCache *cache)
103{
104 int64_t i;
105
106 g_assert(cache);
107 g_assert(cache->page_cache);
108
109 for (i = 0; i < cache->max_num_items; i++) {
110 g_free(cache->page_cache[i].it_data);
111 }
112
113 g_free(cache->page_cache);
114 cache->page_cache = NULL;
115 g_free(cache);
116}
117
118static size_t cache_get_cache_pos(const PageCache *cache,
119 uint64_t address)
120{
121 size_t pos;
122
123 g_assert(cache->max_num_items);
124 pos = (address / cache->page_size) & (cache->max_num_items - 1);
125 return pos;
126}
127
128static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr)
129{
130 size_t pos;
131
132 g_assert(cache);
133 g_assert(cache->page_cache);
134
135 pos = cache_get_cache_pos(cache, addr);
136
137 return &cache->page_cache[pos];
138}
139
140uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
141{
142 return cache_get_by_addr(cache, addr)->it_data;
143}
144
145bool cache_is_cached(const PageCache *cache, uint64_t addr,
146 uint64_t current_age)
147{
148 CacheItem *it;
149
150 it = cache_get_by_addr(cache, addr);
151
152 if (it->it_addr == addr) {
153
154 it->it_age = current_age;
155 return true;
156 }
157 return false;
158}
159
160int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata,
161 uint64_t current_age)
162{
163
164 CacheItem *it;
165
166
167 it = cache_get_by_addr(cache, addr);
168
169 if (it->it_data && it->it_addr != addr &&
170 it->it_age + CACHED_PAGE_LIFETIME > current_age) {
171
172 return -1;
173 }
174
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;
180 }
181 cache->num_items++;
182 }
183
184 memcpy(it->it_data, pdata, cache->page_size);
185
186 it->it_age = current_age;
187 it->it_addr = addr;
188
189 return 0;
190}
191
192int64_t cache_resize(PageCache *cache, int64_t new_num_pages)
193{
194 PageCache *new_cache;
195 int64_t i;
196
197 CacheItem *old_it, *new_it;
198
199 g_assert(cache);
200
201
202 if (cache->page_cache == NULL) {
203 return -1;
204 }
205
206
207 if (pow2floor(new_num_pages) == cache->max_num_items) {
208 return cache->max_num_items;
209 }
210
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;
215 }
216
217
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
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
225 g_free(old_it->it_data);
226 } else {
227 if (!new_it->it_data) {
228 new_cache->num_items++;
229 }
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;
234 }
235 }
236 }
237
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;
242
243 g_free(new_cache);
244
245 return cache->max_num_items;
246}
247