Skip to content

Commit 4c1167f

Browse files
jeffhostetlerdscho
authored andcommitted
fsmonitor--daemon: periodically truncate list of modified files
Teach fsmonitor--daemon to periodically truncate the list of modified files to save some memory. Clients will ask for the set of changes relative to a token that they found in the FSMN index extension in the index. (This token is like a point in time, but different). Clients will then update the index to contain the response token (so that subsequent commands will be relative to this new token). Therefore, the daemon can gradually truncate the in-memory list of changed paths as they become obsolete (older than the previous token). Since we may have multiple clients making concurrent requests with a skew of tokens and clients may be racing to the talk to the daemon, we lazily truncate the list. We introduce a 5 minute delay and truncate batches 5 minutes after they are considered obsolete. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent 065492d commit 4c1167f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

builtin/fsmonitor--daemon.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,75 @@ static void fsmonitor_batch__combine(struct fsmonitor_batch *batch_dest,
299299
batch_src->interned_paths[k];
300300
}
301301

302+
/*
303+
* To keep the batch list from growing unbounded in response to filesystem
304+
* activity, we try to truncate old batches from the end of the list as
305+
* they become irrelevant.
306+
*
307+
* We assume that the .git/index will be updated with the most recent token
308+
* any time the index is updated. And future commands will only ask for
309+
* recent changes *since* that new token. So as tokens advance into the
310+
* future, older batch items will never be requested/needed. So we can
311+
* truncate them without loss of functionality.
312+
*
313+
* However, multiple commands may be talking to the daemon concurrently
314+
* or perform a slow command, so a little "token skew" is possible.
315+
* Therefore, we want this to be a little bit lazy and have a generous
316+
* delay.
317+
*
318+
* The current reader thread walked backwards in time from `token->batch_head`
319+
* back to `batch_marker` somewhere in the middle of the batch list.
320+
*
321+
* Let's walk backwards in time from that marker an arbitrary delay
322+
* and truncate the list there. Note that these timestamps are completely
323+
* artificial (based on when we pinned the batch item) and not on any
324+
* filesystem activity.
325+
*
326+
* Return the obsolete portion of the list after we have removed it from
327+
* the official list so that the caller can free it after leaving the lock.
328+
*/
329+
#define MY_TIME_DELAY_SECONDS (5 * 60) /* seconds */
330+
331+
static struct fsmonitor_batch *with_lock__truncate_old_batches(
332+
struct fsmonitor_daemon_state *state,
333+
const struct fsmonitor_batch *batch_marker)
334+
{
335+
/* assert current thread holding state->main_lock */
336+
337+
const struct fsmonitor_batch *batch;
338+
struct fsmonitor_batch *remainder;
339+
340+
if (!batch_marker)
341+
return NULL;
342+
343+
trace_printf_key(&trace_fsmonitor, "Truncate: mark (%"PRIu64",%"PRIu64")",
344+
batch_marker->batch_seq_nr,
345+
(uint64_t)batch_marker->pinned_time);
346+
347+
for (batch = batch_marker; batch; batch = batch->next) {
348+
time_t t;
349+
350+
if (!batch->pinned_time) /* an overflow batch */
351+
continue;
352+
353+
t = batch->pinned_time + MY_TIME_DELAY_SECONDS;
354+
if (t > batch_marker->pinned_time) /* too close to marker */
355+
continue;
356+
357+
goto truncate_past_here;
358+
}
359+
360+
return NULL;
361+
362+
truncate_past_here:
363+
state->current_token_data->batch_tail = (struct fsmonitor_batch *)batch;
364+
365+
remainder = ((struct fsmonitor_batch *)batch)->next;
366+
((struct fsmonitor_batch *)batch)->next = NULL;
367+
368+
return remainder;
369+
}
370+
302371
static void fsmonitor_free_token_data(struct fsmonitor_token_data *token)
303372
{
304373
if (!token)
@@ -412,6 +481,7 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
412481
const char *p;
413482
const struct fsmonitor_batch *batch_head;
414483
const struct fsmonitor_batch *batch;
484+
struct fsmonitor_batch *remainder = NULL;
415485
intmax_t count = 0, duplicates = 0;
416486
kh_str_t *shown;
417487
int hash_ret;
@@ -641,11 +711,29 @@ static int do_handle_client(struct fsmonitor_daemon_state *state,
641711
* that work.
642712
*/
643713
fsmonitor_free_token_data(token_data);
714+
} else if (batch) {
715+
/*
716+
* We are holding the lock and are the only
717+
* reader of the ref-counted portion of the
718+
* list, so we get the honor of seeing if the
719+
* list can be truncated to save memory.
720+
*
721+
* The main loop did not walk to the end of the
722+
* list, so this batch is the first item in the
723+
* batch-list that is older than the requested
724+
* end-point sequence number. See if the tail
725+
* end of the list is obsolete.
726+
*/
727+
remainder = with_lock__truncate_old_batches(state,
728+
batch);
644729
}
645730
}
646731

647732
pthread_mutex_unlock(&state->main_lock);
648733

734+
if (remainder)
735+
fsmonitor_batch__free_list(remainder);
736+
649737
trace2_data_intmax("fsmonitor", the_repository, "response/length", total_response_len);
650738
trace2_data_intmax("fsmonitor", the_repository, "response/count/files", count);
651739
trace2_data_intmax("fsmonitor", the_repository, "response/count/duplicates", duplicates);

0 commit comments

Comments
 (0)