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

Side by Side Diff: base/linux_util.cc

Issue 2334533002: base: Move renderer threads to the appropriate cpuset. (Closed)
Patch Set: v3 Created 4 years, 3 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 unified diff | Download patch
OLDNEW
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 #include "base/linux_util.h" 5 #include "base/linux_util.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <limits.h> 10 #include <limits.h>
11 #include <stdlib.h> 11 #include <stdlib.h>
12 #include <sys/stat.h> 12 #include <sys/stat.h>
13 #include <sys/types.h> 13 #include <sys/types.h>
14 #include <unistd.h> 14 #include <unistd.h>
15 15
16 #include <memory> 16 #include <memory>
17 #include <vector> 17 #include <vector>
18 18
19 #include "base/command_line.h" 19 #include "base/command_line.h"
20 #include "base/files/file_util.h" 20 #include "base/files/file_util.h"
21 #include "base/memory/singleton.h" 21 #include "base/memory/singleton.h"
22 #include "base/process/launch.h" 22 #include "base/process/launch.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h" 25 #include "base/strings/string_util.h"
24 #include "base/synchronization/lock.h" 26 #include "base/synchronization/lock.h"
25 #include "build/build_config.h" 27 #include "build/build_config.h"
26 28
27 namespace { 29 namespace {
28 30
29 // Not needed for OS_CHROMEOS. 31 // Not needed for OS_CHROMEOS.
30 #if defined(OS_LINUX) 32 #if defined(OS_LINUX)
31 enum LinuxDistroState { 33 enum LinuxDistroState {
32 STATE_DID_NOT_CHECK = 0, 34 STATE_DID_NOT_CHECK = 0,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 DCHECK_EQ(STATE_CHECK_STARTED, state_); 67 DCHECK_EQ(STATE_CHECK_STARTED, state_);
66 state_ = STATE_CHECK_FINISHED; 68 state_ = STATE_CHECK_FINISHED;
67 } 69 }
68 70
69 private: 71 private:
70 base::Lock lock_; 72 base::Lock lock_;
71 LinuxDistroState state_; 73 LinuxDistroState state_;
72 }; 74 };
73 #endif // if defined(OS_LINUX) 75 #endif // if defined(OS_LINUX)
74 76
77 bool GetTasksForProcess(pid_t pid, std::vector<pid_t>* tids) {
78 char buf[256];
79 snprintf(buf, sizeof(buf), "/proc/%d/task", pid);
80
81 DIR* task = opendir(buf);
82 if (!task) {
83 DLOG(WARNING) << "Cannot open " << buf;
84 return false;
85 }
86
87 struct dirent* dent;
88 while ((dent = readdir(task))) {
89 char* endptr;
90 const unsigned long int tid_ul = strtoul(dent->d_name, &endptr, 10);
91 if (tid_ul == ULONG_MAX || *endptr)
92 continue;
93 tids->push_back(tid_ul);
94 }
95 closedir(task);
96 return true;
97 }
98
75 } // namespace 99 } // namespace
76 100
77 namespace base { 101 namespace base {
78 102
79 // Account for the terminating null character. 103 // Account for the terminating null character.
80 static const int kDistroSize = 128 + 1; 104 static const int kDistroSize = 128 + 1;
81 105
82 // We use this static string to hold the Linux distro info. If we 106 // We use this static string to hold the Linux distro info. If we
83 // crash, the crash handler code will send this in the crash dump. 107 // crash, the crash handler code will send this in the crash dump.
84 char g_linux_distro[kDistroSize] = 108 char g_linux_distro[kDistroSize] =
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 } 149 }
126 150
127 void SetLinuxDistro(const std::string& distro) { 151 void SetLinuxDistro(const std::string& distro) {
128 std::string trimmed_distro; 152 std::string trimmed_distro;
129 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro); 153 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro);
130 strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize); 154 strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
131 } 155 }
132 156
133 pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data, 157 pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
134 bool* syscall_supported) { 158 bool* syscall_supported) {
135 char buf[256];
136 snprintf(buf, sizeof(buf), "/proc/%d/task", pid);
137
138 if (syscall_supported != NULL) 159 if (syscall_supported != NULL)
139 *syscall_supported = false; 160 *syscall_supported = false;
140 161
141 DIR* task = opendir(buf); 162 std::vector<pid_t> tids;
142 if (!task) { 163 if (!GetTasksForProcess(pid, &tids))
143 DLOG(WARNING) << "Cannot open " << buf;
144 return -1; 164 return -1;
145 }
146
147 std::vector<pid_t> tids;
148 struct dirent* dent;
149 while ((dent = readdir(task))) {
150 char* endptr;
151 const unsigned long int tid_ul = strtoul(dent->d_name, &endptr, 10);
152 if (tid_ul == ULONG_MAX || *endptr)
153 continue;
154 tids.push_back(tid_ul);
155 }
156 closedir(task);
157 165
158 std::unique_ptr<char[]> syscall_data(new char[expected_data.length()]); 166 std::unique_ptr<char[]> syscall_data(new char[expected_data.length()]);
159 for (std::vector<pid_t>::const_iterator 167 for (std::vector<pid_t>::const_iterator
160 i = tids.begin(); i != tids.end(); ++i) { 168 i = tids.begin(); i != tids.end(); ++i) {
Avi (use Gerrit) 2016/09/16 22:26:29 for (pid_t tid : tids)
reveman 2016/09/16 23:06:41 Done
161 const pid_t current_tid = *i; 169 const pid_t current_tid = *i;
170 char buf[256];
162 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, current_tid); 171 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, current_tid);
163 int fd = open(buf, O_RDONLY); 172 int fd = open(buf, O_RDONLY);
164 if (fd < 0) 173 if (fd < 0)
165 continue; 174 continue;
166 if (syscall_supported != NULL) 175 if (syscall_supported != NULL)
167 *syscall_supported = true; 176 *syscall_supported = true;
168 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length()); 177 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length());
169 close(fd); 178 close(fd);
170 if (!read_ret) 179 if (!read_ret)
171 continue; 180 continue;
172 181
173 if (0 == strncmp(expected_data.c_str(), syscall_data.get(), 182 if (0 == strncmp(expected_data.c_str(), syscall_data.get(),
174 expected_data.length())) { 183 expected_data.length())) {
175 return current_tid; 184 return current_tid;
176 } 185 }
177 } 186 }
178 return -1; 187 return -1;
179 } 188 }
180 189
190 pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) {
191 if (ns_pid_supported)
192 *ns_pid_supported = false;
193
194 std::vector<pid_t> tids;
195 if (!GetTasksForProcess(pid, &tids))
196 return -1;
197
198 for (std::vector<pid_t>::const_iterator i = tids.begin(); i != tids.end();
199 ++i) {
Avi (use Gerrit) 2016/09/16 22:26:29 for (pid_t tid : tids)
reveman 2016/09/16 23:06:41 Done
200 const pid_t current_tid = *i;
201 char buf[256];
202 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/status", pid, current_tid);
203 std::string status;
204 if (!ReadFileToString(FilePath(buf), &status))
205 return -1;
206 StringPairs pairs;
207 SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs);
208 for (const auto& pair : pairs) {
209 const std::string& key = pair.first;
210 const std::string& value_str = pair.second;
211 if (key == "NSpid") {
212 if (ns_pid_supported)
213 *ns_pid_supported = true;
214 std::vector<StringPiece> split_value_str = SplitStringPiece(
215 value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
216 DCHECK_NE(split_value_str.size(), 0u);
217 int value;
218 // The last value in the list is the PID in the namespace.
219 if (StringToInt(split_value_str.back(), &value) && value == ns_tid) {
220 // The first value in the list is the real PID.
221 if (StringToInt(split_value_str.front(), &value))
222 return value;
223 }
224 }
225 }
226 }
227 return -1;
228 }
229
181 } // namespace base 230 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698