Skip to content
This repository
Browse code

Merge pull request #1519 from Fneufneu/freebsd_cpu

Freebsd cpu infos
  • Loading branch information...
commit 41c67e2c8d155dfadc6f5d413b9034738e74b438 2 parents 552580d + d10fa2b
Fneufneu authored October 04, 2012

Showing 1 changed file with 92 additions and 0 deletions. Show diff stats Hide diff stats

  1. 92  xbmc/utils/CPUInfo.cpp
92  xbmc/utils/CPUInfo.cpp
@@ -31,6 +31,12 @@
31 31
 #endif
32 32
 #endif
33 33
 
  34
+#if defined(TARGET_FREEBSD)
  35
+#include <sys/types.h>
  36
+#include <sys/sysctl.h>
  37
+#include <sys/resource.h>
  38
+#endif
  39
+
34 40
 #if defined(TARGET_LINUX) && defined(__ARM_NEON__) && !defined(TARGET_ANDROID)
35 41
 #include <fcntl.h>
36 42
 #include <unistd.h>
@@ -196,6 +202,27 @@ CCPUInfo::CCPUInfo(void)
196 202
   CoreInfo core;
197 203
   m_cores[0] = core;
198 204
 
  205
+#elif defined(TARGET_FREEBSD)
  206
+  size_t len;
  207
+  int i;
  208
+  char cpumodel[512];
  209
+
  210
+  len = sizeof(m_cpuCount);
  211
+  if (sysctlbyname("hw.ncpu", &m_cpuCount, &len, NULL, 0) != 0)
  212
+    m_cpuCount = 1;
  213
+
  214
+  len = sizeof(cpumodel);
  215
+  if (sysctlbyname("hw.model", &cpumodel, &len, NULL, 0) != 0)
  216
+    (void)strncpy(cpumodel, "Unknown", 8);
  217
+  m_cpuModel = cpumodel;
  218
+
  219
+  for (i = 0; i < m_cpuCount; i++)
  220
+  {
  221
+    CoreInfo core;
  222
+    core.m_id = i;
  223
+    core.m_strModel = m_cpuModel;
  224
+    m_cores[core.m_id] = core;
  225
+  }
199 226
 #else
200 227
   m_fProcStat = fopen("/proc/stat", "r");
201 228
   m_fProcTemperature = fopen("/proc/acpi/thermal_zone/THM0/temperature", "r");
@@ -448,6 +475,12 @@ float CCPUInfo::getCPUFrequency()
448 475
     return float(dwMHz);
449 476
   else
450 477
     return 0.f;
  478
+#elif defined(TARGET_FREEBSD)
  479
+  int hz = 0;
  480
+  size_t len = sizeof(hz);
  481
+  if (sysctlbyname("dev.cpu.0.freq", &hz, &len, NULL, 0) != 0)
  482
+    hz = 0;
  483
+  return (float)hz;
451 484
 #else
452 485
   float mhz = 0.f;
453 486
   char buf[256],
@@ -575,6 +608,65 @@ bool CCPUInfo::readProcStat(unsigned long long& user, unsigned long long& nice,
575 608
 
576 609
   io = 0;
577 610
 
  611
+#elif defined(TARGET_FREEBSD)
  612
+  long *cptimes;
  613
+  size_t len;
  614
+  int i;
  615
+
  616
+  len = sizeof(long) * 32 * CPUSTATES;
  617
+  if (sysctlbyname("kern.cp_times", NULL, &len, NULL, 0) != 0)
  618
+    return false;
  619
+  cptimes = (long*)malloc(len);
  620
+  if (cptimes == NULL)
  621
+    return false;
  622
+  if (sysctlbyname("kern.cp_times", cptimes, &len, NULL, 0) != 0)
  623
+  {
  624
+    free(cptimes);
  625
+    return false;
  626
+  }
  627
+  user = 0;
  628
+  nice = 0;
  629
+  system = 0;
  630
+  idle = 0;
  631
+  io = 0;
  632
+  for (i = 0; i < m_cpuCount; i++)
  633
+  {
  634
+    long coreUser, coreNice, coreSystem, coreIdle, coreIO;
  635
+    double total;
  636
+
  637
+    coreUser   = cptimes[i * CPUSTATES + CP_USER];
  638
+    coreNice   = cptimes[i * CPUSTATES + CP_NICE];
  639
+    coreSystem = cptimes[i * CPUSTATES + CP_SYS];
  640
+    coreIO     = cptimes[i * CPUSTATES + CP_INTR];
  641
+    coreIdle   = cptimes[i * CPUSTATES + CP_IDLE];
  642
+
  643
+    map<int, CoreInfo>::iterator iter = m_cores.find(i);
  644
+    if (iter != m_cores.end())
  645
+    {
  646
+      coreUser -= iter->second.m_user;
  647
+      coreNice -= iter->second.m_nice;
  648
+      coreSystem -= iter->second.m_system;
  649
+      coreIdle -= iter->second.m_idle;
  650
+      coreIO -= iter->second.m_io;
  651
+
  652
+      total = (double)(coreUser + coreNice + coreSystem + coreIdle + coreIO);
  653
+      if(total != 0.0f)
  654
+        iter->second.m_fPct = ((double)(coreUser + coreNice + coreSystem) * 100.0) / total;
  655
+
  656
+      iter->second.m_user += coreUser;
  657
+      iter->second.m_nice += coreNice;
  658
+      iter->second.m_system += coreSystem;
  659
+      iter->second.m_idle += coreIdle;
  660
+      iter->second.m_io += coreIO;
  661
+
  662
+      user   += coreUser;
  663
+      nice   += coreNice;
  664
+      system += coreSystem;
  665
+      idle   += coreIdle;
  666
+      io     += coreIO;
  667
+    }
  668
+  }
  669
+  free(cptimes);
578 670
 #else
579 671
   if (m_fProcStat == NULL)
580 672
     return false;

0 notes on commit 41c67e2

Please sign in to comment.
Something went wrong with that request. Please try again.