diff options
-rw-r--r-- | xbmc/utils/CPUInfo.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/xbmc/utils/CPUInfo.cpp b/xbmc/utils/CPUInfo.cpp index f39f3f6206..76ddb765a8 100644 --- a/xbmc/utils/CPUInfo.cpp +++ b/xbmc/utils/CPUInfo.cpp @@ -31,6 +31,12 @@ #endif #endif +#if defined(TARGET_FREEBSD) +#include <sys/types.h> +#include <sys/sysctl.h> +#include <sys/resource.h> +#endif + #if defined(TARGET_LINUX) && defined(__ARM_NEON__) && !defined(TARGET_ANDROID) #include <fcntl.h> #include <unistd.h> @@ -196,6 +202,27 @@ CCPUInfo::CCPUInfo(void) CoreInfo core; m_cores[0] = core; +#elif defined(TARGET_FREEBSD) + size_t len; + int i; + char cpumodel[512]; + + len = sizeof(m_cpuCount); + if (sysctlbyname("hw.ncpu", &m_cpuCount, &len, NULL, 0) != 0) + m_cpuCount = 1; + + len = sizeof(cpumodel); + if (sysctlbyname("hw.model", &cpumodel, &len, NULL, 0) != 0) + (void)strncpy(cpumodel, "Unknown", 8); + m_cpuModel = cpumodel; + + for (i = 0; i < m_cpuCount; i++) + { + CoreInfo core; + core.m_id = i; + core.m_strModel = m_cpuModel; + m_cores[core.m_id] = core; + } #else m_fProcStat = fopen("/proc/stat", "r"); m_fProcTemperature = fopen("/proc/acpi/thermal_zone/THM0/temperature", "r"); @@ -448,6 +475,12 @@ float CCPUInfo::getCPUFrequency() return float(dwMHz); else return 0.f; +#elif defined(TARGET_FREEBSD) + int hz = 0; + size_t len = sizeof(hz); + if (sysctlbyname("dev.cpu.0.freq", &hz, &len, NULL, 0) != 0) + hz = 0; + return (float)hz; #else float mhz = 0.f; char buf[256], @@ -575,6 +608,65 @@ bool CCPUInfo::readProcStat(unsigned long long& user, unsigned long long& nice, io = 0; +#elif defined(TARGET_FREEBSD) + long *cptimes; + size_t len; + int i; + + len = sizeof(long) * 32 * CPUSTATES; + if (sysctlbyname("kern.cp_times", NULL, &len, NULL, 0) != 0) + return false; + cptimes = (long*)malloc(len); + if (cptimes == NULL) + return false; + if (sysctlbyname("kern.cp_times", cptimes, &len, NULL, 0) != 0) + { + free(cptimes); + return false; + } + user = 0; + nice = 0; + system = 0; + idle = 0; + io = 0; + for (i = 0; i < m_cpuCount; i++) + { + long coreUser, coreNice, coreSystem, coreIdle, coreIO; + double total; + + coreUser = cptimes[i * CPUSTATES + CP_USER]; + coreNice = cptimes[i * CPUSTATES + CP_NICE]; + coreSystem = cptimes[i * CPUSTATES + CP_SYS]; + coreIO = cptimes[i * CPUSTATES + CP_INTR]; + coreIdle = cptimes[i * CPUSTATES + CP_IDLE]; + + map<int, CoreInfo>::iterator iter = m_cores.find(i); + if (iter != m_cores.end()) + { + coreUser -= iter->second.m_user; + coreNice -= iter->second.m_nice; + coreSystem -= iter->second.m_system; + coreIdle -= iter->second.m_idle; + coreIO -= iter->second.m_io; + + total = (double)(coreUser + coreNice + coreSystem + coreIdle + coreIO); + if(total != 0.0f) + iter->second.m_fPct = ((double)(coreUser + coreNice + coreSystem) * 100.0) / total; + + iter->second.m_user += coreUser; + iter->second.m_nice += coreNice; + iter->second.m_system += coreSystem; + iter->second.m_idle += coreIdle; + iter->second.m_io += coreIO; + + user += coreUser; + nice += coreNice; + system += coreSystem; + idle += coreIdle; + io += coreIO; + } + } + free(cptimes); #else if (m_fProcStat == NULL) return false; |