diff --git a/blockstore.cpp b/blockstore.cpp index ea06d9d8..c0f176d3 100644 --- a/blockstore.cpp +++ b/blockstore.cpp @@ -71,7 +71,6 @@ void blockstore::loop() { metadata_init_reader = new blockstore_init_meta(this); initialized = 1; - printf("reading blockstore metadata\n"); } if (initialized == 1) { @@ -82,7 +81,6 @@ void blockstore::loop() metadata_init_reader = NULL; journal_init_reader = new blockstore_init_journal(this); initialized = 2; - printf("reading blockstore journal\n"); } } if (initialized == 2) diff --git a/blockstore_flush.cpp b/blockstore_flush.cpp index 976a02a7..f0377edd 100644 --- a/blockstore_flush.cpp +++ b/blockstore_flush.cpp @@ -475,43 +475,10 @@ resume_0: if (!((++flusher->journal_trim_counter) % flusher->journal_trim_interval)) { flusher->journal_trim_counter = 0; - journal_used_it = bs->journal.used_sectors.lower_bound(bs->journal.used_start); -#ifdef BLOCKSTORE_DEBUG - printf( - "Trimming journal (used_start=%lu, next_free=%lu, first_used=%lu, usage_count=%lu)\n", - bs->journal.used_start, bs->journal.next_free, - journal_used_it == bs->journal.used_sectors.end() ? 0 : journal_used_it->first, - journal_used_it == bs->journal.used_sectors.end() ? 0 : journal_used_it->second - ); -#endif - if (journal_used_it == bs->journal.used_sectors.end()) + if (!bs->journal.trim()) { - // Journal is cleared to its end, restart from the beginning - journal_used_it = bs->journal.used_sectors.begin(); - if (journal_used_it == bs->journal.used_sectors.end()) - { - // Journal is empty - bs->journal.used_start = bs->journal.next_free; - } - else - { - bs->journal.used_start = journal_used_it->first; - // next_free does not need updating here - } - } - else if (journal_used_it->first > bs->journal.used_start) - { - // Journal is cleared up to - bs->journal.used_start = journal_used_it->first; - } - else - { - // Can't trim journal goto do_not_trim; } -#ifdef BLOCKSTORE_DEBUG - printf("Journal trimmed to %lu (next_free=%lu)\n", bs->journal.used_start, bs->journal.next_free); -#endif // Update journal "superblock" await_sqe(12); data->callback = simple_callback_w; diff --git a/blockstore_flush.h b/blockstore_flush.h index a8fca3dd..84fec326 100644 --- a/blockstore_flush.h +++ b/blockstore_flush.h @@ -37,7 +37,6 @@ class journal_flusher_co uint64_t offset, len, submit_offset, submit_len, clean_loc, old_clean_loc, meta_sector, meta_pos; std::map::iterator meta_it; std::map::iterator repeat_it; - std::map::iterator journal_used_it; std::function simple_callback_r, simple_callback_w; std::list::iterator cur_sync; friend class journal_flusher_t; diff --git a/blockstore_init.cpp b/blockstore_init.cpp index a4318019..1985d7bb 100644 --- a/blockstore_init.cpp +++ b/blockstore_init.cpp @@ -24,6 +24,7 @@ int blockstore_init_meta::loop() { if (wait_state == 1) goto resume_1; + printf("Reading blockstore metadata\n"); metadata_buffer = (uint8_t*)memalign(512, 2*bs->metadata_buf_size); if (!metadata_buffer) throw std::bad_alloc(); @@ -173,6 +174,7 @@ int blockstore_init_journal::loop() goto resume_3; else if (wait_state == 4) goto resume_4; + printf("Reading blockstore journal\n"); if (!bs->journal.inmemory) { journal_buffer = (uint8_t*)memalign(DISK_ALIGNMENT, 2*JOURNAL_BUFFER_SIZE); @@ -293,7 +295,8 @@ resume_1: } } } - // FIXME Trim journal on start so we don't stall when all entries are older + // Trim journal on start so we don't stall when all entries are older + bs->journal.trim(); printf("Journal entries loaded: %lu, free blocks: %lu / %lu\n", entries_loaded, bs->data_alloc->get_free_count(), bs->block_count); if (!bs->journal.inmemory) { diff --git a/blockstore_journal.cpp b/blockstore_journal.cpp index b2c3bb19..154fd93d 100644 --- a/blockstore_journal.cpp +++ b/blockstore_journal.cpp @@ -121,3 +121,45 @@ journal_t::~journal_t() sector_info = NULL; buffer = NULL; } + +bool journal_t::trim() +{ + auto journal_used_it = used_sectors.lower_bound(used_start); +#ifdef BLOCKSTORE_DEBUG + printf( + "Trimming journal (used_start=%lu, next_free=%lu, first_used=%lu, usage_count=%lu)\n", + used_start, next_free, + journal_used_it == used_sectors.end() ? 0 : journal_used_it->first, + journal_used_it == used_sectors.end() ? 0 : journal_used_it->second + ); +#endif + if (journal_used_it == used_sectors.end()) + { + // Journal is cleared to its end, restart from the beginning + journal_used_it = used_sectors.begin(); + if (journal_used_it == used_sectors.end()) + { + // Journal is empty + used_start = next_free; + } + else + { + used_start = journal_used_it->first; + // next_free does not need updating here + } + } + else if (journal_used_it->first > used_start) + { + // Journal is cleared up to + used_start = journal_used_it->first; + } + else + { + // Can't trim journal + return false; + } +#ifdef BLOCKSTORE_DEBUG + printf("Journal trimmed to %lu (next_free=%lu)\n", used_start, next_free); +#endif + return true; +} diff --git a/blockstore_journal.h b/blockstore_journal.h index ac2a3661..cd8c60c4 100644 --- a/blockstore_journal.h +++ b/blockstore_journal.h @@ -134,6 +134,7 @@ struct journal_t std::map used_sectors; ~journal_t(); + bool trim(); }; struct blockstore_journal_check_t