blob: 7fa0ffeea333e3949441c41c0bbeb5ce91e88329 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2021 The Chromium Authors
Quang Minh Tuan Nguyen636a7ab22021-03-25 22:55:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Quang Minh Tuan Nguyen36d4dca2021-03-30 02:41:225#ifndef CONTENT_BROWSER_METRICS_HISTOGRAMS_MONITOR_H_
6#define CONTENT_BROWSER_METRICS_HISTOGRAMS_MONITOR_H_
Quang Minh Tuan Nguyen636a7ab22021-03-25 22:55:347
8#include <map>
Alexei Svitkine8591a1b42024-02-01 18:48:029#include <string_view>
Quang Minh Tuan Nguyen636a7ab22021-03-25 22:55:3410
11#include "base/metrics/histogram_samples.h"
12#include "base/metrics/statistics_recorder.h"
13#include "content/common/content_export.h"
14
15namespace content {
16
17// This class handles the monitoring feature of chrome://histograms page,
Alexei Svitkine8591a1b42024-02-01 18:48:0218// which allows the page to be updated with histograms logged since the
19// monitoring started.
Yulun Zeng5e521f42022-11-01 00:13:5820//
21// Note that this class does not handle merging histograms from any
22// |HistogramProvider| instances. It also does not handle synchronizing
23// histograms from subprocesses. The caller has the responsibility for these
24// beforehand.
Quang Minh Tuan Nguyen636a7ab22021-03-25 22:55:3425class CONTENT_EXPORT HistogramsMonitor {
26 public:
27 HistogramsMonitor();
28 ~HistogramsMonitor();
29
30 HistogramsMonitor(const HistogramsMonitor&) = delete;
31 HistogramsMonitor& operator=(const HistogramsMonitor&) = delete;
32
Alexei Svitkine8591a1b42024-02-01 18:48:0233 // Fetches and records a snapshot of the current histograms, as the baseline
34 // to compare against in subsequent calls to GetDiff().
Andrzej Fiedukowiczf5d01b52025-01-21 17:56:2635 void StartMonitoring();
Quang Minh Tuan Nguyen636a7ab22021-03-25 22:55:3436
37 // Gets the histogram diffs between the current histograms and the snapshot
Andrzej Fiedukowiczf5d01b52025-01-21 17:56:2638 // recorded in StartMonitoring(). Only returns the histograms that match
39 // the query string.
40 base::Value::List GetDiff(const std::string& query);
Quang Minh Tuan Nguyen636a7ab22021-03-25 22:55:3441
42 private:
Quang Minh Tuan Nguyen636a7ab22021-03-25 22:55:3443 // Gets the difference between the histograms argument and the stored snapshot
44 // recorded in StartMonitoring().
Claudio DeSouza464bba7b2022-08-04 22:55:4045 base::Value::List GetDiffInternal(
Quang Minh Tuan Nguyen636a7ab22021-03-25 22:55:3446 const base::StatisticsRecorder::Histograms& histograms);
47
Roger McFarlane89528592025-02-20 20:50:2748 std::map<std::string, std::unique_ptr<base::HistogramSamples>, std::less<>>
Quang Minh Tuan Nguyen636a7ab22021-03-25 22:55:3449 histograms_snapshot_;
50};
51
52} // namespace content
53
Quang Minh Tuan Nguyen36d4dca2021-03-30 02:41:2254#endif // CONTENT_BROWSER_METRICS_HISTOGRAMS_MONITOR_H_