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 #if !defined(OS_MACOSX) | 363 ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process), |
364 ProcessMetrics::ProcessMetrics(ProcessHandle process) | 364 last_time_(0), |
365 #else | 365 last_system_time_(0) |
366 ProcessMetrics::ProcessMetrics(ProcessHandle process, | |
367 ProcessMetrics::PortProvider* port_provider) | |
368 #endif | |
369 : process_(process), | |
370 last_time_(0), | |
371 last_system_time_(0) | |
372 #if defined(OS_LINUX) | 366 #if defined(OS_LINUX) |
373 , last_cpu_(0) | 367 , last_cpu_(0) |
374 #elif defined (OS_MACOSX) | |
375 , port_provider_(port_provider) | |
376 #endif | 368 #endif |
377 { | 369 { |
378 processor_count_ = base::SysInfo::NumberOfProcessors(); | 370 processor_count_ = base::SysInfo::NumberOfProcessors(); |
379 } | 371 } |
380 | 372 |
381 // static | 373 // static |
382 #if !defined(OS_MACOSX) | |
383 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { | 374 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { |
384 return new ProcessMetrics(process); | 375 return new ProcessMetrics(process); |
385 } | 376 } |
386 #else | |
387 ProcessMetrics* ProcessMetrics::CreateProcessMetrics( | |
388 ProcessHandle process, | |
389 ProcessMetrics::PortProvider* port_provider) { | |
390 return new ProcessMetrics(process, port_provider); | |
391 } | |
392 #endif | |
393 | 377 |
394 ProcessMetrics::~ProcessMetrics() { } | 378 ProcessMetrics::~ProcessMetrics() { } |
395 | 379 |
396 void EnableTerminationOnHeapCorruption() { | 380 void EnableTerminationOnHeapCorruption() { |
397 // On POSIX, there nothing to do AFAIK. | 381 // On POSIX, there nothing to do AFAIK. |
398 } | 382 } |
399 | 383 |
400 bool EnableInProcessStackDumping() { | 384 bool EnableInProcessStackDumping() { |
401 // When running in an application, our code typically expects SIGPIPE | 385 // When running in an application, our code typically expects SIGPIPE |
402 // to be ignored. Therefore, when testing that same code, it should run | 386 // 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... |
503 // If waitpid returned with an error, then the process doesn't exist | 487 // If waitpid returned with an error, then the process doesn't exist |
504 // (which most probably means it didn't exist before our call). | 488 // (which most probably means it didn't exist before our call). |
505 return waitpid_success; | 489 return waitpid_success; |
506 } | 490 } |
507 } | 491 } |
508 | 492 |
509 int64 TimeValToMicroseconds(const struct timeval& tv) { | 493 int64 TimeValToMicroseconds(const struct timeval& tv) { |
510 return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec; | 494 return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec; |
511 } | 495 } |
512 | 496 |
| 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 |
513 // Executes the application specified by |cl| and wait for it to exit. Stores | 540 // Executes the application specified by |cl| and wait for it to exit. Stores |
514 // the output (stdout) in |output|. If |do_search_path| is set, it searches the | 541 // the output (stdout) in |output|. If |do_search_path| is set, it searches the |
515 // path for the application; in that case, |envp| must be null, and it will use | 542 // path for the application; in that case, |envp| must be null, and it will use |
516 // the current environment. If |do_search_path| is false, |cl| should fully | 543 // the current environment. If |do_search_path| is false, |cl| should fully |
517 // specify the path of the application, and |envp| will be used as the | 544 // specify the path of the application, and |envp| will be used as the |
518 // environment. Redirects stderr to /dev/null. Returns true on success | 545 // environment. Redirects stderr to /dev/null. Returns true on success |
519 // (application launched and exited cleanly, with exit code indicating success). | 546 // (application launched and exited cleanly, with exit code indicating success). |
520 // |output| is modified only when the function finished successfully. | 547 // |output| is modified only when the function finished successfully. |
521 static bool GetAppOutputInternal(const CommandLine& cl, char* const envp[], | 548 static bool GetAppOutputInternal(const CommandLine& cl, char* const envp[], |
522 std::string* output, size_t max_output, | 549 std::string* output, size_t max_output, |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 const ProcessFilter* filter) { | 702 const ProcessFilter* filter) { |
676 bool exited_cleanly = | 703 bool exited_cleanly = |
677 WaitForProcessesToExit(executable_name, wait_milliseconds, | 704 WaitForProcessesToExit(executable_name, wait_milliseconds, |
678 filter); | 705 filter); |
679 if (!exited_cleanly) | 706 if (!exited_cleanly) |
680 KillProcesses(executable_name, exit_code, filter); | 707 KillProcesses(executable_name, exit_code, filter); |
681 return exited_cleanly; | 708 return exited_cleanly; |
682 } | 709 } |
683 | 710 |
684 } // namespace base | 711 } // namespace base |
OLD | NEW |