From 5ee1e0d7baef84bbd2e3f211df312991c1011d73 Mon Sep 17 00:00:00 2001 From: Roman Simakov Date: Fri, 20 Nov 2026 21:25:42 +0300 Subject: [PATCH 1/2] Implement new optimization for merge pages from delta --- src/jrd/cch.cpp | 36 ++++++++++++++++++++++++++++++++++-- src/jrd/cch.h | 2 +- src/jrd/cch_proto.h | 6 ++++++ src/jrd/nbak.cpp | 2 +- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/jrd/cch.cpp b/src/jrd/cch.cpp index d5e761e4f90..f137fc9e9a8 100644 --- a/src/jrd/cch.cpp +++ b/src/jrd/cch.cpp @@ -1725,6 +1725,11 @@ void CCH_mark(thread_db* tdbb, WIN* window, bool mark_system, bool must_write) fb_assert(bdb->ourIOLock()); + // A page changed by a regular (non-merge) operation must be written into the + // difference file again, even if the merge process has previously read it from + // there and marked it with BDB_merge. + bdb->bdb_flags &= ~BDB_merge; + // Allocate difference page (if in stalled mode) before mark page as dirty. // It guarantees that disk space is allocated and page could be written later. @@ -1778,6 +1783,32 @@ void CCH_mark(thread_db* tdbb, WIN* window, bool mark_system, bool must_write) } +void CCH_mark_merge(thread_db* tdbb, WIN* window) +{ +/************************************** + * + * C C H _ m a r k _ m e r g e + * + ************************************** + * + * Functional description + * Mark a window as dirty by the difference file merge process. + * The page content is taken from the difference file, so it must + * not be written back into the difference file while merging. + * + **************************************/ + BufferDesc* bdb = window->win_bdb; + BLKCHK(bdb, type_bdb); + + SET_TDBB(tdbb); + + // CCH_mark() clears BDB_merge, so the flag must be set afterwards + CCH_mark(tdbb, window, true, false); + + bdb->bdb_flags |= BDB_merge; +} + + void CCH_must_write(thread_db* tdbb, WIN* window) { /************************************** @@ -5008,7 +5039,8 @@ static bool write_page(thread_db* tdbb, BufferDesc* bdb, FbStatusVector* const s if (!isTempPage && (backupState == Ods::hdr_nbak_stalled || - (backupState == Ods::hdr_nbak_merge && bdb->bdb_difference_page))) + (backupState == Ods::hdr_nbak_merge && bdb->bdb_difference_page && + !(bdb->bdb_flags & BDB_merge)))) { if (!dbb->dbb_backup_manager->writeDifference(tdbb, status, bdb->bdb_difference_page, bdb->bdb_buffer)) @@ -5113,7 +5145,7 @@ static bool write_page(thread_db* tdbb, BufferDesc* bdb, FbStatusVector* const s if (!(bdb->bdb_bcb->bcb_flags & BCB_keep_pages)) removeDirty(bdb->bdb_bcb, bdb); - bdb->bdb_flags &= ~(BDB_must_write | BDB_system_dirty); + bdb->bdb_flags &= ~(BDB_must_write | BDB_system_dirty | BDB_merge); clear_dirty_flag_and_nbak_state(tdbb, bdb); if (bdb->bdb_flags & BDB_io_error) diff --git a/src/jrd/cch.h b/src/jrd/cch.h index 98671de3024..32c5a0c1c99 100644 --- a/src/jrd/cch.h +++ b/src/jrd/cch.h @@ -279,7 +279,7 @@ inline constexpr int BDB_writer = 0x0004; // someone is updating the page inline constexpr int BDB_marked = 0x0008; // page has been updated inline constexpr int BDB_must_write = 0x0010; // forces a write as soon as the page is released inline constexpr int BDB_faked = 0x0020; // page was just allocated -//inline constexpr int BDB_merge = 0x0040; +inline constexpr int BDB_merge = 0x0040; // read from delta by merge and must not go back into it inline constexpr int BDB_system_dirty = 0x0080; // system transaction has marked dirty inline constexpr int BDB_io_error = 0x0100; // page i/o error inline constexpr int BDB_read_pending = 0x0200; // read is pending diff --git a/src/jrd/cch_proto.h b/src/jrd/cch_proto.h index 3834ea61909..26a08da184f 100644 --- a/src/jrd/cch_proto.h +++ b/src/jrd/cch_proto.h @@ -59,6 +59,7 @@ Ods::pag* CCH_handoff(Jrd::thread_db*, Jrd::win*, Jrd::PageNumber, int, SCHAR, i void CCH_init(Jrd::thread_db*, ULONG); void CCH_init2(Jrd::thread_db*); void CCH_mark(Jrd::thread_db*, Jrd::win*, bool, bool); +void CCH_mark_merge(Jrd::thread_db*, Jrd::win*); void CCH_must_write(Jrd::thread_db*, Jrd::win*); void CCH_precedence(Jrd::thread_db*, Jrd::win*, ULONG); void CCH_precedence(Jrd::thread_db*, Jrd::win*, Jrd::PageNumber); @@ -115,6 +116,11 @@ inline void CCH_MARK_SYSTEM(Jrd::thread_db* tdbb, Jrd::win* window) CCH_mark (tdbb, window, 1, 0); } +inline void CCH_MARK_MERGE(Jrd::thread_db* tdbb, Jrd::win* window) +{ + CCH_mark_merge (tdbb, window); +} + inline Ods::pag* CCH_HANDOFF(Jrd::thread_db* tdbb, Jrd::win* window, ULONG page, SSHORT lock, SCHAR page_type) { return CCH_handoff (tdbb, window, Jrd::PageNumber(window->win_page.getPageSpaceID(), page), lock, page_type, 1, false); diff --git a/src/jrd/nbak.cpp b/src/jrd/nbak.cpp index 03e20e228d0..29c2c29d621 100644 --- a/src/jrd/nbak.cpp +++ b/src/jrd/nbak.cpp @@ -540,7 +540,7 @@ void BackupManager::endBackup(thread_db* tdbb, bool recover) NBAK_TRACE(("Merge: page %d is fetched", all.current().db_page)); if (page->pag_scn != current_scn) { - CCH_MARK_SYSTEM(tdbb, &window2); + CCH_MARK_MERGE(tdbb, &window2); NBAK_TRACE(("Merge: page %d is marked", all.current().db_page)); } CCH_RELEASE(tdbb, &window2); From 5ddb155c331db5d8af6b55ed19e0a51dcfada038 Mon Sep 17 00:00:00 2001 From: Roman Simakov Date: Fri, 20 Nov 2026 23:30:53 +0300 Subject: [PATCH 2/2] Small cleanup --- src/jrd/cch_proto.h | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/src/jrd/cch_proto.h b/src/jrd/cch_proto.h index 26a08da184f..23c0fb9fb1d 100644 --- a/src/jrd/cch_proto.h +++ b/src/jrd/cch_proto.h @@ -108,12 +108,12 @@ inline void CCH_RELEASE_TAIL(Jrd::thread_db* tdbb, Jrd::win* window) inline void CCH_MARK(Jrd::thread_db* tdbb, Jrd::win* window) { - CCH_mark (tdbb, window, 0, 0); + CCH_mark (tdbb, window, false, false); } inline void CCH_MARK_SYSTEM(Jrd::thread_db* tdbb, Jrd::win* window) { - CCH_mark (tdbb, window, 1, 0); + CCH_mark (tdbb, window, true, false); } inline void CCH_MARK_MERGE(Jrd::thread_db* tdbb, Jrd::win* window) @@ -143,7 +143,7 @@ inline Ods::pag* CCH_HANDOFF(Jrd::thread_db* tdbb, Jrd::win* window, Jrd::PageNu inline void CCH_MARK_MUST_WRITE(Jrd::thread_db* tdbb, Jrd::win* window) { - CCH_mark(tdbb, window, 0, 1); + CCH_mark(tdbb, window, false, true); } #ifdef SUPERSERVER_V2 @@ -153,21 +153,6 @@ inline void CCH_PREFETCH(Jrd::thread_db* tdbb, SLONG* pages, SSHORT count) } #endif -//#define CCH_FETCH(tdbb, window, lock, type) CCH_fetch (tdbb, window, lock, type, 1, true) -//#define CCH_FETCH_NO_SHADOW(tdbb, window, lock, type) CCH_fetch (tdbb, window, lock, type, 1, false) -//#define CCH_FETCH_TIMEOUT(tdbb, window, lock, type, latch_wait) CCH_fetch (tdbb, window, lock, type, latch_wait, true) -//#define CCH_FETCH_LOCK(tdbb, window, lock, wait, type) CCH_fetch_lock (tdbb, window, lock, wait, type) -//#define CCH_FETCH_PAGE(tdbb, window, read_shadow) CCH_fetch_page (tdbb, window, read_shadow) -//#define CCH_RELEASE(tdbb, window) CCH_release (tdbb, window, false) -//#define CCH_RELEASE_TAIL(tdbb, window) CCH_release (tdbb, window, true) -//#define CCH_MARK(tdbb, window) CCH_mark (tdbb, window, 0) -//#define CCH_MARK_SYSTEM(tdbb, window) CCH_mark (tdbb, window, 1) -//#define CCH_HANDOFF(tdbb, window, page, lock, type) CCH_handoff (tdbb, window, page, lock, type, 1, false) -//#define CCH_HANDOFF_TIMEOUT(tdbb, window, page, lock, type, latch_wait) CCH_handoff (tdbb, window, page, lock, type, latch_wait, false) -//#define CCH_HANDOFF_TAIL(tdbb, window, page, lock, type) CCH_handoff (tdbb, window, page, lock, type, 1, true) -//#define CCH_MARK_MUST_WRITE(tdbb, window) CCH_mark_must_write (tdbb, window) -//#define CCH_PREFETCH(tdbb, pages, count) CCH_prefetch (tdbb, pages, count) - // Flush flags inline constexpr USHORT FLUSH_ALL = 1; // flush all dirty buffers in cache