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