process_metrics_freebsd.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 <sys/sysctl.h>
  7. #include <sys/user.h>
  8. #include <unistd.h>
  9. #include "base/memory/ptr_util.h"
  10. #include "base/process/process_metrics_iocounters.h"
  11. namespace base {
  12. ProcessMetrics::ProcessMetrics(ProcessHandle process)
  13. : process_(process),
  14. last_cpu_(0) {}
  15. // static
  16. std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics(
  17. ProcessHandle process) {
  18. return WrapUnique(new ProcessMetrics(process));
  19. }
  20. double ProcessMetrics::GetPlatformIndependentCPUUsage() {
  21. struct kinfo_proc info;
  22. int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, process_};
  23. size_t length = sizeof(info);
  24. if (sysctl(mib, std::size(mib), &info, &length, NULL, 0) < 0)
  25. return 0;
  26. return (info.ki_pctcpu / FSCALE) * 100.0;
  27. }
  28. TimeDelta ProcessMetrics::GetCumulativeCPUUsage() {
  29. NOTREACHED();
  30. return TimeDelta();
  31. }
  32. bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
  33. return false;
  34. }
  35. size_t GetSystemCommitCharge() {
  36. int mib[2], pagesize;
  37. unsigned long mem_total, mem_free, mem_inactive;
  38. size_t length = sizeof(mem_total);
  39. if (sysctl(mib, std::size(mib), &mem_total, &length, NULL, 0) < 0)
  40. return 0;
  41. length = sizeof(mem_free);
  42. if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length, NULL, 0) < 0)
  43. return 0;
  44. length = sizeof(mem_inactive);
  45. if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &length,
  46. NULL, 0) < 0) {
  47. return 0;
  48. }
  49. pagesize = getpagesize();
  50. return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize);
  51. }
  52. } // namespace base