File: arc_instance_throttle.cc

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 (429 lines) | stat: -rw-r--r-- 16,305 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/instance_throttle/arc_instance_throttle.h"

#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/time/time.h"
#include "chrome/browser/ash/arc/boot_phase_monitor/arc_boot_phase_monitor_bridge.h"
#include "chrome/browser/ash/arc/instance_throttle/arc_active_audio_throttle_observer.h"
#include "chrome/browser/ash/arc/instance_throttle/arc_active_window_throttle_observer.h"
#include "chrome/browser/ash/arc/instance_throttle/arc_app_launch_throttle_observer.h"
#include "chrome/browser/ash/arc/instance_throttle/arc_boot_phase_throttle_observer.h"
#include "chrome/browser/ash/arc/instance_throttle/arc_pip_window_throttle_observer.h"
#include "chrome/browser/ash/arc/instance_throttle/arc_power_throttle_observer.h"
#include "chrome/browser/ash/arc/instance_throttle/arc_provisioning_throttle_observer.h"
#include "chrome/browser/ash/arc/instance_throttle/arc_switch_throttle_observer.h"
#include "chromeos/ash/components/dbus/concierge/concierge_client.h"
#include "chromeos/ash/components/dbus/session_manager/session_manager_client.h"
#include "chromeos/ash/experiences/arc/arc_browser_context_keyed_service_factory_base.h"
#include "chromeos/ash/experiences/arc/arc_features.h"
#include "chromeos/ash/experiences/arc/mojom/power.mojom.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"

namespace arc {

namespace {

enum class UnthrottlingReason {
  // Unthrottling ARC to make its boot faster.
  kFasterBoot = 0,
  // Unthrottling ARC to prevent ANRs from happening.
  kPreAnr = 1,
  // All others.
  kOther = 2,
};

enum class CpuRestrictionVmResult {
  // Successfully set/reset CPU restrictions in ARCVM.
  kSuccess = 0,
  // Other failure reason.
  kOther = 1,
  // VM concierge service is not available.
  kNoConciergeService = 2,
  // VM concierge client is not available.
  kNoConciergeClient = 3,
  // VM Concierge did not respond.
  kConciergeDidNotRespond = 4,

  // Note: kMaxValue is needed only for histograms.
  kMaxValue = kConciergeDidNotRespond,
};

void RecordCpuRestrictionVMResult(CpuRestrictionVmResult result) {
  base::UmaHistogramEnumeration("Arc.CpuRestrictionVmResult", result);
}

// Checks all the |observers| for active ones to find out the reason why the
// instance is being unthrottled.
// This function can only be called when the instance is being unthrottled.
UnthrottlingReason GetUnthrottlingReason(
    const std::vector<std::unique_ptr<ash::ThrottleObserver>>& observers) {
  std::vector<ash::ThrottleObserver*> active_observers;

  // Check which observer(s) are active.
  for (const auto& observer : observers) {
    if (observer->active())
      active_observers.push_back(observer.get());
  }

  UnthrottlingReason result = UnthrottlingReason::kOther;
  DCHECK(!active_observers.empty()) << "All observers are inactive";

  for (const auto* active_observer : active_observers) {
    if (active_observer->name() == kArcBootPhaseThrottleObserverName) {
      result = UnthrottlingReason::kFasterBoot;
    } else if (active_observer->name() == kArcPowerThrottleObserverName) {
      result = UnthrottlingReason::kPreAnr;
    } else {
      // If an unknown observer is found, return kOther immediately.
      return UnthrottlingReason::kOther;
    }
  }

  // Note: If both ArcBootPhaseThrottleObserver and ArcPowerThrottleObserver are
  // active, either kFasterBoot or kPreAnr is returned as a special case.
  return result;
}

void OnSetArcVmCpuRestriction(
    std::optional<vm_tools::concierge::SetVmCpuRestrictionResponse> response) {
  if (!response) {
    LOG(ERROR) << "Failed to call SetVmCpuRestriction";
    RecordCpuRestrictionVMResult(
        CpuRestrictionVmResult::kConciergeDidNotRespond);
    return;
  }
  if (response->success()) {
    RecordCpuRestrictionVMResult(CpuRestrictionVmResult::kSuccess);
  } else {
    LOG(ERROR) << "SetVmCpuRestriction for ARCVM failed";
    RecordCpuRestrictionVMResult(CpuRestrictionVmResult::kOther);
  }
}

void SetArcVmCpuRestrictionImpl(
    vm_tools::concierge::SetVmCpuRestrictionRequest request,
    bool service_is_available) {
  if (!service_is_available) {
    LOG(ERROR)
        << "vm_concierge is not available. ArcInstanceThrottle won't work.";
    RecordCpuRestrictionVMResult(CpuRestrictionVmResult::kNoConciergeService);
    return;
  }

  auto* const client = ash::ConciergeClient::Get();
  // TODO(khmel): This should never be possible. Confirm via histogram and
  // change to DCHECK.
  if (!client) {
    LOG(ERROR) << "ConciergeClient is not available";
    RecordCpuRestrictionVMResult(CpuRestrictionVmResult::kNoConciergeClient);
    return;
  }

  client->SetVmCpuRestriction(request,
                              base::BindOnce(&OnSetArcVmCpuRestriction));
}

void SetArcVmCpuRestriction(CpuRestrictionState cpu_restriction_state,
                            bool use_quota) {
  auto* const client = ash::ConciergeClient::Get();
  // TODO(khmel): This should never be possible. Confirm via histogram and
  // change to DCHECK.
  if (!client) {
    LOG(ERROR) << "ConciergeClient is not available";
    RecordCpuRestrictionVMResult(CpuRestrictionVmResult::kNoConciergeClient);
    return;
  }

  vm_tools::concierge::SetVmCpuRestrictionRequest request;
  request.set_cpu_cgroup(vm_tools::concierge::CPU_CGROUP_ARCVM);
  switch (cpu_restriction_state) {
    case CpuRestrictionState::CPU_RESTRICTION_FOREGROUND:
      DCHECK(!use_quota);
      request.set_cpu_restriction_state(
          vm_tools::concierge::CPU_RESTRICTION_FOREGROUND);
      break;
    case CpuRestrictionState::CPU_RESTRICTION_BACKGROUND:
      request.set_cpu_restriction_state(
          use_quota ? vm_tools::concierge::
                          CPU_RESTRICTION_BACKGROUND_WITH_CFS_QUOTA_ENFORCED
                    : vm_tools::concierge::CPU_RESTRICTION_BACKGROUND);
      break;
  }

  // Unlike the ARC container code where the counterpart (session_manager) is
  // always available, this ARCVM function might be called before vm_concierge
  // is ready. To handle that case, send the D-Bus message via the callback
  // passed to the WaitForServiceToBeAvailable function.
  client->WaitForServiceToBeAvailable(
      base::BindOnce(&SetArcVmCpuRestrictionImpl, std::move(request)));
}

void SetArcCpuRestrictionCallback(
    login_manager::ContainerCpuRestrictionState state,
    bool success) {
  if (success)
    return;
  const char* message =
      (state == login_manager::CONTAINER_CPU_RESTRICTION_BACKGROUND)
          ? "unprioritize"
          : "prioritize";
  LOG(ERROR) << "Failed to " << message << " ARC";
}

void SetArcContainerCpuRestriction(CpuRestrictionState cpu_restriction_state) {
  if (!ash::SessionManagerClient::Get()) {
    LOG(WARNING) << "SessionManagerClient is not available";
    return;
  }

  login_manager::ContainerCpuRestrictionState state;
  switch (cpu_restriction_state) {
    case CpuRestrictionState::CPU_RESTRICTION_FOREGROUND:
      state = login_manager::CONTAINER_CPU_RESTRICTION_FOREGROUND;
      break;
    case CpuRestrictionState::CPU_RESTRICTION_BACKGROUND:
      state = login_manager::CONTAINER_CPU_RESTRICTION_BACKGROUND;
      break;
  }
  ash::SessionManagerClient::Get()->SetArcCpuRestriction(
      state, base::BindOnce(SetArcCpuRestrictionCallback, state));
}

// Adjusts the amount of CPU the ARC instance is allowed to use. When
// |cpu_restriction_state| is CPU_RESTRICTION_BACKGROUND, the limit is adjusted
// so ARC can only use tightly restricted CPU resources.
void SetArcCpuRestriction(CpuRestrictionState cpu_restriction_state,
                          bool use_quota) {
  if (IsArcVmEnabled()) {
    SetArcVmCpuRestriction(cpu_restriction_state, use_quota);
  } else {
    // ARC container does not support |use_quota|.
    SetArcContainerCpuRestriction(cpu_restriction_state);
  }
}

class DefaultDelegateImpl : public ArcInstanceThrottle::Delegate {
 public:
  DefaultDelegateImpl() = default;

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

  ~DefaultDelegateImpl() override = default;
  void SetCpuRestriction(CpuRestrictionState cpu_restriction_state,
                         bool use_quota) override {
    SetArcCpuRestriction(cpu_restriction_state, use_quota);
  }

  void RecordCpuRestrictionDisabledUMA(const std::string& observer_name,
                                       base::TimeDelta delta) override {
    DVLOG(2) << "ARC throttling was disabled for "
             << delta.InMillisecondsRoundedUp()
             << " ms due to: " << observer_name;
    UmaHistogramLongTimes("Arc.CpuRestrictionDisabled." + observer_name, delta);
  }
};

// Singleton factory for ArcInstanceThrottle.
class ArcInstanceThrottleFactory
    : public internal::ArcBrowserContextKeyedServiceFactoryBase<
          ArcInstanceThrottle,
          ArcInstanceThrottleFactory> {
 public:
  static constexpr const char* kName = "ArcInstanceThrottleFactory";
  static ArcInstanceThrottleFactory* GetInstance() {
    static base::NoDestructor<ArcInstanceThrottleFactory> instance;
    return instance.get();
  }

 private:
  friend class base::NoDestructor<ArcInstanceThrottleFactory>;

  ArcInstanceThrottleFactory() {
    DependsOn(ArcBootPhaseMonitorBridgeFactory::GetInstance());
    DependsOn(ArcMetricsServiceFactory::GetInstance());
    DependsOn(ArcAppLaunchNotifierFactory::GetInstance());
  }
  ~ArcInstanceThrottleFactory() override = default;
};

CpuRestrictionState ToCpuRestriction(bool should_throttle) {
  return should_throttle ? CpuRestrictionState::CPU_RESTRICTION_BACKGROUND
                         : CpuRestrictionState::CPU_RESTRICTION_FOREGROUND;
}

}  // namespace

// static
const char ArcInstanceThrottle::kChromeArcPowerControlPageObserver[] =
    "arc-power-control";

// static
ArcInstanceThrottle* ArcInstanceThrottle::GetForBrowserContext(
    content::BrowserContext* context) {
  return ArcInstanceThrottleFactory::GetForBrowserContext(context);
}

// static
ArcInstanceThrottle* ArcInstanceThrottle::GetForBrowserContextForTesting(
    content::BrowserContext* context) {
  return ArcInstanceThrottleFactory::GetForBrowserContextForTesting(context);
}

// Note: This function (especially the AddObserver and StartObservers part) must
// be called right after Chrome browser starts, without waiting for vm_concierge
// to be ready. Otherwise, some of the observers might miss events and fail to
// throttle the instance correctly.
ArcInstanceThrottle::ArcInstanceThrottle(content::BrowserContext* context,
                                         ArcBridgeService* bridge)
    : ThrottleService(context),
      delegate_(std::make_unique<DefaultDelegateImpl>()),
      bridge_(bridge) {
  // Note: When you add a new observer, consider modifying GetUnthrottlingReason
  // so that the CPU quota enforcement feature continues to work as intended. In
  // general, unthrottling that is done automatically without the user's action
  // might have to be listed in the UnthrottlingReason enum class. See the
  // comment in ArcInstanceThrottle::ThrottleInstance too.

  AddObserver(std::make_unique<ArcActiveWindowThrottleObserver>());
  AddObserver(std::make_unique<ArcAppLaunchThrottleObserver>());
  AddObserver(std::make_unique<ArcBootPhaseThrottleObserver>());
  AddObserver(std::make_unique<ArcPipWindowThrottleObserver>());
  AddObserver(std::make_unique<ArcPowerThrottleObserver>());
  AddObserver(std::make_unique<ArcProvisioningThrottleObserver>());
  AddObserver(std::make_unique<ArcSwitchThrottleObserver>());
  // This one is controlled by ash::ArcPowerControlHandler.
  AddObserver(std::make_unique<ash::ThrottleObserver>(
      kChromeArcPowerControlPageObserver));
  if (base::FeatureList::IsEnabled(arc::kUnthrottleOnActiveAudioV2)) {
    AddObserver(std::make_unique<ArcActiveAudioThrottleObserver>());
  }

  StartObservers();
  DCHECK(bridge_);
  bridge_->power()->AddObserver(this);

  ArcMetricsService::GetForBrowserContext(context)->AddBootTypeObserver(this);
}

ArcInstanceThrottle::~ArcInstanceThrottle() = default;

void ArcInstanceThrottle::Shutdown() {
  ArcMetricsService::GetForBrowserContext(context())->RemoveBootTypeObserver(
      this);

  bridge_->power()->RemoveObserver(this);

  StopObservers();
}

void ArcInstanceThrottle::OnConnectionReady() {
  NotifyCpuRestriction(ToCpuRestriction(should_throttle()));
}

void ArcInstanceThrottle::OnBootTypeRetrieved(mojom::BootType boot_type) {
  switch (boot_type) {
    case mojom::BootType::UNKNOWN:
      break;
    case mojom::BootType::FIRST_BOOT:
    case mojom::BootType::FIRST_BOOT_AFTER_UPDATE:
      // ARCVM vCPUs tend to be very busy on those boots. Allow Chrome to use
      // the enforcing quota mode to cap the VM's CPU usage.
      return;
    case mojom::BootType::REGULAR_BOOT:
      // On the other hand, regular boot does not usually consume that much vCPU
      // time. Disable quota enforcement now to prevent unnecessary ANRs from
      // happening.
      never_enforce_quota_ = true;
      return;
  }
  NOTREACHED();
}

void ArcInstanceThrottle::ThrottleInstance(bool should_throttle) {
  const CpuRestrictionState cpu_restriction_state =
      ToCpuRestriction(should_throttle);

  NotifyCpuRestriction(cpu_restriction_state);

  // Check if enforcing quota is possible
  bool use_quota = false;

  if (!should_throttle && !never_enforce_quota_) {
    // We're unthrottling the instance. Figure out why.
    switch (GetUnthrottlingReason(observers())) {
      case UnthrottlingReason::kFasterBoot:
        DVLOG(2) << "Unthrottling for faster boot. Quota is still applicable.";
        break;
      case UnthrottlingReason::kPreAnr:
        DVLOG(2)
            << "Unthrottling for preventing ANRs. Quota is still applicable.";
        break;
      case UnthrottlingReason::kOther:
        // ARC is unthrottled by a user action. After such an event, the quota
        // should never be applied.
        DVLOG(2) << "Unthrottling because of a user action. Quota is no longer "
                    "applicable.";
        never_enforce_quota_ = true;
        break;
    }
    // Note on why other throttle observers that don't monitor the user's action
    // can be classified as |kOther| and can disable quota:
    //
    // * ArcSwitchThrottleObserver:
    //   This is for disabling throttling for testing. If the observer gets
    //   activated, the quota shouldn't be applied either.
    //
    // * ArcProvisioningThrottleObserver:
    //   If this gets activated, the provisioning is ongoing. The quota
    //   shouldn't be applied to make provisioning failures less likely to
    //   happen.
  }

  const std::optional<bool>& arc_is_booting =
      GetBootObserver()->arc_is_booting();
  const bool arc_has_booted = (arc_is_booting && !*arc_is_booting);
  const bool is_throttling = (cpu_restriction_state ==
                              CpuRestrictionState::CPU_RESTRICTION_BACKGROUND);

  if (arc_has_booted && !never_enforce_quota_ && is_throttling) {
    // TODO(khmel): Do not use quota when Android VPN is in use.
    use_quota = true;
    DVLOG(2) << "Enforcing cfs_quota";
  }
  delegate_->SetCpuRestriction(cpu_restriction_state, use_quota);
}

void ArcInstanceThrottle::RecordCpuRestrictionDisabledUMA(
    const std::string& observer_name,
    base::TimeDelta delta) {
  delegate_->RecordCpuRestrictionDisabledUMA(observer_name, delta);
}

void ArcInstanceThrottle::NotifyCpuRestriction(
    CpuRestrictionState cpu_restriction_state) {
  auto* power =
      ARC_GET_INSTANCE_FOR_METHOD(bridge_->power(), OnCpuRestrictionChanged);
  if (!power)
    return;
  power->OnCpuRestrictionChanged(
      static_cast<mojom::CpuRestrictionState>(cpu_restriction_state));
}

ArcBootPhaseThrottleObserver* ArcInstanceThrottle::GetBootObserver() {
  ash::ThrottleObserver* observer =
      GetObserverByName(kArcBootPhaseThrottleObserverName);
  return static_cast<ArcBootPhaseThrottleObserver*>(observer);
}

// static
void ArcInstanceThrottle::EnsureFactoryBuilt() {
  ArcInstanceThrottleFactory::GetInstance();
}

}  // namespace arc