1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 18:45:10 +00:00

synchronize access to kstat

This commit is contained in:
Jan Senolt 2019-04-16 12:02:17 +02:00 committed by Brenden Matthews
parent 000f0779aa
commit 5c7f3b6666

View File

@ -54,6 +54,8 @@ static kstat_ctl_t *kstat;
static time_t kstat_updated;
static int pageshift = INT_MAX;
static pthread_mutex_t kstat_mtx = PTHREAD_MUTEX_INITIALIZER;
static int pagetok(int pages) {
if (pageshift == INT_MAX) {
int pagesize = sysconf(_SC_PAGESIZE);
@ -65,23 +67,32 @@ static int pagetok(int pages) {
}
static void update_kstat() {
time_t now = time(nullptr);
time_t now;
pthread_mutex_lock(&kstat_mtx);
now = time(nullptr);
if (kstat == nullptr) {
if ((kstat = kstat_open()) == nullptr) {
pthread_mutex_unlock(&kstat_mtx);
NORM_ERR("can't open kstat: %s", strerror(errno));
return;
}
kstat_updated = 0;
}
if (now - kstat_updated < 2) /* Do not update kstats too often */
if (now - kstat_updated < 2) {
/* Do not update kstats too often */
pthread_mutex_unlock(&kstat_mtx);
return;
}
if (kstat_chain_update(kstat) == -1) {
pthread_mutex_unlock(&kstat_mtx);
perror("kstat_chain_update");
return;
}
kstat_updated = now;
pthread_mutex_unlock(&kstat_mtx);
}
static kstat_named_t *get_kstat(const char *module, int inst, const char *name,
@ -90,10 +101,12 @@ static kstat_named_t *get_kstat(const char *module, int inst, const char *name,
update_kstat();
pthread_mutex_lock(&kstat_mtx);
ksp = kstat_lookup(kstat, (char *)module, inst, (char *)name);
if (ksp == nullptr) {
NORM_ERR("cannot lookup kstat %s:%d:%s:%s %s", module, inst, name, stat,
strerror(errno));
pthread_mutex_unlock(&kstat_mtx);
return nullptr;
}
@ -101,14 +114,17 @@ static kstat_named_t *get_kstat(const char *module, int inst, const char *name,
if (ksp->ks_type == KSTAT_TYPE_NAMED || ksp->ks_type == KSTAT_TYPE_TIMER) {
kstat_named_t *knp =
(kstat_named_t *)kstat_data_lookup(ksp, (char *)stat);
pthread_mutex_unlock(&kstat_mtx);
return knp;
} else {
NORM_ERR("kstat %s:%d:%s:%s has unexpected type %d", module, inst, name,
stat, ksp->ks_type);
pthread_mutex_unlock(&kstat_mtx);
return nullptr;
}
}
NORM_ERR("cannot read kstat %s:%d:%s:%s", module, inst, name, stat);
pthread_mutex_unlock(&kstat_mtx);
return nullptr;
}
@ -289,6 +305,7 @@ int update_cpu_usage(void) {
info.cpu_usage = (float *)malloc(info.cpu_count * sizeof(float));
pthread_mutex_lock(&kstat_mtx);
for (cpu = 0; cpu < info.cpu_count; cpu++) {
char stat_name[PATH_MAX];
unsigned long cpu_user, cpu_nice, cpu_system, cpu_idle;
@ -312,6 +329,7 @@ int update_cpu_usage(void) {
info.cpu_usage[cpu] = (double)(cpu_use - last_cpu_use[cpu]) / d / 100.0;
last_cpu_use[cpu] = cpu_use;
}
pthread_mutex_unlock(&kstat_mtx);
return 0;
}
@ -377,6 +395,7 @@ int update_diskio(void) {
update_kstat();
pthread_mutex_lock(&kstat_mtx);
for (struct diskio_stat *cur = &stats; cur; cur = cur->next) {
unsigned int read, written;
kstat_io_t *ksio;
@ -392,6 +411,7 @@ int update_diskio(void) {
}
update_diskio_values(&stats, tot_read, tot_written);
pthread_mutex_unlock(&kstat_mtx);
return 0;
}