Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 127 lines (102 sloc) 3.829 kb
1457f56 Use a more compact license header in source files.
license.bot authored
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
3 // found in the LICENSE file.
d7cae12 Add base to the repository.
initial.commit authored
4
5 #include "base/process.h"
6 #include "base/logging.h"
7 #include "base/process_util.h"
8 #include "base/scoped_ptr.h"
9
8bf7e83 Add Terminate() to the Process object, have RenderProcessHost use thi…
brettw@google.com authored
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
5c182f6 Remove some Windows-specific stuff from RenderProcessHost by having c…
brettw@google.com authored
25 bool Process::IsProcessBackgrounded() const {
d7cae12 Add base to the repository.
initial.commit authored
26 DCHECK(process_);
27 DWORD priority = GetPriorityClass(process_);
28 if (priority == 0)
29 return false; // Failure case.
30 return priority == BELOW_NORMAL_PRIORITY_CLASS;
31 }
32
33 bool Process::SetProcessBackgrounded(bool value) {
34 DCHECK(process_);
35 DWORD priority = value ? BELOW_NORMAL_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS;
36 return (SetPriorityClass(process_, priority) != 0);
37 }
38
39 // According to MSDN, these are the default values which XP
40 // uses to govern working set soft limits.
41 // http://msdn.microsoft.com/en-us/library/ms686234.aspx
42 static const int kWinDefaultMinSet = 50 * 4096;
43 static const int kWinDefaultMaxSet = 345 * 4096;
44 static const int kDampingFactor = 2;
45
46 bool Process::ReduceWorkingSet() {
47 if (!process_)
48 return false;
49 // The idea here is that when we the process' working set has gone
50 // down, we want to release those pages to the OS quickly. However,
51 // when it is not going down, we want to be careful not to release
52 // too much back to the OS, as it could cause additional paging.
53
54 // We use a damping function to lessen the working set over time.
55 // As the process grows/shrinks, this algorithm will lag with
56 // working set reduction.
57 //
58 // The intended algorithm is:
59 // TargetWorkingSetSize = (LastWorkingSet/2 + CurrentWorkingSet) /2
60
8bf7e83 Add Terminate() to the Process object, have RenderProcessHost use thi…
brettw@google.com authored
61 scoped_ptr<ProcessMetrics> metrics(
62 ProcessMetrics::CreateProcessMetrics(process_));
63 WorkingSetKBytes working_set;
d7cae12 Add base to the repository.
initial.commit authored
64 if (!metrics->GetWorkingSetKBytes(&working_set))
65 return false;
66
67
68 // We want to compute the amount of working set that the process
69 // needs to keep in memory. Since other processes contain the
70 // pages which are shared, we don't need to reserve them in our
71 // process, the system already has them tagged. Keep in mind, we
72 // don't get to control *which* pages get released, but if we
73 // assume reasonable distribution of pages, this should generally
74 // be the right value.
75 size_t current_working_set_size = working_set.priv +
76 working_set.shareable;
77
78 size_t max_size = current_working_set_size;
79 if (last_working_set_size_)
80 max_size = (max_size + last_working_set_size_) / 2; // Average.
81 max_size *= 1024; // Convert to KBytes.
82 last_working_set_size_ = current_working_set_size / kDampingFactor;
83
84 BOOL rv = SetProcessWorkingSetSize(process_, kWinDefaultMinSet, max_size);
85 return rv == TRUE;
86 }
87
88 bool Process::UnReduceWorkingSet() {
89 if (!process_)
90 return false;
91
92 if (!last_working_set_size_)
93 return true; // There was nothing to undo.
94
95 // We've had a reduced working set. Make sure we have lots of
96 // headroom now that we're active again.
97 size_t limit = last_working_set_size_ * kDampingFactor * 2 * 1024;
98 BOOL rv = SetProcessWorkingSetSize(process_, kWinDefaultMinSet, limit);
99 return rv == TRUE;
100 }
101
102 bool Process::EmptyWorkingSet() {
103 if (!process_)
104 return false;
105
106 BOOL rv = SetProcessWorkingSetSize(process_, -1, -1);
107 return rv == TRUE;
108 }
109
b1f1f7f Use portable typedef for PIDs (process IDs).
phajdan.jr@chromium.org authored
110 ProcessId Process::pid() const {
d7cae12 Add base to the repository.
initial.commit authored
111 if (process_ == 0)
112 return 0;
113
8bf7e83 Add Terminate() to the Process object, have RenderProcessHost use thi…
brettw@google.com authored
114 return GetProcId(process_);
d7cae12 Add base to the repository.
initial.commit authored
115 }
116
117 bool Process::is_current() const {
118 return process_ == GetCurrentProcess();
119 }
120
121 // static
122 Process Process::Current() {
123 return Process(GetCurrentProcess());
124 }
1457f56 Use a more compact license header in source files.
license.bot authored
125
8bf7e83 Add Terminate() to the Process object, have RenderProcessHost use thi…
brettw@google.com authored
126 } // namespace base
Something went wrong with that request. Please try again.