times.c 995 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <u.h>
  2. #include <libc.h>
  3. static
  4. char*
  5. skip(char *p)
  6. {
  7. while(*p == ' ')
  8. p++;
  9. while(*p != ' ' && *p != 0)
  10. p++;
  11. return p;
  12. }
  13. /*
  14. * after a fork with fd's copied, both fd's are pointing to
  15. * the same Chan structure. Since the offset is kept in the Chan
  16. * structure, the seek's and read's in the two processes can be
  17. * are competing moving the offset around. Hence the unusual loop
  18. * in the middle of this routine.
  19. */
  20. long
  21. times(long *t)
  22. {
  23. char b[200], *p;
  24. static int f = -1;
  25. int i, retries;
  26. ulong r;
  27. memset(b, 0, sizeof(b));
  28. for(retries = 0; retries < 100; retries++){
  29. if(f < 0)
  30. f = open("/dev/cputime", OREAD|OCEXEC);
  31. if(f < 0)
  32. break;
  33. if(seek(f, 0, 0) < 0 || (i = read(f, b, sizeof(b))) < 0){
  34. close(f);
  35. f = -1;
  36. } else {
  37. if(i != 0)
  38. break;
  39. }
  40. }
  41. p = b;
  42. if(t)
  43. t[0] = atol(p);
  44. p = skip(p);
  45. if(t)
  46. t[1] = atol(p);
  47. p = skip(p);
  48. r = atol(p);
  49. if(t){
  50. p = skip(p);
  51. t[2] = atol(p);
  52. p = skip(p);
  53. t[3] = atol(p);
  54. }
  55. return r;
  56. }