| 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> |
| 11 #include <sys/time.h> | 11 #include <sys/time.h> |
| 12 #include <sys/types.h> | 12 #include <sys/types.h> |
| 13 #include <sys/wait.h> | 13 #include <sys/wait.h> |
| 14 #include <unistd.h> | 14 #include <unistd.h> |
| 15 | 15 |
| 16 #include <limits> | 16 #include <limits> |
| 17 | 17 |
| 18 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/platform_thread.h" |
| 20 #include "base/process_util.h" | 21 #include "base/process_util.h" |
| 21 #include "base/sys_info.h" | 22 #include "base/sys_info.h" |
| 22 #include "base/time.h" | 23 #include "base/time.h" |
| 23 | 24 |
| 24 const int kMicrosecondsPerSecond = 1000000; | 25 const int kMicrosecondsPerSecond = 1000000; |
| 25 | 26 |
| 26 namespace base { | 27 namespace base { |
| 27 | 28 |
| 28 int GetCurrentProcId() { | 29 int GetCurrentProcId() { |
| 29 return getpid(); | 30 return getpid(); |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 // We add time_delta / 2 so the result is rounded. | 264 // We add time_delta / 2 so the result is rounded. |
| 264 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) / | 265 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) / |
| 265 time_delta); | 266 time_delta); |
| 266 | 267 |
| 267 last_system_time_ = system_time; | 268 last_system_time_ = system_time; |
| 268 last_time_ = time; | 269 last_time_ = time; |
| 269 | 270 |
| 270 return cpu; | 271 return cpu; |
| 271 } | 272 } |
| 272 | 273 |
| 274 int GetProcessCount(const std::wstring& executable_name, |
| 275 const ProcessFilter* filter) { |
| 276 int count = 0; |
| 277 |
| 278 NamedProcessIterator iter(executable_name, filter); |
| 279 while (iter.NextProcessEntry()) |
| 280 ++count; |
| 281 return count; |
| 282 } |
| 283 |
| 284 bool KillProcesses(const std::wstring& executable_name, int exit_code, |
| 285 const ProcessFilter* filter) { |
| 286 bool result = true; |
| 287 const ProcessEntry* entry; |
| 288 |
| 289 NamedProcessIterator iter(executable_name, filter); |
| 290 while ((entry = iter.NextProcessEntry()) != NULL) |
| 291 result = KillProcess((*entry).pid, exit_code, true) && result; |
| 292 |
| 293 return result; |
| 294 } |
| 295 |
| 296 bool WaitForProcessesToExit(const std::wstring& executable_name, |
| 297 int wait_milliseconds, |
| 298 const ProcessFilter* filter) { |
| 299 bool result = false; |
| 300 |
| 301 // TODO(port): This is inefficient, but works if there are multiple procs. |
| 302 // TODO(port): use waitpid to avoid leaving zombies around |
| 303 |
| 304 base::Time end_time = base::Time::Now() + |
| 305 base::TimeDelta::FromMilliseconds(wait_milliseconds); |
| 306 do { |
| 307 NamedProcessIterator iter(executable_name, filter); |
| 308 if (!iter.NextProcessEntry()) { |
| 309 result = true; |
| 310 break; |
| 311 } |
| 312 PlatformThread::Sleep(100); |
| 313 } while ((base::Time::Now() - end_time) > base::TimeDelta()); |
| 314 |
| 315 return result; |
| 316 } |
| 317 |
| 318 bool CleanupProcesses(const std::wstring& executable_name, |
| 319 int wait_milliseconds, |
| 320 int exit_code, |
| 321 const ProcessFilter* filter) { |
| 322 bool exited_cleanly = |
| 323 WaitForProcessesToExit(executable_name, wait_milliseconds, |
| 324 filter); |
| 325 if (!exited_cleanly) |
| 326 KillProcesses(executable_name, exit_code, filter); |
| 327 return exited_cleanly; |
| 328 } |
| 329 |
| 273 } // namespace base | 330 } // namespace base |
| OLD | NEW |