File: metric_source.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 4,369 bytes parent folder | download | duplicates (9)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_SCHEDULER_RESPONSIVENESS_METRIC_SOURCE_H_
#define CONTENT_BROWSER_SCHEDULER_RESPONSIVENESS_METRIC_SOURCE_H_

#include <memory>

#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "content/common/content_export.h"

namespace base {
struct PendingTask;
}  // namespace base

namespace content {
namespace responsiveness {

class MessageLoopObserver;
class NativeEventObserver;

// This class represents the source of browser responsiveness metrics.
// This class watches events and tasks processed on the UI and IO threads of the
// browser process. It notifies the registered Delegate implementer about
// event and task execution through the WillRun* and DidRun* methods.
//
// This class must only be constructed/destroyed on the UI thread. It has some
// private members that are affine to the IO thread. It takes care of deleting
// them appropriately.
//
// TODO(erikchen): Once the browser scheduler is implemented, this entire class
// should become simpler, as either BrowserUIThreadScheduler or
// BrowserIOThreadScheduler should implement much of the same functionality.
class CONTENT_EXPORT MetricSource {
 public:
  class CONTENT_EXPORT Delegate {
   public:
    virtual ~Delegate();

    // Interface for SetUp/TearDown on UI/IO threads in the implementer.
    virtual void SetUpOnIOThread() = 0;
    virtual void TearDownOnUIThread() = 0;
    virtual void TearDownOnIOThread() = 0;

    // These methods are called by the MessageLoopObserver of the UI thread to
    // allow Delegate to collect metadata about the tasks being run.
    virtual void WillRunTaskOnUIThread(const base::PendingTask* task,
                                       bool was_blocked_or_low_priority) = 0;
    virtual void DidRunTaskOnUIThread(const base::PendingTask* task) = 0;

    // These methods are called by the MessageLoopObserver of the IO thread to
    // allow Delegate to collect metadata about the tasks being run.
    virtual void WillRunTaskOnIOThread(const base::PendingTask* task,
                                       bool was_blocked_or_low_priority) = 0;
    virtual void DidRunTaskOnIOThread(const base::PendingTask* task) = 0;

    // These methods are called by the NativeEventObserver of the UI thread to
    // allow Delegate to collect metadata about the events being run.
    virtual void WillRunEventOnUIThread(const void* opaque_identifier) = 0;
    virtual void DidRunEventOnUIThread(const void* opaque_identifier) = 0;
  };

  explicit MetricSource(Delegate* delegate);

  MetricSource(const MetricSource&) = delete;
  MetricSource& operator=(const MetricSource&) = delete;

  virtual ~MetricSource();

  // Must be called immediately after the constructor. This cannot be called
  // from the constructor because subclasses [for tests] need to be able to
  // override functions.
  void SetUp();

  // Destruction requires a thread-hop to the IO thread so cannot be completed
  // synchronously. Owners of this class calling Destroy() should pass one
  // base::ScopedClosureRunner for final cleanup when this class finishes the
  // destruction on the UI thread. It's safe to delete this instance in
  // |on_finish_destroy|.
  void Destroy(base::ScopedClosureRunner on_finish_destroy);

 protected:
  virtual std::unique_ptr<NativeEventObserver> CreateNativeEventObserver();
  virtual void RegisterMessageLoopObserverUI();
  virtual void RegisterMessageLoopObserverIO();

 private:
  void SetUpOnIOThread();
  void TearDownOnIOThread(base::ScopedClosureRunner on_finish_destroy);
  void TearDownOnUIThread(base::ScopedClosureRunner on_finish_destroy);

  raw_ptr<Delegate> delegate_;

  // The following members are all affine to the UI thread.
  std::unique_ptr<MessageLoopObserver> message_loop_observer_ui_;
  std::unique_ptr<NativeEventObserver> native_event_observer_ui_;

  // The following members are all affine to the IO thread.
  std::unique_ptr<MessageLoopObserver> message_loop_observer_io_;

  bool destroy_was_called_ = false;
};

}  // namespace responsiveness
}  // namespace content

#endif  // CONTENT_BROWSER_SCHEDULER_RESPONSIVENESS_METRIC_SOURCE_H_