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 // This file/namespace contains utility functions for enumerating, ending and | 5 // This file/namespace contains utility functions for enumerating, ending and |
6 // computing statistics of processes. | 6 // computing statistics of processes. |
7 | 7 |
8 #ifndef BASE_PROCESS_UTIL_H_ | 8 #ifndef BASE_PROCESS_UTIL_H_ |
9 #define BASE_PROCESS_UTIL_H_ | 9 #define BASE_PROCESS_UTIL_H_ |
10 #pragma once | 10 #pragma once |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 166 |
167 #if defined(OS_LINUX) | 167 #if defined(OS_LINUX) |
168 // Returns the path to the executable of the given process. | 168 // Returns the path to the executable of the given process. |
169 BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); | 169 BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); |
170 | 170 |
171 // Parse the data found in /proc/<pid>/stat and return the sum of the | 171 // Parse the data found in /proc/<pid>/stat and return the sum of the |
172 // CPU-related ticks. Returns -1 on parse error. | 172 // CPU-related ticks. Returns -1 on parse error. |
173 // Exposed for testing. | 173 // Exposed for testing. |
174 BASE_EXPORT int ParseProcStatCPU(const std::string& input); | 174 BASE_EXPORT int ParseProcStatCPU(const std::string& input); |
175 | 175 |
176 static const char kAdjustOOMScoreSwitch[] = "--adjust-oom-score"; | 176 // The command line switch used for supplying the OOM adjustment score |
| 177 // to the setuid sandbox. |
| 178 extern const char kAdjustOOMScoreSwitch[]; |
177 | 179 |
178 // This adjusts /proc/process/oom_adj so the Linux OOM killer will prefer | 180 // The maximum allowed value for the OOM score. |
179 // certain process types over others. The range for the adjustment is | 181 const int kMaxOomScore = 1000; |
180 // [-17,15], with [0,15] being user accessible. | 182 |
| 183 // This adjusts /proc/<pid>/oom_score_adj so the Linux OOM killer will |
| 184 // prefer to kill certain process types over others. The range for the |
| 185 // adjustment is [-1000, 1000], with [0, 1000] being user accessible. |
| 186 // If the Linux system doesn't support the newer oom_score_adj range |
| 187 // of [0, 1000], then we revert to using the older oom_adj, and |
| 188 // translate the given value into [0, 15]. Some aliasing of values |
| 189 // may occur in that case, of course. |
181 BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score); | 190 BASE_EXPORT bool AdjustOOMScore(ProcessId process, int score); |
182 #endif | 191 #endif // defined(OS_LINUX) |
183 | 192 |
184 #if defined(OS_POSIX) | 193 #if defined(OS_POSIX) |
185 // Returns the ID for the parent of the given process. | 194 // Returns the ID for the parent of the given process. |
186 BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); | 195 BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); |
187 | 196 |
188 // Close all file descriptors, except those which are a destination in the | 197 // Close all file descriptors, except those which are a destination in the |
189 // given multimap. Only call this function in a child process where you know | 198 // given multimap. Only call this function in a child process where you know |
190 // that there aren't any other threads. | 199 // that there aren't any other threads. |
191 BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); | 200 BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
192 #endif | 201 #endif // defined(OS_POSIX) |
193 | 202 |
194 // TODO(evan): rename these to use StudlyCaps. | 203 // TODO(evan): rename these to use StudlyCaps. |
195 typedef std::vector<std::pair<std::string, std::string> > environment_vector; | 204 typedef std::vector<std::pair<std::string, std::string> > environment_vector; |
196 typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; | 205 typedef std::vector<std::pair<int, int> > file_handle_mapping_vector; |
197 | 206 |
198 // Options for launching a subprocess that are passed to LaunchProcess(). | 207 // Options for launching a subprocess that are passed to LaunchProcess(). |
199 // The default constructor constructs the object with default options. | 208 // The default constructor constructs the object with default options. |
200 struct LaunchOptions { | 209 struct LaunchOptions { |
201 LaunchOptions() : wait(false), | 210 LaunchOptions() : wait(false), |
202 #if defined(OS_WIN) | 211 #if defined(OS_WIN) |
203 start_hidden(false), inherit_handles(false), as_user(NULL), | 212 start_hidden(false), inherit_handles(false), as_user(NULL), |
204 empty_desktop_name(false) | 213 empty_desktop_name(false) |
205 #else | 214 #else |
206 environ(NULL), fds_to_remap(NULL), new_process_group(false), | 215 environ(NULL), fds_to_remap(NULL), new_process_group(false), |
207 clone_flags(0) | 216 clone_flags(0) |
208 #endif | 217 #endif // !defined(OS_WIN) |
209 {} | 218 {} |
210 | 219 |
211 // If true, wait for the process to complete. | 220 // If true, wait for the process to complete. |
212 bool wait; | 221 bool wait; |
213 | 222 |
214 #if defined(OS_WIN) | 223 #if defined(OS_WIN) |
215 bool start_hidden; | 224 bool start_hidden; |
216 | 225 |
217 // If true, the new process inherits handles from the parent. | 226 // If true, the new process inherits handles from the parent. |
218 bool inherit_handles; | 227 bool inherit_handles; |
(...skipping 22 matching lines...) Expand all Loading... |
241 // call to LaunchProcess(). | 250 // call to LaunchProcess(). |
242 const file_handle_mapping_vector* fds_to_remap; | 251 const file_handle_mapping_vector* fds_to_remap; |
243 | 252 |
244 // If true, start the process in a new process group, instead of | 253 // If true, start the process in a new process group, instead of |
245 // inheriting the parent's process group. The pgid of the child process | 254 // inheriting the parent's process group. The pgid of the child process |
246 // will be the same as its pid. | 255 // will be the same as its pid. |
247 bool new_process_group; | 256 bool new_process_group; |
248 | 257 |
249 // If non-zero, start the process using clone(), using flags as provided. | 258 // If non-zero, start the process using clone(), using flags as provided. |
250 int clone_flags; | 259 int clone_flags; |
251 #endif | 260 #endif // !defined(OS_WIN) |
252 }; | 261 }; |
253 | 262 |
254 // Launch a process via the command line |cmdline|. | 263 // Launch a process via the command line |cmdline|. |
255 // See the documentation of LaunchOptions for details on |options|. | 264 // See the documentation of LaunchOptions for details on |options|. |
256 // | 265 // |
257 // If |process_handle| is non-NULL, it will be filled in with the | 266 // If |process_handle| is non-NULL, it will be filled in with the |
258 // handle of the launched process. NOTE: In this case, the caller is | 267 // handle of the launched process. NOTE: In this case, the caller is |
259 // responsible for closing the handle so that it doesn't leak! | 268 // responsible for closing the handle so that it doesn't leak! |
260 // Otherwise, the process handle will be implicitly closed. | 269 // Otherwise, the process handle will be implicitly closed. |
261 // | 270 // |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 // for the command. | 337 // for the command. |
329 BASE_EXPORT bool GetAppOutputRestricted(const CommandLine& cl, | 338 BASE_EXPORT bool GetAppOutputRestricted(const CommandLine& cl, |
330 std::string* output, size_t max_output); | 339 std::string* output, size_t max_output); |
331 | 340 |
332 // A version of |GetAppOutput()| which also returns the exit code of the | 341 // A version of |GetAppOutput()| which also returns the exit code of the |
333 // executed command. Returns true if the application runs and exits cleanly. If | 342 // executed command. Returns true if the application runs and exits cleanly. If |
334 // this is the case the exit code of the application is available in | 343 // this is the case the exit code of the application is available in |
335 // |*exit_code|. | 344 // |*exit_code|. |
336 BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl, | 345 BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl, |
337 std::string* output, int* exit_code); | 346 std::string* output, int* exit_code); |
338 #endif | 347 #endif // defined(OS_POSIX) |
339 | 348 |
340 // Used to filter processes by process ID. | 349 // Used to filter processes by process ID. |
341 class ProcessFilter { | 350 class ProcessFilter { |
342 public: | 351 public: |
343 // Returns true to indicate set-inclusion and false otherwise. This method | 352 // Returns true to indicate set-inclusion and false otherwise. This method |
344 // should not have side-effects and should be idempotent. | 353 // should not have side-effects and should be idempotent. |
345 virtual bool Includes(const ProcessEntry& entry) const = 0; | 354 virtual bool Includes(const ProcessEntry& entry) const = 0; |
346 | 355 |
347 protected: | 356 protected: |
348 virtual ~ProcessFilter() {} | 357 virtual ~ProcessFilter() {} |
(...skipping 16 matching lines...) Expand all Loading... |
365 // Attempts to kill the process identified by the given process | 374 // Attempts to kill the process identified by the given process |
366 // entry structure, giving it the specified exit code. If |wait| is true, wait | 375 // entry structure, giving it the specified exit code. If |wait| is true, wait |
367 // for the process to be actually terminated before returning. | 376 // for the process to be actually terminated before returning. |
368 // Returns true if this is successful, false otherwise. | 377 // Returns true if this is successful, false otherwise. |
369 BASE_EXPORT bool KillProcess(ProcessHandle process, int exit_code, bool wait); | 378 BASE_EXPORT bool KillProcess(ProcessHandle process, int exit_code, bool wait); |
370 | 379 |
371 #if defined(OS_POSIX) | 380 #if defined(OS_POSIX) |
372 // Attempts to kill the process group identified by |process_group_id|. Returns | 381 // Attempts to kill the process group identified by |process_group_id|. Returns |
373 // true on success. | 382 // true on success. |
374 BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id); | 383 BASE_EXPORT bool KillProcessGroup(ProcessHandle process_group_id); |
375 #endif | 384 #endif // defined(OS_POSIX) |
376 | 385 |
377 #if defined(OS_WIN) | 386 #if defined(OS_WIN) |
378 BASE_EXPORT bool KillProcessById(ProcessId process_id, int exit_code, | 387 BASE_EXPORT bool KillProcessById(ProcessId process_id, int exit_code, |
379 bool wait); | 388 bool wait); |
380 #endif | 389 #endif // defined(OS_WIN) |
381 | 390 |
382 // Get the termination status of the process by interpreting the | 391 // Get the termination status of the process by interpreting the |
383 // circumstances of the child process' death. |exit_code| is set to | 392 // circumstances of the child process' death. |exit_code| is set to |
384 // the status returned by waitpid() on POSIX, and from | 393 // the status returned by waitpid() on POSIX, and from |
385 // GetExitCodeProcess() on Windows. |exit_code| may be NULL if the | 394 // GetExitCodeProcess() on Windows. |exit_code| may be NULL if the |
386 // caller is not interested in it. Note that on Linux, this function | 395 // caller is not interested in it. Note that on Linux, this function |
387 // will only return a useful result the first time it is called after | 396 // will only return a useful result the first time it is called after |
388 // the child exits (because it will reap the child and the information | 397 // the child exits (because it will reap the child and the information |
389 // will no longer be available). | 398 // will no longer be available). |
390 BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle, | 399 BASE_EXPORT TerminationStatus GetTerminationStatus(ProcessHandle handle, |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 // If IO information is retrieved successfully, the function returns true | 634 // If IO information is retrieved successfully, the function returns true |
626 // and fills in the IO_COUNTERS passed in. The function returns false | 635 // and fills in the IO_COUNTERS passed in. The function returns false |
627 // otherwise. | 636 // otherwise. |
628 bool GetIOCounters(IoCounters* io_counters) const; | 637 bool GetIOCounters(IoCounters* io_counters) const; |
629 | 638 |
630 private: | 639 private: |
631 #if !defined(OS_MACOSX) | 640 #if !defined(OS_MACOSX) |
632 explicit ProcessMetrics(ProcessHandle process); | 641 explicit ProcessMetrics(ProcessHandle process); |
633 #else | 642 #else |
634 ProcessMetrics(ProcessHandle process, PortProvider* port_provider); | 643 ProcessMetrics(ProcessHandle process, PortProvider* port_provider); |
635 #endif // !defined(OS_MACOSX) | 644 #endif // defined(OS_MACOSX) |
636 | 645 |
637 ProcessHandle process_; | 646 ProcessHandle process_; |
638 | 647 |
639 int processor_count_; | 648 int processor_count_; |
640 | 649 |
641 // Used to store the previous times and CPU usage counts so we can | 650 // Used to store the previous times and CPU usage counts so we can |
642 // compute the CPU usage between calls. | 651 // compute the CPU usage between calls. |
643 int64 last_time_; | 652 int64 last_time_; |
644 int64 last_system_time_; | 653 int64 last_system_time_; |
645 | 654 |
646 #if defined(OS_MACOSX) | 655 #if defined(OS_MACOSX) |
647 // Queries the port provider if it's set. | 656 // Queries the port provider if it's set. |
648 mach_port_t TaskForPid(ProcessHandle process) const; | 657 mach_port_t TaskForPid(ProcessHandle process) const; |
649 | 658 |
650 PortProvider* port_provider_; | 659 PortProvider* port_provider_; |
651 #elif defined(OS_POSIX) | 660 #elif defined(OS_POSIX) |
652 // Jiffie count at the last_time_ we updated. | 661 // Jiffie count at the last_time_ we updated. |
653 int last_cpu_; | 662 int last_cpu_; |
654 #endif // defined(OS_MACOSX) | 663 #endif // defined(OS_POSIX) |
655 | 664 |
656 DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); | 665 DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); |
657 }; | 666 }; |
658 | 667 |
659 #if defined(OS_LINUX) | 668 #if defined(OS_LINUX) |
660 // Data from /proc/meminfo about system-wide memory consumption. | 669 // Data from /proc/meminfo about system-wide memory consumption. |
661 // Values are in KB. | 670 // Values are in KB. |
662 struct SystemMemoryInfoKB { | 671 struct SystemMemoryInfoKB { |
663 SystemMemoryInfoKB() : total(0), free(0), buffers(0), cached(0), | 672 SystemMemoryInfoKB() : total(0), free(0), buffers(0), cached(0), |
664 active_anon(0), inactive_anon(0), shmem(0) {} | 673 active_anon(0), inactive_anon(0), shmem(0) {} |
665 int total; | 674 int total; |
666 int free; | 675 int free; |
667 int buffers; | 676 int buffers; |
668 int cached; | 677 int cached; |
669 int active_anon; | 678 int active_anon; |
670 int inactive_anon; | 679 int inactive_anon; |
671 int shmem; | 680 int shmem; |
672 }; | 681 }; |
673 // Retrieves data from /proc/meminfo about system-wide memory consumption. | 682 // Retrieves data from /proc/meminfo about system-wide memory consumption. |
674 // Fills in the provided |meminfo| structure. Returns true on success. | 683 // Fills in the provided |meminfo| structure. Returns true on success. |
675 // Exposed for memory debugging widget. | 684 // Exposed for memory debugging widget. |
676 BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo); | 685 BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo); |
677 #endif | 686 #endif // defined(OS_LINUX) |
678 | 687 |
679 // Returns the memory committed by the system in KBytes. | 688 // Returns the memory committed by the system in KBytes. |
680 // Returns 0 if it can't compute the commit charge. | 689 // Returns 0 if it can't compute the commit charge. |
681 BASE_EXPORT size_t GetSystemCommitCharge(); | 690 BASE_EXPORT size_t GetSystemCommitCharge(); |
682 | 691 |
683 // Enables low fragmentation heap (LFH) for every heaps of this process. This | 692 // Enables low fragmentation heap (LFH) for every heaps of this process. This |
684 // won't have any effect on heaps created after this function call. It will not | 693 // won't have any effect on heaps created after this function call. It will not |
685 // modify data allocated in the heaps before calling this function. So it is | 694 // modify data allocated in the heaps before calling this function. So it is |
686 // better to call this function early in initialization and again before | 695 // better to call this function early in initialization and again before |
687 // entering the main loop. | 696 // entering the main loop. |
688 // Note: Returns true on Windows 2000 without doing anything. | 697 // Note: Returns true on Windows 2000 without doing anything. |
689 BASE_EXPORT bool EnableLowFragmentationHeap(); | 698 BASE_EXPORT bool EnableLowFragmentationHeap(); |
690 | 699 |
691 // Enables 'terminate on heap corruption' flag. Helps protect against heap | 700 // Enables 'terminate on heap corruption' flag. Helps protect against heap |
692 // overflow. Has no effect if the OS doesn't provide the necessary facility. | 701 // overflow. Has no effect if the OS doesn't provide the necessary facility. |
693 BASE_EXPORT void EnableTerminationOnHeapCorruption(); | 702 BASE_EXPORT void EnableTerminationOnHeapCorruption(); |
694 | 703 |
695 #if !defined(OS_WIN) | 704 #if !defined(OS_WIN) |
696 // Turns on process termination if memory runs out. This is handled on Windows | 705 // Turns on process termination if memory runs out. This is handled on Windows |
697 // inside RegisterInvalidParamHandler(). | 706 // inside RegisterInvalidParamHandler(). |
698 BASE_EXPORT void EnableTerminationOnOutOfMemory(); | 707 BASE_EXPORT void EnableTerminationOnOutOfMemory(); |
699 #if defined(OS_MACOSX) | 708 #if defined(OS_MACOSX) |
700 // Exposed for testing. | 709 // Exposed for testing. |
701 BASE_EXPORT malloc_zone_t* GetPurgeableZone(); | 710 BASE_EXPORT malloc_zone_t* GetPurgeableZone(); |
702 #endif | 711 #endif // defined(OS_MACOSX) |
703 #endif | 712 #endif // !defined(OS_WIN) |
704 | 713 |
705 // Enables stack dump to console output on exception and signals. | 714 // Enables stack dump to console output on exception and signals. |
706 // When enabled, the process will quit immediately. This is meant to be used in | 715 // When enabled, the process will quit immediately. This is meant to be used in |
707 // unit_tests only! | 716 // unit_tests only! |
708 BASE_EXPORT bool EnableInProcessStackDumping(); | 717 BASE_EXPORT bool EnableInProcessStackDumping(); |
709 | 718 |
710 // If supported on the platform, and the user has sufficent rights, increase | 719 // If supported on the platform, and the user has sufficent rights, increase |
711 // the current process's scheduling priority to a high priority. | 720 // the current process's scheduling priority to a high priority. |
712 BASE_EXPORT void RaiseProcessToHighPriority(); | 721 BASE_EXPORT void RaiseProcessToHighPriority(); |
713 | 722 |
714 #if defined(OS_MACOSX) | 723 #if defined(OS_MACOSX) |
715 // Restore the default exception handler, setting it to Apple Crash Reporter | 724 // Restore the default exception handler, setting it to Apple Crash Reporter |
716 // (ReportCrash). When forking and execing a new process, the child will | 725 // (ReportCrash). When forking and execing a new process, the child will |
717 // inherit the parent's exception ports, which may be set to the Breakpad | 726 // inherit the parent's exception ports, which may be set to the Breakpad |
718 // instance running inside the parent. The parent's Breakpad instance should | 727 // instance running inside the parent. The parent's Breakpad instance should |
719 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler | 728 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
720 // in the child after forking will restore the standard exception handler. | 729 // in the child after forking will restore the standard exception handler. |
721 // See http://crbug.com/20371/ for more details. | 730 // See http://crbug.com/20371/ for more details. |
722 void RestoreDefaultExceptionHandler(); | 731 void RestoreDefaultExceptionHandler(); |
723 #endif // defined(OS_MACOSX) | 732 #endif // defined(OS_MACOSX) |
724 | 733 |
725 } // namespace base | 734 } // namespace base |
726 | 735 |
727 #endif // BASE_PROCESS_UTIL_H_ | 736 #endif // BASE_PROCESS_UTIL_H_ |
OLD | NEW |