File: dbus_schedqos_state_handler.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 (156 lines) | stat: -rw-r--r-- 6,240 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
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_SCHEDQOS_DBUS_SCHEDQOS_STATE_HANDLER_H_
#define CHROME_BROWSER_ASH_SCHEDQOS_DBUS_SCHEDQOS_STATE_HANDLER_H_

#include <map>

#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/process/process.h"
#include "base/process/process_priority_delegate.h"
#include "base/sequence_checker.h"
#include "base/synchronization/lock.h"
#include "base/task/sequenced_task_runner.h"
#include "base/threading/cross_process_platform_thread_delegate.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_type_delegate.h"
#include "base/timer/elapsed_timer.h"
#include "chrome/browser/ash/system/procfs_util.h"
#include "dbus/dbus_result.h"

namespace ash {
// DBusSchedQOSStateHandler sends `SetProcessState` and `SetThreadState` DBus
// requests to resourced on `base::Process::SetPriority()` and
// `base::PlatformThread::SetThreadType()` instead of updating OS scheduler
// settings directly.
//
// DBusSchedQOSStateHandler is for ChromeOS only.
//
// DBusSchedQOSStateHandler caches process priorities to be consistent with
// `base::Process::GetPriority()`. Processes which will change their priorities
// need to call `base::Process::InitializePriority()` before calling
// `base::Process::SetPriority()`. Otherwise `base::Process::SetPriority()`
// fails. Also the processes must call `base::Process::ForgetPriority()` when
// they terminate. Otherwise the cache in this class leaks.
class DBusSchedQOSStateHandler
    : public base::ProcessPriorityDelegate,
      public base::ThreadTypeDelegate,
      public base::CrossProcessPlatformThreadDelegate {
 public:
  DBusSchedQOSStateHandler(const DBusSchedQOSStateHandler&) = delete;
  DBusSchedQOSStateHandler& operator=(const DBusSchedQOSStateHandler&) = delete;

  ~DBusSchedQOSStateHandler() override;

  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class PidReuseResult {
    kNotPidReuseOnFail = 0,
    kPidReuseOnFail = 1,
    kNotPidReuseOnSuccess = 2,
    kPidReuseOnSuccess = 3,
    kMaxValue = kPidReuseOnSuccess,
  };

  // Creates a SandboxedProcessThreadTypeHandler instance and stores it to
  // g_instance. Make sure the g_instance doesn't exist before creation.
  //
  // `main_task_runner` is the main thread's task runner to call D-Bus clients
  // from.
  static DBusSchedQOSStateHandler* Create(
      scoped_refptr<base::SequencedTaskRunner> main_task_runner);

  // base::ProcessPriorityDelegate :
  bool CanSetProcessPriority() override;
  void InitializeProcessPriority(base::ProcessId process_id) override;
  void ForgetProcessPriority(base::ProcessId process_id) override;
  bool SetProcessPriority(base::ProcessId process_id,
                          base::Process::Priority priority) override;

  base::Process::Priority GetProcessPriority(
      base::ProcessId process_id) override;

  bool HandleThreadTypeChange(base::ProcessId process_id,
                              base::PlatformThreadId thread_id,
                              base::ThreadType thread_type) override;
  bool HandleThreadTypeChange(base::PlatformThreadId thread_id,
                              base::ThreadType thread_type) override;

 private:
  struct ProcessState {
    base::Process::Priority priority;
    bool need_retry;
    std::map<base::PlatformThreadId, base::ThreadType>
        preconnected_thread_types;

    explicit ProcessState(base::Process::Priority priority);
    ProcessState() = delete;
    ProcessState(base::Process::Priority priority, bool need_retry);
    ~ProcessState();
    ProcessState(ProcessState&&);
    ProcessState(ProcessState&) = delete;
  };

  explicit DBusSchedQOSStateHandler(
      scoped_refptr<base::SequencedTaskRunner> main_task_runner);

  void WaitForResourcedAvailable();

  void CheckResourcedDisconnected(dbus::DBusResult result);

  void OnServiceConnected(bool success);

  void SetProcessPriorityOnThread(base::ProcessId process_id);

  void OnSetProcessPriorityFinish(base::ProcessId process_id,
                                  base::Process::Priority priority,
                                  base::ElapsedTimer elapsed_timer,
                                  system::ProcStatFile stat_file,
                                  dbus::DBusResult result);

  void MarkProcessToRetry(base::ProcessId process_id);

  void SetThreadTypeOnThread(base::ProcessId process_id,
                             base::PlatformThreadId thread_id,
                             base::ThreadType thread_type);

  void OnSetThreadTypeFinish(base::ProcessId process_id,
                             base::PlatformThreadId thread_id,
                             base::ThreadType thread_type,
                             base::ElapsedTimer elapsed_timer,
                             system::ProcStatFile stat_file,
                             dbus::DBusResult result);

  void AddThreadRetryEntry(base::ProcessId process_id,
                           base::PlatformThreadId thread_id,
                           base::ThreadType thread_type);

  SEQUENCE_CHECKER(sequence_checker_);

  bool is_connected_ GUARDED_BY_CONTEXT(sequence_checker_) = false;
  bool is_dbus_down_ GUARDED_BY_CONTEXT(sequence_checker_) = false;

  base::Lock process_state_map_lock_;

  // process_state_map_ is used for 2 purposes:
  //
  // * To return the latest process state from GetProcessPriority().
  //    * GetProcessPriority() return the latest value set by
  //      SetProcessPriority() as if it is synchronously updated while the
  //      actual request is asynchronously sent to resourced.
  // * To hold pending requests while resourced is not available.
  std::map<base::ProcessId, ProcessState> process_state_map_
      GUARDED_BY(process_state_map_lock_);

  // ResourcedClient need to be called on the main thread.
  scoped_refptr<base::SequencedTaskRunner> main_task_runner_;

  base::WeakPtrFactory<DBusSchedQOSStateHandler> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_SCHEDQOS_DBUS_SCHEDQOS_STATE_HANDLER_H_