OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <dirent.h> | 5 #include <dirent.h> |
6 #include <errno.h> | 6 #include <errno.h> |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <signal.h> | 8 #include <signal.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <sys/resource.h> | 10 #include <sys/resource.h> |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 return LaunchApp(argv, no_env, fds_to_remap, wait, process_handle); | 353 return LaunchApp(argv, no_env, fds_to_remap, wait, process_handle); |
354 } | 354 } |
355 | 355 |
356 bool LaunchApp(const CommandLine& cl, | 356 bool LaunchApp(const CommandLine& cl, |
357 bool wait, bool start_hidden, | 357 bool wait, bool start_hidden, |
358 ProcessHandle* process_handle) { | 358 ProcessHandle* process_handle) { |
359 file_handle_mapping_vector no_files; | 359 file_handle_mapping_vector no_files; |
360 return LaunchApp(cl.argv(), no_files, wait, process_handle); | 360 return LaunchApp(cl.argv(), no_files, wait, process_handle); |
361 } | 361 } |
362 | 362 |
363 ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process), | 363 #if !defined(OS_MACOSX) |
364 last_time_(0), | 364 ProcessMetrics::ProcessMetrics(ProcessHandle process) |
365 last_system_time_(0) | 365 #else |
| 366 ProcessMetrics::ProcessMetrics(ProcessHandle process, |
| 367 ProcessMetrics::PortProvider* port_provider) |
| 368 #endif |
| 369 : process_(process), |
| 370 last_time_(0), |
| 371 last_system_time_(0) |
366 #if defined(OS_LINUX) | 372 #if defined(OS_LINUX) |
367 , last_cpu_(0) | 373 , last_cpu_(0) |
| 374 #elif defined (OS_MACOSX) |
| 375 , port_provider_(port_provider) |
368 #endif | 376 #endif |
369 { | 377 { |
370 processor_count_ = base::SysInfo::NumberOfProcessors(); | 378 processor_count_ = base::SysInfo::NumberOfProcessors(); |
371 } | 379 } |
372 | 380 |
373 // static | 381 // static |
| 382 #if !defined(OS_MACOSX) |
374 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { | 383 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { |
375 return new ProcessMetrics(process); | 384 return new ProcessMetrics(process); |
376 } | 385 } |
| 386 #else |
| 387 ProcessMetrics* ProcessMetrics::CreateProcessMetrics( |
| 388 ProcessHandle process, |
| 389 ProcessMetrics::PortProvider* port_provider) { |
| 390 return new ProcessMetrics(process, port_provider); |
| 391 } |
| 392 #endif |
377 | 393 |
378 ProcessMetrics::~ProcessMetrics() { } | 394 ProcessMetrics::~ProcessMetrics() { } |
379 | 395 |
380 void EnableTerminationOnHeapCorruption() { | 396 void EnableTerminationOnHeapCorruption() { |
381 // On POSIX, there nothing to do AFAIK. | 397 // On POSIX, there nothing to do AFAIK. |
382 } | 398 } |
383 | 399 |
384 bool EnableInProcessStackDumping() { | 400 bool EnableInProcessStackDumping() { |
385 // When running in an application, our code typically expects SIGPIPE | 401 // When running in an application, our code typically expects SIGPIPE |
386 // to be ignored. Therefore, when testing that same code, it should run | 402 // to be ignored. Therefore, when testing that same code, it should run |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 // If waitpid returned with an error, then the process doesn't exist | 503 // If waitpid returned with an error, then the process doesn't exist |
488 // (which most probably means it didn't exist before our call). | 504 // (which most probably means it didn't exist before our call). |
489 return waitpid_success; | 505 return waitpid_success; |
490 } | 506 } |
491 } | 507 } |
492 | 508 |
493 int64 TimeValToMicroseconds(const struct timeval& tv) { | 509 int64 TimeValToMicroseconds(const struct timeval& tv) { |
494 return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec; | 510 return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec; |
495 } | 511 } |
496 | 512 |
497 #if defined(OS_MACOSX) | |
498 // TODO(port): this function only returns the *current* CPU's usage; | |
499 // we want to return this->process_'s CPU usage. | |
500 int ProcessMetrics::GetCPUUsage() { | |
501 struct timeval now; | |
502 struct rusage usage; | |
503 | |
504 int retval = gettimeofday(&now, NULL); | |
505 if (retval) | |
506 return 0; | |
507 retval = getrusage(RUSAGE_SELF, &usage); | |
508 if (retval) | |
509 return 0; | |
510 | |
511 int64 system_time = (TimeValToMicroseconds(usage.ru_stime) + | |
512 TimeValToMicroseconds(usage.ru_utime)) / | |
513 processor_count_; | |
514 int64 time = TimeValToMicroseconds(now); | |
515 | |
516 if ((last_system_time_ == 0) || (last_time_ == 0)) { | |
517 // First call, just set the last values. | |
518 last_system_time_ = system_time; | |
519 last_time_ = time; | |
520 return 0; | |
521 } | |
522 | |
523 int64 system_time_delta = system_time - last_system_time_; | |
524 int64 time_delta = time - last_time_; | |
525 DCHECK(time_delta != 0); | |
526 if (time_delta == 0) | |
527 return 0; | |
528 | |
529 // We add time_delta / 2 so the result is rounded. | |
530 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) / | |
531 time_delta); | |
532 | |
533 last_system_time_ = system_time; | |
534 last_time_ = time; | |
535 | |
536 return cpu; | |
537 } | |
538 #endif | |
539 | |
540 // Executes the application specified by |cl| and wait for it to exit. Stores | 513 // Executes the application specified by |cl| and wait for it to exit. Stores |
541 // the output (stdout) in |output|. If |do_search_path| is set, it searches the | 514 // the output (stdout) in |output|. If |do_search_path| is set, it searches the |
542 // path for the application; in that case, |envp| must be null, and it will use | 515 // path for the application; in that case, |envp| must be null, and it will use |
543 // the current environment. If |do_search_path| is false, |cl| should fully | 516 // the current environment. If |do_search_path| is false, |cl| should fully |
544 // specify the path of the application, and |envp| will be used as the | 517 // specify the path of the application, and |envp| will be used as the |
545 // environment. Redirects stderr to /dev/null. Returns true on success | 518 // environment. Redirects stderr to /dev/null. Returns true on success |
546 // (application launched and exited cleanly, with exit code indicating success). | 519 // (application launched and exited cleanly, with exit code indicating success). |
547 // |output| is modified only when the function finished successfully. | 520 // |output| is modified only when the function finished successfully. |
548 static bool GetAppOutputInternal(const CommandLine& cl, char* const envp[], | 521 static bool GetAppOutputInternal(const CommandLine& cl, char* const envp[], |
549 std::string* output, size_t max_output, | 522 std::string* output, size_t max_output, |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 const ProcessFilter* filter) { | 675 const ProcessFilter* filter) { |
703 bool exited_cleanly = | 676 bool exited_cleanly = |
704 WaitForProcessesToExit(executable_name, wait_milliseconds, | 677 WaitForProcessesToExit(executable_name, wait_milliseconds, |
705 filter); | 678 filter); |
706 if (!exited_cleanly) | 679 if (!exited_cleanly) |
707 KillProcesses(executable_name, exit_code, filter); | 680 KillProcesses(executable_name, exit_code, filter); |
708 return exited_cleanly; | 681 return exited_cleanly; |
709 } | 682 } |
710 | 683 |
711 } // namespace base | 684 } // namespace base |
OLD | NEW |