2005-08-05 01:06:17 +00:00
|
|
|
|
/* linux.c
|
|
|
|
|
* Contains linux specific code
|
|
|
|
|
*
|
|
|
|
|
* $Id$
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#include "conky.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/sysinfo.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
// #include <assert.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include "top.h"
|
|
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
|
#include <net/if.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
2005-11-27 06:56:35 +00:00
|
|
|
|
#define SHORTSTAT_TEMPL "%*s %llu %llu %llu"
|
|
|
|
|
#define LONGSTAT_TEMPL "%*s %llu %llu %llu "
|
|
|
|
|
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
static struct sysinfo s_info;
|
|
|
|
|
|
|
|
|
|
static int show_nice_processes;
|
|
|
|
|
|
|
|
|
|
void prepare_update()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void update_sysinfo()
|
|
|
|
|
{
|
|
|
|
|
sysinfo(&s_info);
|
|
|
|
|
|
|
|
|
|
info.uptime = (double) s_info.uptime;
|
|
|
|
|
|
|
|
|
|
/* there was some problem with these */
|
|
|
|
|
#if 0
|
2005-07-28 04:48:27 +00:00
|
|
|
|
// info.loadavg[0] = s_info.loads[0] / 100000.0f;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
info.loadavg[1] = s_info.loads[1] / 100000.0f;
|
|
|
|
|
info.loadavg[2] = s_info.loads[2] / 100000.0f;
|
|
|
|
|
gkrelltop_process_find_top_three info.mask |= 1 << INFO_LOADAVG;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
info.procs = s_info.procs;
|
|
|
|
|
|
|
|
|
|
/* these aren't nice, no cache and should check kernel version for mem_unit */
|
|
|
|
|
#if 0
|
|
|
|
|
info.memmax = s_info.totalram;
|
|
|
|
|
info.mem = s_info.totalram - s_info.freeram;
|
|
|
|
|
info.swapmax = s_info.totalswap;
|
|
|
|
|
info.swap = s_info.totalswap - s_info.swap;
|
|
|
|
|
info.mask |= 1 << INFO_MEM;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
info.mask |= (1 << INFO_UPTIME) | (1 << INFO_PROCS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void update_uptime()
|
|
|
|
|
{
|
|
|
|
|
/* prefers sysinfo() for uptime, I don't really know which one is better
|
2005-07-28 04:48:27 +00:00
|
|
|
|
* (=faster?) */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#ifdef USE_PROC_UPTIME
|
|
|
|
|
static int rep;
|
|
|
|
|
FILE *fp = open_file("/proc/uptime", &rep);
|
|
|
|
|
if (!fp)
|
|
|
|
|
return 0;
|
|
|
|
|
fscanf(fp, "%lf", &info.uptime);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
info.mask |= (1 << INFO_UPTIME);
|
|
|
|
|
#else
|
|
|
|
|
update_sysinfo();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* these things are also in sysinfo except Buffers:, that's why I'm reading
|
|
|
|
|
* them from proc */
|
|
|
|
|
|
|
|
|
|
static FILE *meminfo_fp;
|
|
|
|
|
|
|
|
|
|
void update_meminfo()
|
|
|
|
|
{
|
|
|
|
|
static int rep;
|
|
|
|
|
/* unsigned int a; */
|
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
|
|
info.mem = info.memmax = info.swap = info.swapmax = info.bufmem =
|
2005-07-28 04:48:27 +00:00
|
|
|
|
info.buffers = info.cached = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
if (meminfo_fp == NULL)
|
|
|
|
|
meminfo_fp = open_file("/proc/meminfo", &rep);
|
|
|
|
|
else
|
|
|
|
|
fseek(meminfo_fp, 0, SEEK_SET);
|
|
|
|
|
if (meminfo_fp == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
while (!feof(meminfo_fp)) {
|
|
|
|
|
if (fgets(buf, 255, meminfo_fp) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (strncmp(buf, "MemTotal:", 9) == 0) {
|
2005-10-18 00:54:51 +00:00
|
|
|
|
sscanf(buf, "%*s %lu", &info.memmax);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (strncmp(buf, "MemFree:", 8) == 0) {
|
2005-10-18 00:54:51 +00:00
|
|
|
|
sscanf(buf, "%*s %lu", &info.mem);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (strncmp(buf, "SwapTotal:", 10) == 0) {
|
2005-10-18 00:54:51 +00:00
|
|
|
|
sscanf(buf, "%*s %lu", &info.swapmax);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (strncmp(buf, "SwapFree:", 9) == 0) {
|
2005-10-18 00:54:51 +00:00
|
|
|
|
sscanf(buf, "%*s %lu", &info.swap);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (strncmp(buf, "Buffers:", 8) == 0) {
|
2005-10-18 00:54:51 +00:00
|
|
|
|
sscanf(buf, "%*s %lu", &info.buffers);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (strncmp(buf, "Cached:", 7) == 0) {
|
2005-10-18 00:54:51 +00:00
|
|
|
|
sscanf(buf, "%*s %lu", &info.cached);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-11-11 05:28:20 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
info.mem = info.memmax - info.mem;
|
|
|
|
|
info.swap = info.swapmax - info.swap;
|
|
|
|
|
|
|
|
|
|
info.bufmem = info.cached + info.buffers;
|
|
|
|
|
|
|
|
|
|
info.mask |= (1 << INFO_MEM) | (1 << INFO_BUFFERS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static FILE *net_dev_fp;
|
|
|
|
|
static FILE *net_wireless_fp;
|
|
|
|
|
|
|
|
|
|
inline void update_net_stats()
|
|
|
|
|
{
|
|
|
|
|
static int rep;
|
|
|
|
|
// FIXME: arbitrary size chosen to keep code simple.
|
|
|
|
|
int i, i2;
|
|
|
|
|
unsigned int curtmp1, curtmp2;
|
|
|
|
|
unsigned int k;
|
|
|
|
|
struct ifconf conf;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
|
double delta;
|
|
|
|
|
|
|
|
|
|
/* get delta */
|
|
|
|
|
delta = current_update_time - last_update_time;
|
|
|
|
|
if (delta <= 0.0001)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* open file and ignore first two lines */
|
2005-11-19 23:05:30 +00:00
|
|
|
|
if (net_dev_fp == NULL) {
|
2005-11-19 23:06:59 +00:00
|
|
|
|
net_dev_fp = open_file("/proc/net/dev", &rep);
|
2005-11-19 23:05:30 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
else
|
|
|
|
|
fseek(net_dev_fp, 0, SEEK_SET);
|
|
|
|
|
if (!net_dev_fp)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
fgets(buf, 255, net_dev_fp); /* garbage */
|
|
|
|
|
fgets(buf, 255, net_dev_fp); /* garbage (field names) */
|
|
|
|
|
|
|
|
|
|
/* read each interface */
|
|
|
|
|
for (i2 = 0; i2 < 16; i2++) {
|
|
|
|
|
struct net_stat *ns;
|
|
|
|
|
char *s, *p;
|
|
|
|
|
long long r, t, last_recv, last_trans;
|
|
|
|
|
|
2005-11-19 23:05:30 +00:00
|
|
|
|
if (fgets(buf, 255, net_dev_fp) == NULL) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
break;
|
2005-11-19 23:05:30 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
p = buf;
|
|
|
|
|
while (isspace((int) *p))
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
|
|
s = p;
|
|
|
|
|
|
|
|
|
|
while (*p && *p != ':')
|
|
|
|
|
p++;
|
|
|
|
|
if (*p == '\0')
|
|
|
|
|
continue;
|
|
|
|
|
*p = '\0';
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
|
|
ns = get_net_stat(s);
|
|
|
|
|
ns->up = 1;
|
2005-11-19 23:05:30 +00:00
|
|
|
|
memset(&(ns->addr.sa_data), 0, 14);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
last_recv = ns->recv;
|
|
|
|
|
last_trans = ns->trans;
|
|
|
|
|
|
|
|
|
|
sscanf(p,
|
|
|
|
|
/* bytes packets errs drop fifo frame compressed multicast|bytes ... */
|
|
|
|
|
"%Ld %*d %*d %*d %*d %*d %*d %*d %Ld",
|
|
|
|
|
&r, &t);
|
|
|
|
|
|
|
|
|
|
/* if recv or trans is less than last time, an overflow happened */
|
|
|
|
|
|
|
|
|
|
if (r < ns->last_read_recv)
|
|
|
|
|
ns->recv +=
|
2005-07-28 04:48:27 +00:00
|
|
|
|
((long long) 4294967295U -
|
|
|
|
|
ns->last_read_recv) + r;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
else
|
|
|
|
|
ns->recv += (r - ns->last_read_recv);
|
|
|
|
|
ns->last_read_recv = r;
|
|
|
|
|
|
|
|
|
|
if (t < ns->last_read_trans)
|
|
|
|
|
ns->trans +=
|
2005-07-28 04:48:27 +00:00
|
|
|
|
((long long) 4294967295U -
|
|
|
|
|
ns->last_read_trans) + t;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
else
|
|
|
|
|
ns->trans += (t - ns->last_read_trans);
|
|
|
|
|
ns->last_read_trans = t;
|
|
|
|
|
|
|
|
|
|
/*** ip addr patch ***/
|
2005-07-25 01:54:01 +00:00
|
|
|
|
i = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
|
2005-07-28 04:48:27 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
conf.ifc_buf = malloc(sizeof(struct ifreq) * 16);
|
2005-07-28 04:48:27 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
conf.ifc_len = sizeof(struct ifreq) * 16;
|
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
ioctl((long) i, SIOCGIFCONF, &conf);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
for (k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) {
|
|
|
|
|
struct net_stat *ns;
|
|
|
|
|
ns = get_net_stat(((struct ifreq *) conf.
|
|
|
|
|
ifc_buf)[k].ifr_ifrn.ifrn_name);
|
|
|
|
|
ns->addr =
|
|
|
|
|
((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.
|
|
|
|
|
ifru_addr;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
close((long) i);
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
free(conf.ifc_buf);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*** end ip addr patch ***/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* calculate speeds */
|
|
|
|
|
ns->net_rec[0] = (ns->recv - last_recv) / delta;
|
|
|
|
|
ns->net_trans[0] = (ns->trans - last_trans) / delta;
|
|
|
|
|
curtmp1 = 0;
|
|
|
|
|
curtmp2 = 0;
|
|
|
|
|
// get an average
|
|
|
|
|
for (i = 0; (unsigned) i < info.net_avg_samples; i++) {
|
|
|
|
|
curtmp1 += ns->net_rec[i];
|
|
|
|
|
curtmp2 += ns->net_trans[i];
|
|
|
|
|
}
|
|
|
|
|
ns->recv_speed = curtmp1 / (double) info.net_avg_samples;
|
|
|
|
|
ns->trans_speed = curtmp2 / (double) info.net_avg_samples;
|
|
|
|
|
if (info.net_avg_samples > 1) {
|
|
|
|
|
for (i = info.net_avg_samples; i > 1; i--) {
|
|
|
|
|
ns->net_rec[i - 1] = ns->net_rec[i - 2];
|
|
|
|
|
ns->net_trans[i - 1] =
|
2005-07-28 04:48:27 +00:00
|
|
|
|
ns->net_trans[i - 2];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* fclose(net_dev_fp); net_dev_fp = NULL; */
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
inline void update_wifi_stats()
|
|
|
|
|
{
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/** wireless stats patch by Bobby Beckmann **/
|
|
|
|
|
static int rep;
|
|
|
|
|
int i;
|
|
|
|
|
char buf[256];
|
2005-07-28 04:48:27 +00:00
|
|
|
|
/*open file and ignore first two lines sorry, this code sucks ass right now, i'll clean it up later */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
if (net_wireless_fp == NULL)
|
|
|
|
|
net_wireless_fp = open_file("/proc/net/wireless", &rep);
|
|
|
|
|
else
|
|
|
|
|
fseek(net_wireless_fp, 0, SEEK_SET);
|
2005-08-08 23:12:43 +00:00
|
|
|
|
if (net_wireless_fp == NULL)
|
2005-07-28 04:48:27 +00:00
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
fgets(buf, 255, net_wireless_fp); /* garbage */
|
|
|
|
|
fgets(buf, 255, net_wireless_fp); /* garbage (field names) */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* read each interface */
|
2005-07-28 04:48:27 +00:00
|
|
|
|
for (i = 0; i < 16; i++) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
struct net_stat *ns;
|
|
|
|
|
char *s, *p;
|
|
|
|
|
int l, m, n;
|
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
if (fgets(buf, 255, net_wireless_fp) == NULL)
|
|
|
|
|
break;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
p = buf;
|
2005-07-28 04:48:27 +00:00
|
|
|
|
while (isspace((int) *p))
|
|
|
|
|
p++;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
s = p;
|
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
while (*p && *p != ':')
|
|
|
|
|
p++;
|
|
|
|
|
if (*p == '\0')
|
|
|
|
|
continue;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
*p = '\0';
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
|
|
ns = get_net_stat(s);
|
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
sscanf(p, "%*d %d. %d. %d", &l, &m, &n);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-10-01 00:59:50 +00:00
|
|
|
|
ns->linkstatus = (int) (log(MIN(MAX(l,1),92)) / log(92) * 100);
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*** end wireless patch ***/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
void update_total_processes()
|
|
|
|
|
{
|
|
|
|
|
update_sysinfo();
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-27 14:55:10 +00:00
|
|
|
|
#define CPU_SAMPLE_COUNT 15
|
2005-08-27 09:29:24 +00:00
|
|
|
|
struct cpu_info {
|
2005-11-27 06:56:35 +00:00
|
|
|
|
unsigned long long cpu_user;
|
|
|
|
|
unsigned long long cpu_system;
|
|
|
|
|
unsigned long long cpu_nice;
|
|
|
|
|
unsigned long long cpu_idle;
|
|
|
|
|
unsigned long long cpu_iowait;
|
|
|
|
|
unsigned long long cpu_irq;
|
|
|
|
|
unsigned long long cpu_softirq;
|
|
|
|
|
unsigned long long cpu_steal;
|
|
|
|
|
unsigned long long cpu_total;
|
|
|
|
|
unsigned long long cpu_active_total;
|
|
|
|
|
unsigned long long cpu_last_total;
|
|
|
|
|
unsigned long long cpu_last_active_total;
|
2005-08-27 14:55:10 +00:00
|
|
|
|
double cpu_val[CPU_SAMPLE_COUNT];
|
2005-08-27 09:29:24 +00:00
|
|
|
|
};
|
|
|
|
|
static short cpu_setup = 0;
|
|
|
|
|
static int rep;
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
static FILE *stat_fp;
|
|
|
|
|
|
2005-11-27 06:56:35 +00:00
|
|
|
|
/*
|
|
|
|
|
determine if this kernel gives us "extended" statistics information in /proc/stat.
|
|
|
|
|
Kernels around 2.5 and earlier only reported user, system, nice and idle values in proc stat.
|
|
|
|
|
Kernels around 2.6 and greater report these PLUS iowait, irq, softirq, and steal
|
|
|
|
|
*/
|
|
|
|
|
void determine_longstat(char * buf) {
|
|
|
|
|
unsigned long long iowait=0;
|
|
|
|
|
KFLAG_SETOFF(KFLAG_IS_LONGSTAT);
|
|
|
|
|
/* scanf will either return -1 or 1 because there is only 1 assignment */
|
|
|
|
|
if (sscanf(buf, "%*s %*d %*d %*d %*d %llu",&iowait)>0) KFLAG_SETON(KFLAG_IS_LONGSTAT);
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-27 09:29:24 +00:00
|
|
|
|
void get_cpu_count()
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2006-05-22 03:26:44 +00:00
|
|
|
|
if (info.cpu_usage) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
char buf[256];
|
|
|
|
|
if (stat_fp == NULL)
|
2005-08-27 09:43:28 +00:00
|
|
|
|
stat_fp = open_file("/proc/stat", &rep);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
else
|
|
|
|
|
fseek(stat_fp, 0, SEEK_SET);
|
|
|
|
|
if (stat_fp == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
info.cpu_count = 0;
|
|
|
|
|
|
|
|
|
|
while (!feof(stat_fp)) {
|
|
|
|
|
if (fgets(buf, 255, stat_fp) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
2005-08-27 09:29:24 +00:00
|
|
|
|
if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) {
|
2005-11-27 06:56:35 +00:00
|
|
|
|
if (info.cpu_count == 0) {
|
|
|
|
|
determine_longstat(buf);
|
|
|
|
|
}
|
2005-08-27 09:29:24 +00:00
|
|
|
|
info.cpu_count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-08-27 14:55:10 +00:00
|
|
|
|
info.cpu_usage = malloc((info.cpu_count + 1) * sizeof(float));
|
2005-08-27 09:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-11-27 06:56:35 +00:00
|
|
|
|
#define TMPL_LONGSTAT "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
|
|
|
|
|
#define TMPL_SHORTSTAT "%*s %llu %llu %llu %llu"
|
2005-08-27 09:29:24 +00:00
|
|
|
|
|
|
|
|
|
inline static void update_stat()
|
|
|
|
|
{
|
|
|
|
|
static struct cpu_info *cpu = NULL;
|
|
|
|
|
char buf[256];
|
|
|
|
|
unsigned int i;
|
|
|
|
|
unsigned int index;
|
|
|
|
|
double curtmp;
|
2005-11-27 06:56:35 +00:00
|
|
|
|
char * stat_template=NULL;
|
|
|
|
|
unsigned int malloc_cpu_size=0;
|
|
|
|
|
|
|
|
|
|
|
2005-08-27 09:29:24 +00:00
|
|
|
|
if (!cpu_setup) {
|
|
|
|
|
get_cpu_count();
|
|
|
|
|
cpu_setup = 1;
|
|
|
|
|
}
|
2005-11-27 06:56:35 +00:00
|
|
|
|
|
|
|
|
|
if (stat_template == NULL) {
|
|
|
|
|
stat_template = KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGSTAT : TMPL_SHORTSTAT ;
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-27 09:29:24 +00:00
|
|
|
|
if (cpu == NULL) {
|
2005-11-27 06:56:35 +00:00
|
|
|
|
malloc_cpu_size = (info.cpu_count + 1) * sizeof(struct cpu_info);
|
|
|
|
|
cpu = malloc(malloc_cpu_size);
|
|
|
|
|
memset(cpu, 0, malloc_cpu_size);
|
2005-08-27 09:29:24 +00:00
|
|
|
|
}
|
2005-11-27 06:56:35 +00:00
|
|
|
|
|
2005-08-27 09:49:09 +00:00
|
|
|
|
if (stat_fp == NULL) {
|
2005-08-27 09:43:28 +00:00
|
|
|
|
stat_fp = open_file("/proc/stat", &rep);
|
2005-08-27 09:49:09 +00:00
|
|
|
|
} else {
|
2005-08-27 09:29:24 +00:00
|
|
|
|
fseek(stat_fp, 0, SEEK_SET);
|
2005-08-27 09:49:09 +00:00
|
|
|
|
}
|
|
|
|
|
if (stat_fp == NULL) {
|
2005-08-27 09:29:24 +00:00
|
|
|
|
return;
|
2005-08-27 09:49:09 +00:00
|
|
|
|
}
|
2005-08-27 09:29:24 +00:00
|
|
|
|
index = 0;
|
2005-08-27 09:49:09 +00:00
|
|
|
|
while (!feof(stat_fp)) {
|
2005-08-27 09:29:24 +00:00
|
|
|
|
if (fgets(buf, 255, stat_fp) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
if (strncmp(buf, "procs_running ", 14) == 0) {
|
2005-11-27 06:56:35 +00:00
|
|
|
|
sscanf(buf, "%*s %hu", &info.run_procs);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
info.mask |= (1 << INFO_RUN_PROCS);
|
2005-11-27 06:56:35 +00:00
|
|
|
|
} else if (strncmp(buf, "cpu", 3) == 0) {
|
|
|
|
|
index = isdigit(buf[3]) ? ((int)buf[3]) - 0x2F : 0;
|
|
|
|
|
sscanf(buf, stat_template
|
|
|
|
|
, &(cpu[index].cpu_user)
|
|
|
|
|
, &(cpu[index].cpu_nice)
|
|
|
|
|
, &(cpu[index].cpu_system)
|
|
|
|
|
, &(cpu[index].cpu_idle)
|
|
|
|
|
, &(cpu[index].cpu_iowait)
|
|
|
|
|
, &(cpu[index].cpu_irq)
|
|
|
|
|
, &(cpu[index].cpu_softirq)
|
|
|
|
|
, &(cpu[index].cpu_steal)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
cpu[index].cpu_total = cpu[index].cpu_user
|
|
|
|
|
+ cpu[index].cpu_nice
|
|
|
|
|
+ cpu[index].cpu_system
|
|
|
|
|
+ cpu[index].cpu_idle
|
|
|
|
|
+ cpu[index].cpu_iowait
|
|
|
|
|
+ cpu[index].cpu_irq
|
|
|
|
|
+ cpu[index].cpu_softirq
|
|
|
|
|
+ cpu[index].cpu_steal
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
cpu[index].cpu_active_total = cpu[index].cpu_total - (cpu[index].cpu_idle + cpu[index].cpu_iowait);
|
2005-08-27 09:29:24 +00:00
|
|
|
|
info.mask |= (1 << INFO_CPU);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-11-27 06:56:35 +00:00
|
|
|
|
double delta = current_update_time - last_update_time;
|
|
|
|
|
if (delta <= 0.001) return;
|
|
|
|
|
|
|
|
|
|
cpu[index].cpu_val[0] = (cpu[index].cpu_active_total - cpu[index].cpu_last_active_total) /
|
|
|
|
|
(float )(cpu[index].cpu_total - cpu[index].cpu_last_total);
|
|
|
|
|
curtmp = 0;
|
|
|
|
|
for (i=0; i < info.cpu_avg_samples; i++ ) {
|
|
|
|
|
curtmp += cpu[index].cpu_val[i];
|
|
|
|
|
}
|
|
|
|
|
/* TESTING -- I've removed this, because I don't think it is right. You shouldn't divide
|
|
|
|
|
by the cpu count here ... removing for testing */
|
2005-11-27 16:42:49 +00:00
|
|
|
|
/* if (index == 0) {
|
2005-11-27 06:56:35 +00:00
|
|
|
|
info.cpu_usage[index] = curtmp / info.cpu_avg_samples / info.cpu_count;
|
|
|
|
|
} else {
|
|
|
|
|
info.cpu_usage[index] = curtmp / info.cpu_avg_samples;
|
2005-11-27 16:42:49 +00:00
|
|
|
|
} */
|
2005-11-27 06:56:35 +00:00
|
|
|
|
/* TESTING -- this line replaces the prev. "suspect" if/else */
|
2005-08-27 10:36:28 +00:00
|
|
|
|
info.cpu_usage[index] = curtmp / info.cpu_avg_samples;
|
2005-11-27 06:56:35 +00:00
|
|
|
|
|
|
|
|
|
cpu[index].cpu_last_total = cpu[index].cpu_total;
|
|
|
|
|
cpu[index].cpu_last_active_total = cpu[index].cpu_active_total;
|
|
|
|
|
for (i = info.cpu_avg_samples - 1; i > 0; i--) {
|
|
|
|
|
cpu[index].cpu_val[i] = cpu[index].cpu_val[i - 1];
|
|
|
|
|
}
|
2005-08-27 10:36:28 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void update_running_processes()
|
|
|
|
|
{
|
|
|
|
|
update_stat();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void update_cpu_usage()
|
|
|
|
|
{
|
|
|
|
|
update_stat();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void update_load_average()
|
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_GETLOADAVG
|
|
|
|
|
double v[3];
|
|
|
|
|
getloadavg(v, 3);
|
|
|
|
|
info.loadavg[0] = (float) v[0];
|
|
|
|
|
info.loadavg[1] = (float) v[1];
|
|
|
|
|
info.loadavg[2] = (float) v[2];
|
|
|
|
|
#else
|
|
|
|
|
static int rep;
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
fp = open_file("/proc/loadavg", &rep);
|
|
|
|
|
if (!fp) {
|
|
|
|
|
v[0] = v[1] = v[2] = 0.0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fscanf(fp, "%f %f %f", &info.loadavg[0], &info.loadavg[1],
|
|
|
|
|
&info.loadavg[2]);
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-27 07:15:44 +00:00
|
|
|
|
#define PROC_I8K "/proc/i8k"
|
|
|
|
|
#define I8K_DELIM " "
|
2005-08-27 07:58:29 +00:00
|
|
|
|
static char *i8k_procbuf = NULL;
|
2005-08-27 07:15:44 +00:00
|
|
|
|
void update_i8k()
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
2005-08-27 07:58:29 +00:00
|
|
|
|
if (!i8k_procbuf) {
|
|
|
|
|
i8k_procbuf = (char*)malloc(128*sizeof(char));
|
|
|
|
|
}
|
2005-08-27 07:15:44 +00:00
|
|
|
|
if ((fp = fopen(PROC_I8K,"r")) == NULL) {
|
|
|
|
|
CRIT_ERR("/proc/i8k doesn't exist! use insmod to make sure the kernel driver is loaded...");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(&i8k_procbuf[0],0,128);
|
|
|
|
|
if (fread(&i8k_procbuf[0],sizeof(char),128,fp) == 0) {
|
|
|
|
|
ERR("something wrong with /proc/i8k...");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
i8k.version = strtok(&i8k_procbuf[0],I8K_DELIM);
|
|
|
|
|
i8k.bios = strtok(NULL,I8K_DELIM);
|
|
|
|
|
i8k.serial = strtok(NULL,I8K_DELIM);
|
|
|
|
|
i8k.cpu_temp = strtok(NULL,I8K_DELIM);
|
|
|
|
|
i8k.left_fan_status = strtok(NULL,I8K_DELIM);
|
|
|
|
|
i8k.right_fan_status = strtok(NULL,I8K_DELIM);
|
|
|
|
|
i8k.left_fan_rpm = strtok(NULL,I8K_DELIM);
|
|
|
|
|
i8k.right_fan_rpm = strtok(NULL,I8K_DELIM);
|
|
|
|
|
i8k.ac_status = strtok(NULL,I8K_DELIM);
|
|
|
|
|
i8k.buttons_status = strtok(NULL,I8K_DELIM);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************/
|
|
|
|
|
/***********************************************************/
|
|
|
|
|
/***********************************************************/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
static int no_dots(const struct dirent *d)
|
|
|
|
|
{
|
|
|
|
|
if (d->d_name[0] == '.')
|
|
|
|
|
return 0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2005-07-28 04:48:27 +00:00
|
|
|
|
get_first_file_in_a_directory(const char *dir, char *s, int *rep)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
struct dirent **namelist;
|
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
|
|
n = scandir(dir, &namelist, no_dots, alphasort);
|
|
|
|
|
if (n < 0) {
|
|
|
|
|
if (!rep || !*rep) {
|
|
|
|
|
ERR("scandir for %s: %s", dir, strerror(errno));
|
|
|
|
|
if (rep)
|
|
|
|
|
*rep = 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
if (n == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
strncpy(s, namelist[0]->d_name, 255);
|
|
|
|
|
s[255] = '\0';
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
|
free(namelist[i]);
|
|
|
|
|
free(namelist);
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define I2C_DIR "/sys/bus/i2c/devices/"
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
open_i2c_sensor(const char *dev, const char *type, int n, int *div,
|
|
|
|
|
char *devtype)
|
|
|
|
|
{
|
|
|
|
|
char path[256];
|
2005-10-15 16:20:25 +00:00
|
|
|
|
char buf[256];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
int fd;
|
|
|
|
|
int divfd;
|
|
|
|
|
|
|
|
|
|
/* if i2c device is NULL or *, get first */
|
|
|
|
|
if (dev == NULL || strcmp(dev, "*") == 0) {
|
|
|
|
|
static int rep;
|
|
|
|
|
if (!get_first_file_in_a_directory(I2C_DIR, buf, &rep))
|
|
|
|
|
return -1;
|
|
|
|
|
dev = buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* change vol to in */
|
|
|
|
|
if (strcmp(type, "vol") == 0)
|
|
|
|
|
type = "in";
|
|
|
|
|
|
|
|
|
|
if (strcmp(type, "tempf") == 0) {
|
2005-09-05 02:09:31 +00:00
|
|
|
|
snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, "temp", n);
|
2005-07-28 04:48:27 +00:00
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, type, n);
|
|
|
|
|
}
|
2005-09-05 02:09:31 +00:00
|
|
|
|
strncpy(devtype, path, 255);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* open file */
|
|
|
|
|
fd = open(path, O_RDONLY);
|
2005-08-06 02:36:59 +00:00
|
|
|
|
if (fd < 0) {
|
2005-08-06 07:13:09 +00:00
|
|
|
|
CRIT_ERR("can't open '%s': %s\nplease fix i2c or remove it from Conky", path, strerror(errno));
|
2005-08-06 02:36:59 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
|
|
|
|
|
|| strcmp(type, "tempf") == 0)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
*div = 1;
|
|
|
|
|
else
|
|
|
|
|
*div = 0;
|
|
|
|
|
/* fan does not use *_div as a read divisor */
|
|
|
|
|
if (strcmp("fan", type) == 0)
|
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
|
|
/* test if *_div file exist, open it and use it as divisor */
|
|
|
|
|
if (strcmp(type, "tempf") == 0) {
|
2005-07-28 04:48:27 +00:00
|
|
|
|
snprintf(path, 255, I2C_DIR "%s/%s%d_div", "one", "two",
|
|
|
|
|
n);
|
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
snprintf(path, 255, I2C_DIR "%s/%s%d_div", dev, type, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
divfd = open(path, O_RDONLY);
|
|
|
|
|
if (divfd > 0) {
|
|
|
|
|
/* read integer */
|
|
|
|
|
char divbuf[64];
|
|
|
|
|
unsigned int divn;
|
|
|
|
|
divn = read(divfd, divbuf, 63);
|
|
|
|
|
/* should read until n == 0 but I doubt that kernel will give these
|
2005-07-28 04:48:27 +00:00
|
|
|
|
* in multiple pieces. :) */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
divbuf[divn] = '\0';
|
|
|
|
|
*div = atoi(divbuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(divfd);
|
|
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double get_i2c_info(int *fd, int div, char *devtype, char *type)
|
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
|
|
|
|
|
if (*fd <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
lseek(*fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
/* read integer */
|
|
|
|
|
{
|
|
|
|
|
char buf[64];
|
|
|
|
|
unsigned int n;
|
|
|
|
|
n = read(*fd, buf, 63);
|
|
|
|
|
/* should read until n == 0 but I doubt that kernel will give these
|
2005-07-28 04:48:27 +00:00
|
|
|
|
* in multiple pieces. :) */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
buf[n] = '\0';
|
|
|
|
|
val = atoi(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(*fd);
|
|
|
|
|
/* open file */
|
|
|
|
|
*fd = open(devtype, O_RDONLY);
|
|
|
|
|
if (*fd < 0)
|
2005-07-28 04:48:27 +00:00
|
|
|
|
ERR("can't open '%s': %s", devtype, strerror(errno));
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* My dirty hack for computing CPU value
|
2005-07-28 04:48:27 +00:00
|
|
|
|
* Filedil, from forums.gentoo.org
|
|
|
|
|
*/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* if (strstr(devtype, "temp1_input") != NULL)
|
|
|
|
|
return -15.096+1.4893*(val / 1000.0); */
|
2005-07-28 04:48:27 +00:00
|
|
|
|
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* divide voltage and temperature by 1000 */
|
|
|
|
|
/* or if any other divisor is given, use that */
|
|
|
|
|
if (strcmp(type, "tempf") == 0) {
|
|
|
|
|
if (div > 1)
|
2005-07-28 04:48:27 +00:00
|
|
|
|
return ((val / div + 40) * 9.0 / 5) - 40;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
else if (div)
|
2005-07-28 04:48:27 +00:00
|
|
|
|
return ((val / 1000.0 + 40) * 9.0 / 5) - 40;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
else
|
2005-07-28 04:48:27 +00:00
|
|
|
|
return ((val + 40) * 9.0 / 5) - 40;
|
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
if (div > 1)
|
|
|
|
|
return val / div;
|
|
|
|
|
else if (div)
|
|
|
|
|
return val / 1000.0;
|
|
|
|
|
else
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-07 18:04:07 +00:00
|
|
|
|
/* Prior to kernel version 2.6.12, the CPU fan speed was available
|
|
|
|
|
* in ADT746X_FAN_OLD, whereas later kernel versions provide this
|
|
|
|
|
* information in ADT746X_FAN.
|
|
|
|
|
*/
|
|
|
|
|
#define ADT746X_FAN "/sys/devices/temperatures/sensor1_fan_speed"
|
|
|
|
|
#define ADT746X_FAN_OLD "/sys/devices/temperatures/cpu_fan_speed"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-11-13 04:04:00 +00:00
|
|
|
|
void get_adt746x_fan( char * p_client_buffer, size_t client_buffer_size )
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
static int rep;
|
2005-11-13 04:04:00 +00:00
|
|
|
|
char adt746x_fan_state[64];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
FILE *fp;
|
|
|
|
|
|
2005-11-13 04:04:00 +00:00
|
|
|
|
if ( !p_client_buffer || client_buffer_size <= 0 )
|
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2006-04-07 18:04:07 +00:00
|
|
|
|
if ((fp = open_file(ADT746X_FAN, &rep)) == NULL
|
|
|
|
|
&& (fp = open_file(ADT746X_FAN_OLD, &rep)) == NULL)
|
|
|
|
|
|
2005-11-13 04:04:00 +00:00
|
|
|
|
{
|
|
|
|
|
sprintf(adt746x_fan_state, "adt746x not found");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-04-07 18:04:07 +00:00
|
|
|
|
fgets(adt746x_fan_state, sizeof(adt746x_fan_state), fp);
|
|
|
|
|
adt746x_fan_state[strlen(adt746x_fan_state) - 1] = 0;
|
2005-11-13 04:04:00 +00:00
|
|
|
|
fclose(fp);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-11-13 04:04:00 +00:00
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "%s", adt746x_fan_state );
|
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-04-07 18:04:07 +00:00
|
|
|
|
/* Prior to kernel version 2.6.12, the CPU temperature was found
|
|
|
|
|
* in ADT746X_CPU_OLD, whereas later kernel versions provide this
|
|
|
|
|
* information in ADT746X_CPU.
|
|
|
|
|
*/
|
|
|
|
|
#define ADT746X_CPU "/sys/devices/temperatures/sensor1_temperature"
|
|
|
|
|
#define ADT746X_CPU_OLD "/sys/devices/temperatures/cpu_temperature"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-11-13 04:04:00 +00:00
|
|
|
|
void get_adt746x_cpu( char * p_client_buffer, size_t client_buffer_size )
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
static int rep;
|
2005-11-13 04:04:00 +00:00
|
|
|
|
char adt746x_cpu_state[64];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
FILE *fp;
|
|
|
|
|
|
2005-11-13 04:04:00 +00:00
|
|
|
|
if ( !p_client_buffer || client_buffer_size <= 0 )
|
|
|
|
|
return;
|
|
|
|
|
|
2006-04-07 18:04:07 +00:00
|
|
|
|
if ((fp = open_file(ADT746X_CPU, &rep)) == NULL
|
|
|
|
|
&& (fp = open_file(ADT746X_CPU_OLD, &rep)) == NULL)
|
2005-11-13 04:04:00 +00:00
|
|
|
|
{
|
|
|
|
|
sprintf(adt746x_cpu_state, "adt746x not found");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fscanf(fp, "%2s", adt746x_cpu_state);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-11-13 04:04:00 +00:00
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "%s", adt746x_cpu_state );
|
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-24 23:08:51 +00:00
|
|
|
|
/* Thanks to "Walt Nelson" <wnelsonjr@comcast.net> */
|
|
|
|
|
|
|
|
|
|
/***********************************************************************/
|
|
|
|
|
/*
|
|
|
|
|
* This file is part of x86info.
|
|
|
|
|
* (C) 2001 Dave Jones.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the terms of the GNU GPL License version 2.
|
|
|
|
|
*
|
|
|
|
|
* Estimate CPU MHz routine by Andrea Arcangeli <andrea@suse.de>
|
|
|
|
|
* Small changes by David Sterba <sterd9am@ss1000.ms.mff.cuni.cz>
|
|
|
|
|
*
|
|
|
|
|
*/
|
2005-08-05 01:06:17 +00:00
|
|
|
|
#if defined(__i386) || defined(__x86_64)
|
2005-07-24 23:08:51 +00:00
|
|
|
|
__inline__ unsigned long long int rdtsc()
|
|
|
|
|
{
|
|
|
|
|
unsigned long long int x;
|
2005-07-28 04:48:27 +00:00
|
|
|
|
__asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
|
2005-07-24 23:08:51 +00:00
|
|
|
|
return x;
|
|
|
|
|
}
|
2005-08-05 01:06:17 +00:00
|
|
|
|
#endif
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-11-13 03:33:26 +00:00
|
|
|
|
/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
|
|
|
|
|
void get_freq_dynamic( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor )
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2005-08-05 01:06:17 +00:00
|
|
|
|
#if defined(__i386) || defined(__x86_64)
|
2005-07-24 23:08:51 +00:00
|
|
|
|
struct timezone tz;
|
|
|
|
|
struct timeval tvstart, tvstop;
|
2005-07-28 04:48:27 +00:00
|
|
|
|
unsigned long long cycles[2]; /* gotta be 64 bit */
|
|
|
|
|
unsigned int microseconds; /* total time taken */
|
|
|
|
|
|
2005-11-13 03:33:26 +00:00
|
|
|
|
if ( !p_client_buffer || client_buffer_size <= 0 || !p_format || divisor <= 0 )
|
|
|
|
|
return;
|
|
|
|
|
|
2005-07-24 23:08:51 +00:00
|
|
|
|
memset(&tz, 0, sizeof(tz));
|
|
|
|
|
|
|
|
|
|
/* get this function in cached memory */
|
|
|
|
|
gettimeofday(&tvstart, &tz);
|
|
|
|
|
cycles[0] = rdtsc();
|
|
|
|
|
gettimeofday(&tvstart, &tz);
|
|
|
|
|
|
|
|
|
|
/* we don't trust that this is any specific length of time */
|
|
|
|
|
usleep(100);
|
|
|
|
|
cycles[1] = rdtsc();
|
|
|
|
|
gettimeofday(&tvstop, &tz);
|
2005-07-28 04:48:27 +00:00
|
|
|
|
microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
|
|
|
|
|
(tvstop.tv_usec - tvstart.tv_usec);
|
2005-07-24 23:08:51 +00:00
|
|
|
|
|
2005-11-13 03:33:26 +00:00
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, p_format, (float)((cycles[1] - cycles[0]) / microseconds) / divisor );
|
|
|
|
|
return;
|
2005-08-05 01:06:17 +00:00
|
|
|
|
#else
|
2006-05-22 02:49:22 +00:00
|
|
|
|
/* FIXME: hardwired: get freq for first cpu!
|
|
|
|
|
this whole function needs to be rethought and redone for
|
|
|
|
|
multi-cpu/multi-core/multi-threaded environments and
|
|
|
|
|
arbitrary combinations thereof
|
|
|
|
|
*/
|
|
|
|
|
get_freq( p_client_buffer, client_buffer_size, p_format, divisor, 1 );
|
2005-11-13 03:33:26 +00:00
|
|
|
|
return;
|
2005-08-26 02:16:35 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-22 02:49:22 +00:00
|
|
|
|
|
|
|
|
|
#define CPUFREQ_PREFIX "/sys/devices/system/cpu"
|
|
|
|
|
#define CPUFREQ_POSTFIX "cpufreq/scaling_cur_freq"
|
2005-08-26 02:16:35 +00:00
|
|
|
|
|
2005-11-13 03:33:26 +00:00
|
|
|
|
/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
|
2006-05-24 00:46:50 +00:00
|
|
|
|
char get_freq( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor, unsigned int cpu )
|
2005-08-26 02:16:35 +00:00
|
|
|
|
{
|
2005-08-05 01:06:17 +00:00
|
|
|
|
FILE *f;
|
2005-11-13 03:33:26 +00:00
|
|
|
|
char frequency[32];
|
|
|
|
|
char s[256];
|
|
|
|
|
double freq = 0;
|
2006-05-22 02:49:22 +00:00
|
|
|
|
char current_freq_file[128];
|
|
|
|
|
|
|
|
|
|
cpu--;
|
|
|
|
|
snprintf(current_freq_file, 127, "%s/cpu%d/%s",
|
|
|
|
|
CPUFREQ_PREFIX, cpu, CPUFREQ_POSTFIX);
|
2005-11-13 03:33:26 +00:00
|
|
|
|
|
|
|
|
|
if ( !p_client_buffer || client_buffer_size <= 0 || !p_format || divisor <= 0 )
|
2006-05-24 00:46:50 +00:00
|
|
|
|
return 0;
|
|
|
|
|
|
2006-05-22 02:49:22 +00:00
|
|
|
|
f = fopen(current_freq_file, "r");
|
2005-08-26 02:16:35 +00:00
|
|
|
|
if (f) {
|
2005-11-13 03:33:26 +00:00
|
|
|
|
/* if there's a cpufreq /sys node, read the current frequency from this node;
|
|
|
|
|
* divide by 1000 to get Mhz. */
|
|
|
|
|
if (fgets(s, sizeof(s), f)) {
|
2005-08-26 02:16:35 +00:00
|
|
|
|
s[strlen(s)-1] = '\0';
|
|
|
|
|
freq = strtod(s, NULL);
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
2005-11-13 03:33:26 +00:00
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, p_format, (freq/1000)/divisor );
|
2006-05-24 00:46:50 +00:00
|
|
|
|
return 1;
|
2005-08-26 02:16:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-22 02:49:22 +00:00
|
|
|
|
cpu++;
|
2005-11-13 03:33:26 +00:00
|
|
|
|
f = fopen("/proc/cpuinfo", "r"); //open the CPU information file
|
2006-05-24 00:46:50 +00:00
|
|
|
|
if (!f) {
|
|
|
|
|
perror("Conky: Failed to access '/proc/cpuinfo' at get_freq()");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2005-11-13 03:33:26 +00:00
|
|
|
|
|
|
|
|
|
while (fgets(s, sizeof(s), f) != NULL){ //read the file
|
2006-05-22 02:49:22 +00:00
|
|
|
|
|
2005-08-26 02:16:35 +00:00
|
|
|
|
#if defined(__i386) || defined(__x86_64)
|
2006-05-22 02:49:22 +00:00
|
|
|
|
if (strncmp(s, "cpu MHz", 7) == 0 && cpu == 0) { //and search for the cpu mhz
|
2006-03-06 00:33:32 +00:00
|
|
|
|
#else
|
|
|
|
|
#if defined(__alpha)
|
2006-05-22 02:49:22 +00:00
|
|
|
|
if (strncmp(s, "cycle frequency [Hz]", 20) == 0 && cpu == 0) { // different on alpha
|
2005-08-26 02:16:35 +00:00
|
|
|
|
#else
|
2006-05-22 02:49:22 +00:00
|
|
|
|
if (strncmp(s, "clock", 5) == 0 && cpu == 0) { // this is different on ppc for some reason
|
2006-03-06 00:33:32 +00:00
|
|
|
|
#endif // defined(__alpha)
|
|
|
|
|
#endif // defined(__i386) || defined(__x86_64)
|
|
|
|
|
|
2005-08-05 01:06:17 +00:00
|
|
|
|
strcpy(frequency, strchr(s, ':') + 2); //copy just the number
|
2006-03-06 00:33:32 +00:00
|
|
|
|
#if defined(__alpha)
|
|
|
|
|
frequency[strlen(frequency) - 6] = '\0';// strip " est.\n"
|
|
|
|
|
freq = strtod(frequency, NULL)/1000000; // kernel reports in Hz
|
|
|
|
|
#else
|
2005-11-13 03:33:26 +00:00
|
|
|
|
frequency[strlen(frequency) - 1] = '\0'; // strip \n
|
|
|
|
|
freq = strtod(frequency, NULL);
|
2006-03-06 00:33:32 +00:00
|
|
|
|
#endif
|
2005-08-05 01:06:17 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2006-05-22 02:49:22 +00:00
|
|
|
|
if (strncmp(s, "processor", 9) == 0) {
|
|
|
|
|
cpu--;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-11 18:13:57 +00:00
|
|
|
|
}
|
2005-11-13 03:33:26 +00:00
|
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, p_format, (float)freq/divisor );
|
2006-05-24 00:46:50 +00:00
|
|
|
|
return 1;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-24 00:23:47 +00:00
|
|
|
|
#define CPUFREQ_VOLTAGE "cpufreq/scaling_voltages"
|
|
|
|
|
|
|
|
|
|
/* return cpu voltage in mV (use divisor=1) or V (use divisor=1000) */
|
2006-05-24 00:46:50 +00:00
|
|
|
|
char get_voltage( char * p_client_buffer, size_t client_buffer_size, char * p_format, int divisor, unsigned int cpu )
|
2006-05-24 00:23:47 +00:00
|
|
|
|
{
|
|
|
|
|
/* /sys/devices/system/cpu/cpu0/cpufreq/scaling_voltages looks
|
|
|
|
|
something like this:
|
|
|
|
|
# frequency voltage
|
|
|
|
|
1800000 1340
|
|
|
|
|
1600000 1292
|
|
|
|
|
1400000 1100
|
|
|
|
|
1200000 988
|
|
|
|
|
1000000 1116
|
|
|
|
|
800000 1004
|
|
|
|
|
600000 988
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* Peter Tarjan (ptarjan@citromail.hu) */
|
|
|
|
|
FILE *f;
|
|
|
|
|
char s[256];
|
|
|
|
|
int freq = 0;
|
|
|
|
|
int voltage = 0;
|
|
|
|
|
char current_freq_file[128];
|
|
|
|
|
int freq_comp = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* build the voltage file name */
|
|
|
|
|
cpu--;
|
|
|
|
|
snprintf(current_freq_file, 127, "%s/cpu%d/%s",
|
|
|
|
|
CPUFREQ_PREFIX, cpu, CPUFREQ_POSTFIX);
|
|
|
|
|
|
|
|
|
|
if ( !p_client_buffer || client_buffer_size <= 0 || !p_format || divisor <= 0 )
|
2006-05-24 00:46:50 +00:00
|
|
|
|
return 0;
|
2006-05-24 00:23:47 +00:00
|
|
|
|
|
|
|
|
|
/* read the current cpu frequency from the /sys node */
|
|
|
|
|
f = fopen(current_freq_file, "r");
|
|
|
|
|
if (f) {
|
|
|
|
|
if (fgets(s, sizeof(s), f)) {
|
|
|
|
|
s[strlen(s)-1] = '\0';
|
|
|
|
|
freq = strtod(s, NULL);
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
2006-05-24 00:46:50 +00:00
|
|
|
|
} else {
|
|
|
|
|
fprintf(stderr, "Conky: Failed to access '%s' at ", current_freq_file);
|
|
|
|
|
perror("get_voltage()");
|
|
|
|
|
if (f) {
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2006-05-24 00:23:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snprintf(current_freq_file, 127, "%s/cpu%d/%s",
|
|
|
|
|
CPUFREQ_PREFIX, cpu, CPUFREQ_VOLTAGE);
|
|
|
|
|
|
|
|
|
|
/* use the current cpu frequency to find the corresponding voltage */
|
|
|
|
|
f = fopen(current_freq_file, "r");
|
|
|
|
|
|
2006-05-24 00:46:50 +00:00
|
|
|
|
if (f) {
|
|
|
|
|
while (!feof(f)) {
|
|
|
|
|
char line[256];
|
|
|
|
|
if (fgets(line, 255, f) == NULL) break;
|
|
|
|
|
sscanf(line, "%d %d", &freq_comp, &voltage);
|
|
|
|
|
if(freq_comp == freq) break;
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
|
|
|
|
} else {
|
|
|
|
|
fprintf(stderr, "Conky: Failed to access '%s' at ", current_freq_file);
|
|
|
|
|
perror("get_voltage()");
|
|
|
|
|
if (f) {
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2006-05-24 00:23:47 +00:00
|
|
|
|
}
|
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, p_format, (float)voltage/divisor );
|
2006-05-24 00:46:50 +00:00
|
|
|
|
return 1;
|
2006-05-24 00:23:47 +00:00
|
|
|
|
|
|
|
|
|
}
|
2005-07-24 23:08:51 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#define ACPI_FAN_DIR "/proc/acpi/fan/"
|
|
|
|
|
|
2005-11-12 21:42:00 +00:00
|
|
|
|
void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
static int rep;
|
|
|
|
|
char buf[256];
|
|
|
|
|
char buf2[256];
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
2005-11-12 21:42:00 +00:00
|
|
|
|
if ( !p_client_buffer || client_buffer_size <= 0 )
|
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* yeah, slow... :/ */
|
|
|
|
|
if (!get_first_file_in_a_directory(ACPI_FAN_DIR, buf, &rep))
|
2005-11-12 21:42:00 +00:00
|
|
|
|
{
|
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "no fans?" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-11-12 21:42:00 +00:00
|
|
|
|
snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_FAN_DIR, buf );
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
fp = open_file(buf2, &rep);
|
|
|
|
|
if (!fp) {
|
2005-11-12 21:42:00 +00:00
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "can't open fan's state file" );
|
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
2005-11-12 21:42:00 +00:00
|
|
|
|
memset(buf,0,sizeof(buf));
|
|
|
|
|
fscanf(fp, "%*s %99s", buf);
|
|
|
|
|
fclose(fp);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-11-12 21:42:00 +00:00
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "%s", buf );
|
|
|
|
|
|
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define ACPI_AC_ADAPTER_DIR "/proc/acpi/ac_adapter/"
|
|
|
|
|
|
2005-11-12 21:42:00 +00:00
|
|
|
|
void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
static int rep;
|
|
|
|
|
char buf[256];
|
|
|
|
|
char buf2[256];
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
2005-11-12 21:42:00 +00:00
|
|
|
|
if ( !p_client_buffer || client_buffer_size <= 0 )
|
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* yeah, slow... :/ */
|
|
|
|
|
if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &rep))
|
2005-11-12 21:42:00 +00:00
|
|
|
|
{
|
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "no ac_adapters?" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-11-12 21:42:00 +00:00
|
|
|
|
snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf );
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
fp = open_file(buf2, &rep);
|
|
|
|
|
if (!fp) {
|
2005-11-12 21:42:00 +00:00
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "No ac adapter found.... where is it?" );
|
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
2005-11-12 21:42:00 +00:00
|
|
|
|
memset(buf,0,sizeof(buf));
|
|
|
|
|
fscanf(fp, "%*s %99s", buf );
|
2005-07-20 00:30:40 +00:00
|
|
|
|
fclose(fp);
|
|
|
|
|
|
2005-11-12 21:42:00 +00:00
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "%s", buf );
|
|
|
|
|
|
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
/proc/acpi/thermal_zone/THRM/cooling_mode
|
|
|
|
|
cooling mode: active
|
|
|
|
|
/proc/acpi/thermal_zone/THRM/polling_frequency
|
|
|
|
|
<polling disabled>
|
|
|
|
|
/proc/acpi/thermal_zone/THRM/state
|
|
|
|
|
state: ok
|
|
|
|
|
/proc/acpi/thermal_zone/THRM/temperature
|
|
|
|
|
temperature: 45 C
|
|
|
|
|
/proc/acpi/thermal_zone/THRM/trip_points
|
|
|
|
|
critical (S5): 73 C
|
|
|
|
|
passive: 73 C: tc1=4 tc2=3 tsp=40 devices=0xcdf6e6c0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define ACPI_THERMAL_DIR "/proc/acpi/thermal_zone/"
|
|
|
|
|
#define ACPI_THERMAL_FORMAT "/proc/acpi/thermal_zone/%s/temperature"
|
|
|
|
|
|
|
|
|
|
int open_acpi_temperature(const char *name)
|
|
|
|
|
{
|
|
|
|
|
char path[256];
|
2005-10-22 01:13:39 +00:00
|
|
|
|
char buf[256];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
if (name == NULL || strcmp(name, "*") == 0) {
|
|
|
|
|
static int rep;
|
|
|
|
|
if (!get_first_file_in_a_directory
|
2005-07-28 04:48:27 +00:00
|
|
|
|
(ACPI_THERMAL_DIR, buf, &rep))
|
2005-07-20 00:30:40 +00:00
|
|
|
|
return -1;
|
|
|
|
|
name = buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snprintf(path, 255, ACPI_THERMAL_FORMAT, name);
|
|
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
ERR("can't open '%s': %s", path, strerror(errno));
|
|
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double last_acpi_temp;
|
|
|
|
|
static double last_acpi_temp_time;
|
|
|
|
|
|
|
|
|
|
double get_acpi_temperature(int fd)
|
|
|
|
|
{
|
|
|
|
|
if (fd <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
/* don't update acpi temperature too often */
|
|
|
|
|
if (current_update_time - last_acpi_temp_time < 11.32) {
|
|
|
|
|
return last_acpi_temp;
|
|
|
|
|
}
|
|
|
|
|
last_acpi_temp_time = current_update_time;
|
|
|
|
|
|
|
|
|
|
/* seek to beginning */
|
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
/* read */
|
|
|
|
|
{
|
|
|
|
|
char buf[256];
|
|
|
|
|
int n;
|
|
|
|
|
n = read(fd, buf, 255);
|
|
|
|
|
if (n < 0)
|
|
|
|
|
ERR("can't read fd %d: %s", fd, strerror(errno));
|
|
|
|
|
else {
|
|
|
|
|
buf[n] = '\0';
|
|
|
|
|
sscanf(buf, "temperature: %lf", &last_acpi_temp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return last_acpi_temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
hipo@lepakko hipo $ cat /proc/acpi/battery/BAT1/info
|
|
|
|
|
present: yes
|
|
|
|
|
design capacity: 4400 mAh
|
|
|
|
|
last full capacity: 4064 mAh
|
|
|
|
|
battery technology: rechargeable
|
|
|
|
|
design voltage: 14800 mV
|
|
|
|
|
design capacity warning: 300 mAh
|
|
|
|
|
design capacity low: 200 mAh
|
|
|
|
|
capacity granularity 1: 32 mAh
|
|
|
|
|
capacity granularity 2: 32 mAh
|
|
|
|
|
model number: 02KT
|
|
|
|
|
serial number: 16922
|
|
|
|
|
battery type: LION
|
|
|
|
|
OEM info: SANYO
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
hipo@lepakko conky $ cat /proc/acpi/battery/BAT1/state
|
|
|
|
|
present: yes
|
|
|
|
|
capacity state: ok
|
|
|
|
|
charging state: unknown
|
|
|
|
|
present rate: 0 mA
|
|
|
|
|
remaining capacity: 4064 mAh
|
|
|
|
|
present voltage: 16608 mV
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
2213<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> jupet@lagi-unstable:~$ cat /proc/apm
|
|
|
|
|
2213<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x01 0xff 0x10 -1% -1 ?
|
|
|
|
|
2213<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> (-1 ollee ei akkua kiinni, koska akku on p<EFBFBD>yd<EFBFBD>ll<EFBFBD>)
|
|
|
|
|
2214<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> jupet@lagi-unstable:~$ cat /proc/apm
|
|
|
|
|
2214<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x01 0x03 0x09 98% -1 ?
|
|
|
|
|
|
|
|
|
|
2238<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x00 0x00 0x01 100% -1 ? ilman verkkovirtaa
|
|
|
|
|
2239<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x01 0x00 0x01 99% -1 ? verkkovirralla
|
|
|
|
|
|
|
|
|
|
2240<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x01 0x03 0x09 100% -1 ? verkkovirralla ja monitori p<EFBFBD><EFBFBD>ll<EFBFBD>
|
|
|
|
|
2241<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x00 0x00 0x01 99% -1 ? monitori p<EFBFBD><EFBFBD>ll<EFBFBD> mutta ilman verkkovirtaa
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery"
|
|
|
|
|
#define APM_PATH "/proc/apm"
|
|
|
|
|
|
|
|
|
|
static FILE *acpi_bat_fp;
|
|
|
|
|
static FILE *apm_bat_fp;
|
|
|
|
|
|
|
|
|
|
static int acpi_last_full;
|
|
|
|
|
|
|
|
|
|
static char last_battery_str[64];
|
|
|
|
|
|
|
|
|
|
static double last_battery_time;
|
|
|
|
|
|
|
|
|
|
void get_battery_stuff(char *buf, unsigned int n, const char *bat)
|
|
|
|
|
{
|
|
|
|
|
static int rep, rep2;
|
|
|
|
|
char acpi_path[128];
|
|
|
|
|
snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
|
|
|
|
|
|
|
|
|
|
/* don't update battery too often */
|
|
|
|
|
if (current_update_time - last_battery_time < 29.5) {
|
|
|
|
|
snprintf(buf, n, "%s", last_battery_str);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
last_battery_time = current_update_time;
|
|
|
|
|
|
|
|
|
|
/* first try ACPI */
|
|
|
|
|
|
|
|
|
|
if (acpi_bat_fp == NULL && apm_bat_fp == NULL)
|
|
|
|
|
acpi_bat_fp = open_file(acpi_path, &rep);
|
|
|
|
|
|
|
|
|
|
if (acpi_bat_fp != NULL) {
|
|
|
|
|
int present_rate = -1;
|
|
|
|
|
int remaining_capacity = -1;
|
|
|
|
|
char charging_state[64];
|
2006-06-22 04:08:02 +00:00
|
|
|
|
char present[4];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* read last full capacity if it's zero */
|
|
|
|
|
if (acpi_last_full == 0) {
|
|
|
|
|
static int rep;
|
|
|
|
|
char path[128];
|
|
|
|
|
FILE *fp;
|
|
|
|
|
snprintf(path, 127,
|
|
|
|
|
ACPI_BATTERY_BASE_PATH "/%s/info", bat);
|
|
|
|
|
fp = open_file(path, &rep);
|
|
|
|
|
if (fp != NULL) {
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
char b[256];
|
|
|
|
|
if (fgets(b, 256, fp) == NULL)
|
|
|
|
|
break;
|
2006-03-07 01:49:51 +00:00
|
|
|
|
if (sscanf(b, "last full capacity: %d", &acpi_last_full) != 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
break;
|
2006-03-07 01:49:51 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fseek(acpi_bat_fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
strcpy(charging_state, "unknown");
|
|
|
|
|
|
|
|
|
|
while (!feof(acpi_bat_fp)) {
|
|
|
|
|
char buf[256];
|
|
|
|
|
if (fgets(buf, 256, acpi_bat_fp) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* let's just hope units are ok */
|
2006-06-22 04:08:02 +00:00
|
|
|
|
if (buf[0] == 'p') {
|
|
|
|
|
sscanf(buf, "present: %4s", present);
|
|
|
|
|
} else if (buf[0] == 'c')
|
2005-07-20 00:30:40 +00:00
|
|
|
|
sscanf(buf, "charging state: %63s",
|
|
|
|
|
charging_state);
|
|
|
|
|
else if (buf[0] == 'p')
|
|
|
|
|
sscanf(buf, "present rate: %d",
|
|
|
|
|
&present_rate);
|
|
|
|
|
else if (buf[0] == 'r')
|
|
|
|
|
sscanf(buf, "remaining capacity: %d",
|
|
|
|
|
&remaining_capacity);
|
|
|
|
|
}
|
2006-06-22 04:08:02 +00:00
|
|
|
|
|
|
|
|
|
/* not present */
|
|
|
|
|
if (strcmp(present, "no") == 0) {
|
|
|
|
|
strncpy(last_battery_str, "not present", 64);
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* charging */
|
2006-06-22 04:08:02 +00:00
|
|
|
|
else if (strcmp(charging_state, "charging") == 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
if (acpi_last_full != 0 && present_rate > 0) {
|
2006-06-08 04:29:23 +00:00
|
|
|
|
strncpy(last_battery_str, "charging ", 64);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
format_seconds(last_battery_str + 9,
|
|
|
|
|
63 - 9,
|
|
|
|
|
(acpi_last_full -
|
2005-07-28 04:48:27 +00:00
|
|
|
|
remaining_capacity) * 60 *
|
|
|
|
|
60 / present_rate);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (acpi_last_full != 0
|
2005-07-28 04:48:27 +00:00
|
|
|
|
&& present_rate <= 0) {
|
2006-06-08 04:29:23 +00:00
|
|
|
|
snprintf(last_battery_str, 64, "charging %d%%",
|
2005-07-28 04:48:27 +00:00
|
|
|
|
remaining_capacity * 100 /
|
|
|
|
|
acpi_last_full);
|
|
|
|
|
} else {
|
2006-06-08 04:29:23 +00:00
|
|
|
|
strncpy(last_battery_str, "charging", 63);
|
2005-07-28 04:48:27 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
/* discharging */
|
2006-06-08 04:29:23 +00:00
|
|
|
|
else if (strncmp(charging_state, "discharging", 64) == 0) {
|
|
|
|
|
if (present_rate > 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
format_seconds(last_battery_str, 63,
|
|
|
|
|
(remaining_capacity * 60 *
|
2005-07-28 04:48:27 +00:00
|
|
|
|
60) / present_rate);
|
2006-06-08 04:29:23 +00:00
|
|
|
|
} else if (present_rate == 0) { /* Thanks to Nexox for this one */
|
|
|
|
|
snprintf(last_battery_str, 64, "full");
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(last_battery_str, 64,
|
2005-07-20 00:30:40 +00:00
|
|
|
|
"discharging %d%%",
|
|
|
|
|
remaining_capacity * 100 /
|
2005-07-28 04:48:27 +00:00
|
|
|
|
acpi_last_full);
|
2006-06-08 04:29:23 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
/* charged */
|
|
|
|
|
/* thanks to Lukas Zapletal <lzap@seznam.cz> */
|
2006-06-08 04:29:23 +00:00
|
|
|
|
else if (strncmp(charging_state, "charged", 64) == 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
strcpy(last_battery_str, "charged");
|
2006-06-22 04:08:02 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* unknown, probably full / AC */
|
|
|
|
|
else {
|
|
|
|
|
if (acpi_last_full != 0
|
2005-07-28 04:48:27 +00:00
|
|
|
|
&& remaining_capacity != acpi_last_full)
|
2006-06-08 04:29:23 +00:00
|
|
|
|
snprintf(last_battery_str, 64, "unknown %d%%",
|
2005-07-20 00:30:40 +00:00
|
|
|
|
remaining_capacity * 100 /
|
2005-07-28 04:48:27 +00:00
|
|
|
|
acpi_last_full);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
else
|
2006-06-08 04:29:23 +00:00
|
|
|
|
strncpy(last_battery_str, "AC", 64);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* APM */
|
|
|
|
|
if (apm_bat_fp == NULL)
|
|
|
|
|
apm_bat_fp = open_file(APM_PATH, &rep2);
|
|
|
|
|
|
|
|
|
|
if (apm_bat_fp != NULL) {
|
|
|
|
|
int ac, status, flag, life;
|
|
|
|
|
|
|
|
|
|
fscanf(apm_bat_fp,
|
|
|
|
|
"%*s %*s %*x %x %x %x %d%%",
|
|
|
|
|
&ac, &status, &flag, &life);
|
|
|
|
|
|
|
|
|
|
if (life == -1) {
|
|
|
|
|
/* could check now that there is ac */
|
|
|
|
|
snprintf(last_battery_str, 64, "AC");
|
|
|
|
|
} else if (ac && life != 100) { /* could check that status==3 here? */
|
|
|
|
|
snprintf(last_battery_str, 64,
|
|
|
|
|
"charging %d%%", life);
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(last_battery_str, 64, "%d%%",
|
|
|
|
|
life);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* it seemed to buffer it so file must be closed (or could use syscalls
|
2005-07-28 04:48:27 +00:00
|
|
|
|
* directly but I don't feel like coding it now) */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
fclose(apm_bat_fp);
|
|
|
|
|
apm_bat_fp = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snprintf(buf, n, "%s", last_battery_str);
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-23 21:35:31 +00:00
|
|
|
|
/* On Apple powerbook and ibook:
|
|
|
|
|
$ cat /proc/pmu/battery_0
|
|
|
|
|
flags : 00000013
|
|
|
|
|
charge : 3623
|
|
|
|
|
max_charge : 3720
|
|
|
|
|
current : 388
|
|
|
|
|
voltage : 16787
|
|
|
|
|
time rem. : 900
|
|
|
|
|
$ cat /proc/pmu/info
|
|
|
|
|
PMU driver version : 2
|
|
|
|
|
PMU firmware version : 0c
|
|
|
|
|
AC Power : 1
|
|
|
|
|
Battery count : 1
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* defines as in <linux/pmu.h> */
|
|
|
|
|
#define PMU_BATT_PRESENT 0x00000001
|
|
|
|
|
#define PMU_BATT_CHARGING 0x00000002
|
|
|
|
|
|
|
|
|
|
static FILE* pmu_battery_fp;
|
|
|
|
|
static FILE* pmu_info_fp;
|
|
|
|
|
static char pb_battery_info[3][32];
|
|
|
|
|
static double pb_battery_info_update;
|
|
|
|
|
|
|
|
|
|
#define PMU_PATH "/proc/pmu"
|
2006-04-23 21:37:59 +00:00
|
|
|
|
void get_powerbook_batt_info(char *buf, size_t n, int i)
|
2006-04-23 21:35:31 +00:00
|
|
|
|
{
|
|
|
|
|
static int rep;
|
|
|
|
|
const char* batt_path = PMU_PATH "/battery_0";
|
|
|
|
|
const char* info_path = PMU_PATH "/info";
|
|
|
|
|
int flags, charge, max_charge, ac = -1;
|
|
|
|
|
long time = -1;
|
|
|
|
|
|
|
|
|
|
/* don't update battery too often */
|
|
|
|
|
if (current_update_time - pb_battery_info_update < 29.5) {
|
|
|
|
|
snprintf(buf, n, "%s", pb_battery_info[i]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pb_battery_info_update = current_update_time;
|
|
|
|
|
|
|
|
|
|
if (pmu_battery_fp == NULL)
|
|
|
|
|
pmu_battery_fp = open_file(batt_path, &rep);
|
|
|
|
|
|
|
|
|
|
if (pmu_battery_fp != NULL) {
|
|
|
|
|
rewind(pmu_battery_fp);
|
|
|
|
|
while (!feof(pmu_battery_fp)) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
if (fgets(buf, sizeof(buf), pmu_battery_fp) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (buf[0] == 'f')
|
|
|
|
|
sscanf(buf, "flags : %8x", &flags);
|
|
|
|
|
else if (buf[0] == 'c' && buf[1] == 'h')
|
|
|
|
|
sscanf(buf, "charge : %d", &charge);
|
|
|
|
|
else if (buf[0] == 'm')
|
|
|
|
|
sscanf(buf, "max_charge : %d", &max_charge);
|
|
|
|
|
else if (buf[0] == 't')
|
|
|
|
|
sscanf(buf, "time rem. : %ld", &time);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (pmu_info_fp == NULL)
|
|
|
|
|
pmu_info_fp = open_file(info_path, &rep);
|
|
|
|
|
|
|
|
|
|
if (pmu_info_fp != NULL) {
|
|
|
|
|
rewind(pmu_info_fp);
|
|
|
|
|
while (!feof(pmu_info_fp)) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
if (fgets(buf, sizeof(buf), pmu_info_fp) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
if (buf[0] == 'A')
|
|
|
|
|
sscanf(buf, "AC Power : %d", &ac);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* update status string */
|
|
|
|
|
if ((ac && !(flags & PMU_BATT_PRESENT)))
|
|
|
|
|
strcpy(pb_battery_info[PB_BATT_STATUS], "AC");
|
|
|
|
|
else if (ac && (flags & PMU_BATT_PRESENT)
|
|
|
|
|
&& !(flags & PMU_BATT_CHARGING))
|
|
|
|
|
strcpy(pb_battery_info[PB_BATT_STATUS], "charged");
|
|
|
|
|
else if ((flags & PMU_BATT_PRESENT)
|
|
|
|
|
&& (flags & PMU_BATT_CHARGING))
|
|
|
|
|
strcpy(pb_battery_info[PB_BATT_STATUS], "charging");
|
|
|
|
|
else
|
|
|
|
|
strcpy(pb_battery_info[PB_BATT_STATUS], "discharging");
|
|
|
|
|
|
|
|
|
|
/* update percentage string */
|
|
|
|
|
if (time == 0)
|
|
|
|
|
pb_battery_info[PB_BATT_PERCENT][0] = 0;
|
|
|
|
|
else
|
|
|
|
|
snprintf(pb_battery_info[PB_BATT_PERCENT],
|
|
|
|
|
sizeof(pb_battery_info[PB_BATT_PERCENT]),
|
|
|
|
|
"%d%%", (charge * 100)/max_charge);
|
|
|
|
|
|
|
|
|
|
/* update time string */
|
|
|
|
|
if (time == 0) /* fully charged or battery not present */
|
|
|
|
|
pb_battery_info[PB_BATT_TIME][0] = 0;
|
|
|
|
|
else if (time < 60*60) /* don't show secs */
|
|
|
|
|
format_seconds_short(pb_battery_info[PB_BATT_TIME],
|
|
|
|
|
sizeof(pb_battery_info[PB_BATT_TIME]), time);
|
|
|
|
|
else
|
|
|
|
|
format_seconds(pb_battery_info[PB_BATT_TIME],
|
|
|
|
|
sizeof(pb_battery_info[PB_BATT_TIME]), time);
|
|
|
|
|
|
|
|
|
|
snprintf(buf, n, "%s", pb_battery_info[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
void update_top()
|
|
|
|
|
{
|
|
|
|
|
show_nice_processes = 1;
|
2005-07-25 00:22:16 +00:00
|
|
|
|
process_find_top(info.cpu, info.memu);
|
2005-11-10 04:19:43 +00:00
|
|
|
|
info.first_process = get_first_process();
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
2005-08-26 02:16:35 +00:00
|
|
|
|
|
2005-08-26 22:35:39 +00:00
|
|
|
|
|
|
|
|
|
/*
|
2005-08-26 22:52:02 +00:00
|
|
|
|
* The following ifdefs were adapted from gkrellm
|
2005-08-26 22:35:39 +00:00
|
|
|
|
*/
|
|
|
|
|
#include <linux/major.h>
|
|
|
|
|
|
|
|
|
|
#if ! defined (MD_MAJOR)
|
2005-08-26 22:52:02 +00:00
|
|
|
|
#define MD_MAJOR 9
|
2005-08-26 22:35:39 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if !defined(LVM_BLK_MAJOR)
|
|
|
|
|
#define LVM_BLK_MAJOR 58
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if !defined(NBD_MAJOR)
|
|
|
|
|
#define NBD_MAJOR 43
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-08-26 02:52:54 +00:00
|
|
|
|
void update_diskio()
|
2005-08-26 02:16:35 +00:00
|
|
|
|
{
|
2005-08-26 16:29:56 +00:00
|
|
|
|
static unsigned int last = UINT_MAX;
|
2005-08-26 02:16:35 +00:00
|
|
|
|
static FILE* fp;
|
|
|
|
|
|
|
|
|
|
char buf[512];
|
2005-08-26 22:15:04 +00:00
|
|
|
|
int major, minor;
|
2005-08-26 02:16:35 +00:00
|
|
|
|
unsigned int current = 0;
|
|
|
|
|
unsigned int reads, writes = 0;
|
2005-08-26 22:15:04 +00:00
|
|
|
|
int col_count = 0;
|
2005-08-26 02:16:35 +00:00
|
|
|
|
|
|
|
|
|
if (!fp) {
|
|
|
|
|
fp = fopen("/proc/diskstats", "r");
|
|
|
|
|
} else {
|
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* read reads and writes from all disks (minor = 0), including
|
|
|
|
|
* cd-roms and floppies, and summ them up
|
|
|
|
|
*/
|
|
|
|
|
current = 0;
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
fgets(buf, 512, fp);
|
2005-08-26 22:15:04 +00:00
|
|
|
|
col_count = sscanf(buf, "%u %u %*s %*u %*u %u %*u %*u %*u %u",
|
|
|
|
|
&major, &minor, &reads, &writes);
|
2005-08-27 11:55:11 +00:00
|
|
|
|
/* ignore subdevices (they have only 3 matching entries in their line)
|
2005-08-26 22:15:04 +00:00
|
|
|
|
* and virtual devices (LVM, network block devices, RAM disks, Loopback)
|
|
|
|
|
*
|
|
|
|
|
* XXX ignore devices which are part of a SW RAID (MD_MAJOR)
|
|
|
|
|
*/
|
2005-08-27 11:55:11 +00:00
|
|
|
|
if (col_count > 3 &&
|
2005-08-26 22:15:04 +00:00
|
|
|
|
major != LVM_BLK_MAJOR && major != NBD_MAJOR &&
|
|
|
|
|
major != RAMDISK_MAJOR && major != LOOP_MAJOR) {
|
2005-08-26 02:16:35 +00:00
|
|
|
|
current += reads + writes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* since the values in /proc/diststats are absolute, we have
|
|
|
|
|
* to substract our last reading. The numbers stand for
|
|
|
|
|
* "sectors read", and we therefore have to divide by two to
|
|
|
|
|
* get KB */
|
|
|
|
|
int tot = ((double)(current-last)/2);
|
2005-08-26 16:29:56 +00:00
|
|
|
|
if (last > current) {
|
|
|
|
|
/* we hit this either if it's the very first time we
|
|
|
|
|
* run this, or when /proc/diskstats overflows; while
|
|
|
|
|
* 0 is not correct, it's at least not way off */
|
2005-08-26 02:16:35 +00:00
|
|
|
|
tot = 0;
|
|
|
|
|
}
|
|
|
|
|
last = current;
|
|
|
|
|
|
2005-08-26 02:52:54 +00:00
|
|
|
|
diskio_value = tot;
|
2005-08-26 02:16:35 +00:00
|
|
|
|
}
|
2005-10-14 00:16:01 +00:00
|
|
|
|
|
2006-04-15 18:00:39 +00:00
|
|
|
|
/* Here come the IBM ACPI-specific things. For reference, see
|
|
|
|
|
http://ibm-acpi.sourceforge.net/README
|
|
|
|
|
If IBM ACPI is installed, /proc/acpi/ibm contains the following files:
|
|
|
|
|
bay
|
|
|
|
|
beep
|
|
|
|
|
bluetooth
|
|
|
|
|
brightness
|
|
|
|
|
cmos
|
|
|
|
|
dock
|
|
|
|
|
driver
|
|
|
|
|
ecdump
|
|
|
|
|
fan
|
|
|
|
|
hotkey
|
|
|
|
|
led
|
|
|
|
|
light
|
|
|
|
|
thermal
|
|
|
|
|
video
|
|
|
|
|
volume
|
|
|
|
|
The content of these files is described in detail in the aforementioned
|
|
|
|
|
README - some of them also in the following functions accessing them.
|
|
|
|
|
Peter Tarjan (ptarjan@citromail.hu)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define IBM_ACPI_DIR "/proc/acpi/ibm"
|
|
|
|
|
|
|
|
|
|
void get_ibm_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
|
|
|
|
|
{
|
|
|
|
|
/* get fan speed on IBM/Lenovo laptops running the ibm acpi.
|
|
|
|
|
/proc/acpi/ibm/fan looks like this (3 lines):
|
|
|
|
|
status: disabled
|
|
|
|
|
speed: 2944
|
|
|
|
|
commands: enable, disable
|
|
|
|
|
Peter Tarjan (ptarjan@citromail.hu)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if ( !p_client_buffer || client_buffer_size <= 0 )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
unsigned int speed=0;
|
|
|
|
|
char fan[128];
|
|
|
|
|
snprintf(fan, 127, "%s/fan",IBM_ACPI_DIR);
|
|
|
|
|
|
|
|
|
|
fp = fopen(fan, "r");
|
|
|
|
|
if (fp != NULL)
|
|
|
|
|
{
|
|
|
|
|
while (!feof(fp))
|
|
|
|
|
{
|
|
|
|
|
char line[256];
|
|
|
|
|
if (fgets(line, 255, fp) == NULL) break;
|
|
|
|
|
if (sscanf(line, "speed: %d", &speed)) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove ibm* from your Conky config file.", fan, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "%d", speed );
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double last_ibm_acpi_temp_time;
|
|
|
|
|
void get_ibm_acpi_temps()
|
|
|
|
|
{
|
|
|
|
|
/* get the measured temperatures from the temperature sensors
|
|
|
|
|
on IBM/Lenovo laptops running the ibm acpi.
|
|
|
|
|
There are 8 values in /proc/acpi/ibm/thermal, and according to
|
|
|
|
|
http://ibm-acpi.sourceforge.net/README
|
|
|
|
|
these mean the following (at least on an IBM R51...)
|
|
|
|
|
0: CPU (also on the T series laptops)
|
|
|
|
|
1: Mini PCI Module (?)
|
|
|
|
|
2: HDD (?)
|
|
|
|
|
3: GPU (also on the T series laptops)
|
|
|
|
|
4: Battery (?)
|
|
|
|
|
5: N/A
|
|
|
|
|
6: Battery (?)
|
|
|
|
|
7: N/A
|
|
|
|
|
I'm not too sure about those with the question mark, but the values I'm
|
|
|
|
|
reading from *my* thermal file (on a T42p) look realistic for the
|
|
|
|
|
hdd and the battery.
|
|
|
|
|
#5 and #7 are always -128.
|
|
|
|
|
/proc/acpi/ibm/thermal looks like this (1 line):
|
|
|
|
|
temperatures: 41 43 31 46 33 -128 29 -128
|
|
|
|
|
Peter Tarjan (ptarjan@citromail.hu)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* don't update too often */
|
|
|
|
|
if (current_update_time - last_ibm_acpi_temp_time < 10.00)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
last_ibm_acpi_temp_time = current_update_time;
|
|
|
|
|
|
|
|
|
|
/* if ( !p_client_buffer || client_buffer_size <= 0 )
|
|
|
|
|
return; */
|
|
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
char thermal[128];
|
|
|
|
|
snprintf(thermal, 127, "%s/thermal",IBM_ACPI_DIR);
|
|
|
|
|
fp = fopen(thermal, "r");
|
|
|
|
|
|
|
|
|
|
if (fp != NULL)
|
|
|
|
|
{
|
|
|
|
|
while (!feof(fp))
|
|
|
|
|
{
|
|
|
|
|
char line[256];
|
|
|
|
|
if (fgets(line, 255, fp) == NULL) break;
|
|
|
|
|
if (sscanf(line, "temperatures: %d %d %d %d %d %d %d %d",
|
|
|
|
|
&ibm_acpi.temps[0], &ibm_acpi.temps[1],
|
|
|
|
|
&ibm_acpi.temps[2], &ibm_acpi.temps[3],
|
|
|
|
|
&ibm_acpi.temps[4], &ibm_acpi.temps[5],
|
|
|
|
|
&ibm_acpi.temps[6], &ibm_acpi.temps[7])) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove ibm* from your Conky config file.", thermal, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void get_ibm_acpi_volume( char * p_client_buffer, size_t client_buffer_size )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/* get volume (0-14) on IBM/Lenovo laptops running the ibm acpi.
|
|
|
|
|
"Volume" here is none of the mixer volumes, but a "master of masters"
|
|
|
|
|
volume adjusted by the IBM volume keys.
|
|
|
|
|
/proc/acpi/ibm/fan looks like this (4 lines):
|
|
|
|
|
level: 4
|
|
|
|
|
mute: off
|
|
|
|
|
commands: up, down, mute
|
|
|
|
|
commands: level <level> (<level> is 0-15)
|
|
|
|
|
Peter Tarjan (ptarjan@citromail.hu)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if ( !p_client_buffer || client_buffer_size <= 0 )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
char volume[128];
|
|
|
|
|
snprintf(volume, 127, "%s/volume",IBM_ACPI_DIR);
|
|
|
|
|
unsigned int vol=-1;
|
|
|
|
|
char mute[3]="";
|
|
|
|
|
|
|
|
|
|
fp = fopen(volume, "r");
|
|
|
|
|
if (fp != NULL)
|
|
|
|
|
{
|
|
|
|
|
while (!feof(fp))
|
|
|
|
|
{
|
|
|
|
|
char line[256];
|
|
|
|
|
if (fgets(line, 255, fp) == NULL) break;
|
|
|
|
|
if (sscanf(line, "level: %d", &vol)) continue;
|
|
|
|
|
if (sscanf(line, "mute: %s", mute)) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove ibm* from your Conky config file.", volume, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
if (strcmp(mute, "on")==0)
|
|
|
|
|
{
|
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "%s", "mute" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "%d", vol );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*static FILE *fp=NULL;*/
|
|
|
|
|
|
|
|
|
|
void get_ibm_acpi_brightness(char * p_client_buffer, size_t client_buffer_size)
|
|
|
|
|
{
|
|
|
|
|
/* get LCD brightness on IBM/Lenovo laptops running the ibm acpi.
|
|
|
|
|
/proc/acpi/ibm/brightness looks like this (3 lines):
|
|
|
|
|
level: 7
|
|
|
|
|
commands: up, down
|
|
|
|
|
commands: level <level> (<level> is 0-7)
|
|
|
|
|
Peter Tarjan (ptarjan@citromail.hu)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if ( !p_client_buffer || client_buffer_size <= 0 )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
unsigned int brightness=0;
|
|
|
|
|
char filename[128];
|
|
|
|
|
snprintf(filename, 127, "%s/brightness",IBM_ACPI_DIR);
|
|
|
|
|
|
|
|
|
|
fp = fopen(filename, "r");
|
|
|
|
|
if (fp != NULL)
|
|
|
|
|
{
|
|
|
|
|
while (!feof(fp))
|
|
|
|
|
{
|
|
|
|
|
char line[256];
|
|
|
|
|
if (fgets(line, 255, fp) == NULL) break;
|
|
|
|
|
if (sscanf(line, "level: %d", &brightness)) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove ibm* from your Conky config file.", filename, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
snprintf( p_client_buffer, client_buffer_size, "%d", brightness );
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
}
|