DD2-monitor/funcs.c

74 lines
1.3 KiB
C
Raw Normal View History

2018-12-05 16:00:11 +01:00
/*
* funcs.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2018-12-05 17:58:55 +01:00
#include <sys/time.h>
2018-12-05 16:00:11 +01:00
#include "funcs.h"
2018-12-05 16:04:09 +01:00
extern int verbosity;
2018-12-05 17:58:55 +01:00
/* --------------------------------------------------------------- */
2018-12-06 21:06:34 +01:00
int random1000(int type)
2018-12-05 16:00:11 +01:00
{
2018-12-05 17:58:55 +01:00
int value;
2018-12-05 16:04:09 +01:00
if (verbosity)
2018-12-06 21:06:34 +01:00
fprintf(stderr, ">>> %s(%d)\n", __func__, type);
switch (type) {
case 0:
value = rand() % 1000;
break;
case 1:
value = (rand()%1000 + rand()%1000) / 2;
break;
default:
value = -1;
break;
}
2018-12-05 16:00:11 +01:00
return value;
}
2018-12-05 17:58:55 +01:00
/* --------------------------------------------------------------- */
double dtime(void)
{
struct timeval tv;
int foo;
foo = gettimeofday(&tv, NULL);
if (foo) fprintf(stderr, "got %d in %s\n", foo, __func__);
return (double)tv.tv_sec + (double)tv.tv_usec / 1e6;
}
/* --------------------------------------------------------------- */
2018-12-08 13:02:24 +01:00
int get_loadavg(double *where)
{
FILE *fp;
2018-12-08 13:02:24 +01:00
double loads[3];
int foo;
if ( ! (fp=fopen("/proc/loadavg", "r")) ) {
perror("read loadavg");
2018-12-08 13:02:24 +01:00
return -1;
}
2018-12-08 13:02:24 +01:00
foo = fscanf(fp, "%lf %lf %lf", loads, loads+1, loads+2);
if (3 != foo) fprintf(stderr, "%s : read %d vals\n", __func__, foo);
memcpy(where, loads, 3 * sizeof(double));
fclose(fp);
2018-12-08 13:02:24 +01:00
return 0;
}
/* --------------------------------------------------------------- */