PageRenderTime 24ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/harbour-2.0/src/rtl/cputime.c

#
C | 193 lines | 103 code | 25 blank | 65 comment | 23 complexity | 782d37dbebac992eb9cf5d0eb6a86177 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*
  2. * $Id: cputime.c 12874 2009-11-14 17:11:06Z vszakats $
  3. */
  4. /*
  5. * Harbour Project source code:
  6. * hb_secondsCPU()
  7. *
  8. * Copyright 2003 Przemyslaw Czerpak <druzus@acn.waw.pl>
  9. * www - http://www.harbour-project.org
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this software; see the file COPYING. If not, write to
  23. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  24. * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
  25. *
  26. * As a special exception, the Harbour Project gives permission for
  27. * additional uses of the text contained in its release of Harbour.
  28. *
  29. * The exception is that, if you link the Harbour libraries with other
  30. * files to produce an executable, this does not by itself cause the
  31. * resulting executable to be covered by the GNU General Public License.
  32. * Your use of that executable is in no way restricted on account of
  33. * linking the Harbour library code into it.
  34. *
  35. * This exception does not however invalidate any other reasons why
  36. * the executable file might be covered by the GNU General Public License.
  37. *
  38. * This exception applies only to the code released by the Harbour
  39. * Project under the name Harbour. If you copy code from other
  40. * Harbour Project or Free Software Foundation releases into a copy of
  41. * Harbour, as the General Public License permits, the exception does
  42. * not apply to the code that you add in this way. To avoid misleading
  43. * anyone as to the status of such modified files, you must delete
  44. * this exception notice from them.
  45. *
  46. * If you write modifications of your own for Harbour, it is your choice
  47. * whether to permit this exception to apply to your modifications.
  48. * If you do not wish that, delete this exception notice.
  49. *
  50. */
  51. #define HB_OS_WIN_USED
  52. #define INCL_DOS
  53. #define INCL_DOSPROFILE
  54. #include "hbapi.h"
  55. #include "hbdate.h"
  56. #if defined( HB_OS_UNIX )
  57. #include <sys/times.h>
  58. #include <unistd.h>
  59. #endif
  60. #if defined( HB_OS_OS2 )
  61. #define BUFSIZE 16 * 1024
  62. #include <unistd.h>
  63. #if defined( __WATCOMC__ )
  64. #include <process.h>
  65. #endif
  66. #endif
  67. /*
  68. secondsCPU(n) -> nTime
  69. FlagShip/CLIP compatible function, which reports how many CPU and/or
  70. system seconds have elapsed since the beginning of the program execution.
  71. n == 1 utime -> user CPU time of the current process
  72. n == 2 stime -> system CPU time behalf of the current process
  73. n == 3 u + s -> sum of utime + stime (default)
  74. n == 11 cutime -> sum of the user CPU time of the current + child process
  75. n == 12 cstime -> sum of the system CPU time of the current + child process
  76. n == 13 cu+cs -> sum of cutime + cstime
  77. */
  78. double hb_secondsCPU( int n )
  79. {
  80. double d = 0.0;
  81. #if defined( HB_OS_WIN ) && !defined( HB_OS_UNIX )
  82. FILETIME Create, Exit, Kernel, User;
  83. #endif
  84. #if defined( HB_OS_OS2 )
  85. static ULONG s_timer_interval = 0;
  86. QSGREC ** pBuf;
  87. #endif
  88. if( ( n < 1 || n > 3 ) && ( n < 11 || n > 13 ) )
  89. n = 3;
  90. #if defined( HB_OS_UNIX )
  91. {
  92. struct tms tm;
  93. times( &tm );
  94. if( n > 10 )
  95. {
  96. n -= 10;
  97. if( n & 1 )
  98. d += tm.tms_cutime;
  99. if( n & 2 )
  100. d += tm.tms_cstime;
  101. }
  102. if( n & 1 )
  103. d += tm.tms_utime;
  104. if( n & 2 )
  105. d += tm.tms_stime;
  106. /* In POSIX-1996 the CLK_TCK symbol is mentioned as obsolescent */
  107. /* d /= CLK_TCK; */
  108. d /= ( double ) sysconf( _SC_CLK_TCK );
  109. }
  110. #else
  111. if( n > 10 )
  112. n -= 10;
  113. #if defined( HB_OS_WIN )
  114. if( hb_iswinnt() &&
  115. GetProcessTimes( GetCurrentProcess(), &Create, &Exit, &Kernel, &User ) )
  116. {
  117. if( n & 1 )
  118. {
  119. d += ( double ) ( ( ( HB_LONG ) User.dwHighDateTime << 32 ) +
  120. ( HB_LONG ) User.dwLowDateTime );
  121. }
  122. if( n & 2 )
  123. {
  124. d += ( double ) ( ( ( HB_LONG ) Kernel.dwHighDateTime << 32 ) +
  125. ( HB_LONG ) Kernel.dwLowDateTime );
  126. }
  127. d /= 10000000.0;
  128. }
  129. else
  130. #elif defined( HB_OS_OS2 )
  131. if( s_timer_interval == 0 )
  132. DosQuerySysInfo( QSV_TIMER_INTERVAL, QSV_TIMER_INTERVAL, ( PVOID ) &s_timer_interval, sizeof( ULONG ) );
  133. pBuf = ( QSGREC ** ) hb_xalloc( BUFSIZE );
  134. if( pBuf )
  135. {
  136. #if defined( __GNUC__ )
  137. APIRET rc = DosQuerySysState( QS_PROCESS, 0L, _getpid(), 0L, pBuf, BUFSIZE );
  138. #else
  139. APIRET rc = DosQuerySysState( QS_PROCESS, 0L, getpid(), 0L, pBuf, BUFSIZE );
  140. #endif
  141. if( rc == NO_ERROR )
  142. {
  143. QSGREC * pGrec = * pBuf;
  144. QSPREC * pPrec = ( QSPREC * ) ( ( ULONG ) pGrec + sizeof( QSGREC ) );
  145. QSTREC * pTrec = pPrec->pThrdRec;
  146. int i;
  147. for( i = 0; i < pPrec->cTCB; i++, pTrec++ )
  148. {
  149. if( n & 1 )
  150. d += pTrec->usertime;
  151. if( n & 2 )
  152. d += pTrec->systime;
  153. }
  154. d = d * 10.0 / s_timer_interval;
  155. }
  156. hb_xfree( pBuf );
  157. }
  158. else
  159. #endif
  160. {
  161. /* TODO: this code is only for DOS and other platforms which cannot
  162. calculate process time */
  163. if( n & 1 )
  164. d = hb_dateSeconds();
  165. }
  166. #endif
  167. return d;
  168. }