chromium/components/memory_system/allocation_trace_recorder_statistics_reporter.h

// Copyright 2023 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_SYSTEM_ALLOCATION_TRACE_RECORDER_STATISTICS_REPORTER_H_
#define COMPONENTS_MEMORY_SYSTEM_ALLOCATION_TRACE_RECORDER_STATISTICS_REPORTER_H_

#include <string_view>

#include "base/logging.h"
#include "base/threading/sequence_bound.h"
#include "base/time/time.h"

namespace base::debug::tracer {
class AllocationTraceRecorder;
}

namespace memory_system::internal {

// The reporter for AllocationTraceRecorder periodically fetches the current
// statistics from the passed AllocationTraceRecorder and prints them.
class AllocationTraceRecorderStatisticsReporter {
 public:
  AllocationTraceRecorderStatisticsReporter();

  // Create a new reporting instance and start reporting for the given
  // |recorder|. Log messages contain information on the |process_type| to
  // distinguish between the various processes. Messages will be logged using
  // the passed |severity| at the given |interval|.
  //
  // Note: The |recorder| must remain alive after the reporter is cleaned up due
  // to callbacks generated by the reporter.
  AllocationTraceRecorderStatisticsReporter(
      const base::debug::tracer::AllocationTraceRecorder& recorder,
      std::string_view process_type,
      base::TimeDelta interval,
      logging::LogSeverity severity);

  AllocationTraceRecorderStatisticsReporter(
      AllocationTraceRecorderStatisticsReporter&&);
  AllocationTraceRecorderStatisticsReporter& operator=(
      AllocationTraceRecorderStatisticsReporter&&);

  // Destroy this object and stop any in-progress reporting. Reporting callbacks
  // may continue to run after this destructor.
  ~AllocationTraceRecorderStatisticsReporter();

 private:
  class Core;
  base::SequenceBound<Core> core_;
};

}  // namespace memory_system::internal

#endif  // COMPONENTS_MEMORY_SYSTEM_ALLOCATION_TRACE_RECORDER_STATISTICS_REPORTER_H_