OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" | |
6 | |
7 #include <cstdio> | |
8 #include <sstream> | |
9 | |
10 #include "base/file_util.h" | |
11 #include "base/format_macros.h" | |
12 | |
13 namespace extensions { | |
14 | |
15 namespace { | |
16 | |
17 const char kProcStat[] = "/proc/stat"; | |
18 | |
19 } // namespace | |
20 | |
21 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { | |
22 DCHECK(times); | |
23 | |
24 std::string contents; | |
25 if (!file_util::ReadFileToString(base::FilePath(kProcStat), &contents)) | |
26 return false; | |
27 | |
28 std::istringstream iss(contents); | |
29 uint64 user = 0, nice = 0, sys = 0, idle = 0; | |
30 std::string line; | |
31 | |
32 // Skip the first line because it is just an aggregated number of | |
33 // all cpuN lines. | |
34 std::getline(iss, line); | |
35 std::vector<CpuTime> results; | |
36 while (std::getline(iss, line)) { | |
37 if (line.compare(0, 3, "cpu") != 0) | |
38 continue; | |
39 | |
40 sscanf(line.c_str(), "%*s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, | |
41 &user, &nice, &sys, &idle); | |
42 | |
43 CpuTime time; | |
44 time.kernel = sys; | |
45 time.user = user + nice; | |
46 time.idle = idle; | |
47 results.push_back(time); | |
48 } | |
49 times->swap(results); | |
50 return true; | |
51 } | |
52 | |
53 } // namespace extensions | |
OLD | NEW |