About Social Code
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichel Dänzer <mdaenzer@redhat.com>2024-11-11 11:49:00 +0100
committerMarge Bot <emma+marge@anholt.net>2024-11-14 14:20:18 +0000
commit66d68263f8fe2fff70bccdb91f700e58a9e8019c (patch)
tree0339e709e4b8a9d658b56a5faef0599e285c5baa
parent5cfd841ddaa2c5b7c38725595f1a3cd6c964ceab (diff)
Revert "util/mesa-db: Further simplify mesa_db_compact"
This reverts commit 92893309bcc0c1a9ab9eab844a896d99cbc4b4e2. Need to revert this as well for the next revert. Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32078>
-rw-r--r--src/util/mesa_cache_db.c58
1 files changed, 36 insertions, 22 deletions
diff --git a/src/util/mesa_cache_db.c b/src/util/mesa_cache_db.c
index 4372051c839..4ee926f94be 100644
--- a/src/util/mesa_cache_db.c
+++ b/src/util/mesa_cache_db.c
@@ -605,7 +605,7 @@ mesa_db_compact(struct mesa_cache_db *db, int64_t blob_size,
FILE *compacted_cache = NULL, *compacted_index = NULL;
struct mesa_index_db_file_entry *index_entry;
struct sort_entry *entries;
- bool success = false;
+ bool success = false, compact = false;
void *buffer = NULL;
unsigned int i = 0;
@@ -665,38 +665,52 @@ mesa_db_compact(struct mesa_cache_db *db, int64_t blob_size,
!mesa_db_write_header(&db->index, 0, false))
goto cleanup;
- /* Skip non-evicted entries at the start of the files */
- for (i = 0; i < num_entries; i++) {
- if (entries[i].evicted)
- break;
- }
-
/* Sync the file pointers */
- if (!mesa_db_seek(compacted_cache, entries[i].index_entry->cache_db_file_offset) ||
- !mesa_db_seek(compacted_index, ftell(db->index.file) +
- i * sizeof(struct mesa_index_db_file_entry)))
+ if (!mesa_db_seek(compacted_cache, ftell(db->cache.file)) ||
+ !mesa_db_seek(compacted_index, ftell(db->index.file)))
goto cleanup;
/* Do the compaction */
- for (; i < num_entries; i++) {
+ for (i = 0; i < num_entries; i++) {
struct mesa_index_db_file_entry *index_entry = entries[i].index_entry;
- if (entries[i].evicted)
- continue;
-
blob_size = blob_file_size(index_entry->size);
- /* Compact the cache file */
- if (!mesa_db_seek(db->cache.file, index_entry->cache_db_file_offset) ||
- !mesa_db_read_data(db->cache.file, buffer, blob_size) ||
- !mesa_db_cache_entry_valid(buffer) ||
- !mesa_db_write_data(compacted_cache, buffer, blob_size))
+ /* Sanity-check the cache-read offset */
+ if (ftell(db->cache.file) != index_entry->cache_db_file_offset)
goto cleanup;
- index_entry->cache_db_file_offset = ftell(compacted_cache) - blob_size;
+ if (entries[i].evicted) {
+ /* Jump over the evicted entry */
+ if (!mesa_db_seek_cur(db->cache.file, blob_size))
+ goto cleanup;
- if (!mesa_db_write(compacted_index, index_entry))
- goto cleanup;
+ compact = true;
+ continue;
+ }
+
+ if (compact) {
+ /* Compact the cache file */
+ if (!mesa_db_read_data(db->cache.file, buffer, blob_size) ||
+ !mesa_db_cache_entry_valid(buffer) ||
+ !mesa_db_write_data(compacted_cache, buffer, blob_size))
+ goto cleanup;
+
+ index_entry->cache_db_file_offset = ftell(compacted_cache) - blob_size;
+
+ if (!mesa_db_write(compacted_index, index_entry))
+ goto cleanup;
+ } else {
+ /* Sanity-check the cache-write offset */
+ if (ftell(compacted_cache) != index_entry->cache_db_file_offset)
+ goto cleanup;
+
+ /* Jump over the unchanged entry */
+ if (!mesa_db_seek_cur(compacted_index, sizeof(*index_entry)) ||
+ !mesa_db_seek_cur(db->cache.file, blob_size) ||
+ !mesa_db_seek_cur(compacted_cache, blob_size))
+ goto cleanup;
+ }
}
fflush(compacted_cache);