pstats.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/times.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include "pstats.h"
  7. static struct tms tms_beg;
  8. static struct tms tms_end;
  9. static int has_times = 0;
  10. void pstats_init(void)
  11. {
  12. has_times = times(&tms_beg) != -1;
  13. }
  14. static void tms_report(void)
  15. {
  16. double cputime;
  17. if (! has_times )
  18. return;
  19. times(&tms_end);
  20. cputime = ( ((float)tms_end.tms_utime + tms_end.tms_stime + tms_end.tms_cutime + tms_end.tms_cstime ) -
  21. ((float)tms_beg.tms_utime + tms_beg.tms_stime + tms_beg.tms_cutime + tms_beg.tms_cstime ) )
  22. / sysconf(_SC_CLK_TCK);
  23. fprintf(stderr,"\tcputime=%.3f\n" , cputime);
  24. }
  25. static void ps_report(void)
  26. {
  27. char buf[1024];
  28. #ifdef __APPLE__ /* MAC OS X */
  29. sprintf(buf,"ps -o command,majflt,minflt,rss,pagein,vsz -p %d 1>&2",getpid() );
  30. #else /* GNU/Linux */
  31. sprintf(buf,"ps -o comm,majflt,minflt,rss,drs,pagein,sz,trs,vsz %d 1>&2",getpid() );
  32. #endif
  33. if (system( buf )==-1) {
  34. perror("system call to ps failed");
  35. }
  36. }
  37. void pstats_report()
  38. {
  39. ps_report();
  40. tms_report();
  41. }