times.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Get process times
  2. Copyright (C) 2008-2017 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
  14. #include <config.h>
  15. /* Get times prototype. */
  16. #include <sys/times.h>
  17. /* Get round. */
  18. #include <math.h>
  19. /* Get GetProcessTimes etc. */
  20. #include <windows.h>
  21. static clock_t
  22. filetime2clock (FILETIME time)
  23. {
  24. float f;
  25. /* We have a 64-bit value, in the form of two DWORDS aka unsigned
  26. int, counting the number of 100-nanosecond intervals. We need to
  27. convert these to clock ticks. Older POSIX uses CLK_TCK to
  28. indicate the number of clock ticks per second while modern POSIX
  29. uses sysconf(_SC_CLK_TCK). Mingw32 does not appear to have
  30. sysconf(_SC_CLK_TCK), but appears to have CLK_TCK = 1000 so we
  31. use it. Note that CLOCKS_PER_SEC constant does not apply here,
  32. it is for use with the clock function. */
  33. f = (unsigned long long) time.dwHighDateTime << 32;
  34. f += time.dwLowDateTime;
  35. f = f * CLK_TCK / 10000000;
  36. return (clock_t) round (f);
  37. }
  38. clock_t
  39. times (struct tms * buffer)
  40. {
  41. FILETIME creation_time, exit_time, kernel_time, user_time;
  42. if (GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
  43. &kernel_time, &user_time) == 0)
  44. return (clock_t) -1;
  45. buffer->tms_utime = filetime2clock (user_time);
  46. buffer->tms_stime = filetime2clock (kernel_time);
  47. buffer->tms_cutime = 0;
  48. buffer->tms_cstime = 0;
  49. return clock ();
  50. }