OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/process_metrics.h" | 5 #include "base/process/process_metrics.h" |
6 | 6 |
7 #include <sys/param.h> | 7 #include <sys/param.h> |
8 #include <sys/sysctl.h> | 8 #include <sys/sysctl.h> |
9 | 9 |
10 namespace base { | 10 namespace base { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 | 99 |
100 mib[5] = (length / sizeof(struct kinfo_proc)); | 100 mib[5] = (length / sizeof(struct kinfo_proc)); |
101 | 101 |
102 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) | 102 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) |
103 return 0; | 103 return 0; |
104 | 104 |
105 return info.p_pctcpu; | 105 return info.p_pctcpu; |
106 } | 106 } |
107 | 107 |
108 double ProcessMetrics::GetCPUUsage() { | 108 double ProcessMetrics::GetCPUUsage() { |
109 struct timeval now; | 109 TimeTicks time = TimeTicks::Now(); |
110 | 110 |
111 int retval = gettimeofday(&now, NULL); | 111 if (last_cpu_ == 0) { |
112 if (retval) | |
113 return 0; | |
114 | |
115 int64 time = TimeValToMicroseconds(now); | |
116 | |
117 if (last_cpu_time_ == 0) { | |
118 // First call, just set the last values. | 112 // First call, just set the last values. |
119 last_cpu_time_ = time; | 113 last_cpu_time_ = time; |
120 last_cpu_ = GetProcessCPU(process_); | 114 last_cpu_ = GetProcessCPU(process_); |
121 return 0; | 115 return 0; |
122 } | 116 } |
123 | 117 |
124 int64 time_delta = time - last_cpu_time_; | 118 int64 time_delta = (time - last_cpu_time_).InMicroseconds(); |
125 DCHECK_NE(time_delta, 0); | 119 DCHECK_NE(time_delta, 0); |
126 | 120 |
127 if (time_delta == 0) | 121 if (time_delta == 0) |
128 return 0; | 122 return 0; |
129 | 123 |
130 int cpu = GetProcessCPU(process_); | 124 int cpu = GetProcessCPU(process_); |
131 | 125 |
132 last_cpu_time_ = time; | 126 last_cpu_time_ = time; |
133 last_cpu_ = cpu; | 127 last_cpu_ = cpu; |
134 | 128 |
135 double percentage = static_cast<double>((cpu * 100.0) / FSCALE); | 129 double percentage = static_cast<double>((cpu * 100.0) / FSCALE); |
136 | 130 |
137 return percentage; | 131 return percentage; |
138 } | 132 } |
139 | 133 |
140 ProcessMetrics::ProcessMetrics(ProcessHandle process) | 134 ProcessMetrics::ProcessMetrics(ProcessHandle process) |
141 : process_(process), | 135 : process_(process), |
142 last_cpu_time_(0), | |
143 last_system_time_(0), | 136 last_system_time_(0), |
144 last_cpu_(0) { | 137 last_cpu_(0) { |
145 | 138 |
146 processor_count_ = base::SysInfo::NumberOfProcessors(); | 139 processor_count_ = base::SysInfo::NumberOfProcessors(); |
147 } | 140 } |
148 | 141 |
149 size_t GetSystemCommitCharge() { | 142 size_t GetSystemCommitCharge() { |
150 int mib[] = { CTL_VM, VM_METER }; | 143 int mib[] = { CTL_VM, VM_METER }; |
151 int pagesize; | 144 int pagesize; |
152 struct vmtotal vmtotal; | 145 struct vmtotal vmtotal; |
153 unsigned long mem_total, mem_free, mem_inactive; | 146 unsigned long mem_total, mem_free, mem_inactive; |
154 size_t len = sizeof(vmtotal); | 147 size_t len = sizeof(vmtotal); |
155 | 148 |
156 if (sysctl(mib, arraysize(mib), &vmtotal, &len, NULL, 0) < 0) | 149 if (sysctl(mib, arraysize(mib), &vmtotal, &len, NULL, 0) < 0) |
157 return 0; | 150 return 0; |
158 | 151 |
159 mem_total = vmtotal.t_vm; | 152 mem_total = vmtotal.t_vm; |
160 mem_free = vmtotal.t_free; | 153 mem_free = vmtotal.t_free; |
161 mem_inactive = vmtotal.t_vm - vmtotal.t_avm; | 154 mem_inactive = vmtotal.t_vm - vmtotal.t_avm; |
162 | 155 |
163 pagesize = getpagesize(); | 156 pagesize = getpagesize(); |
164 | 157 |
165 return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize); | 158 return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize); |
166 } | 159 } |
167 | 160 |
168 } // namespace base | 161 } // namespace base |
OLD | NEW |