summaryrefslogtreecommitdiff
path: root/chromium/extensions/browser/api/system_cpu/cpu_info_provider_linux.cc
blob: 2adbdb15f0f7df42ede79f1be9587cbb6a5707ba (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

#include <stdint.h>

#include <cstdio>
#include <sstream>

#include "base/files/file_util.h"
#include "base/format_macros.h"

namespace extensions {

namespace {

const char kProcStat[] = "/proc/stat";

}  // namespace

bool CpuInfoProvider::QueryCpuTimePerProcessor(
    std::vector<api::system_cpu::ProcessorInfo>* infos) {
  DCHECK(infos);

  // WARNING: this method may return incomplete data because some processors may
  // be brought offline at runtime. /proc/stat does not report statistics of
  // offline processors. CPU usages of offline processors will be filled with
  // zeros.
  //
  // An example of output of /proc/stat when processor 0 and 3 are online, but
  // processor 1 and 2 are offline:
  //
  //   cpu  145292 20018 83444 1485410 995 44 3578 0 0 0
  //   cpu0 138060 19947 78350 1479514 570 44 3576 0 0 0
  //   cpu3 2033 32 1075 1400 52 0 1 0 0 0
  std::string contents;
  if (!base::ReadFileToString(base::FilePath(kProcStat), &contents))
    return false;

  std::istringstream iss(contents);
  std::string line;

  // Skip the first line because it is just an aggregated number of
  // all cpuN lines.
  std::getline(iss, line);
  while (std::getline(iss, line)) {
    if (line.compare(0, 3, "cpu") != 0)
      continue;

    uint64_t user = 0, nice = 0, sys = 0, idle = 0;
    uint32_t pindex = 0;
    int vals =
        sscanf(line.c_str(),
               "cpu%" PRIu32 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64,
               &pindex,
               &user,
               &nice,
               &sys,
               &idle);
    if (vals != 5 || pindex >= infos->size()) {
      NOTREACHED();
      return false;
    }

    infos->at(pindex).usage.kernel = static_cast<double>(sys);
    infos->at(pindex).usage.user = static_cast<double>(user + nice);
    infos->at(pindex).usage.idle = static_cast<double>(idle);
    infos->at(pindex).usage.total =
        static_cast<double>(sys + user + nice + idle);
  }

  return true;
}

}  // namespace extensions