| 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 "base/process.h" | 5 #include "base/process.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/process_util.h" | 7 #include "base/process_util.h" |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 | 9 |
| 10 namespace base { |
| 11 |
| 12 void Process::Close() { |
| 13 if (!process_) |
| 14 return; |
| 15 ::CloseHandle(process_); |
| 16 process_ = NULL; |
| 17 } |
| 18 |
| 19 void Process::Terminate(int result_code) { |
| 20 if (!process_) |
| 21 return; |
| 22 ::TerminateProcess(process_, result_code); |
| 23 } |
| 24 |
| 10 bool Process::IsProcessBackgrounded() const { | 25 bool Process::IsProcessBackgrounded() const { |
| 11 DCHECK(process_); | 26 DCHECK(process_); |
| 12 DWORD priority = GetPriorityClass(process_); | 27 DWORD priority = GetPriorityClass(process_); |
| 13 if (priority == 0) | 28 if (priority == 0) |
| 14 return false; // Failure case. | 29 return false; // Failure case. |
| 15 return priority == BELOW_NORMAL_PRIORITY_CLASS; | 30 return priority == BELOW_NORMAL_PRIORITY_CLASS; |
| 16 } | 31 } |
| 17 | 32 |
| 18 bool Process::SetProcessBackgrounded(bool value) { | 33 bool Process::SetProcessBackgrounded(bool value) { |
| 19 DCHECK(process_); | 34 DCHECK(process_); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 36 // when it is not going down, we want to be careful not to release | 51 // when it is not going down, we want to be careful not to release |
| 37 // too much back to the OS, as it could cause additional paging. | 52 // too much back to the OS, as it could cause additional paging. |
| 38 | 53 |
| 39 // We use a damping function to lessen the working set over time. | 54 // We use a damping function to lessen the working set over time. |
| 40 // As the process grows/shrinks, this algorithm will lag with | 55 // As the process grows/shrinks, this algorithm will lag with |
| 41 // working set reduction. | 56 // working set reduction. |
| 42 // | 57 // |
| 43 // The intended algorithm is: | 58 // The intended algorithm is: |
| 44 // TargetWorkingSetSize = (LastWorkingSet/2 + CurrentWorkingSet) /2 | 59 // TargetWorkingSetSize = (LastWorkingSet/2 + CurrentWorkingSet) /2 |
| 45 | 60 |
| 46 scoped_ptr<process_util::ProcessMetrics> metrics( | 61 scoped_ptr<ProcessMetrics> metrics( |
| 47 process_util::ProcessMetrics::CreateProcessMetrics(process_)); | 62 ProcessMetrics::CreateProcessMetrics(process_)); |
| 48 process_util::WorkingSetKBytes working_set; | 63 WorkingSetKBytes working_set; |
| 49 if (!metrics->GetWorkingSetKBytes(&working_set)) | 64 if (!metrics->GetWorkingSetKBytes(&working_set)) |
| 50 return false; | 65 return false; |
| 51 | 66 |
| 52 | 67 |
| 53 // We want to compute the amount of working set that the process | 68 // We want to compute the amount of working set that the process |
| 54 // needs to keep in memory. Since other processes contain the | 69 // needs to keep in memory. Since other processes contain the |
| 55 // pages which are shared, we don't need to reserve them in our | 70 // pages which are shared, we don't need to reserve them in our |
| 56 // process, the system already has them tagged. Keep in mind, we | 71 // process, the system already has them tagged. Keep in mind, we |
| 57 // don't get to control *which* pages get released, but if we | 72 // don't get to control *which* pages get released, but if we |
| 58 // assume reasonable distribution of pages, this should generally | 73 // assume reasonable distribution of pages, this should generally |
| (...skipping 30 matching lines...) Expand all Loading... |
| 89 return false; | 104 return false; |
| 90 | 105 |
| 91 BOOL rv = SetProcessWorkingSetSize(process_, -1, -1); | 106 BOOL rv = SetProcessWorkingSetSize(process_, -1, -1); |
| 92 return rv == TRUE; | 107 return rv == TRUE; |
| 93 } | 108 } |
| 94 | 109 |
| 95 int32 Process::pid() const { | 110 int32 Process::pid() const { |
| 96 if (process_ == 0) | 111 if (process_ == 0) |
| 97 return 0; | 112 return 0; |
| 98 | 113 |
| 99 return process_util::GetProcId(process_); | 114 return GetProcId(process_); |
| 100 } | 115 } |
| 101 | 116 |
| 102 bool Process::is_current() const { | 117 bool Process::is_current() const { |
| 103 return process_ == GetCurrentProcess(); | 118 return process_ == GetCurrentProcess(); |
| 104 } | 119 } |
| 105 | 120 |
| 106 // static | 121 // static |
| 107 Process Process::Current() { | 122 Process Process::Current() { |
| 108 return Process(GetCurrentProcess()); | 123 return Process(GetCurrentProcess()); |
| 109 } | 124 } |
| 110 | 125 |
| 126 } // namespace base |
| OLD | NEW |