1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-25 04:06:03 +00:00

Conky for mac os master (#579)

* Try to amend #31

* BUILD_WLAN should be available for all OS.

Keep BUILD_WLAN OFF by default for compatibility reasons.

* WLAN-related variables should be available for every OS.

There are some problems (probably null-dereference)

* Fix $wireless_essid crashing conky if no argument provided.

Conky wasn't parsing the argument of the variable as it should, thus wasn't allocating the `dev` member variable.

Also fix some documentation stuff.

* Improve `get_freq` #20

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.

* Oops, this accidently slipped in

* Introduce BUILD_IPGFREQ build option

This build option has been introduced for one particular reason:

On macOS getting current core-frequency is not supported by the APIs.  A solution is to install Intel's ® Power Gadget which comes with an .app, a Framework and a kernel-extension.  Though, this may trouble some alot, thus introduce BUILD_IPGFREQ.

* Forgot static here.

* Some improvements for get_freq again.

Fix frequency not printing correctly (I wasn't using the divisor)
Add more guards.

* Setup cmake files and project code for Objective-C code #17

We want to use CoreWLAN framework.

* update_cpu_usage() now supports multiple cores

Also, some cleanup.

* Updated default conky config to monitor Mac Networking

* Made Mac Friendly BuildOptions and generic default conky configs

* Undid Xdamage config and cleaned up previous IF statements

* Re-Added XDamage fix

* Finish up the algorithm. I think its now correct. Closes: #33

* Cleanup macro and introduce a no-op free_cpu() function for ALL cpu-related variables

free_cpu() must be implemented for every OS and on all except macOS its a no-op function.

* Reformat, add empty comment.
This commit is contained in:
Brenden Matthews 2018-08-07 11:54:01 -04:00 committed by GitHub
parent b17f2c2ca9
commit 8aa9c819f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 231 additions and 156 deletions

View File

@ -63,6 +63,12 @@ if (NOT LIB_INSTALL_DIR)
endif (NOT LIB_INSTALL_DIR) endif (NOT LIB_INSTALL_DIR)
set(PACKAGE_LIBRARY_DIR "${LIB_INSTALL_DIR}/conky" CACHE STRING "Package library path (where Lua bindings are installed" FORCE) set(PACKAGE_LIBRARY_DIR "${LIB_INSTALL_DIR}/conky" CACHE STRING "Package library path (where Lua bindings are installed" FORCE)
set(DEFAULTNETDEV "eth0" CACHE STRING "Default networkdevice") set(DEFAULTNETDEV "eth0" CACHE STRING "Default networkdevice")
# Mac only override
if(OS_DARWIN)
set(DEFAULTNETDEV "en0" CACHE STRING "Default networkdevice" FORCE)
endif(OS_DARWIN)
set(XDG_CONFIG_FILE "$HOME/.config/conky/conky.conf" CACHE STRING "Configfile of the user (XDG)") set(XDG_CONFIG_FILE "$HOME/.config/conky/conky.conf" CACHE STRING "Configfile of the user (XDG)")
set(CONFIG_FILE "$HOME/.conkyrc" CACHE STRING "Configfile of the user") set(CONFIG_FILE "$HOME/.conkyrc" CACHE STRING "Configfile of the user")
set(MAX_USER_TEXT_DEFAULT "16384" CACHE STRING "Default maximum size of config TEXT buffer, i.e. below TEXT line.") set(MAX_USER_TEXT_DEFAULT "16384" CACHE STRING "Default maximum size of config TEXT buffer, i.e. below TEXT line.")
@ -76,7 +82,6 @@ if(OS_LINUX)
option(BUILD_PORT_MONITORS "Build TCP portmon support" true) option(BUILD_PORT_MONITORS "Build TCP portmon support" true)
option(BUILD_IBM "Support for IBM/Lenovo notebooks" true) option(BUILD_IBM "Support for IBM/Lenovo notebooks" true)
option(BUILD_HDDTEMP "Support for hddtemp" true) option(BUILD_HDDTEMP "Support for hddtemp" true)
option(BUILD_WLAN "Enable wireless support" false)
# nvidia may also work on FreeBSD, not sure # nvidia may also work on FreeBSD, not sure
option(BUILD_NVIDIA "Enable nvidia support" false) option(BUILD_NVIDIA "Enable nvidia support" false)
option(BUILD_IPV6 "Enable if you want IPv6 support" true) option(BUILD_IPV6 "Enable if you want IPv6 support" true)
@ -84,14 +89,20 @@ else(OS_LINUX)
set(BUILD_PORT_MONITORS false) set(BUILD_PORT_MONITORS false)
set(BUILD_IBM false) set(BUILD_IBM false)
set(BUILD_HDDTEMP false) set(BUILD_HDDTEMP false)
set(BUILD_WLAN false)
set(BUILD_NVIDIA false) set(BUILD_NVIDIA false)
set(BUILD_IPV6 false) set(BUILD_IPV6 false)
endif(OS_LINUX) endif(OS_LINUX)
# macOS Only
if(OS_DARWIN)
option(BUILD_IPGFREQ "Enable cpu freq calculation based on Intel® Power Gadget; otherwise use constant factory value" false)
endif(OS_DARWIN)
# Optional features etc # Optional features etc
# #
option(BUILD_WLAN "Enable wireless support" false)
option(BUILD_BUILTIN_CONFIG "Enable builtin default configuration" true) option(BUILD_BUILTIN_CONFIG "Enable builtin default configuration" true)
option(BUILD_IOSTATS "Enable disk I/O stats" true) option(BUILD_IOSTATS "Enable disk I/O stats" true)
@ -110,7 +121,14 @@ endif(BUILD_NCURSES)
option(BUILD_X11 "Build X11 support" true) option(BUILD_X11 "Build X11 support" true)
if(BUILD_X11) if(BUILD_X11)
option(OWN_WINDOW "Enable own_window support" true) option(OWN_WINDOW "Enable own_window support" true)
# Mac Fix
if(OS_DARWIN)
option(BUILD_XDAMAGE "Build Xdamage support" false)
else(OS_DARWIN)
option(BUILD_XDAMAGE "Build Xdamage support" true) option(BUILD_XDAMAGE "Build Xdamage support" true)
endif(OS_DARWIN)
option(BUILD_XINERAMA "Build Xinerama support" true) option(BUILD_XINERAMA "Build Xinerama support" true)
option(BUILD_XDBE "Build Xdbe (double-buffer) support" true) option(BUILD_XDBE "Build Xdbe (double-buffer) support" true)
option(BUILD_XFT "Build Xft (freetype fonts) support" true) option(BUILD_XFT "Build Xft (freetype fonts) support" true)

View File

@ -137,6 +137,16 @@ if(BUILD_NCURSES AND OS_DARWIN)
set(conky_libs ${conky_libs} -lncurses) set(conky_libs ${conky_libs} -lncurses)
endif(BUILD_NCURSES AND OS_DARWIN) endif(BUILD_NCURSES AND OS_DARWIN)
if(BUILD_WLAN AND OS_DARWIN)
find_library(CW CoreWLAN)
set(conky_libs ${conky_libs} ${CW})
endif(BUILD_WLAN AND OS_DARWIN)
if(OS_DARWIN AND BUILD_IPGFREQ)
find_library(IPG IntelPowerGadget)
set(conky_libs ${conky_libs} ${IPG})
endif(OS_DARWIN AND BUILD_IPGFREQ)
if(BUILD_MATH) if(BUILD_MATH)
set(conky_libs ${conky_libs} -lm) set(conky_libs ${conky_libs} -lm)
endif(BUILD_MATH) endif(BUILD_MATH)
@ -198,7 +208,7 @@ if(BUILD_MYSQL)
set(conky_libs ${conky_libs} ${MYSQLCLIENT_LIB}) set(conky_libs ${conky_libs} ${MYSQLCLIENT_LIB})
endif(BUILD_MYSQL) endif(BUILD_MYSQL)
if(BUILD_WLAN) if(BUILD_WLAN AND OS_LINUX)
set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_include_files(iwlib.h IWLIB_H) check_include_files(iwlib.h IWLIB_H)
if(NOT IWLIB_H) if(NOT IWLIB_H)
@ -210,7 +220,7 @@ if(BUILD_WLAN)
endif(NOT IWLIB_LIB) endif(NOT IWLIB_LIB)
set(conky_libs ${conky_libs} ${IWLIB_LIB}) set(conky_libs ${conky_libs} ${IWLIB_LIB})
check_function_exists(iw_sockets_open IWLIB_SOCKETS_OPEN_FUNC) check_function_exists(iw_sockets_open IWLIB_SOCKETS_OPEN_FUNC)
endif(BUILD_WLAN) endif(BUILD_WLAN AND OS_LINUX)
if(BUILD_PORT_MONITORS) if(BUILD_PORT_MONITORS)
check_function_exists(getnameinfo HAVE_GETNAMEINFO) check_function_exists(getnameinfo HAVE_GETNAMEINFO)

View File

@ -102,6 +102,8 @@
#cmakedefine BUILD_IOSTATS 1 #cmakedefine BUILD_IOSTATS 1
#cmakedefine BUILD_IPGFREQ 0
#cmakedefine BUILD_WLAN 1 #cmakedefine BUILD_WLAN 1
#cmakedefine BUILD_ICAL 1 #cmakedefine BUILD_ICAL 1

View File

@ -72,7 +72,7 @@ $hr
${color grey}File systems: ${color grey}File systems:
/ $color${fs_used /}/${fs_size /} ${fs_bar 6 /} / $color${fs_used /}/${fs_size /} ${fs_bar 6 /}
${color grey}Networking: ${color grey}Networking:
Up:$color ${upspeed eth0} ${color grey} - Down:$color ${downspeed eth0} Up:$color ${upspeed} ${color grey} - Down:$color ${downspeed}
$hr $hr
${color grey}Name PID CPU% MEM% ${color grey}Name PID CPU% MEM%
${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1} ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}

View File

@ -46,7 +46,7 @@ Processes: $processes Running: $running_processes
File systems: File systems:
/ ${fs_used /}/${fs_size /} ${fs_bar 6 /} / ${fs_used /}/${fs_size /} ${fs_bar 6 /}
Networking: Networking:
Up: ${upspeed eth0} - Down: ${downspeed eth0} Up: ${upspeed} - Down: ${downspeed}
Name PID CPU% MEM% Name PID CPU% MEM%
${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}
${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}

View File

@ -4784,7 +4784,7 @@
</command> </command>
<option>(net)</option> <option>(net)</option>
</term> </term>
<listitem>WLAN channel on which device 'net' is listening (Linux only) <listitem>WLAN channel on which device 'net' is listening
<para /></listitem> <para /></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
@ -4804,7 +4804,7 @@
</command> </command>
<option>(net)</option> <option>(net)</option>
</term> </term>
<listitem>Frequency on which device 'net' is listening (Linux only) <listitem>Frequency on which device 'net' is listening
<para /></listitem> <para /></listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -135,7 +135,7 @@ endif(OS_HAIKU)
if(OS_DARWIN) if(OS_DARWIN)
set(darwin set(darwin
darwin.cc darwin.h darwin.mm darwin.h
darwin_sip.h darwin_sip.h
i18n.h i18n.h
) )

View File

@ -158,6 +158,8 @@ uint8_t battery_percentage(struct text_object *);
void print_battery_short(struct text_object *, char *, int); void print_battery_short(struct text_object *, char *, int);
#endif /* !__OpenBSD__ */ #endif /* !__OpenBSD__ */
void free_cpu(struct text_object *);
void print_blink(struct text_object *, char *, int); void print_blink(struct text_object *, char *, int);
void print_include(struct text_object *, char *, int); void print_include(struct text_object *, char *, int);

View File

@ -485,9 +485,12 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
} }
obj->callbacks.print = &print_voltage_v; obj->callbacks.print = &print_voltage_v;
#endif /* __linux__ */
#ifdef BUILD_WLAN #ifdef BUILD_WLAN
END OBJ(wireless_essid, &update_net_stats) obj->data.opaque = END OBJ(wireless_essid, &update_net_stats) obj->data.opaque =
get_net_stat(arg, obj, free_at_crash); get_net_stat(arg, obj, free_at_crash);
parse_net_stat_arg(obj, arg, free_at_crash);
obj->callbacks.print = &print_wireless_essid; obj->callbacks.print = &print_wireless_essid;
END OBJ(wireless_channel, &update_net_stats) END OBJ(wireless_channel, &update_net_stats)
parse_net_stat_arg(obj, arg, free_at_crash); parse_net_stat_arg(obj, arg, free_at_crash);
@ -518,8 +521,6 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
obj->callbacks.barval = &wireless_link_barval; obj->callbacks.barval = &wireless_link_barval;
#endif /* BUILD_WLAN */ #endif /* BUILD_WLAN */
#endif /* __linux__ */
#ifndef __OpenBSD__ #ifndef __OpenBSD__
END OBJ(acpifan, nullptr) obj->callbacks.print = &print_acpifan; END OBJ(acpifan, nullptr) obj->callbacks.print = &print_acpifan;
END OBJ(battery, nullptr) char bat[64]; END OBJ(battery, nullptr) char bat[64];
@ -673,18 +674,21 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
END OBJ(cpu, &update_cpu_usage) get_cpu_count(); END OBJ(cpu, &update_cpu_usage) get_cpu_count();
SCAN_CPU(arg, obj->data.i); SCAN_CPU(arg, obj->data.i);
obj->callbacks.percentage = &cpu_percentage; obj->callbacks.percentage = &cpu_percentage;
obj->callbacks.free = &free_cpu;
DBGP2("Adding $cpu for CPU %d", obj->data.i); DBGP2("Adding $cpu for CPU %d", obj->data.i);
#ifdef BUILD_X11 #ifdef BUILD_X11
END OBJ(cpugauge, &update_cpu_usage) get_cpu_count(); END OBJ(cpugauge, &update_cpu_usage) get_cpu_count();
SCAN_CPU(arg, obj->data.i); SCAN_CPU(arg, obj->data.i);
scan_gauge(obj, arg, 1); scan_gauge(obj, arg, 1);
obj->callbacks.gaugeval = &cpu_barval; obj->callbacks.gaugeval = &cpu_barval;
obj->callbacks.free = &free_cpu;
DBGP2("Adding $cpugauge for CPU %d", obj->data.i); DBGP2("Adding $cpugauge for CPU %d", obj->data.i);
#endif #endif
END OBJ(cpubar, &update_cpu_usage) get_cpu_count(); END OBJ(cpubar, &update_cpu_usage) get_cpu_count();
SCAN_CPU(arg, obj->data.i); SCAN_CPU(arg, obj->data.i);
scan_bar(obj, arg, 1); scan_bar(obj, arg, 1);
obj->callbacks.barval = &cpu_barval; obj->callbacks.barval = &cpu_barval;
obj->callbacks.free = &free_cpu;
DBGP2("Adding $cpubar for CPU %d", obj->data.i); DBGP2("Adding $cpubar for CPU %d", obj->data.i);
#ifdef BUILD_X11 #ifdef BUILD_X11
END OBJ(cpugraph, &update_cpu_usage) get_cpu_count(); END OBJ(cpugraph, &update_cpu_usage) get_cpu_count();
@ -694,6 +698,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
DBGP2("Adding $cpugraph for CPU %d", obj->data.i); DBGP2("Adding $cpugraph for CPU %d", obj->data.i);
free_and_zero(buf); free_and_zero(buf);
obj->callbacks.graphval = &cpu_barval; obj->callbacks.graphval = &cpu_barval;
obj->callbacks.free = &free_cpu;
END OBJ(loadgraph, &update_load_average) scan_loadgraph_arg(obj, arg); END OBJ(loadgraph, &update_load_average) scan_loadgraph_arg(obj, arg);
obj->callbacks.graphval = &loadgraphval; obj->callbacks.graphval = &loadgraphval;
#endif /* BUILD_X11 */ #endif /* BUILD_X11 */

View File

@ -56,4 +56,6 @@ int get_entropy_poolsize(const unsigned int *);
int get_sip_status(void); int get_sip_status(void);
void print_sip_status(struct text_object *obj, char *p, int p_max_size); void print_sip_status(struct text_object *obj, char *p, int p_max_size);
void deallocate_cpu_sample(struct text_object *obj);
#endif /*DARWIN_H*/ #endif /*DARWIN_H*/

View File

@ -42,12 +42,6 @@
*is available. patched the _csr_check function to return the bool bit instead. *is available. patched the _csr_check function to return the bool bit instead.
*/ */
/*
* Apologies for the code style...
* In my eyes it feels better to have
* different styles at some specific places... :)
*/
#include "conky.h" // for struct info #include "conky.h" // for struct info
#include "darwin.h" #include "darwin.h"
@ -74,6 +68,16 @@
#include "darwin_sip.h" // sip status #include "darwin_sip.h" // sip status
#include <vector>
#ifdef BUILD_IPGFREQ
#include <IntelPowerGadget/EnergyLib.h>
#endif
#ifdef BUILD_WLAN
#import <CoreWLAN/CoreWLAN.h>
#endif
/* clock_gettime includes */ /* clock_gettime includes */
#ifndef HAVE_CLOCK_GETTIME #ifndef HAVE_CLOCK_GETTIME
#include <errno.h> #include <errno.h>
@ -106,6 +110,10 @@ void eprintf(const char *fmt, ...) {
*/ */
static conky::simple_config_setting<bool> top_cpu_separate("top_cpu_separate", static conky::simple_config_setting<bool> top_cpu_separate("top_cpu_separate",
false, true); false, true);
/*
* used by update_cpu_usage()
*/
struct cpusample *sample_handle = nullptr;
static int getsysctl(const char *name, void *ptr, size_t len) { static int getsysctl(const char *name, void *ptr, size_t len) {
size_t nlen = len; size_t nlen = len;
@ -219,12 +227,11 @@ typedef struct memorysample {
* Gets systemTime, userTime and idleTime for CPU * Gets systemTime, userTime and idleTime for CPU
* MenuMeters has been great inspiration for this function * MenuMeters has been great inspiration for this function
*/ */
static void get_cpu_sample(struct cpusample *sample) { static void get_cpu_sample(struct cpusample **sample) {
host_name_port_t machHost; host_name_port_t machHost;
natural_t processorCount; natural_t processorCount;
processor_cpu_load_info_t processorTickInfo; processor_cpu_load_info_t processorTickInfo;
mach_msg_type_number_t processorInfoCount; mach_msg_type_number_t processorInfoCount;
struct cpusample *samples = nullptr;
machHost = mach_host_self(); machHost = mach_host_self();
@ -237,22 +244,15 @@ static void get_cpu_sample(struct cpusample *sample) {
return; return;
} }
/*
* allocate ncpus+1 cpusample structs (one foreach CPU)
* ** samples[0] is overal CPU usage
*/
samples = new struct cpusample[processorCount + 1];
memset(samples, 0, sizeof(cpusample) * (processorCount + 1));
/* /*
* start from samples[1] because samples[0] is overall CPU usage * start from samples[1] because samples[0] is overall CPU usage
*/ */
for (natural_t i = 1; i < processorCount + 1; i++) { for (natural_t i = 1; i < processorCount + 1; i++) {
samples[i].totalSystemTime = (*sample)[i].totalSystemTime =
processorTickInfo[i - 1].cpu_ticks[CPU_STATE_SYSTEM], processorTickInfo[i - 1].cpu_ticks[CPU_STATE_SYSTEM],
samples[i].totalUserTime = (*sample)[i].totalUserTime =
processorTickInfo[i - 1].cpu_ticks[CPU_STATE_USER], processorTickInfo[i - 1].cpu_ticks[CPU_STATE_USER],
samples[i].totalIdleTime = (*sample)[i].totalIdleTime =
processorTickInfo[i - 1].cpu_ticks[CPU_STATE_IDLE]; processorTickInfo[i - 1].cpu_ticks[CPU_STATE_IDLE];
} }
@ -260,24 +260,37 @@ static void get_cpu_sample(struct cpusample *sample) {
* sum up all totals * sum up all totals
*/ */
for (natural_t i = 1; i < processorCount + 1; i++) { for (natural_t i = 1; i < processorCount + 1; i++) {
samples[0].totalSystemTime += samples[i].totalSystemTime; (*sample)[0].totalSystemTime += (*sample)[i].totalSystemTime;
samples[0].totalUserTime += samples[i].totalUserTime; (*sample)[0].totalUserTime += (*sample)[i].totalUserTime;
samples[0].totalIdleTime += samples[i].totalIdleTime; (*sample)[0].totalIdleTime += (*sample)[i].totalIdleTime;
} }
/*
* set the sample pointer
*/
sample->totalSystemTime = samples[0].totalSystemTime;
sample->totalUserTime = samples[0].totalUserTime;
sample->totalIdleTime = samples[0].totalIdleTime;
/* /*
* Dealloc * Dealloc
*/ */
vm_deallocate(mach_task_self(), (vm_address_t)processorTickInfo, vm_deallocate(mach_task_self(), (vm_address_t)processorTickInfo,
static_cast<vm_size_t>(processorInfoCount * sizeof(natural_t))); static_cast<vm_size_t>(processorInfoCount * sizeof(natural_t)));
delete[] samples; }
void allocate_cpu_sample(struct cpusample **sample) {
if (*sample != nullptr)
return;
/*
* allocate ncpus+1 cpusample structs (one foreach CPU)
* ** sample_handle[0] is overal CPU usage
*/
*sample = reinterpret_cast<struct cpusample *>(malloc(sizeof(cpusample) * (info.cpu_count + 1)));
memset(*sample, 0, sizeof(cpusample) * (info.cpu_count + 1));
sample_handle = *sample; /* use a public handle for deallocating */
}
void free_cpu(struct text_object *) {
if (sample_handle != nullptr) {
free(sample_handle);
sample_handle = nullptr;
}
} }
/* /*
@ -567,10 +580,8 @@ uint64_t get_physical_memory() {
} }
int update_meminfo() { int update_meminfo() {
/* XXX implement remaining memory-related variables (see conky.h) */
/* XXX conky breaks the values ... :( probably some rounding problem... /* XXX See #34 */
Though we get the right values (based on top) */
/* XXX probably investigate the "probably apple keeps some info secret" */
vm_size_t page_size = getpagesize(); // get pagesize in bytes vm_size_t page_size = getpagesize(); // get pagesize in bytes
unsigned long swap_avail, swap_free; unsigned long swap_avail, swap_free;
@ -638,6 +649,14 @@ int update_meminfo() {
return 0; return 0;
} }
#ifdef BUILD_WLAN
void update_wlan_stats(struct net_stat *ns) {
}
#endif
int update_net_stats() { int update_net_stats() {
struct net_stat *ns; struct net_stat *ns;
double delta; double delta;
@ -657,6 +676,10 @@ int update_net_stats() {
if ((ifa->ifa_flags & IFF_UP) != 0u) { if ((ifa->ifa_flags & IFF_UP) != 0u) {
struct ifaddrs *iftmp; struct ifaddrs *iftmp;
#ifdef BUILD_WLAN
update_wlan_stats(ns);
#endif
ns->up = 1; ns->up = 1;
last_recv = ns->recv; last_recv = ns->recv;
last_trans = ns->trans; last_trans = ns->trans;
@ -843,8 +866,6 @@ void get_cpu_count() {
info.cpu_count = 0; info.cpu_count = 0;
} }
/* XXX this can be moved to update_cpu_usage() but keep here to follow linux
* implementation */
if (info.cpu_usage == nullptr) { if (info.cpu_usage == nullptr) {
/* /*
* Allocate ncpus+1 slots because cpu_usage[0] is overall usage. * Allocate ncpus+1 slots because cpu_usage[0] is overall usage.
@ -864,8 +885,6 @@ struct cpu_info {
}; };
int update_cpu_usage() { int update_cpu_usage() {
/* XXX add support for multiple cpus (see linux.cc) */
static bool cpu_setup = 0; static bool cpu_setup = 0;
long used, total; long used, total;
@ -873,7 +892,7 @@ int update_cpu_usage() {
unsigned int malloc_cpu_size = 0; unsigned int malloc_cpu_size = 0;
extern void *global_cpu; extern void *global_cpu;
struct cpusample sample {}; static struct cpusample *sample = nullptr;
static pthread_mutex_t last_stat_update_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t last_stat_update_mutex = PTHREAD_MUTEX_INITIALIZER;
static double last_stat_update = 0.0; static double last_stat_update = 0.0;
@ -905,19 +924,29 @@ int update_cpu_usage() {
global_cpu = cpu; global_cpu = cpu;
} }
get_cpu_sample(&sample); allocate_cpu_sample(&sample);
total = sample.totalUserTime + sample.totalIdleTime + sample.totalSystemTime;
used = total - sample.totalIdleTime;
if ((total - cpu[0].oldtotal) != 0) { get_cpu_sample(&sample);
info.cpu_usage[0] = (static_cast<double>(used - cpu[0].oldused)) /
static_cast<double>(total - cpu[0].oldtotal); /*
* Setup conky's structs for-each core
*/
for (int i = 1; i < info.cpu_count + 1; i++) {
int j = i - 1;
total = sample[i].totalUserTime + sample[i].totalIdleTime + sample[i].totalSystemTime;
used = total - sample[i].totalIdleTime;
if ((total - cpu[j].oldtotal) != 0) {
info.cpu_usage[j] = (static_cast<double>(used - cpu[j].oldused)) /
static_cast<double>(total - cpu[j].oldtotal);
} else { } else {
info.cpu_usage[0] = 0; info.cpu_usage[j] = 0;
} }
cpu[0].oldused = used; cpu[j].oldused = used;
cpu[0].oldtotal = total; cpu[j].oldtotal = total;
}
return 0; return 0;
} }
@ -974,23 +1003,41 @@ void get_acpi_fan(char * /*p_client_buffer*/, size_t /*client_buffer_size*/) {
/* void */ /* void */
char get_freq(char *p_client_buffer, size_t client_buffer_size, 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)
*/
// 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 || if ((p_client_buffer == nullptr) || client_buffer_size <= 0 ||
(p_format == nullptr) || divisor <= 0) { (p_format == nullptr) || divisor <= 0) {
return 0; return 0;
} }
#ifdef BUILD_IPGFREQ
/*
* Our data is always the same for every core, so ignore |cpu| argument.
*/
static bool initialised = false;
if (!initialised) {
IntelEnergyLibInitialize();
initialised = true;
}
int freq = 0;
GetIAFrequency(cpu, &freq);
snprintf(p_client_buffer, client_buffer_size, p_format,
static_cast<float>(freq) / divisor);
#else
/*
* We get the factory cpu frequency, not **current** cpu frequency
* (Also, it is always the same for every core, so ignore |cpu| argument)
* Enable BUILD_IPGFREQ for getting current frequency.
*/
int mib[2];
unsigned int freq;
size_t len;
mib[0] = CTL_HW; mib[0] = CTL_HW;
mib[1] = HW_CPU_FREQ; mib[1] = HW_CPU_FREQ;
len = sizeof(freq); len = sizeof(freq);
@ -1007,17 +1054,11 @@ char get_freq(char *p_client_buffer, size_t client_buffer_size,
snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f); snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
return 0; return 0;
} }
#endif
return 1; return 1;
} }
#if 0
void update_wifi_stats(void)
{
printf("update_wifi_stats: STUB but also in #if 0\n");
}
#endif
int update_diskio() { int update_diskio() {
printf("update_diskio: STUB\n"); printf("update_diskio: STUB\n");
return 0; return 0;
@ -1058,11 +1099,13 @@ static void calc_cpu_usage_for_proc(struct process *proc, uint64_t total) {
*/ */
static void calc_cpu_total(struct process *proc, uint64_t *total) { static void calc_cpu_total(struct process *proc, uint64_t *total) {
uint64_t current_total = 0; /* of current iteration */ uint64_t current_total = 0; /* of current iteration */
struct cpusample sample {}; struct cpusample *sample = nullptr;
allocate_cpu_sample(&sample);
get_cpu_sample(&sample); get_cpu_sample(&sample);
current_total = current_total =
sample.totalUserTime + sample.totalIdleTime + sample.totalSystemTime; sample[0].totalUserTime + sample[0].totalIdleTime + sample[0].totalSystemTime;
*total = current_total - proc->previous_total_cpu_time; *total = current_total - proc->previous_total_cpu_time;
proc->previous_total_cpu_time = current_total; proc->previous_total_cpu_time = current_total;
@ -1175,17 +1218,7 @@ void get_top_info() {
struct kinfo_proc *p = nullptr; struct kinfo_proc *p = nullptr;
/* /*
* QUICKFIX for #16 * See #16
* XXX if we run conky -t '${top_mem mem 1}' it will crash because
* info.cpu_count is not initialised.
*
* We can initialise it down here, but it seems like in the linux
* implementation of get_top_info() there is no call to the get_cpu_count()
* function. Neither is there in core.cc... If this is the case, when is
* info.cpu_count initialised???
*
* Find a proper better place for get_cpu_count() call. (for comformance with
* linux.cc)
*/ */
get_cpu_count(); get_cpu_count();

View File

@ -126,9 +126,7 @@ int check_mount(char *s) {
mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
for (i = mntsize - 1; i >= 0; i--) { for (i = mntsize - 1; i >= 0; i--) {
if (strcmp(mntbuf[i].f_mntonname, s) == 0) { if (strcmp(mntbuf[i].f_mntonname, s) == 0) { return 1; }
return 1;
}
} }
return 0; return 0;
@ -178,13 +176,9 @@ int update_net_stats(void) {
/* get delta */ /* get delta */
delta = current_update_time - last_update_time; delta = current_update_time - last_update_time;
if (delta <= 0.0001) { if (delta <= 0.0001) { return 0; }
return 0;
}
if (getifaddrs(&ifap) < 0) { if (getifaddrs(&ifap) < 0) { return 0; }
return 0;
}
for (ifa = ifap; ifa; ifa = ifa->ifa_next) { for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
ns = get_net_stat((const char *)ifa->ifa_name, nullptr, NULL); ns = get_net_stat((const char *)ifa->ifa_name, nullptr, NULL);
@ -196,9 +190,7 @@ int update_net_stats(void) {
last_recv = ns->recv; last_recv = ns->recv;
last_trans = ns->trans; last_trans = ns->trans;
if (ifa->ifa_addr->sa_family != AF_LINK) { if (ifa->ifa_addr->sa_family != AF_LINK) { continue; }
continue;
}
for (iftmp = ifa->ifa_next; for (iftmp = ifa->ifa_next;
iftmp != nullptr && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0; iftmp != nullptr && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
@ -286,9 +278,7 @@ void get_cpu_count(void) {
} }
info.cpu_usage = (float *)malloc((info.cpu_count + 1) * sizeof(float)); info.cpu_usage = (float *)malloc((info.cpu_count + 1) * sizeof(float));
if (info.cpu_usage == nullptr) { if (info.cpu_usage == nullptr) { CRIT_ERR(nullptr, NULL, "malloc"); }
CRIT_ERR(nullptr, NULL, "malloc");
}
} }
struct cpu_info { struct cpu_info {
@ -357,6 +347,8 @@ int update_cpu_usage(void) {
return 0; return 0;
} }
void free_cpu(struct text_object *) { /* no-op */ }
int update_load_average(void) { int update_load_average(void) {
double v[3]; double v[3];
@ -489,9 +481,7 @@ void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size,
(void)adapter; // only linux uses this (void)adapter; // only linux uses this
if (!p_client_buffer || client_buffer_size <= 0) { if (!p_client_buffer || client_buffer_size <= 0) { return; }
return;
}
if (GETSYSCTL("hw.acpi.acline", state)) { if (GETSYSCTL("hw.acpi.acline", state)) {
fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n"); fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
@ -711,9 +701,7 @@ void get_top_info(void) {
#define APM_UNKNOWN 255 #define APM_UNKNOWN 255
int apm_getinfo(int fd, apm_info_t aip) { int apm_getinfo(int fd, apm_info_t aip) {
if (ioctl(fd, APMIO_GETINFO, aip) == -1) { if (ioctl(fd, APMIO_GETINFO, aip) == -1) { return -1; }
return -1;
}
return 0; return 0;
} }

View File

@ -371,6 +371,8 @@ int update_cpu_usage(void) {
return 0; return 0;
} }
void free_cpu(struct text_object *) { /* no-op */ }
int update_load_average(void) { int update_load_average(void) {
double v[3]; double v[3];
@ -641,9 +643,7 @@ int update_diskio(void) {
free(dev_select); free(dev_select);
} }
if (statinfo_cur.dinfo->mem_ptr) { if (statinfo_cur.dinfo->mem_ptr) { free(statinfo_cur.dinfo->mem_ptr); }
free(statinfo_cur.dinfo->mem_ptr);
}
free(statinfo_cur.dinfo); free(statinfo_cur.dinfo);
return 0; return 0;
} }

View File

@ -143,6 +143,8 @@ int update_cpu_usage() {
return 1; return 1;
} }
void free_cpu(struct text_object *) { /* no-op */ }
int update_load_average() { int update_load_average() {
// TODO // TODO
return 1; return 1;

View File

@ -936,6 +936,8 @@ int update_cpu_usage(void) {
return 0; return 0;
} }
void free_cpu(struct text_object *) { /* no-op */ }
// fscanf() that reads floats with points even if you are using a locale where // fscanf() that reads floats with points even if you are using a locale where
// floats are with commas // floats are with commas
int fscanf_no_i18n(FILE *stream, const char *format, ...) { int fscanf_no_i18n(FILE *stream, const char *format, ...) {

View File

@ -476,7 +476,11 @@ int interface_up(struct text_object *obj) {
if (dev == nullptr) { return 0; } if (dev == nullptr) { return 0; }
#if defined(__APPLE__) && defined(__MACH__)
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
#else
if ((fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0) { if ((fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0) {
#endif
CRIT_ERR(nullptr, nullptr, "could not create sockfd"); CRIT_ERR(nullptr, nullptr, "could not create sockfd");
return 0; return 0;
} }

View File

@ -286,6 +286,8 @@ void update_cpu_usage() {
oldtotal = total; oldtotal = total;
} }
void free_cpu(struct text_object *) { /* no-op */ }
void update_load_average() { void update_load_average() {
double v[3]; double v[3];

View File

@ -400,6 +400,9 @@ void update_cpu_usage() {
#endif #endif
} }
void free_cpu(struct text_object *) { /* no-op */
}
void update_load_average() { void update_load_average() {
double v[3]; double v[3];

View File

@ -314,6 +314,8 @@ int update_cpu_usage(void) {
return 0; return 0;
} }
void free_cpu(struct text_object *) { /* no-op */ }
void update_proc_entry(struct process *p) { void update_proc_entry(struct process *p) {
psinfo_t proc; psinfo_t proc;
int fd; int fd;