| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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> |
| 11 #include <fcntl.h> | 11 #include <fcntl.h> |
| 12 #include <sys/time.h> | 12 #include <sys/time.h> |
| 13 #include <sys/types.h> | 13 #include <sys/types.h> |
| 14 #include <sys/wait.h> | 14 #include <sys/wait.h> |
| 15 #include <time.h> | 15 #include <time.h> |
| 16 #include <unistd.h> | 16 #include <unistd.h> |
| 17 | 17 |
| 18 #include "base/file_util.h" | 18 #include "base/file_util.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/stringprintf.h" | 20 #include "base/stringprintf.h" |
| 21 #include "base/string_number_conversions.h" | 21 #include "base/string_number_conversions.h" |
| 22 #include "base/string_split.h" | 22 #include "base/string_split.h" |
| 23 #include "base/string_tokenizer.h" | 23 #include "base/string_tokenizer.h" |
| 24 #include "base/string_util.h" | 24 #include "base/string_util.h" |
| 25 #include "base/sys_info.h" | 25 #include "base/sys_info.h" |
| 26 #include "base/threading/thread_restrictions.h" | 26 #include "base/threading/thread_restrictions.h" |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // Max score for the old oom_adj range. Used for conversion of new |
| 31 // values to old values. |
| 32 const int kMaxOldOomScore = 15; |
| 33 |
| 30 enum ParsingState { | 34 enum ParsingState { |
| 31 KEY_NAME, | 35 KEY_NAME, |
| 32 KEY_VALUE | 36 KEY_VALUE |
| 33 }; | 37 }; |
| 34 | 38 |
| 35 // Reads /proc/<pid>/stat and populates |proc_stats| with the values split by | 39 // Reads /proc/<pid>/stat and populates |proc_stats| with the values split by |
| 36 // spaces. Returns true if successful. | 40 // spaces. Returns true if successful. |
| 37 bool GetProcStats(pid_t pid, std::vector<std::string>* proc_stats) { | 41 bool GetProcStats(pid_t pid, std::vector<std::string>* proc_stats) { |
| 38 // Synchronously reading files in /proc is safe. | 42 // Synchronously reading files in /proc is safe. |
| 39 base::ThreadRestrictions::ScopedAllowIO allow_io; | 43 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| (...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 // Android doesn't support setting a new handler. | 731 // Android doesn't support setting a new handler. |
| 728 DLOG(WARNING) << "Not feasible."; | 732 DLOG(WARNING) << "Not feasible."; |
| 729 #else | 733 #else |
| 730 // Set the new-out of memory handler. | 734 // Set the new-out of memory handler. |
| 731 std::set_new_handler(&OnNoMemory); | 735 std::set_new_handler(&OnNoMemory); |
| 732 // If we're using glibc's allocator, the above functions will override | 736 // If we're using glibc's allocator, the above functions will override |
| 733 // malloc and friends and make them die on out of memory. | 737 // malloc and friends and make them die on out of memory. |
| 734 #endif | 738 #endif |
| 735 } | 739 } |
| 736 | 740 |
| 741 // NOTE: This is not the only version of this function in the source: |
| 742 // the setuid sandbox (in process_util_linux.c, in the sandbox source) |
| 743 // also has it's own C version. |
| 737 bool AdjustOOMScore(ProcessId process, int score) { | 744 bool AdjustOOMScore(ProcessId process, int score) { |
| 738 if (score < 0 || score > 15) | 745 if (score < 0 || score > kMaxOomScore) |
| 739 return false; | 746 return false; |
| 740 | 747 |
| 741 FilePath oom_adj("/proc"); | 748 FilePath oom_path("/proc"); |
| 742 oom_adj = oom_adj.Append(base::Int64ToString(process)); | 749 oom_path = oom_path.Append(base::Int64ToString(process)); |
| 743 oom_adj = oom_adj.AppendASCII("oom_adj"); | |
| 744 | 750 |
| 745 if (!file_util::PathExists(oom_adj)) | 751 // Attempt to write the newer oom_score_adj file first. |
| 746 return false; | 752 FilePath oom_file = oom_path.AppendASCII("oom_score_adj"); |
| 753 if (file_util::PathExists(oom_file)) { |
| 754 std::string score_str = base::IntToString(score); |
| 755 VLOG(1) << "Adjusting oom_score_adj of " << process << " to " << score_str; |
| 756 int score_len = static_cast<int>(score_str.length()); |
| 757 return (score_len == file_util::WriteFile(oom_file, |
| 758 score_str.c_str(), |
| 759 score_len)); |
| 760 } |
| 747 | 761 |
| 748 std::string score_str = base::IntToString(score); | 762 // If the oom_score_adj file doesn't exist, then we write the old |
| 749 return (static_cast<int>(score_str.length()) == | 763 // style file and translate the oom_adj score to the range 0-15. |
| 750 file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); | 764 oom_file = oom_path.AppendASCII("oom_adj"); |
| 765 if (file_util::PathExists(oom_file)) { |
| 766 std::string score_str = base::IntToString( |
| 767 score * kMaxOldOomScore / kMaxOomScore); |
| 768 VLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str; |
| 769 int score_len = static_cast<int>(score_str.length()); |
| 770 return (score_len == file_util::WriteFile(oom_file, |
| 771 score_str.c_str(), |
| 772 score_len)); |
| 773 } |
| 774 |
| 775 return false; |
| 751 } | 776 } |
| 752 | 777 |
| 753 } // namespace base | 778 } // namespace base |
| OLD | NEW |