OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <ctype.h> | 7 #include <ctype.h> |
8 #include <dirent.h> | 8 #include <dirent.h> |
9 #include <dlfcn.h> | 9 #include <dlfcn.h> |
10 #include <errno.h> | 10 #include <errno.h> |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 } | 312 } |
313 | 313 |
314 // Private and Shared working set sizes are obtained from /proc/<pid>/smaps. | 314 // Private and Shared working set sizes are obtained from /proc/<pid>/smaps. |
315 // When that's not available, use the values from /proc<pid>/statm as a | 315 // When that's not available, use the values from /proc<pid>/statm as a |
316 // close approximation. | 316 // close approximation. |
317 // See http://www.pixelbeat.org/scripts/ps_mem.py | 317 // See http://www.pixelbeat.org/scripts/ps_mem.py |
318 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | 318 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { |
319 // Synchronously reading files in /proc is safe. | 319 // Synchronously reading files in /proc is safe. |
320 base::ThreadRestrictions::ScopedAllowIO allow_io; | 320 base::ThreadRestrictions::ScopedAllowIO allow_io; |
321 | 321 |
322 FilePath stat_file = | 322 FilePath proc_dir = FilePath("/proc").Append(base::IntToString(process_)); |
323 FilePath("/proc").Append(base::IntToString(process_)).Append("smaps"); | |
324 std::string smaps; | 323 std::string smaps; |
325 int private_kb = 0; | 324 int private_kb = 0; |
326 int pss_kb = 0; | 325 int pss_kb = 0; |
327 bool have_pss = false; | 326 bool have_pss = false; |
328 if (file_util::ReadFileToString(stat_file, &smaps) && smaps.length() > 0) { | 327 bool ret; |
| 328 |
| 329 { |
| 330 FilePath smaps_file = proc_dir.Append("smaps"); |
| 331 // Synchronously reading files in /proc is safe. |
| 332 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 333 ret = file_util::ReadFileToString(smaps_file, &smaps); |
| 334 } |
| 335 if (ret && smaps.length() > 0) { |
329 const std::string private_prefix = "Private_"; | 336 const std::string private_prefix = "Private_"; |
330 const std::string pss_prefix = "Pss"; | 337 const std::string pss_prefix = "Pss"; |
331 StringTokenizer tokenizer(smaps, ":\n"); | 338 StringTokenizer tokenizer(smaps, ":\n"); |
332 StringPiece last_key_name; | 339 StringPiece last_key_name; |
333 ParsingState state = KEY_NAME; | 340 ParsingState state = KEY_NAME; |
334 while (tokenizer.GetNext()) { | 341 while (tokenizer.GetNext()) { |
335 switch (state) { | 342 switch (state) { |
336 case KEY_NAME: | 343 case KEY_NAME: |
337 last_key_name = tokenizer.token_piece(); | 344 last_key_name = tokenizer.token_piece(); |
338 state = KEY_VALUE; | 345 state = KEY_VALUE; |
(...skipping 17 matching lines...) Expand all Loading... |
356 break; | 363 break; |
357 } | 364 } |
358 } | 365 } |
359 } else { | 366 } else { |
360 // Try statm if smaps is empty because of the SUID sandbox. | 367 // Try statm if smaps is empty because of the SUID sandbox. |
361 // First we need to get the page size though. | 368 // First we need to get the page size though. |
362 int page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; | 369 int page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; |
363 if (page_size_kb <= 0) | 370 if (page_size_kb <= 0) |
364 return false; | 371 return false; |
365 | 372 |
366 stat_file = | |
367 FilePath("/proc").Append(base::IntToString(process_)).Append("statm"); | |
368 std::string statm; | 373 std::string statm; |
369 if (!file_util::ReadFileToString(stat_file, &statm) || statm.length() == 0) | 374 { |
| 375 FilePath statm_file = proc_dir.Append("statm"); |
| 376 // Synchronously reading files in /proc is safe. |
| 377 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 378 ret = file_util::ReadFileToString(statm_file, &statm); |
| 379 } |
| 380 if (!ret || statm.length() == 0) |
370 return false; | 381 return false; |
371 | 382 |
372 std::vector<std::string> statm_vec; | 383 std::vector<std::string> statm_vec; |
373 base::SplitString(statm, ' ', &statm_vec); | 384 base::SplitString(statm, ' ', &statm_vec); |
374 if (statm_vec.size() != 7) | 385 if (statm_vec.size() != 7) |
375 return false; // Not the format we expect. | 386 return false; // Not the format we expect. |
376 | 387 |
377 int statm1, statm2; | 388 int statm1, statm2; |
378 base::StringToInt(statm_vec[1], &statm1); | 389 base::StringToInt(statm_vec[1], &statm1); |
379 base::StringToInt(statm_vec[2], &statm2); | 390 base::StringToInt(statm_vec[2], &statm2); |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 | 707 |
697 if (!file_util::PathExists(oom_adj)) | 708 if (!file_util::PathExists(oom_adj)) |
698 return false; | 709 return false; |
699 | 710 |
700 std::string score_str = base::IntToString(score); | 711 std::string score_str = base::IntToString(score); |
701 return (static_cast<int>(score_str.length()) == | 712 return (static_cast<int>(score_str.length()) == |
702 file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); | 713 file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); |
703 } | 714 } |
704 | 715 |
705 } // namespace base | 716 } // namespace base |
OLD | NEW |