#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
static int *values;
static int nsamples = 0;
static double timeout = 1.0;
static const char *fname, *foutname;
int loop(int i, int count) {
FILE *fp = fopen(fname, "r");
unsigned long int sum;
int j;
fscanf(fp, "%d", &values[i]);
fclose(fp);
for (j = 0, sum = 0; j < count; j++) sum += values[j];
double avg = sum / (double) count;
printf("%0.2lf\n", avg); //print continuosly
//...but dump to a file everytime we start to fill a new window:
if (i == 0 ) {
FILE *fpout = fopen(foutname,"w");
fprintf(fpout, "%f", avg );//writing data into file
fclose(fpout);
}
usleep(1000000 * timeout);
}
int main(int argc, const char *argv[]) {
if (argc != 5 || access(argv[3], R_OK) != 0) {
fprintf(stderr, "Samples values from \"filename-input\" at rate \"samples-per-second\" and smooth them over a moving window sized \"window-size\"\n");
fprintf(stderr, "dump continuosly on screen and less frequently to \"filename-output\"\n\n");
fprintf(stderr, "Usage: %s <samples-per-second> <window-size> <filename-input> <filename-output>\n", argv[0]);
return 1;
}
timeout = 1/atof(argv[1]);
nsamples = atoi(argv[2]);
fname = argv[3];
foutname = argv[4];
if (timeout < 0) timeout = 1.0;
if (nsamples < 0) nsamples = 5;
values = malloc(nsamples * sizeof(int));
int i;
for (i = 0; i < nsamples; i++) loop(i, i+1);
while (1) for (i = 0; i < nsamples; i++) loop(i, nsamples);
return 0;
}
Thank you seth, i learned how to use read and redirections with loops, finally.
]]>seems to work this way
Seems the driver doesn't write data while the device is open
Why not just read from stdin
Because the moment any input appears on stdin the read command succeeds and terminates (before the timeout) - you want a dead source that's not gonna read ever.
]]>#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
static int *values;
static int nsamples = 0;
static double timeout = 1.0;
static const char *fname;
int loop(int i, int count) {
FILE *fp = fopen(fname, "r");
int sum, j;
fscanf(fp, "%d", &values[i]);
fclose(fp);
for (j = 0, sum = 0; j < count; j++) sum += values[j];
printf("%0.2lf\n", sum / (double) count);
usleep(1000000 * timeout);
}
int main(int argc, const char *argv[]) {
if (argc != 4 || access(argv[3], R_OK) != 0) {
fprintf(stderr, "Usage: %s <loop-duration> <n-samples> <filename>\n", argv[0]);
return 1;
}
timeout = atof(argv[1]);
nsamples = atoi(argv[2]);
fname = argv[3];
if (timeout < 0) timeout = 1.0;
if (nsamples < 0) nsamples = 5;
values = malloc(nsamples * sizeof(int));
int i;
for (i = 0; i < nsamples; i++) loop(i, i+1);
while (1) for (i = 0; i < nsamples; i++) loop(i, nsamples);
return 0;
}
]]>koko@Gozer# awk -f /tmp/avg.awk < <(while true; do read line; echo "$line"; sleep 0.1; done < /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)
52583.4
0
0
0
..
0
^C
See also:
while true; do read line; echo "$line"; sleep 0.1; done < /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
koko@Gozer# while true; do read line; echo "$line"; sleep 0.1; done < /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
1317826
^C
-EDIT
seems to work this way:
awk -f /tmp/avg.awk < <(while true ; do read line < /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq ; echo $line ; sleep 0.1 ; done)
(Now waiting for eschwartz to explain why stalling on reading /dev/null is bad ;-)
Why not just read from stdin, it works:
awk -f /tmp/avg.awk < <(while true ; do read line < /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq ; echo $line ; read -t 0.1 ; done)
awk -f… < <(while true; do read line; echo "$line"; sleep 5; done < /sys/class/drm/card1/device/gpu_busy_percent)
Instead of the external sleep, you can use https://blog.dhampir.no/content/sleepin … ep-forever
"echo" and "read" should™ be internal functions for bash/zsh - maybe "print" is more useful.
bash/zsh do have arithmetic routines ((++i)), so technically you don't need awk either
Edit, linked script is way to complicated.
snore(){ [ -z "$1" ] && read /dev/null || read -t "$1" /dev/null }
and you can just "read -t 5 /dev/null" instead of sleeping.
(Now waiting for eschwartz to explain why stalling on reading /dev/null is bad ;-)
Edit #2: at least zsh doesn't like the ":+-t" parameter substitution and read omits the "t"… somehow
]]>BEGIN { N=30 }
{
s+=array[i++]=$1
if (i>=N) i=0
}
NR>=N {
print s/N
s-=array[i]
}
Now i need to feed it in an efficient way, and i'm a bit lost because i'm not that good in using read in loops, and I don't even know if that would be the right way.
I need something like: keep_echoing(/sys/class/drm/card1/device/gpu_busy_percent) | awk -f /tmp/avg.awk
Any hint, please?
Thanks!
]]>