]>
git.proxmox.com Git - mirror_qemu.git/blob - migration/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.
15 #include "qemu/osdep.h"
17 #include "qemu-common.h"
18 #include "qemu/host-utils.h"
19 #include "migration/page_cache.h"
22 #define DPRINTF(fmt, ...) \
23 do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
25 #define DPRINTF(fmt, ...) \
29 /* the page in cache will not be replaced in two cycles */
30 #define CACHED_PAGE_LIFETIME 2
32 typedef struct CacheItem CacheItem
;
41 CacheItem
*page_cache
;
42 unsigned int page_size
;
43 int64_t max_num_items
;
47 PageCache
*cache_init(int64_t num_pages
, unsigned int page_size
)
54 DPRINTF("invalid number of pages\n");
58 /* We prefer not to abort if there is no memory */
59 cache
= g_try_malloc(sizeof(*cache
));
61 DPRINTF("Failed to allocate cache\n");
64 /* round down to the nearest power of 2 */
65 if (!is_power_of_2(num_pages
)) {
66 num_pages
= pow2floor(num_pages
);
67 DPRINTF("rounding down to %" PRId64
"\n", num_pages
);
69 cache
->page_size
= page_size
;
71 cache
->max_num_items
= num_pages
;
73 DPRINTF("Setting cache buckets to %" PRId64
"\n", cache
->max_num_items
);
75 /* We prefer not to abort if there is no memory */
76 cache
->page_cache
= g_try_malloc((cache
->max_num_items
) *
77 sizeof(*cache
->page_cache
));
78 if (!cache
->page_cache
) {
79 DPRINTF("Failed to allocate cache->page_cache\n");
84 for (i
= 0; i
< cache
->max_num_items
; i
++) {
85 cache
->page_cache
[i
].it_data
= NULL
;
86 cache
->page_cache
[i
].it_age
= 0;
87 cache
->page_cache
[i
].it_addr
= -1;
93 void cache_fini(PageCache
*cache
)
98 g_assert(cache
->page_cache
);
100 for (i
= 0; i
< cache
->max_num_items
; i
++) {
101 g_free(cache
->page_cache
[i
].it_data
);
104 g_free(cache
->page_cache
);
105 cache
->page_cache
= NULL
;
109 static size_t cache_get_cache_pos(const PageCache
*cache
,
112 g_assert(cache
->max_num_items
);
113 return (address
/ cache
->page_size
) & (cache
->max_num_items
- 1);
116 static CacheItem
*cache_get_by_addr(const PageCache
*cache
, uint64_t addr
)
121 g_assert(cache
->page_cache
);
123 pos
= cache_get_cache_pos(cache
, addr
);
125 return &cache
->page_cache
[pos
];
128 uint8_t *get_cached_data(const PageCache
*cache
, uint64_t addr
)
130 return cache_get_by_addr(cache
, addr
)->it_data
;
133 bool cache_is_cached(const PageCache
*cache
, uint64_t addr
,
134 uint64_t current_age
)
138 it
= cache_get_by_addr(cache
, addr
);
140 if (it
->it_addr
== addr
) {
141 /* update the it_age when the cache hit */
142 it
->it_age
= current_age
;
148 int cache_insert(PageCache
*cache
, uint64_t addr
, const uint8_t *pdata
,
149 uint64_t current_age
)
154 /* actual update of entry */
155 it
= cache_get_by_addr(cache
, addr
);
157 if (it
->it_data
&& it
->it_addr
!= addr
&&
158 it
->it_age
+ CACHED_PAGE_LIFETIME
> current_age
) {
159 /* the cache page is fresh, don't replace it */
164 it
->it_data
= g_try_malloc(cache
->page_size
);
166 DPRINTF("Error allocating page\n");
172 memcpy(it
->it_data
, pdata
, cache
->page_size
);
174 it
->it_age
= current_age
;