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.5KB

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