Skip to content

Commit

Permalink
stat functions (#616)
Browse files Browse the repository at this point in the history
* free engine on cleanup

* move system queries to c threads (wip)

* cpu, temp, disk collected from c thread. done
  • Loading branch information
tehn authored Oct 23, 2018
1 parent e341ad4 commit bc1dd2b
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 22 deletions.
7 changes: 4 additions & 3 deletions lua/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,6 @@ m.sys.pos = 1
m.sys.list = {"AUDIO > ", "DEVICES > ", "WIFI >", "SYNC >", "UPDATE >", "RESET AUDIO"}
m.sys.pages = {pAUDIO, pDEVICES, pWIFI, pSYNC, pUPDATE, pRESET}
m.sys.input = 0
m.sys.disk = ""

m.key[pSYSTEM] = function(n,z)
if n==2 and z==1 then
Expand Down Expand Up @@ -968,15 +967,17 @@ m.redraw[pSYSTEM] = function()
screen.text_right(m.sys.net)

screen.move(127,40)
screen.text_right("disk free: "..m.sys.disk)
screen.text_right("disk free: "..norns.disk.."MB")

screen.move(127,50)
screen.text_right(norns.version.update)

screen.move(127,60)
screen.text_right(norns.temp .. 'c / ' .. norns.cpu .. '%')
screen.update()
end

m.init[pSYSTEM] = function()
m.sys.disk = util.os_capture("df -hl | grep '/dev/root' | awk '{print $4}'")
u.callback = function()
m.sysquery()
menu.redraw()
Expand Down
9 changes: 9 additions & 0 deletions lua/norns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ norns.power = function(present)
--print("power: "..present)
end

--- stat handler
norns.stat = function(disk, temp, cpu)
--print("stat",disk,temp,cpu)
norns.disk = disk
norns.temp = temp
norns.cpu = cpu
end


--- key callback (redefined in menu)
norns.key = function(n,z)
--print ("norns.key "..n.." "..z)
Expand Down
12 changes: 11 additions & 1 deletion matron/src/event_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ typedef enum {
EVENT_BATTERY,
// power cable present
EVENT_POWER,
// stat update (disk, temp, cpu)
EVENT_STAT,
// libmonome device added
EVENT_MONOME_ADD,
// libmonome device removed
Expand Down Expand Up @@ -166,6 +168,13 @@ struct event_power {
uint8_t present;
}; // +8

struct event_stat {
struct event_common common;
uint16_t disk;
uint8_t temp;
uint8_t cpu;
};

struct event_enc {
struct event_common common;
uint8_t n;
Expand Down Expand Up @@ -203,7 +212,7 @@ struct event_startup_ready_ok {
}; // + 0

struct event_startup_ready_timeout {
struct event_common common;
struct event_common common;
}; // + 0

union event_data {
Expand All @@ -223,6 +232,7 @@ union event_data {
struct event_enc enc;
struct event_battery battery;
struct event_power power;
struct event_stat stat;
struct event_metro metro;
struct event_poll_value poll_value;
struct event_poll_data poll_data;
Expand Down
4 changes: 4 additions & 0 deletions matron/src/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "gpio.h"
#include "oracle.h"
#include "battery.h"
#include "stat.h"
#include "weaver.h"

#include "event_types.h"
Expand Down Expand Up @@ -184,6 +185,9 @@ static void handle_event(union event_data *ev) {
case EVENT_POWER:
w_handle_power(ev->power.present);
break;
case EVENT_STAT:
w_handle_stat(ev->stat.disk, ev->stat.temp, ev->stat.cpu);
break;
case EVENT_MONOME_ADD:
w_handle_monome_add(ev->monome_add.dev);
break;
Expand Down
134 changes: 134 additions & 0 deletions matron/src/hardware/stat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* stat.c
*
* hardware status monitoring
*/

#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <pthread.h>

#include "events.h"

#define STAT_INTERVAL 2

//static int fd[3];
//static char buf[8];
static pthread_t p;

void *stat_check(void *);

// extern def

void stat_init() {
if (pthread_create(&p, NULL, stat_check, 0) ) {
fprintf(stderr, "STAT: Error creating thread\n");
}
}

void stat_deinit() {
pthread_cancel(p);
}

void *stat_check(void *x) {
(void)x;
int number = -1;
int disk = 0;
int temp = 0;
int cpu = 0;
int _disk = -1;
int _temp = -1;
int _cpu = -1;

FILE *fd;
char buf[64];
char bufsub[8];

uint32_t user, nice, system, idle, iowait, irq, softirq, steal;
uint32_t sumidle=0, prevsumidle=0, sumnonidle=0, total=0, prevtotal=0;
int32_t totald, idled;

while (1) {
number++;
if(number==5) number=0;

// check disk ever 5 sleeps
if(number==0) {
if ((fd = popen("df -l | grep '/dev/root' | awk '{print $4}'", "r")) == NULL) {
fprintf(stderr,"Error opening pipe: disk free read\n");
}
else {
while (fgets(buf, 12, fd) != NULL) {
disk = atoi(buf) / 1000; // convert to MB
//fprintf(stderr,"disk free: %d\n", disk);
}
}
pclose(fd);
}

// check temp
if ((fd = popen("vcgencmd measure_temp", "r")) == NULL) {
fprintf(stderr,"Error opening pipe: temp read\n");
}
else {
while (fgets(buf, 16, fd) != NULL) {
strncpy(bufsub,buf+5,2);
temp = atoi(bufsub);
//fprintf(stderr,"temp: %d\r\n", temp);
}
}
pclose(fd);

// check cpu
if ((fd = popen("head -n1 /proc/stat", "r")) == NULL) {
fprintf(stderr,"Error opening pipe: cpu read\n");
}
else {
while (fgets(buf, 64, fd) != NULL) {
//fprintf(stderr,"%s\n", buf);
strtok(buf," ");
user = atoi(strtok(NULL," "));
nice = atoi(strtok(NULL," "));
system = atoi(strtok(NULL," "));
idle = atoi(strtok(NULL," "));
iowait = atoi(strtok(NULL," "));
irq = atoi(strtok(NULL," "));
softirq = atoi(strtok(NULL," "));
steal = atoi(strtok(NULL," "));

prevsumidle = sumidle;
sumidle = idle + iowait;
sumnonidle = user + nice + system + irq + softirq + steal;
prevtotal = total;
total = sumnonidle + sumidle;
totald = total - prevtotal;
idled = sumidle - prevsumidle;
cpu = 100 * (totald - idled) / totald;

//fprintf(stderr,"%d\n", cpu);
}
}
pclose(fd);


if(_disk != disk || _temp != temp || _cpu != cpu) {
_disk = disk;
_temp = temp;
_cpu = cpu;

union event_data *ev = event_data_new(EVENT_STAT);
ev->stat.disk = disk;
ev->stat.temp = temp;
ev->stat.cpu = cpu;
event_post(ev);
}

sleep(STAT_INTERVAL);
}
}
6 changes: 6 additions & 0 deletions matron/src/hardware/stat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include <stdint.h>

extern void stat_init(void);
extern void stat_deinit(void);
23 changes: 5 additions & 18 deletions matron/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "osc.h"
#include "metro.h"
#include "screen.h"
#include "stat.h"

#include "oracle.h"
#include "weaver.h"
Expand All @@ -36,6 +37,7 @@ void cleanup(void) {
i2c_deinit();
screen_deinit();
battery_deinit();
stat_deinit();

fprintf(stderr, "matron shutdown complete\n");
exit(0);
Expand All @@ -52,31 +54,16 @@ int main(int argc, char **argv) {
metros_init();
gpio_init();
battery_init();
stat_init();
i2c_init();
osc_init();

o_init(); // oracle (audio)

/*
norns_hello_init();
// wait here for a signal from the audio server...
fprintf(stderr, "waiting for crone...\n");
do {
norns_hello(1);
usleep(5000);
} while(o_ready() != 1);

// fade out
while(norns_hello(0)) {
usleep(5000);
}
*/
o_init(); // oracle (audio)

w_init(); // weaver (scripting)
dev_list_init();
dev_monitor_init();

// now is a good time to set our cleanup
atexit(cleanup);
// start reading input to interpreter
Expand Down
11 changes: 11 additions & 0 deletions matron/src/weaver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,17 @@ void w_handle_power(const int present) {
l_report(lvm, l_docall(lvm, 1, 0));
}

// stat
void w_handle_stat(const uint32_t disk, const uint16_t temp, const uint16_t cpu) {
lua_getglobal(lvm, "norns");
lua_getfield(lvm, -1, "stat");
lua_remove(lvm, -2);
lua_pushinteger(lvm, disk);
lua_pushinteger(lvm, temp);
lua_pushinteger(lvm, cpu);
l_report(lvm, l_docall(lvm, 3, 0));
}

void w_handle_poll_value(int idx, float val) {
// fprintf(stderr, "_handle_poll_value: %d, %f\n", idx, val);
lua_getglobal(lvm, "norns");
Expand Down
3 changes: 3 additions & 0 deletions matron/src/weaver.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ extern void w_handle_enc(const int n, const int delta);
extern void w_handle_battery(const int percent, const int current);
extern void w_handle_power(const int present);

//--- system/stat
extern void w_handle_stat(const uint32_t disk, const uint16_t temp, const uint16_t cpu);

//--- metro bang handler
extern void w_handle_metro(const int idx, const int stage);

Expand Down
1 change: 1 addition & 0 deletions matron/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def build(bld):
'src/hardware/gpio.c',
'src/hardware/i2c.c',
'src/hardware/screen.c',
'src/hardware/stat.c',
'src/args.c',
'src/events.c',
'src/hello.c',
Expand Down

0 comments on commit bc1dd2b

Please sign in to comment.