Monitoring du Phytotron
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

funcs.c 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * funcs.c
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/time.h>
  7. #include "funcs.h"
  8. extern int verbosity;
  9. /* --------------------------------------------------------------- */
  10. int random1000(int type)
  11. {
  12. int value;
  13. if (verbosity)
  14. fprintf(stderr, ">>> %s(%d)\n", __func__, type);
  15. switch (type) {
  16. case 0:
  17. value = rand() % 1000;
  18. break;
  19. case 1:
  20. value = (rand()%1000 + rand()%1000) / 2;
  21. break;
  22. default:
  23. value = -1;
  24. break;
  25. }
  26. return value;
  27. }
  28. /* --------------------------------------------------------------- */
  29. double dtime(void)
  30. {
  31. struct timeval tv;
  32. int foo;
  33. foo = gettimeofday(&tv, NULL);
  34. if (foo) fprintf(stderr, "got %d in %s\n", foo, __func__);
  35. return (double)tv.tv_sec + (double)tv.tv_usec / 1e6;
  36. }
  37. /* --------------------------------------------------------------- */
  38. int get_loadavg(double *where)
  39. {
  40. FILE *fp;
  41. double loads[3];
  42. int foo;
  43. if ( ! (fp=fopen("/proc/loadavg", "r")) ) {
  44. perror("read loadavg");
  45. return -1;
  46. }
  47. foo = fscanf(fp, "%lf %lf %lf", loads, loads+1, loads+2);
  48. if (3 != foo) fprintf(stderr, "%s : read %d vals\n", __func__, foo);
  49. memcpy(where, loads, 3 * sizeof(double));
  50. fclose(fp);
  51. return 0;
  52. }
  53. /* --------------------------------------------------------------- */