| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 | 12 |
| 13 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
| 14 #include <windows.h> | 14 #include <windows.h> |
| 15 #include <tlhelp32.h> | 15 #include <tlhelp32.h> |
| 16 #elif defined(OS_MACOSX) | 16 #elif defined(OS_MACOSX) |
| 17 // kinfo_proc is defined in <sys/sysctl.h>, but this forward declaration | 17 // kinfo_proc is defined in <sys/sysctl.h>, but this forward declaration |
| 18 // is sufficient for the vector<kinfo_proc> below. | 18 // is sufficient for the vector<kinfo_proc> below. |
| 19 struct kinfo_proc; | 19 struct kinfo_proc; |
| 20 #include <mach/mach.h> | 20 #include <mach/mach.h> |
| 21 #elif defined(OS_POSIX) | 21 #elif defined(OS_POSIX) |
| 22 #include <dirent.h> | 22 #include <dirent.h> |
| 23 #include <limits.h> | 23 #include <limits.h> |
| 24 #include <sys/types.h> | 24 #include <sys/types.h> |
| 25 #endif | 25 #endif |
| 26 | 26 |
| 27 #include <list> |
| 27 #include <string> | 28 #include <string> |
| 28 #include <utility> | 29 #include <utility> |
| 29 #include <vector> | 30 #include <vector> |
| 30 | 31 |
| 31 #include "base/command_line.h" | 32 #include "base/command_line.h" |
| 32 #include "base/file_descriptor_shuffle.h" | 33 #include "base/file_descriptor_shuffle.h" |
| 33 #include "base/file_path.h" | 34 #include "base/file_path.h" |
| 34 #include "base/process.h" | 35 #include "base/process.h" |
| 35 | 36 |
| 36 #ifndef NAME_MAX // Solaris and some BSDs have no NAME_MAX | 37 #ifndef NAME_MAX // Solaris and some BSDs have no NAME_MAX |
| 37 #ifdef MAXNAMLEN | 38 #ifdef MAXNAMLEN |
| 38 #define NAME_MAX MAXNAMLEN | 39 #define NAME_MAX MAXNAMLEN |
| 39 #else | 40 #else |
| 40 #define NAME_MAX 256 | 41 #define NAME_MAX 256 |
| 41 #endif | 42 #endif |
| 42 #endif | 43 #endif |
| 43 | 44 |
| 44 namespace base { | 45 namespace base { |
| 45 | 46 |
| 46 #if defined(OS_WIN) | 47 #if defined(OS_WIN) |
| 47 | 48 |
| 48 struct ProcessEntry : public PROCESSENTRY32 { | 49 struct ProcessEntry : public PROCESSENTRY32 { |
| 50 ProcessId pid() const { return th32ProcessID; } |
| 51 ProcessId parent_pid() const { return th32ParentProcessID; } |
| 52 const wchar_t* exe_file() const { return szExeFile; } |
| 49 }; | 53 }; |
| 54 |
| 50 struct IoCounters : public IO_COUNTERS { | 55 struct IoCounters : public IO_COUNTERS { |
| 51 }; | 56 }; |
| 52 | 57 |
| 53 #elif defined(OS_POSIX) | 58 #elif defined(OS_POSIX) |
| 54 | 59 |
| 55 struct ProcessEntry { | 60 struct ProcessEntry { |
| 56 base::ProcessId pid; | 61 ProcessId pid_; |
| 57 base::ProcessId ppid; | 62 ProcessId ppid_; |
| 58 char szExeFile[NAME_MAX + 1]; | 63 ProcessId gid_; |
| 64 std::string exe_file_; |
| 65 |
| 66 ProcessId pid() const { return pid_; } |
| 67 ProcessId parent_pid() const { return ppid_; } |
| 68 const char* exe_file() const { return exe_file_.c_str(); } |
| 59 }; | 69 }; |
| 60 | 70 |
| 61 struct IoCounters { | 71 struct IoCounters { |
| 62 uint64_t ReadOperationCount; | 72 uint64_t ReadOperationCount; |
| 63 uint64_t WriteOperationCount; | 73 uint64_t WriteOperationCount; |
| 64 uint64_t OtherOperationCount; | 74 uint64_t OtherOperationCount; |
| 65 uint64_t ReadTransferCount; | 75 uint64_t ReadTransferCount; |
| 66 uint64_t WriteTransferCount; | 76 uint64_t WriteTransferCount; |
| 67 uint64_t OtherTransferCount; | 77 uint64_t OtherTransferCount; |
| 68 }; | 78 }; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 // This adjusts /proc/process/oom_adj so the Linux OOM killer will prefer | 129 // This adjusts /proc/process/oom_adj so the Linux OOM killer will prefer |
| 120 // certain process types over others. The range for the adjustment is | 130 // certain process types over others. The range for the adjustment is |
| 121 // [-17,15], with [0,15] being user accessible. | 131 // [-17,15], with [0,15] being user accessible. |
| 122 bool AdjustOOMScore(ProcessId process, int score); | 132 bool AdjustOOMScore(ProcessId process, int score); |
| 123 #endif | 133 #endif |
| 124 | 134 |
| 125 #if defined(OS_POSIX) | 135 #if defined(OS_POSIX) |
| 126 // Close all file descriptors, expect those which are a destination in the | 136 // Close all file descriptors, expect those which are a destination in the |
| 127 // given multimap. Only call this function in a child process where you know | 137 // given multimap. Only call this function in a child process where you know |
| 128 // that there aren't any other threads. | 138 // that there aren't any other threads. |
| 129 void CloseSuperfluousFds(const base::InjectiveMultimap& saved_map); | 139 void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
| 130 #endif | 140 #endif |
| 131 | 141 |
| 132 #if defined(OS_WIN) | 142 #if defined(OS_WIN) |
| 133 // Runs the given application name with the given command line. Normally, the | 143 // Runs the given application name with the given command line. Normally, the |
| 134 // first command line argument should be the path to the process, and don't | 144 // first command line argument should be the path to the process, and don't |
| 135 // forget to quote it. | 145 // forget to quote it. |
| 136 // | 146 // |
| 137 // If wait is true, it will block and wait for the other process to finish, | 147 // If wait is true, it will block and wait for the other process to finish, |
| 138 // otherwise, it will just continue asynchronously. | 148 // otherwise, it will just continue asynchronously. |
| 139 // | 149 // |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 // for the command. | 242 // for the command. |
| 233 bool GetAppOutputRestricted(const CommandLine& cl, | 243 bool GetAppOutputRestricted(const CommandLine& cl, |
| 234 std::string* output, size_t max_output); | 244 std::string* output, size_t max_output); |
| 235 #endif | 245 #endif |
| 236 | 246 |
| 237 // Used to filter processes by process ID. | 247 // Used to filter processes by process ID. |
| 238 class ProcessFilter { | 248 class ProcessFilter { |
| 239 public: | 249 public: |
| 240 // Returns true to indicate set-inclusion and false otherwise. This method | 250 // Returns true to indicate set-inclusion and false otherwise. This method |
| 241 // should not have side-effects and should be idempotent. | 251 // should not have side-effects and should be idempotent. |
| 242 virtual bool Includes(ProcessId pid, ProcessId parent_pid) const = 0; | 252 virtual bool Includes(const ProcessEntry& entry) const = 0; |
| 243 }; | 253 }; |
| 244 | 254 |
| 245 // Returns the number of processes on the machine that are running from the | 255 // Returns the number of processes on the machine that are running from the |
| 246 // given executable name. If filter is non-null, then only processes selected | 256 // given executable name. If filter is non-null, then only processes selected |
| 247 // by the filter will be counted. | 257 // by the filter will be counted. |
| 248 int GetProcessCount(const std::wstring& executable_name, | 258 int GetProcessCount(const std::wstring& executable_name, |
| 249 const ProcessFilter* filter); | 259 const ProcessFilter* filter); |
| 250 | 260 |
| 251 // Attempts to kill all the processes on the current machine that were launched | 261 // Attempts to kill all the processes on the current machine that were launched |
| 252 // from the given executable name, ending them with the given exit code. If | 262 // from the given executable name, ending them with the given exit code. If |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // executable name to exit, then kills off any of them that are still around. | 315 // executable name to exit, then kills off any of them that are still around. |
| 306 // If filter is non-null, then only processes selected by the filter are waited | 316 // If filter is non-null, then only processes selected by the filter are waited |
| 307 // on. Killed processes are ended with the given exit code. Returns false if | 317 // on. Killed processes are ended with the given exit code. Returns false if |
| 308 // any processes needed to be killed, true if they all exited cleanly within | 318 // any processes needed to be killed, true if they all exited cleanly within |
| 309 // the wait_milliseconds delay. | 319 // the wait_milliseconds delay. |
| 310 bool CleanupProcesses(const std::wstring& executable_name, | 320 bool CleanupProcesses(const std::wstring& executable_name, |
| 311 int64 wait_milliseconds, | 321 int64 wait_milliseconds, |
| 312 int exit_code, | 322 int exit_code, |
| 313 const ProcessFilter* filter); | 323 const ProcessFilter* filter); |
| 314 | 324 |
| 315 // This class provides a way to iterate through the list of processes | 325 // This class provides a way to iterate through a list of processes on the |
| 316 // on the current machine that were started from the given executable | 326 // current machine with a specified filter. |
| 317 // name. To use, create an instance and then call NextProcessEntry() | 327 // To use, create an instance and then call NextProcessEntry() until it returns |
| 318 // until it returns false. | 328 // false. |
| 319 class NamedProcessIterator { | 329 class ProcessIterator { |
| 320 public: | 330 public: |
| 321 NamedProcessIterator(const std::wstring& executable_name, | 331 explicit ProcessIterator(const ProcessFilter* filter); |
| 322 const ProcessFilter* filter); | 332 virtual ~ProcessIterator(); |
| 323 ~NamedProcessIterator(); | |
| 324 | 333 |
| 325 // If there's another process that matches the given executable name, | 334 // If there's another process that matches the given executable name, |
| 326 // returns a const pointer to the corresponding PROCESSENTRY32. | 335 // returns a const pointer to the corresponding PROCESSENTRY32. |
| 327 // If there are no more matching processes, returns NULL. | 336 // If there are no more matching processes, returns NULL. |
| 328 // The returned pointer will remain valid until NextProcessEntry() | 337 // The returned pointer will remain valid until NextProcessEntry() |
| 329 // is called again or this NamedProcessIterator goes out of scope. | 338 // is called again or this NamedProcessIterator goes out of scope. |
| 330 const ProcessEntry* NextProcessEntry(); | 339 const ProcessEntry* NextProcessEntry(); |
| 331 | 340 |
| 341 // Takes a snapshot of all the ProcessEntry found. |
| 342 std::list<ProcessEntry> Snapshot(); |
| 343 |
| 344 protected: |
| 345 virtual bool IncludeEntry(); |
| 346 const ProcessEntry& entry() { return entry_; } |
| 347 |
| 332 private: | 348 private: |
| 333 // Determines whether there's another process (regardless of executable) | 349 // Determines whether there's another process (regardless of executable) |
| 334 // left in the list of all processes. Returns true and sets entry_ to | 350 // left in the list of all processes. Returns true and sets entry_ to |
| 335 // that process's info if there is one, false otherwise. | 351 // that process's info if there is one, false otherwise. |
| 336 bool CheckForNextProcess(); | 352 bool CheckForNextProcess(); |
| 337 | 353 |
| 338 bool IncludeEntry(); | |
| 339 | |
| 340 // Initializes a PROCESSENTRY32 data structure so that it's ready for | 354 // Initializes a PROCESSENTRY32 data structure so that it's ready for |
| 341 // use with Process32First/Process32Next. | 355 // use with Process32First/Process32Next. |
| 342 void InitProcessEntry(ProcessEntry* entry); | 356 void InitProcessEntry(ProcessEntry* entry); |
| 343 | 357 |
| 344 std::wstring executable_name_; | |
| 345 | |
| 346 #if defined(OS_WIN) | 358 #if defined(OS_WIN) |
| 347 HANDLE snapshot_; | 359 HANDLE snapshot_; |
| 348 bool started_iteration_; | 360 bool started_iteration_; |
| 349 #elif defined(OS_MACOSX) | 361 #elif defined(OS_MACOSX) |
| 350 std::vector<kinfo_proc> kinfo_procs_; | 362 std::vector<kinfo_proc> kinfo_procs_; |
| 351 size_t index_of_kinfo_proc_; | 363 size_t index_of_kinfo_proc_; |
| 352 #elif defined(OS_POSIX) | 364 #elif defined(OS_POSIX) |
| 353 DIR *procfs_dir_; | 365 DIR *procfs_dir_; |
| 354 #endif | 366 #endif |
| 355 ProcessEntry entry_; | 367 ProcessEntry entry_; |
| 356 const ProcessFilter* filter_; | 368 const ProcessFilter* filter_; |
| 357 | 369 |
| 358 DISALLOW_EVIL_CONSTRUCTORS(NamedProcessIterator); | 370 DISALLOW_COPY_AND_ASSIGN(ProcessIterator); |
| 371 }; |
| 372 |
| 373 // This class provides a way to iterate through the list of processes |
| 374 // on the current machine that were started from the given executable |
| 375 // name. To use, create an instance and then call NextProcessEntry() |
| 376 // until it returns false. |
| 377 class NamedProcessIterator : public ProcessIterator { |
| 378 public: |
| 379 NamedProcessIterator(const std::wstring& executable_name, |
| 380 const ProcessFilter* filter); |
| 381 virtual ~NamedProcessIterator(); |
| 382 |
| 383 protected: |
| 384 virtual bool IncludeEntry(); |
| 385 |
| 386 private: |
| 387 std::wstring executable_name_; |
| 388 |
| 389 DISALLOW_COPY_AND_ASSIGN(NamedProcessIterator); |
| 359 }; | 390 }; |
| 360 | 391 |
| 361 // Working Set (resident) memory usage broken down by | 392 // Working Set (resident) memory usage broken down by |
| 362 // | 393 // |
| 363 // On Windows: | 394 // On Windows: |
| 364 // priv (private): These pages (kbytes) cannot be shared with any other process. | 395 // priv (private): These pages (kbytes) cannot be shared with any other process. |
| 365 // shareable: These pages (kbytes) can be shared with other processes under | 396 // shareable: These pages (kbytes) can be shared with other processes under |
| 366 // the right circumstances. | 397 // the right circumstances. |
| 367 // shared : These pages (kbytes) are currently shared with at least one | 398 // shared : These pages (kbytes) are currently shared with at least one |
| 368 // other process. | 399 // other process. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 // metrics on OS X (except for the current process, which always gets | 459 // metrics on OS X (except for the current process, which always gets |
| 429 // metrics). | 460 // metrics). |
| 430 virtual mach_port_t TaskForPid(ProcessHandle process) const = 0; | 461 virtual mach_port_t TaskForPid(ProcessHandle process) const = 0; |
| 431 }; | 462 }; |
| 432 | 463 |
| 433 // The port provider needs to outlive the ProcessMetrics object returned by | 464 // The port provider needs to outlive the ProcessMetrics object returned by |
| 434 // this function. If NULL is passed as provider, the returned object | 465 // this function. If NULL is passed as provider, the returned object |
| 435 // only returns valid metrics if |process| is the current process. | 466 // only returns valid metrics if |process| is the current process. |
| 436 static ProcessMetrics* CreateProcessMetrics(ProcessHandle process, | 467 static ProcessMetrics* CreateProcessMetrics(ProcessHandle process, |
| 437 PortProvider* port_provider); | 468 PortProvider* port_provider); |
| 438 #endif | 469 #endif // !defined(OS_MACOSX) |
| 439 | 470 |
| 440 ~ProcessMetrics(); | 471 ~ProcessMetrics(); |
| 441 | 472 |
| 442 // Returns the current space allocated for the pagefile, in bytes (these pages | 473 // Returns the current space allocated for the pagefile, in bytes (these pages |
| 443 // may or may not be in memory). On Linux, this returns the total virtual | 474 // may or may not be in memory). On Linux, this returns the total virtual |
| 444 // memory size. | 475 // memory size. |
| 445 size_t GetPagefileUsage() const; | 476 size_t GetPagefileUsage() const; |
| 446 // Returns the peak space allocated for the pagefile, in bytes. | 477 // Returns the peak space allocated for the pagefile, in bytes. |
| 447 size_t GetPeakPagefileUsage() const; | 478 size_t GetPeakPagefileUsage() const; |
| 448 // Returns the current working set size, in bytes. On Linux, this returns | 479 // Returns the current working set size, in bytes. On Linux, this returns |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 // If IO information is retrieved successfully, the function returns true | 511 // If IO information is retrieved successfully, the function returns true |
| 481 // and fills in the IO_COUNTERS passed in. The function returns false | 512 // and fills in the IO_COUNTERS passed in. The function returns false |
| 482 // otherwise. | 513 // otherwise. |
| 483 bool GetIOCounters(IoCounters* io_counters) const; | 514 bool GetIOCounters(IoCounters* io_counters) const; |
| 484 | 515 |
| 485 private: | 516 private: |
| 486 #if !defined(OS_MACOSX) | 517 #if !defined(OS_MACOSX) |
| 487 explicit ProcessMetrics(ProcessHandle process); | 518 explicit ProcessMetrics(ProcessHandle process); |
| 488 #else | 519 #else |
| 489 ProcessMetrics(ProcessHandle process, PortProvider* port_provider); | 520 ProcessMetrics(ProcessHandle process, PortProvider* port_provider); |
| 490 #endif | 521 #endif // !defined(OS_MACOSX) |
| 491 | 522 |
| 492 ProcessHandle process_; | 523 ProcessHandle process_; |
| 493 | 524 |
| 494 int processor_count_; | 525 int processor_count_; |
| 495 | 526 |
| 496 // Used to store the previous times and CPU usage counts so we can | 527 // Used to store the previous times and CPU usage counts so we can |
| 497 // compute the CPU usage between calls. | 528 // compute the CPU usage between calls. |
| 498 int64 last_time_; | 529 int64 last_time_; |
| 499 int64 last_system_time_; | 530 int64 last_system_time_; |
| 500 | 531 |
| 501 #if defined(OS_MACOSX) | 532 #if defined(OS_MACOSX) |
| 502 // Queries the port provider if it's set. | 533 // Queries the port provider if it's set. |
| 503 mach_port_t TaskForPid(ProcessHandle process) const; | 534 mach_port_t TaskForPid(ProcessHandle process) const; |
| 504 | 535 |
| 505 PortProvider* port_provider_; | 536 PortProvider* port_provider_; |
| 506 #elif defined(OS_POSIX) | 537 #elif defined(OS_POSIX) |
| 507 // Jiffie count at the last_time_ we updated. | 538 // Jiffie count at the last_time_ we updated. |
| 508 int last_cpu_; | 539 int last_cpu_; |
| 509 #endif | 540 #endif // defined(OS_MACOSX) |
| 510 | 541 |
| 511 DISALLOW_EVIL_CONSTRUCTORS(ProcessMetrics); | 542 DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); |
| 512 }; | 543 }; |
| 513 | 544 |
| 514 // Returns the memory commited by the system in KBytes. | 545 // Returns the memory commited by the system in KBytes. |
| 515 // Returns 0 if it can't compute the commit charge. | 546 // Returns 0 if it can't compute the commit charge. |
| 516 size_t GetSystemCommitCharge(); | 547 size_t GetSystemCommitCharge(); |
| 517 | 548 |
| 518 // Enables low fragmentation heap (LFH) for every heaps of this process. This | 549 // Enables low fragmentation heap (LFH) for every heaps of this process. This |
| 519 // won't have any effect on heaps created after this function call. It will not | 550 // won't have any effect on heaps created after this function call. It will not |
| 520 // modify data allocated in the heaps before calling this function. So it is | 551 // modify data allocated in the heaps before calling this function. So it is |
| 521 // better to call this function early in initialization and again before | 552 // better to call this function early in initialization and again before |
| (...skipping 24 matching lines...) Expand all Loading... |
| 546 | 577 |
| 547 #if defined(OS_MACOSX) | 578 #if defined(OS_MACOSX) |
| 548 // Restore the default exception handler, setting it to Apple Crash Reporter | 579 // Restore the default exception handler, setting it to Apple Crash Reporter |
| 549 // (ReportCrash). When forking and execing a new process, the child will | 580 // (ReportCrash). When forking and execing a new process, the child will |
| 550 // inherit the parent's exception ports, which may be set to the Breakpad | 581 // inherit the parent's exception ports, which may be set to the Breakpad |
| 551 // instance running inside the parent. The parent's Breakpad instance should | 582 // instance running inside the parent. The parent's Breakpad instance should |
| 552 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler | 583 // not handle the child's exceptions. Calling RestoreDefaultExceptionHandler |
| 553 // in the child after forking will restore the standard exception handler. | 584 // in the child after forking will restore the standard exception handler. |
| 554 // See http://crbug.com/20371/ for more details. | 585 // See http://crbug.com/20371/ for more details. |
| 555 void RestoreDefaultExceptionHandler(); | 586 void RestoreDefaultExceptionHandler(); |
| 556 #endif | 587 #endif // defined(OS_MACOSX) |
| 557 | 588 |
| 558 } // namespace base | 589 } // namespace base |
| 559 | 590 |
| 560 #endif // BASE_PROCESS_UTIL_H_ | 591 #endif // BASE_PROCESS_UTIL_H_ |
| OLD | NEW |