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

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