chromium/components/memory_pressure/unnecessary_discard_monitor.h

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_MEMORY_PRESSURE_UNNECESSARY_DISCARD_MONITOR_H_
#define COMPONENTS_MEMORY_PRESSURE_UNNECESSARY_DISCARD_MONITOR_H_

#include <vector>

#include "base/functional/callback.h"
#include "base/sequence_checker.h"
#include "components/memory_pressure/reclaim_target.h"

namespace memory_pressure {

// The UnnecessaryDiscardMonitor can be used to track and report metrics about
// tab discards that were unnecessary due to the age of the reclaim target that
// caused the discard. This is necessary because discarding a tab is slow, and
// tab discards from a given reclaim target can overlap with the calculation of
// subsequent reclaim targets. Here is an example scenario:
// - Reclaim target 1 is calculated with size 100.
// - Chrome receives reclaim target 1 and begins processing it.
// - Chrome determines that Tab A (size 200) must be discarded to fulfil the
// reclaim target of 100.
// - Chrome begins discarding Tab A.
// - Reclaim target 2 is calculated with size 110. At this point, Tab A has not
// yet been fully discarded and its memory has not been freed.
// - Chrome finishes discarding Tab A.
// - Chrome begins processing reclaim target 2.
// - Chrome determines that Tab B (size 200) must be discarded to fulfil the
// reclaim target of 110.
// - Chrome finishes discarding Tab B.
//
// In this example, discarding Tab B was unnecessary. Discarding Tab A alone
// would fulfil both reclaim targets since they were both calculated before Tab
// A's memory was freed.
class UnnecessaryDiscardMonitor {};

}  // namespace memory_pressure

#endif  // COMPONENTS_MEMORY_PRESSURE_UNNECESSARY_DISCARD_MONITOR_H_