Lines Matching full:db
4 * Based on Fossilize DB:
94 mesa_db_lock(struct mesa_cache_db *db) in mesa_db_lock() argument
96 simple_mtx_lock(&db->flock_mtx); in mesa_db_lock()
98 if (flock(fileno(db->cache.file), LOCK_EX) == -1) in mesa_db_lock()
101 if (flock(fileno(db->index.file), LOCK_EX) == -1) in mesa_db_lock()
107 flock(fileno(db->cache.file), LOCK_UN); in mesa_db_lock()
109 simple_mtx_unlock(&db->flock_mtx); in mesa_db_lock()
115 mesa_db_unlock(struct mesa_cache_db *db) in mesa_db_unlock() argument
117 flock(fileno(db->index.file), LOCK_UN); in mesa_db_unlock()
118 flock(fileno(db->cache.file), LOCK_UN); in mesa_db_unlock()
119 simple_mtx_unlock(&db->flock_mtx); in mesa_db_unlock()
171 static bool mesa_db_uuid_changed(struct mesa_cache_db *db) in mesa_db_uuid_changed() argument
176 if (!mesa_db_read_header(db->cache.file, &cache_header) || in mesa_db_uuid_changed()
177 !mesa_db_read_header(db->index.file, &index_header) || in mesa_db_uuid_changed()
179 cache_header.uuid != db->uuid) in mesa_db_uuid_changed()
216 * entries is acceptable, hence it's more practical to repair DB using
220 mesa_db_zap(struct mesa_cache_db *db) in mesa_db_zap() argument
223 db->alive = false; in mesa_db_zap()
226 if (!mesa_db_truncate(db->cache.file, 0) || in mesa_db_zap()
227 !mesa_db_truncate(db->index.file, 0)) in mesa_db_zap()
230 fflush(db->cache.file); in mesa_db_zap()
231 fflush(db->index.file); in mesa_db_zap()
250 mesa_db_update_index(struct mesa_cache_db *db) in mesa_db_update_index() argument
256 if (!mesa_db_seek_end(db->index.file)) in mesa_db_update_index()
259 file_length = ftell(db->index.file); in mesa_db_update_index()
261 if (!mesa_db_seek(db->index.file, db->index.offset)) in mesa_db_update_index()
264 while (db->index.offset < file_length) { in mesa_db_update_index()
265 if (!mesa_db_read(db->index.file, &index_entry)) in mesa_db_update_index()
268 /* Check whether the index entry looks valid or we have a corrupted DB */ in mesa_db_update_index()
272 hash_entry = ralloc(db->mem_ctx, struct mesa_index_db_hash_entry); in mesa_db_update_index()
277 hash_entry->index_db_file_offset = db->index.offset; in mesa_db_update_index()
281 _mesa_hash_table_u64_insert(db->index_db, index_entry.hash, hash_entry); in mesa_db_update_index()
283 db->index.offset += sizeof(index_entry); in mesa_db_update_index()
286 if (!mesa_db_seek(db->index.file, db->index.offset)) in mesa_db_update_index()
289 return db->index.offset == file_length; in mesa_db_update_index()
293 mesa_db_hash_table_reset(struct mesa_cache_db *db) in mesa_db_hash_table_reset() argument
295 _mesa_hash_table_u64_clear(db->index_db); in mesa_db_hash_table_reset()
296 ralloc_free(db->mem_ctx); in mesa_db_hash_table_reset()
297 db->mem_ctx = ralloc_context(NULL); in mesa_db_hash_table_reset()
301 mesa_db_recreate_files(struct mesa_cache_db *db) in mesa_db_recreate_files() argument
303 db->uuid = mesa_db_generate_uuid(); in mesa_db_recreate_files()
305 if (!mesa_db_write_header(&db->cache, db->uuid, true) || in mesa_db_recreate_files()
306 !mesa_db_write_header(&db->index, db->uuid, true)) in mesa_db_recreate_files()
313 mesa_db_load(struct mesa_cache_db *db, bool reload) in mesa_db_load() argument
317 if (!mesa_db_lock(db)) in mesa_db_load()
322 if (!mesa_db_load_header(&db->cache) || in mesa_db_load()
323 !mesa_db_load_header(&db->index) || in mesa_db_load()
324 db->cache.uuid != db->index.uuid) { in mesa_db_load()
330 if (!mesa_db_recreate_files(db)) in mesa_db_load()
333 db->uuid = db->cache.uuid; in mesa_db_load()
336 db->index.offset = ftell(db->index.file); in mesa_db_load()
339 mesa_db_hash_table_reset(db); in mesa_db_load()
341 if (!mesa_db_update_index(db)) in mesa_db_load()
345 mesa_db_unlock(db); in mesa_db_load()
347 db->alive = true; in mesa_db_load()
353 mesa_db_unlock(db); in mesa_db_load()
359 mesa_db_reload(struct mesa_cache_db *db) in mesa_db_reload() argument
361 fflush(db->cache.file); in mesa_db_reload()
362 fflush(db->index.file); in mesa_db_reload()
364 return mesa_db_load(db, true); in mesa_db_reload()
435 struct mesa_cache_db *db = arg; in entry_sort_offset() local
437 /* Two entries will never have the identical offset, otherwise DB is in entry_sort_offset()
440 mesa_db_zap(db); in entry_sort_offset()
451 mesa_db_compact(struct mesa_cache_db *db, int64_t blob_size, in mesa_db_compact() argument
464 if (!remove_entry && !mesa_db_reload(db)) in mesa_db_compact()
467 num_entries = _mesa_hash_table_num_entries(db->index_db->table); in mesa_db_compact()
472 compacted_cache = fopen(db->cache.path, "r+b"); in mesa_db_compact()
473 compacted_index = fopen(db->index.path, "r+b"); in mesa_db_compact()
481 cache_header.uuid != db->uuid || in mesa_db_compact()
482 index_header.uuid != db->uuid) in mesa_db_compact()
485 hash_table_foreach(db->index_db->table, entry) { in mesa_db_compact()
493 entry_sort_lru, db); in mesa_db_compact()
501 entry_sort_offset, db); in mesa_db_compact()
504 if (!db->alive) in mesa_db_compact()
513 if (!mesa_db_write_header(&db->cache, 0, false) || in mesa_db_compact()
514 !mesa_db_write_header(&db->index, 0, false)) in mesa_db_compact()
518 if (!mesa_db_seek(compacted_cache, ftell(db->cache.file)) || in mesa_db_compact()
519 !mesa_db_seek(compacted_index, ftell(db->index.file))) in mesa_db_compact()
527 if (ftell(db->cache.file) != entries[i]->cache_db_file_offset) in mesa_db_compact()
532 if (!mesa_db_seek_cur(db->cache.file, blob_size) || in mesa_db_compact()
533 !mesa_db_seek_cur(db->index.file, sizeof(index_entry))) in mesa_db_compact()
542 if (!mesa_db_read_data(db->cache.file, buffer, blob_size) || in mesa_db_compact()
548 if (!mesa_db_read(db->index.file, &index_entry) || in mesa_db_compact()
564 if (!mesa_db_seek_cur(db->index.file, sizeof(index_entry)) || in mesa_db_compact()
566 !mesa_db_seek_cur(db->cache.file, blob_size) || in mesa_db_compact()
576 if (!mesa_db_truncate(db->cache.file, ftell(compacted_cache)) || in mesa_db_compact()
577 !mesa_db_truncate(db->index.file, ftell(compacted_index))) in mesa_db_compact()
581 db->uuid = mesa_db_generate_uuid(); in mesa_db_compact()
583 if (!mesa_db_write_header(&db->cache, db->uuid, false) || in mesa_db_compact()
584 !mesa_db_write_header(&db->index, db->uuid, false)) in mesa_db_compact()
598 if (success && !mesa_db_reload(db)) in mesa_db_compact()
605 mesa_cache_db_open(struct mesa_cache_db *db, const char *cache_path) in mesa_cache_db_open() argument
607 if (!mesa_db_open_file(&db->cache, cache_path, "mesa_cache.db")) in mesa_cache_db_open()
610 if (!mesa_db_open_file(&db->index, cache_path, "mesa_cache.idx")) in mesa_cache_db_open()
613 db->mem_ctx = ralloc_context(NULL); in mesa_cache_db_open()
614 if (!db->mem_ctx) in mesa_cache_db_open()
617 simple_mtx_init(&db->flock_mtx, mtx_plain); in mesa_cache_db_open()
619 db->index_db = _mesa_hash_table_u64_create(NULL); in mesa_cache_db_open()
620 if (!db->index_db) in mesa_cache_db_open()
623 if (!mesa_db_load(db, false)) in mesa_cache_db_open()
629 _mesa_hash_table_u64_destroy(db->index_db); in mesa_cache_db_open()
631 simple_mtx_destroy(&db->flock_mtx); in mesa_cache_db_open()
633 ralloc_free(db->mem_ctx); in mesa_cache_db_open()
635 mesa_db_close_file(&db->index); in mesa_cache_db_open()
637 mesa_db_close_file(&db->cache); in mesa_cache_db_open()
645 struct mesa_cache_db db = {0}; in mesa_db_wipe_path() local
648 if (!mesa_db_remove_file(&db.cache, cache_path, "mesa_cache.db") || in mesa_db_wipe_path()
649 !mesa_db_remove_file(&db.index, cache_path, "mesa_cache.idx")) in mesa_db_wipe_path()
652 free(db.cache.path); in mesa_db_wipe_path()
653 free(db.index.path); in mesa_db_wipe_path()
659 mesa_cache_db_close(struct mesa_cache_db *db) in mesa_cache_db_close() argument
661 _mesa_hash_table_u64_destroy(db->index_db); in mesa_cache_db_close()
662 simple_mtx_destroy(&db->flock_mtx); in mesa_cache_db_close()
663 ralloc_free(db->mem_ctx); in mesa_cache_db_close()
665 mesa_db_close_file(&db->index); in mesa_cache_db_close()
666 mesa_db_close_file(&db->cache); in mesa_cache_db_close()
670 mesa_cache_db_set_size_limit(struct mesa_cache_db *db, in mesa_cache_db_set_size_limit() argument
673 db->max_cache_size = max_cache_size; in mesa_cache_db_set_size_limit()
683 mesa_cache_db_read_entry(struct mesa_cache_db *db, in mesa_cache_db_read_entry() argument
693 if (!mesa_db_lock(db)) in mesa_cache_db_read_entry()
696 if (!db->alive) in mesa_cache_db_read_entry()
699 if (mesa_db_uuid_changed(db) && !mesa_db_reload(db)) in mesa_cache_db_read_entry()
702 if (!mesa_db_update_index(db)) in mesa_cache_db_read_entry()
705 hash_entry = _mesa_hash_table_u64_search(db->index_db, hash); in mesa_cache_db_read_entry()
709 if (!mesa_db_seek(db->cache.file, hash_entry->cache_db_file_offset) || in mesa_cache_db_read_entry()
710 !mesa_db_read(db->cache.file, &cache_entry) || in mesa_cache_db_read_entry()
721 if (!mesa_db_read_data(db->cache.file, data, cache_entry.size) || in mesa_cache_db_read_entry()
725 if (!mesa_db_seek(db->index.file, hash_entry->index_db_file_offset) || in mesa_cache_db_read_entry()
726 !mesa_db_read(db->index.file, &index_entry) || in mesa_cache_db_read_entry()
735 if (!mesa_db_seek(db->index.file, hash_entry->index_db_file_offset) || in mesa_cache_db_read_entry()
736 !mesa_db_write(db->index.file, &index_entry)) in mesa_cache_db_read_entry()
739 fflush(db->index.file); in mesa_cache_db_read_entry()
741 mesa_db_unlock(db); in mesa_cache_db_read_entry()
748 mesa_db_zap(db); in mesa_cache_db_read_entry()
752 mesa_db_unlock(db); in mesa_cache_db_read_entry()
758 mesa_cache_db_has_space_locked(struct mesa_cache_db *db, size_t blob_size) in mesa_cache_db_has_space_locked() argument
760 return ftell(db->cache.file) + blob_file_size(blob_size) - in mesa_cache_db_has_space_locked()
761 sizeof(struct mesa_db_file_header) <= db->max_cache_size; in mesa_cache_db_has_space_locked()
765 mesa_cache_db_eviction_size(struct mesa_cache_db *db) in mesa_cache_db_eviction_size() argument
767 return db->max_cache_size / 2 - sizeof(struct mesa_db_file_header); in mesa_cache_db_eviction_size()
771 mesa_cache_db_entry_write(struct mesa_cache_db *db, in mesa_cache_db_entry_write() argument
780 if (!mesa_db_lock(db)) in mesa_cache_db_entry_write()
783 if (!db->alive) in mesa_cache_db_entry_write()
786 if (mesa_db_uuid_changed(db) && !mesa_db_reload(db)) in mesa_cache_db_entry_write()
789 if (!mesa_db_seek_end(db->cache.file)) in mesa_cache_db_entry_write()
792 if (!mesa_cache_db_has_space_locked(db, blob_size)) { in mesa_cache_db_entry_write()
793 if (!mesa_db_compact(db, MAX2(blob_size, mesa_cache_db_eviction_size(db)), in mesa_cache_db_entry_write()
797 if (!mesa_db_update_index(db)) in mesa_cache_db_entry_write()
801 hash_entry = _mesa_hash_table_u64_search(db->index_db, hash); in mesa_cache_db_entry_write()
807 if (!mesa_db_seek_end(db->cache.file) || in mesa_cache_db_entry_write()
808 !mesa_db_seek_end(db->index.file)) in mesa_cache_db_entry_write()
818 index_entry.cache_db_file_offset = ftell(db->cache.file); in mesa_cache_db_entry_write()
820 hash_entry = ralloc(db->mem_ctx, struct mesa_index_db_hash_entry); in mesa_cache_db_entry_write()
825 hash_entry->index_db_file_offset = ftell(db->index.file); in mesa_cache_db_entry_write()
829 if (!mesa_db_write(db->cache.file, &cache_entry) || in mesa_cache_db_entry_write()
830 !mesa_db_write_data(db->cache.file, blob, blob_size) || in mesa_cache_db_entry_write()
831 !mesa_db_write(db->index.file, &index_entry)) in mesa_cache_db_entry_write()
834 fflush(db->cache.file); in mesa_cache_db_entry_write()
835 fflush(db->index.file); in mesa_cache_db_entry_write()
837 db->index.offset = ftell(db->index.file); in mesa_cache_db_entry_write()
839 _mesa_hash_table_u64_insert(db->index_db, hash, hash_entry); in mesa_cache_db_entry_write()
841 mesa_db_unlock(db); in mesa_cache_db_entry_write()
846 mesa_db_zap(db); in mesa_cache_db_entry_write()
848 mesa_db_unlock(db); in mesa_cache_db_entry_write()
857 mesa_cache_db_entry_remove(struct mesa_cache_db *db, in mesa_cache_db_entry_remove() argument
864 if (!mesa_db_lock(db)) in mesa_cache_db_entry_remove()
867 if (!db->alive) in mesa_cache_db_entry_remove()
870 if (mesa_db_uuid_changed(db) && !mesa_db_reload(db)) in mesa_cache_db_entry_remove()
873 if (!mesa_db_update_index(db)) in mesa_cache_db_entry_remove()
876 hash_entry = _mesa_hash_table_u64_search(db->index_db, hash); in mesa_cache_db_entry_remove()
880 if (!mesa_db_seek(db->cache.file, hash_entry->cache_db_file_offset) || in mesa_cache_db_entry_remove()
881 !mesa_db_read(db->cache.file, &cache_entry) || in mesa_cache_db_entry_remove()
888 if (!mesa_db_compact(db, 0, hash_entry)) in mesa_cache_db_entry_remove()
891 mesa_db_unlock(db); in mesa_cache_db_entry_remove()
896 mesa_db_zap(db); in mesa_cache_db_entry_remove()
898 mesa_db_unlock(db); in mesa_cache_db_entry_remove()
904 mesa_cache_db_has_space(struct mesa_cache_db *db, size_t blob_size) in mesa_cache_db_has_space() argument
908 if (!mesa_db_lock(db)) in mesa_cache_db_has_space()
911 if (!mesa_db_seek_end(db->cache.file)) in mesa_cache_db_has_space()
914 has_space = mesa_cache_db_has_space_locked(db, blob_size); in mesa_cache_db_has_space()
916 mesa_db_unlock(db); in mesa_cache_db_has_space()
921 mesa_db_zap(db); in mesa_cache_db_has_space()
922 mesa_db_unlock(db); in mesa_cache_db_has_space()
943 mesa_cache_db_eviction_score(struct mesa_cache_db *db) in mesa_cache_db_eviction_score() argument
945 int64_t eviction_size = mesa_cache_db_eviction_size(db); in mesa_cache_db_eviction_score()
950 if (!mesa_db_lock(db)) in mesa_cache_db_eviction_score()
953 if (!db->alive) in mesa_cache_db_eviction_score()
956 if (!mesa_db_reload(db)) in mesa_cache_db_eviction_score()
959 num_entries = _mesa_hash_table_num_entries(db->index_db->table); in mesa_cache_db_eviction_score()
964 hash_table_foreach(db->index_db->table, entry) in mesa_cache_db_eviction_score()
968 entry_sort_lru, db); in mesa_cache_db_eviction_score()
987 mesa_db_unlock(db); in mesa_cache_db_eviction_score()
992 mesa_db_zap(db); in mesa_cache_db_eviction_score()
994 mesa_db_unlock(db); in mesa_cache_db_eviction_score()