$OpenBSD: patch-base_process_process_metrics_openbsd_cc,v 1.11 2019/05/27 16:11:17 robert Exp $

Index: base/process/process_metrics_openbsd.cc
--- base/process/process_metrics_openbsd.cc.orig
+++ base/process/process_metrics_openbsd.cc
@@ -4,10 +4,21 @@
 
 #include "base/process/process_metrics.h"
 
+#include "base/files/file_util.h"
+#include "base/logging.h"
+#include "base/process/internal_linux.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_split.h"
+#include "base/strings/string_tokenizer.h"
+#include "base/strings/string_util.h"
+#include "base/system/sys_info.h"
+#include "base/threading/thread_restrictions.h"
+
 #include <stddef.h>
 #include <stdint.h>
 #include <sys/param.h>
 #include <sys/sysctl.h>
+#include <sys/vmmeter.h>
 
 #include "base/memory/ptr_util.h"
 #include "base/process/process_metrics_iocounters.h"
@@ -25,48 +36,13 @@ bool ProcessMetrics::GetIOCounters(IoCounters* io_coun
   return false;
 }
 
-static int GetProcessCPU(pid_t pid) {
-  struct kinfo_proc info;
-  size_t length;
-  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid,
-                sizeof(struct kinfo_proc), 0 };
-
-  if (sysctl(mib, base::size(mib), NULL, &length, NULL, 0) < 0)
-    return -1;
-
-  mib[5] = (length / sizeof(struct kinfo_proc));
-
-  if (sysctl(mib, base::size(mib), &info, &length, NULL, 0) < 0)
-    return 0;
-
-  return info.p_pctcpu;
-}
-
-double ProcessMetrics::GetPlatformIndependentCPUUsage() {
-  TimeTicks time = TimeTicks::Now();
-
-  if (last_cpu_time_.is_zero()) {
-    // First call, just set the last values.
-    last_cpu_time_ = time;
-    return 0;
-  }
-
-  int cpu = GetProcessCPU(process_);
-
-  last_cpu_time_ = time;
-  double percentage = static_cast<double>((cpu * 100.0) / FSCALE);
-
-  return percentage;
-}
-
 TimeDelta ProcessMetrics::GetCumulativeCPUUsage() {
   NOTREACHED();
   return TimeDelta();
 }
 
 ProcessMetrics::ProcessMetrics(ProcessHandle process)
-    : process_(process),
-      last_cpu_(0) {}
+    : process_(process) {}
 
 size_t GetSystemCommitCharge() {
   int mib[] = { CTL_VM, VM_METER };
@@ -85,6 +61,149 @@ size_t GetSystemCommitCharge() {
   pagesize = getpagesize();
 
   return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize);
+}
+
+int ProcessMetrics::GetOpenFdCount() const {
+  struct kinfo_proc *info;
+  size_t length;
+  int total_count = 0;
+
+  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_,
+                sizeof(struct kinfo_proc), 0 };
+
+  if (sysctl(mib, base::size(mib), NULL, &length, NULL, 0) < 0)
+    return -1;
+
+  info = (struct kinfo_proc *)malloc(length);
+
+  mib[5] = (length / sizeof(struct kinfo_proc));
+
+  if (sysctl(mib, base::size(mib), info, &length, NULL, 0) < 0) {
+    total_count = -1;
+    goto out;
+  }
+  //total_count = info->p_fd->fd_openfd;
+  total_count = info->p_fd;
+
+out:
+  free(info);
+  return total_count;
+}
+
+int ProcessMetrics::GetOpenFdSoftLimit() const {
+  struct kinfo_proc *info;
+  size_t length;
+  int total_count = 0;
+
+  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_,
+                sizeof(struct kinfo_proc), 0 };
+
+  if (sysctl(mib, base::size(mib), NULL, &length, NULL, 0) < 0)
+    return -1;
+
+  info = (struct kinfo_proc *)malloc(length);
+
+  mib[5] = (length / sizeof(struct kinfo_proc));
+
+  if (sysctl(mib, base::size(mib), info, &length, NULL, 0) < 0) {
+    total_count = -1;
+    goto out;
+  }
+  //total_count = info->p_fd->fd_openfd;
+  total_count = info->p_limit;
+
+out:
+  free(info);
+  return total_count;
+}
+
+uint64_t ProcessMetrics::GetVmSwapBytes() const {
+  NOTIMPLEMENTED();
+  return 0;
+}
+
+std::unique_ptr<DictionaryValue> SystemMemoryInfoKB::ToValue() const {
+  auto res = std::make_unique<DictionaryValue>();
+  res->SetInteger("total", total);
+  res->SetInteger("free", free);
+  res->SetInteger("available", available);
+  res->SetInteger("buffers", buffers);
+  res->SetInteger("cached", cached);
+  res->SetInteger("active_anon", active_anon);
+  res->SetInteger("inactive_anon", inactive_anon);
+  res->SetInteger("active_file", active_file);
+  res->SetInteger("inactive_file", inactive_file);
+  res->SetInteger("swap_total", swap_total);
+  res->SetInteger("swap_free", swap_free);
+  res->SetInteger("swap_used", swap_total - swap_free);
+  res->SetInteger("dirty", dirty);
+  res->SetInteger("reclaimable", reclaimable);
+
+  return res;
+}
+
+bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
+  NOTIMPLEMENTED_LOG_ONCE();
+  return false;
+}
+
+SystemDiskInfo::SystemDiskInfo() {
+  reads = 0;
+  reads_merged = 0;
+  sectors_read = 0;
+  read_time = 0;
+  writes = 0;
+  writes_merged = 0;
+  sectors_written = 0;
+  write_time = 0;
+  io = 0;
+  io_time = 0;
+  weighted_io_time = 0;
+}
+     
+SystemDiskInfo::SystemDiskInfo(const SystemDiskInfo& other) = default;
+      
+std::unique_ptr<Value> SystemDiskInfo::ToValue() const {
+  auto res = std::make_unique<DictionaryValue>();
+  
+  // Write out uint64_t variables as doubles.
+  // Note: this may discard some precision, but for JS there's no other option.
+  res->SetDouble("reads", static_cast<double>(reads)); 
+  res->SetDouble("reads_merged", static_cast<double>(reads_merged));
+  res->SetDouble("sectors_read", static_cast<double>(sectors_read));
+  res->SetDouble("read_time", static_cast<double>(read_time));
+  res->SetDouble("writes", static_cast<double>(writes));
+  res->SetDouble("writes_merged", static_cast<double>(writes_merged));
+  res->SetDouble("sectors_written", static_cast<double>(sectors_written));
+  res->SetDouble("write_time", static_cast<double>(write_time));
+  res->SetDouble("io", static_cast<double>(io));
+  res->SetDouble("io_time", static_cast<double>(io_time));
+  res->SetDouble("weighted_io_time", static_cast<double>(weighted_io_time));
+    
+  return std::move(res);
+}
+    
+bool GetSystemDiskInfo(SystemDiskInfo* diskinfo) {
+  NOTIMPLEMENTED();
+  return false;
+}
+
+std::unique_ptr<DictionaryValue> VmStatInfo::ToValue() const {
+  auto res = std::make_unique<DictionaryValue>();
+  res->SetInteger("pswpin", pswpin);
+  res->SetInteger("pswpout", pswpout);
+  res->SetInteger("pgmajfault", pgmajfault);
+  return res;
+}
+
+bool GetVmStatInfo(VmStatInfo* vmstat) {
+  NOTIMPLEMENTED();
+  return false;
+}
+
+int ProcessMetrics::GetIdleWakeupsPerSecond() {
+  NOTIMPLEMENTED();
+  return 0;
 }
 
 }  // namespace base
