process_metrics_openbsd.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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
  3. // found in the LICENSE file.
  4. #include "base/process/process_metrics.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <sys/param.h>
  8. #include <sys/sysctl.h>
  9. #include "base/memory/ptr_util.h"
  10. #include "base/process/process_metrics_iocounters.h"
  11. namespace base {
  12. // static
  13. std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics(
  14. ProcessHandle process) {
  15. return WrapUnique(new ProcessMetrics(process));
  16. }
  17. bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
  18. return false;
  19. }
  20. static int GetProcessCPU(pid_t pid) {
  21. struct kinfo_proc info;
  22. size_t length;
  23. int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid,
  24. sizeof(struct kinfo_proc), 0 };
  25. if (sysctl(mib, std::size(mib), NULL, &length, NULL, 0) < 0)
  26. return -1;
  27. mib[5] = (length / sizeof(struct kinfo_proc));
  28. if (sysctl(mib, std::size(mib), &info, &length, NULL, 0) < 0)
  29. return 0;
  30. return info.p_pctcpu;
  31. }
  32. double ProcessMetrics::GetPlatformIndependentCPUUsage() {
  33. TimeTicks time = TimeTicks::Now();
  34. if (last_cpu_time_.is_zero()) {
  35. // First call, just set the last values.
  36. last_cpu_time_ = time;
  37. return 0;
  38. }
  39. int cpu = GetProcessCPU(process_);
  40. last_cpu_time_ = time;
  41. double percentage = static_cast<double>((cpu * 100.0) / FSCALE);
  42. return percentage;
  43. }
  44. TimeDelta ProcessMetrics::GetCumulativeCPUUsage() {
  45. NOTREACHED();
  46. return TimeDelta();
  47. }
  48. ProcessMetrics::ProcessMetrics(ProcessHandle process)
  49. : process_(process),
  50. last_cpu_(0) {}
  51. size_t GetSystemCommitCharge() {
  52. int mib[] = { CTL_VM, VM_METER };
  53. int pagesize;
  54. struct vmtotal vmtotal;
  55. unsigned long mem_total, mem_free, mem_inactive;
  56. size_t len = sizeof(vmtotal);
  57. if (sysctl(mib, std::size(mib), &vmtotal, &len, NULL, 0) < 0)
  58. return 0;
  59. mem_total = vmtotal.t_vm;
  60. mem_free = vmtotal.t_free;
  61. mem_inactive = vmtotal.t_vm - vmtotal.t_avm;
  62. pagesize = getpagesize();
  63. return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize);
  64. }
  65. } // namespace base