Skip to content

Commit

Permalink
Improve get_freq #20
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
npyl authored and brndnmtthws committed Aug 7, 2018
1 parent 15c7e9b commit 74d1ec0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
52 changes: 17 additions & 35 deletions src/darwin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@

#include "darwin_sip.h" // sip status

#include <IntelPowerGadget/EnergyLib.h>

/* clock_gettime includes */
#ifndef HAVE_CLOCK_GETTIME
#include <errno.h>
Expand Down Expand Up @@ -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<float>(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<float>(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;
Expand Down
4 changes: 4 additions & 0 deletions src/net_stat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,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) {
Expand Down

0 comments on commit 74d1ec0

Please sign in to comment.