[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PATCH v4 02/39] qcow2: Add table size field to Qcow2Cache
From: |
Alberto Garcia |
Subject: |
[Qemu-block] [PATCH v4 02/39] qcow2: Add table size field to Qcow2Cache |
Date: |
Mon, 5 Feb 2018 16:33:02 +0200 |
The table size in the qcow2 cache is currently equal to the cluster
size. This doesn't allow us to use the cache memory efficiently,
particularly with large cluster sizes, so we need to be able to have
smaller cache tables that are independent from the cluster size. This
patch adds a new field to Qcow2Cache that we can use instead of the
cluster size.
The current table size is still being initialized to the cluster size,
so there are no semantic changes yet, but this patch will allow us to
prepare the rest of the code and simplify a few function calls.
Signed-off-by: Alberto Garcia <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
Reviewed-by: Max Reitz <address@hidden>
---
block/qcow2-cache.c | 29 ++++++++++++++---------------
1 file changed, 14 insertions(+), 15 deletions(-)
diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c
index c48ffebd8f..38c03770b4 100644
--- a/block/qcow2-cache.c
+++ b/block/qcow2-cache.c
@@ -39,6 +39,7 @@ struct Qcow2Cache {
Qcow2CachedTable *entries;
struct Qcow2Cache *depends;
int size;
+ int table_size;
bool depends_on_flush;
void *table_array;
uint64_t lru_counter;
@@ -48,17 +49,15 @@ struct Qcow2Cache {
static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs,
Qcow2Cache *c, int table)
{
- BDRVQcow2State *s = bs->opaque;
- return (uint8_t *) c->table_array + (size_t) table * s->cluster_size;
+ return (uint8_t *) c->table_array + (size_t) table * c->table_size;
}
static inline int qcow2_cache_get_table_idx(BlockDriverState *bs,
Qcow2Cache *c, void *table)
{
- BDRVQcow2State *s = bs->opaque;
ptrdiff_t table_offset = (uint8_t *) table - (uint8_t *) c->table_array;
- int idx = table_offset / s->cluster_size;
- assert(idx >= 0 && idx < c->size && table_offset % s->cluster_size == 0);
+ int idx = table_offset / c->table_size;
+ assert(idx >= 0 && idx < c->size && table_offset % c->table_size == 0);
return idx;
}
@@ -79,10 +78,9 @@ static void qcow2_cache_table_release(BlockDriverState *bs,
Qcow2Cache *c,
{
/* Using MADV_DONTNEED to discard memory is a Linux-specific feature */
#ifdef CONFIG_LINUX
- BDRVQcow2State *s = bs->opaque;
void *t = qcow2_cache_get_table_addr(bs, c, i);
int align = getpagesize();
- size_t mem_size = (size_t) s->cluster_size * num_tables;
+ size_t mem_size = (size_t) c->table_size * num_tables;
size_t offset = QEMU_ALIGN_UP((uintptr_t) t, align) - (uintptr_t) t;
size_t length = QEMU_ALIGN_DOWN(mem_size - offset, align);
if (mem_size > offset && length > 0) {
@@ -132,9 +130,10 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int
num_tables)
c = g_new0(Qcow2Cache, 1);
c->size = num_tables;
+ c->table_size = s->cluster_size;
c->entries = g_try_new0(Qcow2CachedTable, num_tables);
c->table_array = qemu_try_blockalign(bs->file->bs,
- (size_t) num_tables *
s->cluster_size);
+ (size_t) num_tables * c->table_size);
if (!c->entries || !c->table_array) {
qemu_vfree(c->table_array);
@@ -203,13 +202,13 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs,
Qcow2Cache *c, int i)
if (c == s->refcount_block_cache) {
ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_BLOCK,
- c->entries[i].offset, s->cluster_size);
+ c->entries[i].offset, c->table_size);
} else if (c == s->l2_table_cache) {
ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
- c->entries[i].offset, s->cluster_size);
+ c->entries[i].offset, c->table_size);
} else {
ret = qcow2_pre_write_overlap_check(bs, 0,
- c->entries[i].offset, s->cluster_size);
+ c->entries[i].offset, c->table_size);
}
if (ret < 0) {
@@ -223,7 +222,7 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs,
Qcow2Cache *c, int i)
}
ret = bdrv_pwrite(bs->file, c->entries[i].offset,
- qcow2_cache_get_table_addr(bs, c, i), s->cluster_size);
+ qcow2_cache_get_table_addr(bs, c, i), c->table_size);
if (ret < 0) {
return ret;
}
@@ -331,7 +330,7 @@ static int qcow2_cache_do_get(BlockDriverState *bs,
Qcow2Cache *c,
trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache,
offset, read_from_disk);
- if (offset_into_cluster(s, offset)) {
+ if (!QEMU_IS_ALIGNED(offset, c->table_size)) {
qcow2_signal_corruption(bs, true, -1, -1, "Cannot get entry from %s "
"cache: Offset %#" PRIx64 " is unaligned",
qcow2_cache_get_name(s, c), offset);
@@ -339,7 +338,7 @@ static int qcow2_cache_do_get(BlockDriverState *bs,
Qcow2Cache *c,
}
/* Check if the table is already cached */
- i = lookup_index = (offset / s->cluster_size * 4) % c->size;
+ i = lookup_index = (offset / c->table_size * 4) % c->size;
do {
const Qcow2CachedTable *t = &c->entries[i];
if (t->offset == offset) {
@@ -380,7 +379,7 @@ static int qcow2_cache_do_get(BlockDriverState *bs,
Qcow2Cache *c,
ret = bdrv_pread(bs->file, offset,
qcow2_cache_get_table_addr(bs, c, i),
- s->cluster_size);
+ c->table_size);
if (ret < 0) {
return ret;
}
--
2.11.0
- [Qemu-block] [PATCH v4 15/39] qcow2: Update l2_load() to support L2 slices, (continued)
- [Qemu-block] [PATCH v4 15/39] qcow2: Update l2_load() to support L2 slices, Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 20/39] qcow2: Update qcow2_get_cluster_offset() to support L2 slices, Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 13/39] qcow2: Add l2_slice_size field to BDRVQcow2State, Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 11/39] qcow2: Remove BDS parameter from qcow2_cache_is_table_offset(), Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 12/39] qcow2: Add offset_to_l1_index(), Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 03/39] qcow2: Remove BDS parameter from qcow2_cache_get_table_addr(), Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 01/39] qcow2: Fix documentation of get_cluster_table(), Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 09/39] qcow2: Remove BDS parameter from qcow2_cache_clean_unused(), Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 34/39] qcow2: Rename l2_table in count_contiguous_clusters_unallocated(), Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 02/39] qcow2: Add table size field to Qcow2Cache,
Alberto Garcia <=
- [Qemu-block] [PATCH v4 37/39] iotests: Test valid values of l2-cache-entry-size, Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 33/39] qcow2: Rename l2_table in count_contiguous_clusters(), Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 38/39] iotests: Test downgrading an image using a small L2 slice size, Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 36/39] qcow2: Allow configuring the L2 slice size, Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 32/39] qcow2: Rename l2_table in qcow2_alloc_compressed_cluster_offset(), Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 30/39] qcow2: Update expand_zero_clusters_in_l1() to support L2 slices, Alberto Garcia, 2018/02/05
- [Qemu-block] [PATCH v4 31/39] qcow2: Update qcow2_truncate() to support L2 slices, Alberto Garcia, 2018/02/05