File: cleanup_scheduler.cc

package info (click to toggle)
chromium 140.0.7339.127-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,772 kB
  • sloc: cpp: 35,093,800; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,798; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (162 lines) | stat: -rw-r--r-- 5,835 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/indexed_db/instance/leveldb/cleanup_scheduler.h"

#include "base/feature_list.h"
#include "base/metrics/histogram_functions.h"
#include "content/browser/indexed_db/instance/leveldb/compaction_task.h"
#include "content/browser/indexed_db/instance/leveldb/tombstone_sweeper.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_metadata.h"

namespace content::indexed_db::level_db {

BASE_FEATURE(kIdbInSessionDbCleanup,
             "IdbInSessionDbCleanup",
             base::FEATURE_DISABLED_BY_DEFAULT);

namespace {
constexpr base::TimeDelta kTimeBetweenRuns = base::Minutes(30);
}  // namespace

LevelDBCleanupScheduler::LevelDBCleanupScheduler(leveldb::DB* database,
                                                 Delegate* delegate)
    : database_(database),
      delegate_(delegate),
      // The last_run_ is initialized to a past value so short lived apps
      // which accrue a lot of tombstones will get a chance to have their
      // tombstones cleaned.
      last_run_(base::TimeTicks::Min()) {}

LevelDBCleanupScheduler::~LevelDBCleanupScheduler() {
  if (running_state_) {
    base::UmaHistogramEnumeration(
        "IndexedDB.LevelDBCleanupScheduler.PrematureTerminationPhase",
        running_state_->cleanup_phase);
    base::UmaHistogramCounts10000(
        "IndexedDB.LevelDBCleanupScheduler.CleanerPostponedCount",
        running_state_->postpone_count);
  }
}

void LevelDBCleanupScheduler::OnTransactionStart() {
  ++active_transactions_count_;
  if (running_state_ &&
      running_state_->clean_up_scheduling_timer_.IsRunning()) {
    ++running_state_->postpone_count;
    running_state_->clean_up_scheduling_timer_.Stop();
  }
}

void LevelDBCleanupScheduler::OnTransactionComplete() {
  --active_transactions_count_;
  if (active_transactions_count_ == 0 && running_state_ &&
      !running_state_->clean_up_scheduling_timer_.IsRunning()) {
    // Schedule a run if there are no active transactions and
    // `kTimeBetweenRuns` has been exceeded. The check for `time_since_last_run`
    // is only required when it's the first time the run is being scheduled
    // and has not been paused by `OnTransactionStart`. However, it will
    // always be true if the run was paused, as the condition was met when
    // it was scheduled the first time.
    base::TimeDelta time_since_last_run = base::TimeTicks::Now() - last_run_;
    if (time_since_last_run > kTimeBetweenRuns) {
      ScheduleNextCleanupTask();
    }
  }
}

void LevelDBCleanupScheduler::Initialize() {
  if (!base::FeatureList::IsEnabled(kIdbInSessionDbCleanup)) {
    return;
  }

  if (running_state_) {
    return;
  }

  running_state_.emplace();
}

void LevelDBCleanupScheduler::ScheduleNextCleanupTask() {
  CHECK(running_state_);
  running_state_->clean_up_scheduling_timer_.Start(
      FROM_HERE, kDeferTime,
      base::BindRepeating(&LevelDBCleanupScheduler::RunCleanupTask,
                          base::Unretained(this)));
}

void LevelDBCleanupScheduler::RunCleanupTask() {
  CHECK(running_state_);

  bool tombstone_sweeper_run_complete = false;
  base::TimeTicks time_before_round = base::TimeTicks::Now();
  switch (running_state_->cleanup_phase) {
    case Phase::kRunScheduled:
      running_state_->cleanup_phase = Phase::kTombstoneSweeper;
      ABSL_FALLTHROUGH_INTENDED;
    case Phase::kTombstoneSweeper:
      tombstone_sweeper_run_complete = RunTombstoneSweeper();
      running_state_->tombstone_sweeper_duration +=
          base::TimeTicks::Now() - time_before_round;

      // If not `tombstone_sweeper_run_complete`, we stay in this
      // `kTombstoneSweeper` phase and schedule another round of tombstone
      // sweeper. If done, we reset the timer and move to db compaction.
      if (tombstone_sweeper_run_complete) {
        base::UmaHistogramTimes(
            "IndexedDB.LevelDBCleanupScheduler.TombstoneSweeperDuration",
            running_state_->tombstone_sweeper_duration);
        running_state_->cleanup_phase = Phase::kDatabaseCompaction;
      }
      ScheduleNextCleanupTask();
      break;
    case Phase::kDatabaseCompaction:
      IndexedDBCompactionTask(database_).RunRound();
      running_state_->db_compaction_duration =
          base::TimeTicks::Now() - time_before_round;
      base::UmaHistogramTimes(
          "IndexedDB.LevelDBCleanupScheduler.DBCompactionDuration",
          running_state_->db_compaction_duration);
      running_state_->cleanup_phase = Phase::kLoggingAndCleanup;
      ScheduleNextCleanupTask();
      break;
    case Phase::kLoggingAndCleanup:
      LogAndResetState();
      break;
  }
}

void LevelDBCleanupScheduler::LogAndResetState() {
  CHECK(running_state_);

  base::UmaHistogramCounts10000(
      "IndexedDB.LevelDBCleanupScheduler.CleanerPostponedCount",
      running_state_->postpone_count);

  // Reset the cleanup state
  last_run_ = base::TimeTicks::Now();
  running_state_.reset();

  // Update the timers for traditional sweeper.
  delegate_->UpdateEarliestSweepTime();
  delegate_->UpdateEarliestCompactionTime();
}

bool LevelDBCleanupScheduler::RunTombstoneSweeper() {
  CHECK(running_state_);
  if (!running_state_->tombstone_sweeper) {
    delegate_->GetCompleteMetadata(&running_state_->metadata_vector);
    running_state_->tombstone_sweeper =
        std::make_unique<LevelDbTombstoneSweeper>(database_);
    running_state_->tombstone_sweeper->SetMetadata(
        &running_state_->metadata_vector);
  }

  return running_state_->tombstone_sweeper->RunRound();
}

LevelDBCleanupScheduler::RunningState::RunningState() = default;
LevelDBCleanupScheduler::RunningState::~RunningState() = default;

}  // namespace content::indexed_db::level_db