]>
git.proxmox.com Git - 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>
26 #include "qemu-common.h"
27 #include "qemu/page_cache.h"
30 #define DPRINTF(fmt, ...) \
31 do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
33 #define DPRINTF(fmt, ...) \
37 typedef struct CacheItem CacheItem
;
46 CacheItem
*page_cache
;
47 unsigned int page_size
;
48 int64_t max_num_items
;
49 uint64_t max_item_age
;
53 PageCache
*cache_init(int64_t num_pages
, unsigned int page_size
)
60 DPRINTF("invalid number of pages\n");
64 cache
= g_malloc(sizeof(*cache
));
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 cache
->page_cache
= g_malloc((cache
->max_num_items
) *
79 sizeof(*cache
->page_cache
));
81 for (i
= 0; i
< cache
->max_num_items
; i
++) {
82 cache
->page_cache
[i
].it_data
= NULL
;
83 cache
->page_cache
[i
].it_age
= 0;
84 cache
->page_cache
[i
].it_addr
= -1;
90 void cache_fini(PageCache
*cache
)
95 g_assert(cache
->page_cache
);
97 for (i
= 0; i
< cache
->max_num_items
; i
++) {
98 g_free(cache
->page_cache
[i
].it_data
);
101 g_free(cache
->page_cache
);
102 cache
->page_cache
= NULL
;
105 static size_t cache_get_cache_pos(const PageCache
*cache
,
110 g_assert(cache
->max_num_items
);
111 pos
= (address
/ cache
->page_size
) & (cache
->max_num_items
- 1);
115 bool cache_is_cached(const PageCache
*cache
, uint64_t addr
)
120 g_assert(cache
->page_cache
);
122 pos
= cache_get_cache_pos(cache
, addr
);
124 return (cache
->page_cache
[pos
].it_addr
== addr
);
127 static CacheItem
*cache_get_by_addr(const PageCache
*cache
, uint64_t addr
)
132 g_assert(cache
->page_cache
);
134 pos
= cache_get_cache_pos(cache
, addr
);
136 return &cache
->page_cache
[pos
];
139 uint8_t *get_cached_data(const PageCache
*cache
, uint64_t addr
)
141 return cache_get_by_addr(cache
, addr
)->it_data
;
144 void cache_insert(PageCache
*cache
, uint64_t addr
, uint8_t *pdata
)
147 CacheItem
*it
= NULL
;
150 g_assert(cache
->page_cache
);
152 /* actual update of entry */
153 it
= cache_get_by_addr(cache
, addr
);
160 it
->it_age
= ++cache
->max_item_age
;
164 int64_t cache_resize(PageCache
*cache
, int64_t new_num_pages
)
166 PageCache
*new_cache
;
169 CacheItem
*old_it
, *new_it
;
173 /* cache was not inited */
174 if (cache
->page_cache
== NULL
) {
179 if (pow2floor(new_num_pages
) == cache
->max_num_items
) {
180 return cache
->max_num_items
;
183 new_cache
= cache_init(new_num_pages
, cache
->page_size
);
185 DPRINTF("Error creating new cache\n");
189 /* move all data from old cache */
190 for (i
= 0; i
< cache
->max_num_items
; i
++) {
191 old_it
= &cache
->page_cache
[i
];
192 if (old_it
->it_addr
!= -1) {
193 /* check for collision, if there is, keep MRU page */
194 new_it
= cache_get_by_addr(new_cache
, old_it
->it_addr
);
195 if (new_it
->it_data
) {
196 /* keep the MRU page */
197 if (new_it
->it_age
>= old_it
->it_age
) {
198 g_free(old_it
->it_data
);
200 g_free(new_it
->it_data
);
201 new_it
->it_data
= old_it
->it_data
;
202 new_it
->it_age
= old_it
->it_age
;
203 new_it
->it_addr
= old_it
->it_addr
;
206 cache_insert(new_cache
, old_it
->it_addr
, old_it
->it_data
);
211 cache
->page_cache
= new_cache
->page_cache
;
212 cache
->max_num_items
= new_cache
->max_num_items
;
213 cache
->num_items
= new_cache
->num_items
;
217 return cache
->max_num_items
;