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.

136 lines
4.2 KiB

  1. /**
  2. @file
  3. @ingroup util
  4. @brief System time calls
  5. @details Provide a uniform interface across different operating systems.
  6. @copyright@parblock
  7. Copyright (c) 1994-1998 The Regents of the Univ. of California.
  8. All rights reserved.
  9. Permission is hereby granted, without written agreement and without license
  10. or royalty fees, to use, copy, modify, and distribute this software and its
  11. documentation for any purpose, provided that the above copyright notice and
  12. the following two paragraphs appear in all copies of this software.
  13. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  14. DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  15. OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  16. CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  18. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19. FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
  20. "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
  21. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  22. @endparblock
  23. @copyright@parblock
  24. Copyright (c) 1999-2015, Regents of the University of Colorado
  25. All rights reserved.
  26. Redistribution and use in source and binary forms, with or without
  27. modification, are permitted provided that the following conditions
  28. are met:
  29. Redistributions of source code must retain the above copyright
  30. notice, this list of conditions and the following disclaimer.
  31. Redistributions in binary form must reproduce the above copyright
  32. notice, this list of conditions and the following disclaimer in the
  33. documentation and/or other materials provided with the distribution.
  34. Neither the name of the University of Colorado nor the names of its
  35. contributors may be used to endorse or promote products derived from
  36. this software without specific prior written permission.
  37. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  38. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  39. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  40. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  41. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  42. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  43. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  47. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  48. POSSIBILITY OF SUCH DAMAGE.
  49. @endparblock
  50. */
  51. #include "util.h"
  52. #if HAVE_SYS_TIMES_H == 1
  53. #include <sys/times.h>
  54. #endif
  55. #ifdef _WIN32
  56. #include <windows.h>
  57. #endif
  58. /**
  59. * @brief returns a long which represents the elapsed processor
  60. * time in milliseconds since some constant reference.
  61. */
  62. long
  63. util_cpu_time(void)
  64. {
  65. #if HAVE_SYSCONF == 1
  66. /* Code for POSIX systems */
  67. struct tms buffer;
  68. long nticks; /* number of clock ticks per second */
  69. nticks = sysconf(_SC_CLK_TCK);
  70. times(&buffer);
  71. return (long) ((buffer.tms_utime + buffer.tms_stime) * (1000.0/nticks));
  72. #elif defined(_WIN32)
  73. FILETIME creationTime, exitTime, kernelTime, userTime;
  74. if (GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime,
  75. &kernelTime, &userTime)) {
  76. ULARGE_INTEGER integerTime;
  77. integerTime.u.LowPart = userTime.dwLowDateTime;
  78. integerTime.u.HighPart = userTime.dwHighDateTime;
  79. return (long) (integerTime.QuadPart / 10000);
  80. } else {
  81. return 0;
  82. }
  83. #else
  84. return 0L;
  85. #endif
  86. }
  87. /**
  88. * @brief returns a long which represents the elapsed processor
  89. * time in milliseconds since some constant reference. It includes
  90. * waited-for terminated children.
  91. */
  92. long
  93. util_cpu_ctime(void)
  94. {
  95. #if HAVE_SYSCONF == 1
  96. /* Code for POSIX systems */
  97. struct tms buffer;
  98. long nticks; /* number of clock ticks per second */
  99. nticks = sysconf(_SC_CLK_TCK);
  100. times(&buffer);
  101. return (long) ((buffer.tms_utime + buffer.tms_cutime) * (1000.0/nticks));
  102. #else
  103. return 0L;
  104. #endif
  105. }