File: cpu_info_provider_mac.cc

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "extensions/browser/api/system_cpu/cpu_info_provider.h"

#include <mach/mach_host.h>

#include "base/apple/scoped_mach_port.h"
#include "base/mac/mac_util.h"
#include "base/system/sys_info.h"

namespace extensions {

bool CpuInfoProvider::QueryCpuTimePerProcessor(
    std::vector<api::system_cpu::ProcessorInfo>* infos) {
  if (base::mac::GetCPUType() == base::mac::CPUType::kTranslatedIntel) {
    // In writing Rosetta, Apple needed to stop simulating an x86 environment
    // somewhere, and they did so before they got to `host_processor_info()`.
    // `host_processor_info()` is a Mach call to a host server in the kernel,
    // and that server does not maintain data corresponding to the simulated
    // processors. See https://crbug.com/1138707#c42 for details. See also
    // FB8832191.
    return false;
  }

  DCHECK(infos);

  natural_t num_of_processors;
  base::apple::ScopedMachSendRight host(mach_host_self());
  mach_msg_type_number_t type;
  processor_cpu_load_info_data_t* cpu_infos;

  if (host_processor_info(host.get(), PROCESSOR_CPU_LOAD_INFO,
                          &num_of_processors,
                          reinterpret_cast<processor_info_array_t*>(&cpu_infos),
                          &type) == KERN_SUCCESS) {
    DCHECK_EQ(num_of_processors,
              static_cast<natural_t>(base::SysInfo::NumberOfProcessors()));
    DCHECK_EQ(num_of_processors, static_cast<natural_t>(infos->size()));

    for (natural_t i = 0; i < num_of_processors; ++i) {
      double user = static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_USER]),
             sys =
                 static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_SYSTEM]),
             nice = static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_NICE]),
             idle = static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_IDLE]);

      infos->at(i).usage.kernel = sys;
      infos->at(i).usage.user = user + nice;
      infos->at(i).usage.idle = idle;
      infos->at(i).usage.total = sys + user + nice + idle;
    }

    vm_deallocate(mach_task_self(), reinterpret_cast<vm_address_t>(cpu_infos),
                  num_of_processors * sizeof(processor_cpu_load_info));

    return true;
  }

  return false;
}

}  // namespace extensions