From 9e0c19ad98c9a3119e195025cc80c729883d7de9 Mon Sep 17 00:00:00 2001 From: npyl Date: Fri, 27 Jul 2018 02:36:14 +0300 Subject: [PATCH] Improve `get_freq` #20 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using the IntelĀ® Power Gadget API (https://software.intel.com/en-us/blogs/2012/12/13/using-the-intel-power-gadget-api-on-mac-os-x) we can now get actual Core frequency and not the constant factory one. Though, for some weird reason the API gives the same freq for all Cores, thus the |cpu| arg becomes useless. --- src/darwin.cc | 52 ++++++++++++++++--------------------------------- src/net_stat.cc | 4 ++++ 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/src/darwin.cc b/src/darwin.cc index cfb95c50a..e3ea1c016 100644 --- a/src/darwin.cc +++ b/src/darwin.cc @@ -74,6 +74,8 @@ #include "darwin_sip.h" // sip status +#include + /* clock_gettime includes */ #ifndef HAVE_CLOCK_GETTIME #include @@ -974,50 +976,30 @@ void get_acpi_fan(char * /*p_client_buffer*/, size_t /*client_buffer_size*/) { /* void */ char get_freq(char *p_client_buffer, size_t client_buffer_size, - const char *p_format, int divisor, unsigned int /*cpu*/) { + const char *p_format, int divisor, unsigned int cpu) { /* - * For now, we get the factory cpu frequency, not **current** cpu frequency - * (Also, it is always the same for every core, so ignore |cpu| argument) + * Our data is always the same for every core, so ignore |cpu| argument. */ - // XXX Probably find a way to get **current** cpu frequency - - int mib[2]; - unsigned int freq; - size_t len; - - if ((p_client_buffer == nullptr) || client_buffer_size <= 0 || - (p_format == nullptr) || divisor <= 0) { - return 0; + bool initialised = false; + + if (!initialised) + { + IntelEnergyLibInitialize(); + initialised = true; } + + int freq = 0; + GetIAFrequency(cpu, &freq); - mib[0] = CTL_HW; - mib[1] = HW_CPU_FREQ; - len = sizeof(freq); - - if (sysctl(mib, 2, &freq, &len, nullptr, 0) == 0) { - /* - * convert to MHz - */ - divisor *= 1000000; - - snprintf(p_client_buffer, client_buffer_size, p_format, - static_cast(freq) / divisor); - } else { - snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f); - return 0; - } + snprintf(p_client_buffer, client_buffer_size, p_format, + static_cast(freq)); + + // XXX provide a func which will call IntelEnergyLibShutdown() return 1; } -#if 0 -void update_wifi_stats(void) -{ - printf("update_wifi_stats: STUB but also in #if 0\n"); -} -#endif - int update_diskio() { printf("update_diskio: STUB\n"); return 0; diff --git a/src/net_stat.cc b/src/net_stat.cc index c5137333a..ce9077934 100644 --- a/src/net_stat.cc +++ b/src/net_stat.cc @@ -466,6 +466,10 @@ int interface_up(struct text_object *obj) { if (dev == nullptr) { return 0; } #if defined(__APPLE__) && defined(__MACH__) + // + // TODO: when opening pull request remember to ask if we should have only the macOS + // version for all OS, as shown at: showif.c + if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { #else if ((fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0) {