@@ -15,7 +15,7 @@ use syscall::error::{
15
15
use crate :: {
16
16
AllocEntry , AllocList , Allocator , BlockAddr , BlockData , BlockLevel , BlockPtr , BlockTrait ,
17
17
DirEntry , DirList , Disk , FileSystem , Header , Node , NodeLevel , RecordRaw , TreeData , TreePtr ,
18
- ALLOC_LIST_ENTRIES , DIR_ENTRY_MAX_LENGTH , HEADER_RING ,
18
+ ALLOC_GC_THRESHOLD , ALLOC_LIST_ENTRIES , DIR_ENTRY_MAX_LENGTH , HEADER_RING ,
19
19
} ;
20
20
21
21
pub struct Transaction < ' a , D : Disk > {
@@ -113,11 +113,14 @@ impl<'a, D: Disk> Transaction<'a, D> {
113
113
/// This method does not write anything to disk,
114
114
/// all writes are cached.
115
115
///
116
- /// If `squash` is true, fully rebuild the allocator log
117
- /// using the state of `self.allocator`.
118
- fn sync_allocator ( & mut self , squash : bool ) -> Result < bool > {
116
+ /// To keep the allocator log from growing excessively, it will
117
+ /// periodically be fully rebuilt using the state of `self.allocator`.
118
+ /// This rebuild can be forced by setting `force_squash` to `true`.
119
+ fn sync_allocator ( & mut self , force_squash : bool ) -> Result < bool > {
119
120
let mut prev_ptr = BlockPtr :: default ( ) ;
120
- if squash {
121
+ let should_gc = self . header . generation ( ) % ALLOC_GC_THRESHOLD == 0
122
+ && self . header . generation ( ) >= ALLOC_GC_THRESHOLD ;
123
+ if force_squash || should_gc {
121
124
// Clear and rebuild alloc log
122
125
self . allocator_log . clear ( ) ;
123
126
let levels = self . allocator . levels ( ) ;
@@ -204,11 +207,10 @@ impl<'a, D: Disk> Transaction<'a, D> {
204
207
Ok ( true )
205
208
}
206
209
207
- // TODO: change this function, provide another way to squash, only write header in commit
208
210
/// Write all changes cached in this [`Transaction`] to disk.
209
- pub fn sync ( & mut self , squash : bool ) -> Result < bool > {
211
+ pub fn sync ( & mut self , force_squash : bool ) -> Result < bool > {
210
212
// Make sure alloc is synced
211
- self . sync_allocator ( squash ) ?;
213
+ self . sync_allocator ( force_squash ) ?;
212
214
213
215
// Write all items in write cache
214
216
for ( addr, raw) in self . write_cache . iter_mut ( ) {
0 commit comments