File: process_snapshot_server.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 4,583 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_PROCESS_SNAPSHOT_PROCESS_SNAPSHOT_SERVER_H_
#define CHROMEOS_ASH_COMPONENTS_PROCESS_SNAPSHOT_PROCESS_SNAPSHOT_SERVER_H_

#include "base/component_export.h"
#include "base/lazy_instance.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/process/process_iterator.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/timer/timer.h"

namespace ash {

// Provides an interface to regularly getting a full snapshot of the running
// processes on the system.
// The process snapshot will be refreshed at the highest refresh rate (i.e.
// lowest desired refresh time) requested by all clients.
// This system is useful for clients that regularly need a process snapshot but
// don't care if it's stale for a short amount of time. This is to avoid
// multiple clients building a separate process snapshot individually which can
// lead to performance issues.
// (See b/154362057 and https://crbug.com/).
// TODO(afakhry): Currently, this is only used by chome-os-specific task manager
// providers (for VM/Crostini and ARC++ tasks). Consider moving this server
// outside chrome/browser/chromeos/ so that other non-chrome-os clients can use
// it.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_PROCESS_SNAPSHOT)
    ProcessSnapshotServer {
 public:
  class Observer : public base::CheckedObserver {
   public:
    explicit Observer(base::TimeDelta desired_refresh_time);

    base::TimeDelta desired_refresh_time() const {
      return desired_refresh_time_;
    }

    // Called when the process |snapshot| is refreshed. This may be called with
    // intervals shorter than |desired_refresh_time_| as there might be other
    // observers demanding higher refresh rates.
    virtual void OnProcessSnapshotRefreshed(
        const base::ProcessIterator::ProcessEntries& snapshot) = 0;

   protected:
    ~Observer() override = default;

   private:
    // The desired time between two successive refreshes of the process
    // snapshot. The server will use the value from the observer which is
    // minimum among all other observers.
    const base::TimeDelta desired_refresh_time_;
  };

  ProcessSnapshotServer(const ProcessSnapshotServer&) = delete;
  ProcessSnapshotServer& operator=(const ProcessSnapshotServer&) = delete;
  ~ProcessSnapshotServer();

  static ProcessSnapshotServer* Get();

  const base::ProcessIterator::ProcessEntries& snapshot() const {
    return snapshot_;
  }

  // If the added |observer| has a lower |desired_refresh_time_| than all
  // current observers, the refresh rate will be adjusted to match that,
  // otherwise, the server will continue at the same current refresh rate.
  //
  // The first |observer| to be added will trigger an immediate refresh of the
  // process snapshot regardless of the |desired_refresh_time_|.
  void AddObserver(Observer* observer);

  // If the removed |observer| used to have the lowset |desired_refresh_time_|,
  // the refresh rate will be adjusted according to the remaining observers.
  // If |observer| is the last one to be removed, then refreshing will stop.
  void RemoveObserver(Observer* observer);

  const base::RepeatingTimer& GetTimerForTesting() const { return timer_; }

 private:
  friend struct base::LazyInstanceTraitsBase<ProcessSnapshotServer>;

  ProcessSnapshotServer();

  // Triggers a refresh of the process snapshot on |task_runner_|.
  void RefreshSnapshot();

  // Called on the UI thread when the process snapshot is refreshed.
  void OnSnapshotRefreshed(base::ProcessIterator::ProcessEntries snapshot);

  // Updates the current refresh rate based on |new_refresh_time| which could
  // also lead to stopping the timer if |base::TimeDelta::Max()| is provided.
  void RefreshTimer(base::TimeDelta new_refresh_time);

  // The current cached process snapshot. It's only valid if the |timer_| is
  // running and it's being refreshed regularly. Depending on the current
  // refresh rate, it can be too stale.
  base::ProcessIterator::ProcessEntries snapshot_;

  base::RepeatingTimer timer_;

  base::ObserverList<Observer> observers_;

  scoped_refptr<base::SequencedTaskRunner> task_runner_;

  base::WeakPtrFactory<ProcessSnapshotServer> weak_ptr_factory_;
};

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_PROCESS_SNAPSHOT_PROCESS_SNAPSHOT_SERVER_H_