Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: base/process_util_mac.mm

Issue 501138: Mac: Create a pid->task_t mapping in the browser process. (Closed)
Patch Set: trunglify Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 5
6 #include "base/process_util.h" 6 #include "base/process_util.h"
7 7
8 #import <Cocoa/Cocoa.h> 8 #import <Cocoa/Cocoa.h>
9 #include <crt_externs.h> 9 #include <crt_externs.h>
10 #include <mach/mach.h> 10 #include <mach/mach.h>
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 // general, so there doesn't really seem to be a way to do these (and spinning 186 // general, so there doesn't really seem to be a way to do these (and spinning
187 // up ps to fetch each stats seems dangerous to put in a base api for anyone to 187 // up ps to fetch each stats seems dangerous to put in a base api for anyone to
188 // call). Child processes ipc their port, so return something if available, 188 // call). Child processes ipc their port, so return something if available,
189 // otherwise return 0. 189 // otherwise return 0.
190 // 190 //
191 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { 191 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
192 return false; 192 return false;
193 } 193 }
194 194
195 static bool GetTaskInfo(mach_port_t task, task_basic_info_64* task_info_data) { 195 static bool GetTaskInfo(mach_port_t task, task_basic_info_64* task_info_data) {
196 if (!task) 196 if (task == MACH_PORT_NULL)
197 return false; 197 return false;
198 mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT; 198 mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT;
199 kern_return_t kr = task_info(task, 199 kern_return_t kr = task_info(task,
200 TASK_BASIC_INFO_64, 200 TASK_BASIC_INFO_64,
201 reinterpret_cast<task_info_t>(task_info_data), 201 reinterpret_cast<task_info_t>(task_info_data),
202 &count); 202 &count);
203 // Most likely cause for failure: |task| is a zombie. 203 // Most likely cause for failure: |task| is a zombie.
204 return kr == KERN_SUCCESS; 204 return kr == KERN_SUCCESS;
205 } 205 }
206 206
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 return true; 243 return true;
244 } 244 }
245 245
246 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \ 246 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \
247 (r)->tv_sec = (a)->seconds; \ 247 (r)->tv_sec = (a)->seconds; \
248 (r)->tv_usec = (a)->microseconds; \ 248 (r)->tv_usec = (a)->microseconds; \
249 } while (0) 249 } while (0)
250 250
251 int ProcessMetrics::GetCPUUsage() { 251 int ProcessMetrics::GetCPUUsage() {
252 mach_port_t task = TaskForPid(process_); 252 mach_port_t task = TaskForPid(process_);
253 if (!task) 253 if (task == MACH_PORT_NULL)
254 return 0; 254 return 0;
255 255
256 kern_return_t kr; 256 kern_return_t kr;
257 257
258 // TODO(thakis): Libtop doesn't use thread info. How can they get away 258 // TODO(thakis): Libtop doesn't use thread info. How can they get away
259 // without it? 259 // without it?
260 task_thread_times_info thread_info_data; 260 task_thread_times_info thread_info_data;
261 mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT; 261 mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
262 kr = task_info(task, 262 kr = task_info(task,
263 TASK_THREAD_TIMES_INFO, 263 TASK_THREAD_TIMES_INFO,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) / 310 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) /
311 (time_delta)); 311 (time_delta));
312 312
313 last_system_time_ = task_time; 313 last_system_time_ = task_time;
314 last_time_ = time; 314 last_time_ = time;
315 315
316 return cpu; 316 return cpu;
317 } 317 }
318 318
319 mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const { 319 mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const {
320 mach_port_t task = 0; 320 mach_port_t task = MACH_PORT_NULL;
321 if (port_provider_) 321 if (port_provider_)
322 task = port_provider_->TaskForPid(process_); 322 task = port_provider_->TaskForPid(process_);
323 if (!task && process_ == getpid()) 323 if (task == MACH_PORT_NULL && process_ == getpid())
324 task = mach_task_self(); 324 task = mach_task_self();
325 return task; 325 return task;
326 } 326 }
327 327
328 // ------------------------------------------------------------------------ 328 // ------------------------------------------------------------------------
329 329
330 // Bytes committed by the system. 330 // Bytes committed by the system.
331 size_t GetSystemCommitCharge() { 331 size_t GetSystemCommitCharge() {
332 host_name_port_t host = mach_host_self(); 332 host_name_port_t host = mach_host_self();
333 mach_msg_type_number_t count = HOST_VM_INFO_COUNT; 333 mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 CHECK(g_old_malloc && g_old_calloc && g_old_valloc && g_old_realloc) 432 CHECK(g_old_malloc && g_old_calloc && g_old_valloc && g_old_realloc)
433 << "Failed to get system allocation functions."; 433 << "Failed to get system allocation functions.";
434 434
435 default_zone->malloc = oom_killer_malloc; 435 default_zone->malloc = oom_killer_malloc;
436 default_zone->calloc = oom_killer_calloc; 436 default_zone->calloc = oom_killer_calloc;
437 default_zone->valloc = oom_killer_valloc; 437 default_zone->valloc = oom_killer_valloc;
438 default_zone->realloc = oom_killer_realloc; 438 default_zone->realloc = oom_killer_realloc;
439 } 439 }
440 440
441 } // namespace base 441 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698