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 "qapi/qmp/qerror.h"
18 #include "qapi/error.h"
19 #include "qemu/host-utils.h"
20 #include "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
;
48 PageCache
*cache_init(int64_t new_size
, size_t page_size
, Error
**errp
)
51 size_t num_pages
= new_size
/ page_size
;
54 if (new_size
< page_size
) {
55 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
56 "is smaller than one target page size");
60 /* round down to the nearest power of 2 */
61 if (!is_power_of_2(num_pages
)) {
62 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
63 "is not a power of two number of pages");
67 /* We prefer not to abort if there is no memory */
68 cache
= g_try_malloc(sizeof(*cache
));
70 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
71 "Failed to allocate cache");
74 cache
->page_size
= page_size
;
76 cache
->max_num_items
= num_pages
;
78 DPRINTF("Setting cache buckets to %" PRId64
"\n", cache
->max_num_items
);
80 /* We prefer not to abort if there is no memory */
81 cache
->page_cache
= g_try_malloc((cache
->max_num_items
) *
82 sizeof(*cache
->page_cache
));
83 if (!cache
->page_cache
) {
84 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
85 "Failed to allocate page cache");
90 for (i
= 0; i
< cache
->max_num_items
; i
++) {
91 cache
->page_cache
[i
].it_data
= NULL
;
92 cache
->page_cache
[i
].it_age
= 0;
93 cache
->page_cache
[i
].it_addr
= -1;
99 void cache_fini(PageCache
*cache
)
104 g_assert(cache
->page_cache
);
106 for (i
= 0; i
< cache
->max_num_items
; i
++) {
107 g_free(cache
->page_cache
[i
].it_data
);
110 g_free(cache
->page_cache
);
111 cache
->page_cache
= NULL
;
115 static size_t cache_get_cache_pos(const PageCache
*cache
,
118 g_assert(cache
->max_num_items
);
119 return (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
;