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

Unified Diff: base/process/process_metrics_linux.cc

Issue 1384363002: [NOT TO COMMIT YET] Using proc/pid/status file contents to get process metrics in linux. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@smaps_fscan
Patch Set: Renames and fixes. Created 5 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: base/process/process_metrics_linux.cc
diff --git a/base/process/process_metrics_linux.cc b/base/process/process_metrics_linux.cc
index adca7c5ee23bd53914342cffd2aab166402fe920..8b87aa772832bc03045a76ab62e0a718dc730285 100644
--- a/base/process/process_metrics_linux.cc
+++ b/base/process/process_metrics_linux.cc
@@ -48,20 +48,12 @@ static uint64 ReadFileToUint64(const FilePath file) {
}
#endif
-// Read /proc/<pid>/status and return the value for |field|, or 0 on failure.
-// Only works for fields in the form of "Field: value kB".
-size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) {
- std::string status;
- {
- // Synchronously reading files in /proc does not hit the disk.
- ThreadRestrictions::ScopedAllowIO allow_io;
- FilePath stat_file = internal::GetProcPidDir(pid).Append("status");
- if (!ReadFileToString(stat_file, &status))
- return 0;
- }
-
+// Parse the contents of status file and return the value of the field
+// requested.
+size_t ParseProcStatsAndGetFieldAsSizeT(const char* status_contents,
Lei Zhang 2015/10/06 21:09:00 Just use StringPiece instead of const char* ? Then
ssid 2015/10/07 09:12:15 changed to string
+ const std::string& field) {
StringPairs pairs;
- SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs);
+ SplitStringIntoKeyValuePairs(status_contents, ':', '\n', &pairs);
TrimKeyValuePairs(&pairs);
for (size_t i = 0; i < pairs.size(); ++i) {
const std::string& key = pairs[i].first;
@@ -85,6 +77,35 @@ size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) {
return 0;
}
+// Read /proc/<pid>/status and return the value for |field|, or 0 on failure.
+// Only works for fields in the form of "Field: value kB".
+size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) {
+ std::string status;
+ {
+ // Synchronously reading files in /proc does not hit the disk.
+ ThreadRestrictions::ScopedAllowIO allow_io;
+ FilePath stat_file = internal::GetProcPidDir(pid).Append("status");
+ if (!ReadFileToString(stat_file, &status))
+ return 0;
+ }
+ return ParseProcStatsAndGetFieldAsSizeT(status.c_str(), field);
+}
+
+// Read file descriptor passed file descritor of /proc/<pid>/status and return
+// the value for |field|, or 0 on failure.
+// Only works for fields in the form of "Field: value kB".
+size_t ReadProcStatusFdAndGetFieldAsSizeT(PlatformFile proc_status_fd,
Lei Zhang 2015/10/06 21:09:00 /proc/<pid>/status isn't that big, right? Why both
ssid 2015/10/07 09:12:14 yes, I moved the reading part to other file and no
+ const std::string& field) {
+ const size_t kMaxStatFileSize = 1 << 12;
Lei Zhang 2015/10/06 21:09:00 Easier to just write 4096?
ssid 2015/10/07 09:12:15 Done.
+ char status_data[kMaxStatFileSize];
+ status_data[0] = '\0';
+ lseek(proc_status_fd, 0, SEEK_SET);
Lei Zhang 2015/10/06 21:09:00 Check return value?
ssid 2015/10/07 09:12:15 Done.
+ ReadFromFD(proc_status_fd, status_data, kMaxStatFileSize);
Lei Zhang 2015/10/06 21:09:00 Ditto, check this return value in place of the nex
ssid 2015/10/07 09:12:14 Done.
+ if (strlen(status_data) == 0)
+ return 0;
+ return ParseProcStatsAndGetFieldAsSizeT(status_data, field);
+}
+
#if defined(OS_LINUX)
// Read /proc/<pid>/sched and look for |field|. On succes, return true and
// write the value for |field| into |result|.
@@ -393,6 +414,14 @@ bool ProcessMetrics::GetWorkingSetKBytesStatm(WorkingSetKBytes* ws_usage)
return ret;
}
+size_t ProcessMetrics::GetWorkingSetSize(int proc_status_fd) const {
+ return ReadProcStatusFdAndGetFieldAsSizeT(proc_status_fd, "VmRSS") * 1024;
+}
+
+size_t ProcessMetrics::GetPeakWorkingSetSize(int proc_status_fd) const {
+ return ReadProcStatusFdAndGetFieldAsSizeT(proc_status_fd, "VmHWM") * 1024;
+}
+
size_t GetSystemCommitCharge() {
SystemMemoryInfoKB meminfo;
if (!GetSystemMemoryInfo(&meminfo))

Powered by Google App Engine
This is Rietveld 408576698