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 683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 // Android doesn't support setting a new handler. | 727 // Android doesn't support setting a new handler. |
724 DLOG(WARNING) << "Not feasible."; | 728 DLOG(WARNING) << "Not feasible."; |
725 #else | 729 #else |
726 // Set the new-out of memory handler. | 730 // Set the new-out of memory handler. |
727 std::set_new_handler(&OnNoMemory); | 731 std::set_new_handler(&OnNoMemory); |
728 // If we're using glibc's allocator, the above functions will override | 732 // If we're using glibc's allocator, the above functions will override |
729 // malloc and friends and make them die on out of memory. | 733 // malloc and friends and make them die on out of memory. |
730 #endif | 734 #endif |
731 } | 735 } |
732 | 736 |
| 737 // NOTE: This is not the only version of this function in the source: |
| 738 // the setuid sandbox (in process_util_linux.c, in the sandbox source) |
| 739 // also has it's own C version. |
733 bool AdjustOOMScore(ProcessId process, int score) { | 740 bool AdjustOOMScore(ProcessId process, int score) { |
734 if (score < 0 || score > 15) | 741 if (score < 0 || score > kMaxOomScore) |
735 return false; | 742 return false; |
736 | 743 |
737 FilePath oom_adj("/proc"); | 744 FilePath oom_path("/proc"); |
738 oom_adj = oom_adj.Append(base::Int64ToString(process)); | 745 oom_path = oom_path.Append(base::Int64ToString(process)); |
739 oom_adj = oom_adj.AppendASCII("oom_adj"); | |
740 | 746 |
741 if (!file_util::PathExists(oom_adj)) | 747 // Attempt to write the newer oom_score_adj file first. |
742 return false; | 748 FilePath oom_file = oom_path.AppendASCII("oom_score_adj"); |
| 749 if (file_util::PathExists(oom_file)) { |
| 750 std::string score_str = base::IntToString(score); |
| 751 VLOG(1) << "Adjusting oom_score_adj of " << process << " to " << score_str; |
| 752 int score_len = static_cast<int>(score_str.length()); |
| 753 return (score_len == file_util::WriteFile(oom_file, |
| 754 score_str.c_str(), |
| 755 score_len)); |
| 756 } |
743 | 757 |
744 std::string score_str = base::IntToString(score); | 758 // If the oom_score_adj file doesn't exist, then we write the old |
745 return (static_cast<int>(score_str.length()) == | 759 // style file and translate the oom_adj score to the range 0-15. |
746 file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); | 760 oom_file = oom_path.AppendASCII("oom_adj"); |
| 761 if (file_util::PathExists(oom_file)) { |
| 762 std::string score_str = base::IntToString( |
| 763 score * kMaxOldOomScore / kMaxOomScore); |
| 764 VLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str; |
| 765 int score_len = static_cast<int>(score_str.length()); |
| 766 return (score_len == file_util::WriteFile(oom_file, |
| 767 score_str.c_str(), |
| 768 score_len)); |
| 769 } |
| 770 |
| 771 return false; |
747 } | 772 } |
748 | 773 |
749 } // namespace base | 774 } // namespace base |
OLD | NEW |