File: resourced_client.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (180 lines) | stat: -rw-r--r-- 6,140 bytes parent folder | download | duplicates (7)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2021 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_DBUS_RESOURCED_RESOURCED_CLIENT_H_
#define CHROMEOS_ASH_COMPONENTS_DBUS_RESOURCED_RESOURCED_CLIENT_H_

#include <cstdint>
#include <vector>

#include "base/component_export.h"
#include "base/observer_list_types.h"
#include "base/process/process_handle.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "chromeos/dbus/common/dbus_callback.h"
#include "components/memory_pressure/reclaim_target.h"
#include "dbus/dbus_result.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/resource_manager/dbus-constants.h"

namespace dbus {
class Bus;
}

namespace ash {

class FakeResourcedClient;

// ResourcedClient is used to communicate with the org.chromium.ResourceManager
// service. The browser uses the ResourceManager service to get resource usage
// status.
class COMPONENT_EXPORT(RESOURCED) ResourcedClient {
 public:
  enum PressureLevel {
    // There is enough memory to use.
    NONE = 0,
    // Chrome is advised to free buffers that are cheap to re-allocate and not
    // immediately needed.
    MODERATE = 1,
    // Chrome is advised to free all possible memory.
    CRITICAL = 2,
  };

  // Observer class for memory pressure signal.
  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override = default;

    virtual void OnMemoryPressure(PressureLevel level,
                                  memory_pressure::ReclaimTarget target) = 0;
  };

  // Indicates whether game mode is on, and which kind of game mode if it is on.
  // Borealis game mode will put more memory pressure on ARCVM processes than
  // will ARC game mode.
  // D-Bus serializes this as a u8, hence the uint8_t specifier.
  enum class GameMode : uint8_t {
    OFF = 0,
    BOREALIS = 1,
    ARC = 2,
  };

  enum class PressureLevelArcContainer {
    // There is enough memory to use.
    kNone = 0,
    // ARC container is advised to kill cached apps to free memory.
    kCached = 1,
    // ARC container is advised to kill perceptible apps to free memory.
    kPerceptible = 2,
    // ARC container is advised to kill foreground apps to free memory.
    kForeground = 3,
  };

  // Observer class for ARC container memory pressure signal.
  class ArcContainerObserver : public base::CheckedObserver {
   public:
    ~ArcContainerObserver() override = default;

    virtual void OnMemoryPressure(PressureLevelArcContainer level,
                                  uint64_t reclaim_target_kb) = 0;
  };

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

  // Creates and initializes the global instance. |bus| must not be null.
  static void Initialize(dbus::Bus* bus);

  // Creates and initializes a fake global instance if not already created.
  // The newly created object will persist until Shutdown() is called.
  static FakeResourcedClient* InitializeFake();

  // Destroys the global instance.
  static void Shutdown();

  // Returns the global instance which may be null if not initialized.
  static ResourcedClient* Get();

  // Attempts to enter or exit game mode depending on the value of |game_mode|.
  // Will automatically exit game mode once refresh_seconds is reached.
  // Callback will be called with whether game mode was on prior to this.
  virtual void SetGameModeWithTimeout(
      GameMode game_mode,
      uint32_t refresh_seconds,
      chromeos::DBusMethodCallback<GameMode> callback) = 0;

  // The bps fields are in basis points which represent one-one hundredth of a
  // percent, e.g., 1354 bps = 13.54%.
  struct MemoryMargins {
    uint32_t moderate_bps = 0;
    uint32_t critical_bps = 0;
    uint32_t critical_protected_bps = 0;
  };

  // Informs resourced that it should use a different value for the memory
  // margins.
  virtual void SetMemoryMargins(MemoryMargins margins) = 0;

  struct Process {
    Process(base::ProcessHandle pid,
            bool is_protected,
            bool is_visible,
            bool is_focused,
            base::TimeTicks last_visible)
        : pid(pid),
          is_protected(is_protected),
          is_visible(is_visible),
          is_focused(is_focused),
          last_visible(last_visible) {}
    base::ProcessHandle pid;
    bool is_protected;
    bool is_visible;
    bool is_focused;
    base::TimeTicks last_visible;
  };

  virtual void ReportBrowserProcesses(
      const std::vector<Process>& processes) = 0;

  using SetQoSStateCallback = base::OnceCallback<void(dbus::DBusResult)>;

  // Set qos state of a process.
  virtual void SetProcessState(base::ProcessId,
                               resource_manager::ProcessState,
                               SetQoSStateCallback) = 0;

  // Set qos state of a thread.
  virtual void SetThreadState(base::ProcessId,
                              base::PlatformThreadId,
                              resource_manager::ThreadState,
                              SetQoSStateCallback) = 0;

  // Adds an observer to the observer list to listen on memory pressure events.
  virtual void AddObserver(Observer* observer) = 0;

  // Removes an observer from observer list.
  virtual void RemoveObserver(Observer* observer) = 0;

  virtual void AddArcContainerObserver(ArcContainerObserver* observer) = 0;

  virtual void RemoveArcContainerObserver(ArcContainerObserver* observer) = 0;

  // Registers |callback| to run when the ResourceManager service becomes
  // available. If the service is already available, or if connecting to the
  // name-owner-changed signal fails, |callback| will be run once
  // asynchronously.
  // Otherwise, |callback| will be run once in the future after the service
  // becomes available.
  virtual void WaitForServiceToBeAvailable(
      dbus::ObjectProxy::WaitForServiceToBeAvailableCallback callback) = 0;

 protected:
  ResourcedClient();
  virtual ~ResourcedClient();
};

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_DBUS_RESOURCED_RESOURCED_CLIENT_H_