OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_util.h" | 5 #include "base/process_util.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <malloc.h> | 8 #include <malloc.h> |
9 #include <sys/time.h> | 9 #include <sys/time.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
(...skipping 30 matching lines...) Expand all Loading... |
41 VM_COMM = 1, // Filename of executable, without parentheses. | 41 VM_COMM = 1, // Filename of executable, without parentheses. |
42 VM_STATE = 2, // Letter indicating the state of the process. | 42 VM_STATE = 2, // Letter indicating the state of the process. |
43 VM_PPID = 3, // PID of the parent. | 43 VM_PPID = 3, // PID of the parent. |
44 VM_PGRP = 4, // Process group id. | 44 VM_PGRP = 4, // Process group id. |
45 VM_UTIME = 13, // Time scheduled in user mode in clock ticks. | 45 VM_UTIME = 13, // Time scheduled in user mode in clock ticks. |
46 VM_STIME = 14, // Time scheduled in kernel mode in clock ticks. | 46 VM_STIME = 14, // Time scheduled in kernel mode in clock ticks. |
47 VM_VSIZE = 22, // Virtual memory size in bytes. | 47 VM_VSIZE = 22, // Virtual memory size in bytes. |
48 VM_RSS = 23, // Resident Set Size in pages. | 48 VM_RSS = 23, // Resident Set Size in pages. |
49 }; | 49 }; |
50 | 50 |
51 // Reads /proc/<pid>/stat into |buffer|. Returns true if successful. | 51 // Reads /proc/<pid>/stat into |buffer|. Returns true if the file can be read |
| 52 // and is non-empty. |
52 bool ReadProcStats(pid_t pid, std::string* buffer) { | 53 bool ReadProcStats(pid_t pid, std::string* buffer) { |
53 buffer->clear(); | 54 buffer->clear(); |
54 // Synchronously reading files in /proc is safe. | 55 // Synchronously reading files in /proc is safe. |
55 base::ThreadRestrictions::ScopedAllowIO allow_io; | 56 base::ThreadRestrictions::ScopedAllowIO allow_io; |
56 | 57 |
57 FilePath stat_file = GetProcPidDir(pid).Append(kStatFile); | 58 FilePath stat_file = GetProcPidDir(pid).Append(kStatFile); |
58 if (!file_util::ReadFileToString(stat_file, buffer)) { | 59 if (!file_util::ReadFileToString(stat_file, buffer)) { |
59 DLOG(WARNING) << "Failed to get process stats."; | 60 DLOG(WARNING) << "Failed to get process stats."; |
60 return false; | 61 return false; |
61 } | 62 } |
62 return true; | 63 return !buffer->empty(); |
63 } | 64 } |
64 | 65 |
65 // Takes |stats_data| and populates |proc_stats| with the values split by | 66 // Takes |stats_data| and populates |proc_stats| with the values split by |
66 // spaces. Taking into account the 2nd field may, in itself, contain spaces. | 67 // spaces. Taking into account the 2nd field may, in itself, contain spaces. |
67 // Returns true if successful. | 68 // Returns true if successful. |
68 bool ParseProcStats(const std::string& stats_data, | 69 bool ParseProcStats(const std::string& stats_data, |
69 std::vector<std::string>* proc_stats) { | 70 std::vector<std::string>* proc_stats) { |
70 // The stat file is formatted as: | 71 // The stat file is formatted as: |
71 // pid (process name) data1 data2 .... dataN | 72 // pid (process name) data1 data2 .... dataN |
72 // Look for the closing paren by scanning backwards, to avoid being fooled by | 73 // Look for the closing paren by scanning backwards, to avoid being fooled by |
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
818 int score_len = static_cast<int>(score_str.length()); | 819 int score_len = static_cast<int>(score_str.length()); |
819 return (score_len == file_util::WriteFile(oom_file, | 820 return (score_len == file_util::WriteFile(oom_file, |
820 score_str.c_str(), | 821 score_str.c_str(), |
821 score_len)); | 822 score_len)); |
822 } | 823 } |
823 | 824 |
824 return false; | 825 return false; |
825 } | 826 } |
826 | 827 |
827 } // namespace base | 828 } // namespace base |
OLD | NEW |