xref: /openbmc/qemu/migration/page_cache.c (revision 5ade579b)
1 /*
2  * Page cache for QEMU
3  * The cache is base on a hash of the page address
4  *
5  * Copyright 2012 Red Hat, Inc. and/or its affiliates
6  *
7  * Authors:
8  *  Orit Wasserman  <owasserm@redhat.com>
9  *
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.
12  *
13  */
14 
15 #include "qemu/osdep.h"
16 
17 #include "qapi/qmp/qerror.h"
18 #include "qapi/error.h"
19 #include "qemu/host-utils.h"
20 #include "page_cache.h"
21 #include "trace.h"
22 
23 /* the page in cache will not be replaced in two cycles */
24 #define CACHED_PAGE_LIFETIME 2
25 
26 typedef struct CacheItem CacheItem;
27 
28 struct CacheItem {
29     uint64_t it_addr;
30     uint64_t it_age;
31     uint8_t *it_data;
32 };
33 
34 struct PageCache {
35     CacheItem *page_cache;
36     size_t page_size;
37     size_t max_num_items;
38     size_t num_items;
39 };
40 
41 PageCache *cache_init(int64_t new_size, size_t page_size, Error **errp)
42 {
43     int64_t i;
44     size_t num_pages = new_size / page_size;
45     PageCache *cache;
46 
47     if (new_size < page_size) {
48         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
49                    "is smaller than one target page size");
50         return NULL;
51     }
52 
53     /* round down to the nearest power of 2 */
54     if (!is_power_of_2(num_pages)) {
55         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
56                    "is not a power of two number of pages");
57         return NULL;
58     }
59 
60     /* We prefer not to abort if there is no memory */
61     cache = g_try_malloc(sizeof(*cache));
62     if (!cache) {
63         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
64                    "Failed to allocate cache");
65         return NULL;
66     }
67     cache->page_size = page_size;
68     cache->num_items = 0;
69     cache->max_num_items = num_pages;
70 
71     trace_migration_pagecache_init(cache->max_num_items);
72 
73     /* We prefer not to abort if there is no memory */
74     cache->page_cache = g_try_malloc((cache->max_num_items) *
75                                      sizeof(*cache->page_cache));
76     if (!cache->page_cache) {
77         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
78                    "Failed to allocate page cache");
79         g_free(cache);
80         return NULL;
81     }
82 
83     for (i = 0; i < cache->max_num_items; i++) {
84         cache->page_cache[i].it_data = NULL;
85         cache->page_cache[i].it_age = 0;
86         cache->page_cache[i].it_addr = -1;
87     }
88 
89     return cache;
90 }
91 
92 void cache_fini(PageCache *cache)
93 {
94     int64_t i;
95 
96     g_assert(cache);
97     g_assert(cache->page_cache);
98 
99     for (i = 0; i < cache->max_num_items; i++) {
100         g_free(cache->page_cache[i].it_data);
101     }
102 
103     g_free(cache->page_cache);
104     cache->page_cache = NULL;
105     g_free(cache);
106 }
107 
108 static size_t cache_get_cache_pos(const PageCache *cache,
109                                   uint64_t address)
110 {
111     g_assert(cache->max_num_items);
112     return (address / cache->page_size) & (cache->max_num_items - 1);
113 }
114 
115 static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr)
116 {
117     size_t pos;
118 
119     g_assert(cache);
120     g_assert(cache->page_cache);
121 
122     pos = cache_get_cache_pos(cache, addr);
123 
124     return &cache->page_cache[pos];
125 }
126 
127 uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
128 {
129     return cache_get_by_addr(cache, addr)->it_data;
130 }
131 
132 bool cache_is_cached(const PageCache *cache, uint64_t addr,
133                      uint64_t current_age)
134 {
135     CacheItem *it;
136 
137     it = cache_get_by_addr(cache, addr);
138 
139     if (it->it_addr == addr) {
140         /* update the it_age when the cache hit */
141         it->it_age = current_age;
142         return true;
143     }
144     return false;
145 }
146 
147 int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata,
148                  uint64_t current_age)
149 {
150 
151     CacheItem *it;
152 
153     /* actual update of entry */
154     it = cache_get_by_addr(cache, addr);
155 
156     if (it->it_data && it->it_addr != addr &&
157         it->it_age + CACHED_PAGE_LIFETIME > current_age) {
158         /* the cache page is fresh, don't replace it */
159         return -1;
160     }
161     /* allocate page */
162     if (!it->it_data) {
163         it->it_data = g_try_malloc(cache->page_size);
164         if (!it->it_data) {
165             trace_migration_pagecache_insert();
166             return -1;
167         }
168         cache->num_items++;
169     }
170 
171     memcpy(it->it_data, pdata, cache->page_size);
172 
173     it->it_age = current_age;
174     it->it_addr = addr;
175 
176     return 0;
177 }
178