]>
git.proxmox.com Git - mirror_qemu.git/blob - page_cache.c
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 bool cache_is_cached(const PageCache
*cache
, uint64_t addr
,
129 uint64_t current_age
)
134 g_assert(cache
->page_cache
);
136 pos
= cache_get_cache_pos(cache
, addr
);
138 if (cache
->page_cache
[pos
].it_addr
== addr
) {
139 /* update the it_age when the cache hit */
140 cache
->page_cache
[pos
].it_age
= current_age
;
146 static CacheItem
*cache_get_by_addr(const PageCache
*cache
, uint64_t addr
)
151 g_assert(cache
->page_cache
);
153 pos
= cache_get_cache_pos(cache
, addr
);
155 return &cache
->page_cache
[pos
];
158 uint8_t *get_cached_data(const PageCache
*cache
, uint64_t addr
)
160 return cache_get_by_addr(cache
, addr
)->it_data
;
163 int cache_insert(PageCache
*cache
, uint64_t addr
, const uint8_t *pdata
,
164 uint64_t current_age
)
167 CacheItem
*it
= NULL
;
170 g_assert(cache
->page_cache
);
172 /* actual update of entry */
173 it
= cache_get_by_addr(cache
, addr
);
175 if (it
->it_data
&& it
->it_addr
!= addr
&&
176 it
->it_age
+ CACHED_PAGE_LIFETIME
> current_age
) {
177 /* the cache page is fresh, don't replace it */
182 it
->it_data
= g_try_malloc(cache
->page_size
);
184 DPRINTF("Error allocating page\n");
190 memcpy(it
->it_data
, pdata
, cache
->page_size
);
192 it
->it_age
= current_age
;
198 int64_t cache_resize(PageCache
*cache
, int64_t new_num_pages
)
200 PageCache
*new_cache
;
203 CacheItem
*old_it
, *new_it
;
207 /* cache was not inited */
208 if (cache
->page_cache
== NULL
) {
213 if (pow2floor(new_num_pages
) == cache
->max_num_items
) {
214 return cache
->max_num_items
;
217 new_cache
= cache_init(new_num_pages
, cache
->page_size
);
219 DPRINTF("Error creating new cache\n");
223 /* move all data from old cache */
224 for (i
= 0; i
< cache
->max_num_items
; i
++) {
225 old_it
= &cache
->page_cache
[i
];
226 if (old_it
->it_addr
!= -1) {
227 /* check for collision, if there is, keep MRU page */
228 new_it
= cache_get_by_addr(new_cache
, old_it
->it_addr
);
229 if (new_it
->it_data
&& new_it
->it_age
>= old_it
->it_age
) {
230 /* keep the MRU page */
231 g_free(old_it
->it_data
);
233 if (!new_it
->it_data
) {
234 new_cache
->num_items
++;
236 g_free(new_it
->it_data
);
237 new_it
->it_data
= old_it
->it_data
;
238 new_it
->it_age
= old_it
->it_age
;
239 new_it
->it_addr
= old_it
->it_addr
;
244 g_free(cache
->page_cache
);
245 cache
->page_cache
= new_cache
->page_cache
;
246 cache
->max_num_items
= new_cache
->max_num_items
;
247 cache
->num_items
= new_cache
->num_items
;
251 return cache
->max_num_items
;