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 // The following is the C version of code from base/process_utils_linux.cc. | 5 // The following is the C version of code from base/process_utils_linux.cc. |
6 // We shouldn't link against C++ code in a setuid binary. | 6 // We shouldn't link against C++ code in a setuid binary. |
7 | 7 |
8 #define _GNU_SOURCE // needed for O_DIRECTORY | 8 #define _GNU_SOURCE // needed for O_DIRECTORY |
9 | 9 |
10 #include "process_util.h" | 10 #include "process_util.h" |
11 | 11 |
12 #include <fcntl.h> | 12 #include <fcntl.h> |
13 #include <inttypes.h> | 13 #include <inttypes.h> |
14 #include <limits.h> | 14 #include <limits.h> |
15 #include <stdio.h> | 15 #include <stdio.h> |
16 #include <stdlib.h> | 16 #include <stdlib.h> |
17 #include <string.h> | 17 #include <string.h> |
18 #include <sys/stat.h> | 18 #include <sys/stat.h> |
19 #include <sys/types.h> | 19 #include <sys/types.h> |
20 #include <unistd.h> | 20 #include <unistd.h> |
21 | 21 |
22 // Ranges for the current (oom_score_adj) and previous (oom_adj) | 22 // Ranges for the current (oom_score_adj) and previous (oom_adj) |
23 // flavors of OOM score. | 23 // flavors of OOM score. |
24 static const int kMaxOomScore = 1000; | 24 static const int kMaxOomScore = 1000; |
25 static const int kMaxOldOomScore = 15; | 25 static const int kMaxOldOomScore = 15; |
26 | 26 |
27 // Kernel pseudo-file that allows setting of the low memory margin. | |
28 static const char kLowMemMarginFile[] = | |
29 "/sys/kernel/mm/chromeos-low_mem/margin"; | |
30 | |
31 // NOTE: This is not the only version of this function in the source: | 27 // NOTE: This is not the only version of this function in the source: |
32 // the base library (in process_util_linux.cc) also has its own C++ version. | 28 // the base library (in process_util_linux.cc) also has its own C++ version. |
33 bool AdjustOOMScore(pid_t process, int score) { | 29 bool AdjustOOMScore(pid_t process, int score) { |
34 if (score < 0 || score > kMaxOomScore) | 30 if (score < 0 || score > kMaxOomScore) |
35 return false; | 31 return false; |
36 | 32 |
37 char oom_adj[27]; // "/proc/" + log_10(2**64) + "\0" | 33 char oom_adj[27]; // "/proc/" + log_10(2**64) + "\0" |
38 // 6 + 20 + 1 = 27 | 34 // 6 + 20 + 1 = 27 |
39 snprintf(oom_adj, sizeof(oom_adj), "/proc/%" PRIdMAX, (intmax_t)process); | 35 snprintf(oom_adj, sizeof(oom_adj), "/proc/%" PRIdMAX, (intmax_t)process); |
40 | 36 |
(...skipping 29 matching lines...) Expand all Loading... |
70 close(dirfd); | 66 close(dirfd); |
71 | 67 |
72 char buf[11]; // 0 <= |score| <= kMaxOomScore; using log_10(2**32) + 1 size | 68 char buf[11]; // 0 <= |score| <= kMaxOomScore; using log_10(2**32) + 1 size |
73 snprintf(buf, sizeof(buf), "%d", score); | 69 snprintf(buf, sizeof(buf), "%d", score); |
74 size_t len = strlen(buf); | 70 size_t len = strlen(buf); |
75 | 71 |
76 ssize_t bytes_written = write(fd, buf, len); | 72 ssize_t bytes_written = write(fd, buf, len); |
77 close(fd); | 73 close(fd); |
78 return (bytes_written == len); | 74 return (bytes_written == len); |
79 } | 75 } |
80 | |
81 bool AdjustLowMemoryMargin(int64_t margin_mb) { | |
82 int file_descriptor = open(kLowMemMarginFile, O_WRONLY); | |
83 if (file_descriptor < 0) | |
84 return false; | |
85 | |
86 // Only allow those values which are reasonable, to prevent mischief. | |
87 char value[21]; | |
88 switch (margin_mb) { | |
89 case -1L: | |
90 snprintf(value, sizeof(value), "off"); | |
91 break; | |
92 case 0L: | |
93 case 25L: | |
94 case 50L: | |
95 case 100L: | |
96 case 200L: | |
97 snprintf(value, sizeof(value), "%lld", (long long int)margin_mb); | |
98 break; | |
99 default: | |
100 return false; | |
101 } | |
102 | |
103 bool success = (write(file_descriptor, value, strlen(value)) >= 0); | |
104 close(file_descriptor); | |
105 return success; | |
106 } | |
OLD | NEW |