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.
15 #include "qemu/osdep.h"
18 #include "qemu-common.h"
19 #include "qemu/host-utils.h"
20 #include "migration/page_cache.h"
23 #define DPRINTF(fmt, ...) \
24 do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
26 #define DPRINTF(fmt, ...) \
30 /* the page in cache will not be replaced in two cycles */
31 #define CACHED_PAGE_LIFETIME 2
33 typedef struct CacheItem CacheItem
;
42 CacheItem
*page_cache
;
43 unsigned int page_size
;
44 int64_t max_num_items
;
45 uint64_t max_item_age
;
49 PageCache
*cache_init(int64_t num_pages
, unsigned int page_size
)
56 DPRINTF("invalid number of pages\n");
60 /* We prefer not to abort if there is no memory */
61 cache
= g_try_malloc(sizeof(*cache
));
63 DPRINTF("Failed to allocate cache\n");
66 /* round down to the nearest power of 2 */
67 if (!is_power_of_2(num_pages
)) {
68 num_pages
= pow2floor(num_pages
);
69 DPRINTF("rounding down to %" PRId64
"\n", num_pages
);
71 cache
->page_size
= page_size
;
73 cache
->max_item_age
= 0;
74 cache
->max_num_items
= num_pages
;
76 DPRINTF("Setting cache buckets to %" PRId64
"\n", cache
->max_num_items
);
78 /* We prefer not to abort if there is no memory */
79 cache
->page_cache
= g_try_malloc((cache
->max_num_items
) *
80 sizeof(*cache
->page_cache
));
81 if (!cache
->page_cache
) {
82 DPRINTF("Failed to allocate cache->page_cache\n");
87 for (i
= 0; i
< cache
->max_num_items
; i
++) {
88 cache
->page_cache
[i
].it_data
= NULL
;
89 cache
->page_cache
[i
].it_age
= 0;
90 cache
->page_cache
[i
].it_addr
= -1;
96 void cache_fini(PageCache
*cache
)
101 g_assert(cache
->page_cache
);
103 for (i
= 0; i
< cache
->max_num_items
; i
++) {
104 g_free(cache
->page_cache
[i
].it_data
);
107 g_free(cache
->page_cache
);
108 cache
->page_cache
= NULL
;
112 static size_t cache_get_cache_pos(const PageCache
*cache
,
117 g_assert(cache
->max_num_items
);
118 pos
= (address
/ cache
->page_size
) & (cache
->max_num_items
- 1);
122 static CacheItem
*cache_get_by_addr(const PageCache
*cache
, uint64_t addr
)
127 g_assert(cache
->page_cache
);
129 pos
= cache_get_cache_pos(cache
, addr
);
131 return &cache
->page_cache
[pos
];
134 uint8_t *get_cached_data(const PageCache
*cache
, uint64_t addr
)
136 return cache_get_by_addr(cache
, addr
)->it_data
;
139 bool cache_is_cached(const PageCache
*cache
, uint64_t addr
,
140 uint64_t current_age
)
144 it
= cache_get_by_addr(cache
, addr
);
146 if (it
->it_addr
== addr
) {
147 /* update the it_age when the cache hit */
148 it
->it_age
= current_age
;
154 int cache_insert(PageCache
*cache
, uint64_t addr
, const uint8_t *pdata
,
155 uint64_t current_age
)
160 /* actual update of entry */
161 it
= cache_get_by_addr(cache
, addr
);
163 if (it
->it_data
&& it
->it_addr
!= addr
&&
164 it
->it_age
+ CACHED_PAGE_LIFETIME
> current_age
) {
165 /* the cache page is fresh, don't replace it */
170 it
->it_data
= g_try_malloc(cache
->page_size
);
172 DPRINTF("Error allocating page\n");
178 memcpy(it
->it_data
, pdata
, cache
->page_size
);
180 it
->it_age
= current_age
;
186 int64_t cache_resize(PageCache
*cache
, int64_t new_num_pages
)
188 PageCache
*new_cache
;
191 CacheItem
*old_it
, *new_it
;
195 /* cache was not inited */
196 if (cache
->page_cache
== NULL
) {
201 if (pow2floor(new_num_pages
) == cache
->max_num_items
) {
202 return cache
->max_num_items
;
205 new_cache
= cache_init(new_num_pages
, cache
->page_size
);
207 DPRINTF("Error creating new cache\n");
211 /* move all data from old cache */
212 for (i
= 0; i
< cache
->max_num_items
; i
++) {
213 old_it
= &cache
->page_cache
[i
];
214 if (old_it
->it_addr
!= -1) {
215 /* check for collision, if there is, keep MRU page */
216 new_it
= cache_get_by_addr(new_cache
, old_it
->it_addr
);
217 if (new_it
->it_data
&& new_it
->it_age
>= old_it
->it_age
) {
218 /* keep the MRU page */
219 g_free(old_it
->it_data
);
221 if (!new_it
->it_data
) {
222 new_cache
->num_items
++;
224 g_free(new_it
->it_data
);
225 new_it
->it_data
= old_it
->it_data
;
226 new_it
->it_age
= old_it
->it_age
;
227 new_it
->it_addr
= old_it
->it_addr
;
232 g_free(cache
->page_cache
);
233 cache
->page_cache
= new_cache
->page_cache
;
234 cache
->max_num_items
= new_cache
->max_num_items
;
235 cache
->num_items
= new_cache
->num_items
;
239 return cache
->max_num_items
;