Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: base/process/process_metrics_linux.cc

Issue 19579005: Move ReadFileToString to the base namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/process/process_linux.cc ('k') | base/sys_info_chromeos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/process/process_metrics.h" 5 #include "base/process/process_metrics.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <sys/time.h> 10 #include <sys/time.h>
(...skipping 16 matching lines...) Expand all
27 27
28 enum ParsingState { 28 enum ParsingState {
29 KEY_NAME, 29 KEY_NAME,
30 KEY_VALUE 30 KEY_VALUE
31 }; 31 };
32 32
33 #ifdef OS_CHROMEOS 33 #ifdef OS_CHROMEOS
34 // Read a file with a single number string and return the number as a uint64. 34 // Read a file with a single number string and return the number as a uint64.
35 static uint64 ReadFileToUint64(const base::FilePath file) { 35 static uint64 ReadFileToUint64(const base::FilePath file) {
36 std::string file_as_string; 36 std::string file_as_string;
37 if (!file_util::ReadFileToString(file, &file_as_string)) 37 if (!ReadFileToString(file, &file_as_string))
38 return 0; 38 return 0;
39 TrimWhitespaceASCII(file_as_string, TRIM_ALL, &file_as_string); 39 TrimWhitespaceASCII(file_as_string, TRIM_ALL, &file_as_string);
40 uint64 file_as_uint64 = 0; 40 uint64 file_as_uint64 = 0;
41 if (!base::StringToUint64(file_as_string, &file_as_uint64)) 41 if (!base::StringToUint64(file_as_string, &file_as_uint64))
42 return 0; 42 return 0;
43 return file_as_uint64; 43 return file_as_uint64;
44 } 44 }
45 #endif 45 #endif
46 46
47 // Read /proc/<pid>/status and returns the value for |field|, or 0 on failure. 47 // Read /proc/<pid>/status and returns the value for |field|, or 0 on failure.
48 // Only works for fields in the form of "Field: value kB". 48 // Only works for fields in the form of "Field: value kB".
49 size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) { 49 size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) {
50 FilePath stat_file = internal::GetProcPidDir(pid).Append("status"); 50 FilePath stat_file = internal::GetProcPidDir(pid).Append("status");
51 std::string status; 51 std::string status;
52 { 52 {
53 // Synchronously reading files in /proc is safe. 53 // Synchronously reading files in /proc is safe.
54 ThreadRestrictions::ScopedAllowIO allow_io; 54 ThreadRestrictions::ScopedAllowIO allow_io;
55 if (!file_util::ReadFileToString(stat_file, &status)) 55 if (!ReadFileToString(stat_file, &status))
56 return 0; 56 return 0;
57 } 57 }
58 58
59 StringTokenizer tokenizer(status, ":\n"); 59 StringTokenizer tokenizer(status, ":\n");
60 ParsingState state = KEY_NAME; 60 ParsingState state = KEY_NAME;
61 StringPiece last_key_name; 61 StringPiece last_key_name;
62 while (tokenizer.GetNext()) { 62 while (tokenizer.GetNext()) {
63 switch (state) { 63 switch (state) {
64 case KEY_NAME: 64 case KEY_NAME:
65 last_key_name = tokenizer.token_piece(); 65 last_key_name = tokenizer.token_piece();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 pid_t tid = internal::ProcDirSlotToPid(ent->d_name); 110 pid_t tid = internal::ProcDirSlotToPid(ent->d_name);
111 if (!tid) 111 if (!tid)
112 continue; 112 continue;
113 113
114 // Synchronously reading files in /proc is safe. 114 // Synchronously reading files in /proc is safe.
115 ThreadRestrictions::ScopedAllowIO allow_io; 115 ThreadRestrictions::ScopedAllowIO allow_io;
116 116
117 std::string stat; 117 std::string stat;
118 FilePath stat_path = 118 FilePath stat_path =
119 task_path.Append(ent->d_name).Append(internal::kStatFile); 119 task_path.Append(ent->d_name).Append(internal::kStatFile);
120 if (file_util::ReadFileToString(stat_path, &stat)) { 120 if (ReadFileToString(stat_path, &stat)) {
121 int cpu = ParseProcStatCPU(stat); 121 int cpu = ParseProcStatCPU(stat);
122 if (cpu > 0) 122 if (cpu > 0)
123 total_cpu += cpu; 123 total_cpu += cpu;
124 } 124 }
125 } 125 }
126 closedir(dir); 126 closedir(dir);
127 127
128 return total_cpu; 128 return total_cpu;
129 } 129 }
130 130
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 } 216 }
217 217
218 // To have /proc/self/io file you must enable CONFIG_TASK_IO_ACCOUNTING 218 // To have /proc/self/io file you must enable CONFIG_TASK_IO_ACCOUNTING
219 // in your kernel configuration. 219 // in your kernel configuration.
220 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { 220 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
221 // Synchronously reading files in /proc is safe. 221 // Synchronously reading files in /proc is safe.
222 ThreadRestrictions::ScopedAllowIO allow_io; 222 ThreadRestrictions::ScopedAllowIO allow_io;
223 223
224 std::string proc_io_contents; 224 std::string proc_io_contents;
225 FilePath io_file = internal::GetProcPidDir(process_).Append("io"); 225 FilePath io_file = internal::GetProcPidDir(process_).Append("io");
226 if (!file_util::ReadFileToString(io_file, &proc_io_contents)) 226 if (!ReadFileToString(io_file, &proc_io_contents))
227 return false; 227 return false;
228 228
229 (*io_counters).OtherOperationCount = 0; 229 (*io_counters).OtherOperationCount = 0;
230 (*io_counters).OtherTransferCount = 0; 230 (*io_counters).OtherTransferCount = 0;
231 231
232 StringTokenizer tokenizer(proc_io_contents, ": \n"); 232 StringTokenizer tokenizer(proc_io_contents, ": \n");
233 ParsingState state = KEY_NAME; 233 ParsingState state = KEY_NAME;
234 StringPiece last_key_name; 234 StringPiece last_key_name;
235 while (tokenizer.GetNext()) { 235 while (tokenizer.GetNext()) {
236 switch (state) { 236 switch (state) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 // Locked: XXX kB 288 // Locked: XXX kB
289 const size_t kPssIndex = (1 * 3) + 1; 289 const size_t kPssIndex = (1 * 3) + 1;
290 const size_t kPrivate_CleanIndex = (4 * 3) + 1; 290 const size_t kPrivate_CleanIndex = (4 * 3) + 1;
291 const size_t kPrivate_DirtyIndex = (5 * 3) + 1; 291 const size_t kPrivate_DirtyIndex = (5 * 3) + 1;
292 const size_t kSwapIndex = (9 * 3) + 1; 292 const size_t kSwapIndex = (9 * 3) + 1;
293 293
294 std::string totmaps_data; 294 std::string totmaps_data;
295 { 295 {
296 FilePath totmaps_file = internal::GetProcPidDir(process_).Append("totmaps"); 296 FilePath totmaps_file = internal::GetProcPidDir(process_).Append("totmaps");
297 ThreadRestrictions::ScopedAllowIO allow_io; 297 ThreadRestrictions::ScopedAllowIO allow_io;
298 bool ret = file_util::ReadFileToString(totmaps_file, &totmaps_data); 298 bool ret = ReadFileToString(totmaps_file, &totmaps_data);
299 if (!ret || totmaps_data.length() == 0) 299 if (!ret || totmaps_data.length() == 0)
300 return false; 300 return false;
301 } 301 }
302 302
303 std::vector<std::string> totmaps_fields; 303 std::vector<std::string> totmaps_fields;
304 SplitStringAlongWhitespace(totmaps_data, &totmaps_fields); 304 SplitStringAlongWhitespace(totmaps_data, &totmaps_fields);
305 305
306 DCHECK_EQ("Pss:", totmaps_fields[kPssIndex-1]); 306 DCHECK_EQ("Pss:", totmaps_fields[kPssIndex-1]);
307 DCHECK_EQ("Private_Clean:", totmaps_fields[kPrivate_CleanIndex - 1]); 307 DCHECK_EQ("Private_Clean:", totmaps_fields[kPrivate_CleanIndex - 1]);
308 DCHECK_EQ("Private_Dirty:", totmaps_fields[kPrivate_DirtyIndex - 1]); 308 DCHECK_EQ("Private_Dirty:", totmaps_fields[kPrivate_DirtyIndex - 1]);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 // For details, see: man 5 proc. 340 // For details, see: man 5 proc.
341 const int page_size_kb = getpagesize() / 1024; 341 const int page_size_kb = getpagesize() / 1024;
342 if (page_size_kb <= 0) 342 if (page_size_kb <= 0)
343 return false; 343 return false;
344 344
345 std::string statm; 345 std::string statm;
346 { 346 {
347 FilePath statm_file = internal::GetProcPidDir(process_).Append("statm"); 347 FilePath statm_file = internal::GetProcPidDir(process_).Append("statm");
348 // Synchronously reading files in /proc is safe. 348 // Synchronously reading files in /proc is safe.
349 ThreadRestrictions::ScopedAllowIO allow_io; 349 ThreadRestrictions::ScopedAllowIO allow_io;
350 bool ret = file_util::ReadFileToString(statm_file, &statm); 350 bool ret = ReadFileToString(statm_file, &statm);
351 if (!ret || statm.length() == 0) 351 if (!ret || statm.length() == 0)
352 return false; 352 return false;
353 } 353 }
354 354
355 std::vector<std::string> statm_vec; 355 std::vector<std::string> statm_vec;
356 SplitString(statm, ' ', &statm_vec); 356 SplitString(statm, ' ', &statm_vec);
357 if (statm_vec.size() != 7) 357 if (statm_vec.size() != 7)
358 return false; // Not the format we expect. 358 return false; // Not the format we expect.
359 359
360 int statm_rss, statm_shared; 360 int statm_rss, statm_shared;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 gem_size(-1) { 437 gem_size(-1) {
438 } 438 }
439 439
440 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { 440 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
441 // Synchronously reading files in /proc is safe. 441 // Synchronously reading files in /proc is safe.
442 ThreadRestrictions::ScopedAllowIO allow_io; 442 ThreadRestrictions::ScopedAllowIO allow_io;
443 443
444 // Used memory is: total - free - buffers - caches 444 // Used memory is: total - free - buffers - caches
445 FilePath meminfo_file("/proc/meminfo"); 445 FilePath meminfo_file("/proc/meminfo");
446 std::string meminfo_data; 446 std::string meminfo_data;
447 if (!file_util::ReadFileToString(meminfo_file, &meminfo_data)) { 447 if (!ReadFileToString(meminfo_file, &meminfo_data)) {
448 DLOG(WARNING) << "Failed to open " << meminfo_file.value(); 448 DLOG(WARNING) << "Failed to open " << meminfo_file.value();
449 return false; 449 return false;
450 } 450 }
451 std::vector<std::string> meminfo_fields; 451 std::vector<std::string> meminfo_fields;
452 SplitStringAlongWhitespace(meminfo_data, &meminfo_fields); 452 SplitStringAlongWhitespace(meminfo_data, &meminfo_fields);
453 453
454 if (meminfo_fields.size() < kMemCachedIndex) { 454 if (meminfo_fields.size() < kMemCachedIndex) {
455 DLOG(WARNING) << "Failed to parse " << meminfo_file.value() 455 DLOG(WARNING) << "Failed to parse " << meminfo_file.value()
456 << ". Only found " << meminfo_fields.size() << " fields."; 456 << ". Only found " << meminfo_fields.size() << " fields.";
457 return false; 457 return false;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 // bind mount into /sys/kernel/debug and synchronously reading the in-memory 492 // bind mount into /sys/kernel/debug and synchronously reading the in-memory
493 // files in /sys is fast. 493 // files in /sys is fast.
494 #if defined(ARCH_CPU_ARM_FAMILY) 494 #if defined(ARCH_CPU_ARM_FAMILY)
495 FilePath geminfo_file("/var/run/debugfs_gpu/exynos_gem_objects"); 495 FilePath geminfo_file("/var/run/debugfs_gpu/exynos_gem_objects");
496 #else 496 #else
497 FilePath geminfo_file("/var/run/debugfs_gpu/i915_gem_objects"); 497 FilePath geminfo_file("/var/run/debugfs_gpu/i915_gem_objects");
498 #endif 498 #endif
499 std::string geminfo_data; 499 std::string geminfo_data;
500 meminfo->gem_objects = -1; 500 meminfo->gem_objects = -1;
501 meminfo->gem_size = -1; 501 meminfo->gem_size = -1;
502 if (file_util::ReadFileToString(geminfo_file, &geminfo_data)) { 502 if (ReadFileToString(geminfo_file, &geminfo_data)) {
503 int gem_objects = -1; 503 int gem_objects = -1;
504 long long gem_size = -1; 504 long long gem_size = -1;
505 int num_res = sscanf(geminfo_data.c_str(), 505 int num_res = sscanf(geminfo_data.c_str(),
506 "%d objects, %lld bytes", 506 "%d objects, %lld bytes",
507 &gem_objects, &gem_size); 507 &gem_objects, &gem_size);
508 if (num_res == 2) { 508 if (num_res == 2) {
509 meminfo->gem_objects = gem_objects; 509 meminfo->gem_objects = gem_objects;
510 meminfo->gem_size = gem_size; 510 meminfo->gem_size = gem_size;
511 } 511 }
512 } 512 }
513 513
514 #if defined(ARCH_CPU_ARM_FAMILY) 514 #if defined(ARCH_CPU_ARM_FAMILY)
515 // Incorporate Mali graphics memory if present. 515 // Incorporate Mali graphics memory if present.
516 FilePath mali_memory_file("/sys/devices/platform/mali.0/memory"); 516 FilePath mali_memory_file("/sys/devices/platform/mali.0/memory");
517 std::string mali_memory_data; 517 std::string mali_memory_data;
518 if (file_util::ReadFileToString(mali_memory_file, &mali_memory_data)) { 518 if (ReadFileToString(mali_memory_file, &mali_memory_data)) {
519 long long mali_size = -1; 519 long long mali_size = -1;
520 int num_res = sscanf(mali_memory_data.c_str(), "%lld bytes", &mali_size); 520 int num_res = sscanf(mali_memory_data.c_str(), "%lld bytes", &mali_size);
521 if (num_res == 1) 521 if (num_res == 1)
522 meminfo->gem_size += mali_size; 522 meminfo->gem_size += mali_size;
523 } 523 }
524 #endif // defined(ARCH_CPU_ARM_FAMILY) 524 #endif // defined(ARCH_CPU_ARM_FAMILY)
525 #endif // defined(OS_CHROMEOS) 525 #endif // defined(OS_CHROMEOS)
526 526
527 return true; 527 return true;
528 } 528 }
(...skipping 19 matching lines...) Expand all
548 swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads")); 548 swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads"));
549 swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes")); 549 swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes"));
550 swap_info->compr_data_size = 550 swap_info->compr_data_size =
551 ReadFileToUint64(zram_path.Append("compr_data_size")); 551 ReadFileToUint64(zram_path.Append("compr_data_size"));
552 swap_info->mem_used_total = 552 swap_info->mem_used_total =
553 ReadFileToUint64(zram_path.Append("mem_used_total")); 553 ReadFileToUint64(zram_path.Append("mem_used_total"));
554 } 554 }
555 #endif // defined(OS_CHROMEOS) 555 #endif // defined(OS_CHROMEOS)
556 556
557 } // namespace base 557 } // namespace base
OLDNEW
« no previous file with comments | « base/process/process_linux.cc ('k') | base/sys_info_chromeos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698