plong

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.inz.fi/plong
Log | Files | Refs

cpu.c (674B)


      1 #include <stdio.h>
      2 #include <errno.h>
      3 #include "cpu.h"
      4 #include "util.h"
      5 
      6 static int _cpu_read(struct cpu *c)
      7 {
      8 	FILE *f = fopen("/proc/stat", "r");
      9 	if (!f)
     10 		return errno;
     11 	if (fscanf(f, "cpu" UINT_SCAN(CPU_FIELDS)
     12 #define X(a) , &c->a
     13 		   CPU_FIELDS(X)
     14 #undef X
     15 	   ) < COUNT(CPU_FIELDS)) {
     16 		fclose(f);
     17 		return ENODATA;
     18 	}
     19 	fclose(f);
     20 
     21 	return 0;
     22 }
     23 
     24 int cpu_init(struct cpu *c)
     25 {
     26 	return _cpu_read(c);
     27 }
     28 
     29 int cpu_poll(struct cpu *c, struct json *j)
     30 {
     31 	int r;
     32 	struct cpu c0 = *c;
     33 	if ((r = _cpu_read(c)))
     34 		return r;
     35 #define X(a) \
     36 	if ((r = json_obj_key(j, #a))) \
     37 		return r; \
     38 	if ((r = json_int(j, c->a - c0.a))) \
     39 		return r;
     40 	CPU_FIELDS(X)
     41 #undef X
     42 	return 0;
     43 }