| 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 #include "base/threading/platform_thread.h" | 5 #include "base/threading/platform_thread.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <pthread.h> | 8 #include <pthread.h> |
| 9 #include <sched.h> | 9 #include <sched.h> |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| 11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 #include <sys/resource.h> | 12 #include <sys/resource.h> |
| 13 #include <sys/time.h> | 13 #include <sys/time.h> |
| 14 #include <sys/types.h> | 14 #include <sys/types.h> |
| 15 #include <unistd.h> | 15 #include <unistd.h> |
| 16 | 16 |
| 17 #include <memory> | 17 #include <memory> |
| 18 | 18 |
| 19 #include "base/debug/activity_tracker.h" | 19 #include "base/debug/activity_tracker.h" |
| 20 #include "base/lazy_instance.h" | 20 #include "base/lazy_instance.h" |
| 21 #include "base/logging.h" | 21 #include "base/logging.h" |
| 22 #include "base/threading/platform_thread_internal_posix.h" | 22 #include "base/threading/platform_thread_internal_posix.h" |
| 23 #include "base/threading/thread_id_name_manager.h" | 23 #include "base/threading/thread_id_name_manager.h" |
| 24 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
| 25 #include "build/build_config.h" | 25 #include "build/build_config.h" |
| 26 | 26 |
| 27 #if defined(OS_LINUX) | 27 #if defined(OS_LINUX) |
| 28 #include "base/files/file_util.h" | |
| 29 #include "base/strings/string_number_conversions.h" | |
| 30 #include <sys/syscall.h> | 28 #include <sys/syscall.h> |
| 31 #endif | 29 #endif |
| 32 | 30 |
| 33 namespace base { | 31 namespace base { |
| 34 | 32 |
| 35 void InitThreading(); | 33 void InitThreading(); |
| 36 void TerminateOnThread(); | 34 void TerminateOnThread(); |
| 37 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes); | 35 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes); |
| 38 | 36 |
| 39 namespace { | 37 namespace { |
| 40 | 38 |
| 41 #if defined(OS_LINUX) | |
| 42 const FilePath::CharType kCpusetDirectory[] = | |
| 43 FILE_PATH_LITERAL("/sys/fs/cgroup/cpuset/chrome"); | |
| 44 #endif | |
| 45 | |
| 46 struct ThreadParams { | 39 struct ThreadParams { |
| 47 ThreadParams() | 40 ThreadParams() |
| 48 : delegate(NULL), joinable(false), priority(ThreadPriority::NORMAL) {} | 41 : delegate(NULL), joinable(false), priority(ThreadPriority::NORMAL) {} |
| 49 | 42 |
| 50 PlatformThread::Delegate* delegate; | 43 PlatformThread::Delegate* delegate; |
| 51 bool joinable; | 44 bool joinable; |
| 52 ThreadPriority priority; | 45 ThreadPriority priority; |
| 53 }; | 46 }; |
| 54 | 47 |
| 55 void* ThreadFunc(void* params) { | 48 void* ThreadFunc(void* params) { |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 return geteuid() == 0; | 240 return geteuid() == 0; |
| 248 #endif // defined(OS_NACL) | 241 #endif // defined(OS_NACL) |
| 249 } | 242 } |
| 250 | 243 |
| 251 // static | 244 // static |
| 252 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { | 245 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { |
| 253 #if defined(OS_NACL) | 246 #if defined(OS_NACL) |
| 254 NOTIMPLEMENTED(); | 247 NOTIMPLEMENTED(); |
| 255 #else | 248 #else |
| 256 #if defined(OS_LINUX) | 249 #if defined(OS_LINUX) |
| 257 // Move thread into the appropriate cpuset when chrome cpuset directory | 250 SetThreadAffinity(PlatformThread::CurrentId(), priority); |
| 258 // exists. Threads with BACKGROUND priority are move to the "non-urgent" | 251 #endif |
| 259 // cpuset and threads with DISPLAY and REALTIME_AUDIO priority are moved | |
| 260 // to "urgent" cpuset. | |
| 261 FilePath cpuset_filepath(kCpusetDirectory); | |
| 262 switch (priority) { | |
| 263 case ThreadPriority::NORMAL: | |
| 264 break; | |
| 265 case ThreadPriority::BACKGROUND: | |
| 266 cpuset_filepath = cpuset_filepath.Append(FILE_PATH_LITERAL("non-urgent")); | |
| 267 break; | |
| 268 case ThreadPriority::DISPLAY: | |
| 269 case ThreadPriority::REALTIME_AUDIO: | |
| 270 cpuset_filepath = cpuset_filepath.Append(FILE_PATH_LITERAL("urgent")); | |
| 271 break; | |
| 272 } | |
| 273 if (DirectoryExists(cpuset_filepath)) { | |
| 274 FilePath tasks_filepath = | |
| 275 cpuset_filepath.Append(FILE_PATH_LITERAL("tasks")); | |
| 276 std::string tid = IntToString(PlatformThread::CurrentId()); | |
| 277 int bytes_written = WriteFile(tasks_filepath, tid.c_str(), tid.size()); | |
| 278 if (bytes_written != static_cast<int>(tid.size())) { | |
| 279 DVLOG(1) << "Failed to add " << tid << " to " << tasks_filepath.value(); | |
| 280 } | |
| 281 } | |
| 282 #endif // defined(OS_LINUX) | |
| 283 | 252 |
| 284 if (internal::SetCurrentThreadPriorityForPlatform(priority)) | 253 if (internal::SetCurrentThreadPriorityForPlatform(priority)) |
| 285 return; | 254 return; |
| 286 | 255 |
| 287 // setpriority(2) should change the whole thread group's (i.e. process) | 256 // setpriority(2) should change the whole thread group's (i.e. process) |
| 288 // priority. However, as stated in the bugs section of | 257 // priority. However, as stated in the bugs section of |
| 289 // http://man7.org/linux/man-pages/man2/getpriority.2.html: "under the current | 258 // http://man7.org/linux/man-pages/man2/getpriority.2.html: "under the current |
| 290 // Linux/NPTL implementation of POSIX threads, the nice value is a per-thread | 259 // Linux/NPTL implementation of POSIX threads, the nice value is a per-thread |
| 291 // attribute". Also, 0 is prefered to the current thread id since it is | 260 // attribute". Also, 0 is prefered to the current thread id since it is |
| 292 // equivalent but makes sandboxing easier (https://crbug.com/399473). | 261 // equivalent but makes sandboxing easier (https://crbug.com/399473). |
| (...skipping 28 matching lines...) Expand all Loading... |
| 321 return ThreadPriority::NORMAL; | 290 return ThreadPriority::NORMAL; |
| 322 } | 291 } |
| 323 | 292 |
| 324 return internal::NiceValueToThreadPriority(nice_value); | 293 return internal::NiceValueToThreadPriority(nice_value); |
| 325 #endif // !defined(OS_NACL) | 294 #endif // !defined(OS_NACL) |
| 326 } | 295 } |
| 327 | 296 |
| 328 #endif // !defined(OS_MACOSX) | 297 #endif // !defined(OS_MACOSX) |
| 329 | 298 |
| 330 } // namespace base | 299 } // namespace base |
| OLD | NEW |