3 * The cache is base on a hash of the page address
5 * Copyright 2012 Red Hat, Inc. and/or its affiliates
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.
21 #include <sys/types.h>
25 #include "qemu-common.h"
26 #include "migration/page_cache.h"
29 #define DPRINTF(fmt, ...) \
30 do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
32 #define DPRINTF(fmt, ...) \
36 /* the page in cache will not be replaced in two cycles */
37 #define CACHED_PAGE_LIFETIME 2
39 typedef struct CacheItem CacheItem
;
48 CacheItem
*page_cache
;
49 unsigned int page_size
;
50 int64_t max_num_items
;
51 uint64_t max_item_age
;
55 PageCache
*cache_init(int64_t num_pages
, unsigned int page_size
)
62 DPRINTF("invalid number of pages\n");
66 /* We prefer not to abort if there is no memory */
67 cache
= g_try_malloc(sizeof(*cache
));
69 DPRINTF("Failed to allocate cache\n");
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
;
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");
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;
102 void cache_fini(PageCache
*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
;
118 static size_t cache_get_cache_pos(const PageCache
*cache
,
123 g_assert(cache
->max_num_items
);
124 pos
= (address
/ cache
->page_size
) & (cache
->max_num_items
- 1);
128 static CacheItem
*cache_get_by_addr(const PageCache
*cache
, uint64_t addr
)
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
)
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
;
160 int cache_insert(PageCache
*cache
, uint64_t addr
, const uint8_t *pdata
,
161 uint64_t current_age
)
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 */
176 it
->it_data
= g_try_malloc(cache
->page_size
);
178 DPRINTF("Error allocating page\n");
184 memcpy(it
->it_data
, pdata
, cache
->page_size
);
186 it
->it_age
= current_age
;
192 int64_t cache_resize(PageCache
*cache
, int64_t new_num_pages
)
194 PageCache
*new_cache
;
197 CacheItem
*old_it
, *new_it
;
201 /* cache was not inited */
202 if (cache
->page_cache
== NULL
) {
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
);
213 DPRINTF("Error creating new cache\n");
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
);
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
;
245 return cache
->max_num_items
;