Merge remote-tracking branch 'bonzini/nbd-next' into staging
[qemu/opensuse.git] / page_cache.c
blob0294f7e9f611aeff3449eb0355595b6f556124f7
1 /*
2 * Page cache for QEMU
3 * The cache is base on a hash of the page address
5 * Copyright 2012 Red Hat, Inc. and/or its affiliates
7 * Authors:
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 <stdint.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <strings.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <stdbool.h>
23 #include <glib.h>
24 #include <strings.h>
26 #include "qemu-common.h"
27 #include "qemu/page_cache.h"
29 #ifdef DEBUG_CACHE
30 #define DPRINTF(fmt, ...) \
31 do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
32 #else
33 #define DPRINTF(fmt, ...) \
34 do { } while (0)
35 #endif
37 typedef struct CacheItem CacheItem;
39 struct CacheItem {
40 uint64_t it_addr;
41 uint64_t it_age;
42 uint8_t *it_data;
45 struct PageCache {
46 CacheItem *page_cache;
47 unsigned int page_size;
48 int64_t max_num_items;
49 uint64_t max_item_age;
50 int64_t num_items;
53 PageCache *cache_init(int64_t num_pages, unsigned int page_size)
55 int64_t i;
57 PageCache *cache;
59 if (num_pages <= 0) {
60 DPRINTF("invalid number of pages\n");
61 return NULL;
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;
72 cache->num_items = 0;
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;
87 return cache;
90 void cache_fini(PageCache *cache)
92 int64_t i;
94 g_assert(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,
106 uint64_t address)
108 size_t pos;
110 g_assert(cache->max_num_items);
111 pos = (address / cache->page_size) & (cache->max_num_items - 1);
112 return pos;
115 bool cache_is_cached(const PageCache *cache, uint64_t addr)
117 size_t pos;
119 g_assert(cache);
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)
129 size_t pos;
131 g_assert(cache);
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;
149 g_assert(cache);
150 g_assert(cache->page_cache);
152 /* actual update of entry */
153 it = cache_get_by_addr(cache, addr);
155 if (!it->it_data) {
156 cache->num_items++;
159 it->it_data = pdata;
160 it->it_age = ++cache->max_item_age;
161 it->it_addr = addr;
164 int64_t cache_resize(PageCache *cache, int64_t new_num_pages)
166 PageCache *new_cache;
167 int64_t i;
169 CacheItem *old_it, *new_it;
171 g_assert(cache);
173 /* cache was not inited */
174 if (cache->page_cache == NULL) {
175 return -1;
178 /* same size */
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);
184 if (!(new_cache)) {
185 DPRINTF("Error creating new cache\n");
186 return -1;
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);
199 } else {
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;
205 } else {
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;
215 g_free(new_cache);
217 return cache->max_num_items;