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.

162 lines
4.2 KiB

24 years ago
24 years ago
27 years ago
27 years ago
24 years ago
27 years ago
26 years ago
26 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2002 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Paul Panotzki - Bunyip Information Systems |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "php.h"
  20. #ifdef HAVE_SYS_TYPES_H
  21. #include <sys/types.h>
  22. #endif
  23. #ifdef PHP_WIN32
  24. #include "win32/time.h"
  25. #elif defined(NETWARE)
  26. #ifdef NEW_LIBC
  27. #include <sys/timeval.h>
  28. #else
  29. #include "netware/time_nw.h"
  30. #endif
  31. #else
  32. #include <sys/time.h>
  33. #endif
  34. #ifdef HAVE_SYS_RESOURCE_H
  35. #include <sys/resource.h>
  36. #endif
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <stdio.h>
  43. #include <errno.h>
  44. #include "microtime.h"
  45. #define NUL '\0'
  46. #define MICRO_IN_SEC 1000000.00
  47. #define SEC_IN_MIN 60
  48. /* {{{ proto string microtime(void)
  49. Returns a string containing the current time in seconds and microseconds */
  50. #ifdef HAVE_GETTIMEOFDAY
  51. PHP_FUNCTION(microtime)
  52. {
  53. struct timeval tp;
  54. long sec = 0L;
  55. double msec = 0.0;
  56. char ret[100];
  57. if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) {
  58. msec = (double) (tp.tv_usec / MICRO_IN_SEC);
  59. sec = tp.tv_sec;
  60. if (msec >= 1.0) msec -= (long) msec;
  61. snprintf(ret, 100, "%.8f %ld", msec, sec);
  62. RETVAL_STRING(ret,1);
  63. } else {
  64. RETURN_FALSE;
  65. }
  66. }
  67. #endif
  68. /* }}} */
  69. /* {{{ proto array gettimeofday(void)
  70. Returns the current time as array */
  71. #ifdef HAVE_GETTIMEOFDAY
  72. PHP_FUNCTION(gettimeofday)
  73. {
  74. struct timeval tp;
  75. struct timezone tz;
  76. memset(&tp, 0, sizeof(tp));
  77. memset(&tz, 0, sizeof(tz));
  78. if(gettimeofday(&tp, &tz) == 0) {
  79. array_init(return_value);
  80. add_assoc_long(return_value, "sec", tp.tv_sec);
  81. add_assoc_long(return_value, "usec", tp.tv_usec);
  82. #ifdef PHP_WIN32
  83. add_assoc_long(return_value, "minuteswest", tz.tz_minuteswest/SEC_IN_MIN);
  84. #else
  85. add_assoc_long(return_value, "minuteswest", tz.tz_minuteswest);
  86. #endif
  87. add_assoc_long(return_value, "dsttime", tz.tz_dsttime);
  88. return;
  89. } else {
  90. RETURN_FALSE;
  91. }
  92. }
  93. #endif
  94. /* }}} */
  95. #ifdef HAVE_GETRUSAGE
  96. /* {{{ proto array getrusage([int who])
  97. Returns an array of usage statistics */
  98. PHP_FUNCTION(getrusage)
  99. {
  100. struct rusage usg;
  101. int ac = ZEND_NUM_ARGS();
  102. pval **pwho;
  103. int who = RUSAGE_SELF;
  104. if(ac == 1 &&
  105. zend_get_parameters_ex(ac, &pwho) != FAILURE) {
  106. convert_to_long_ex(pwho);
  107. if(Z_LVAL_PP(pwho) == 1)
  108. who = RUSAGE_CHILDREN;
  109. }
  110. memset(&usg, 0, sizeof(usg));
  111. if(getrusage(who, &usg) == -1) {
  112. RETURN_FALSE;
  113. }
  114. array_init(return_value);
  115. #define PHP_RUSAGE_PARA(a) \
  116. add_assoc_long(return_value, #a, usg.a)
  117. #if !defined( _OSD_POSIX) && !defined(__BEOS__) /* BS2000 has only a few fields in the rusage struct */
  118. PHP_RUSAGE_PARA(ru_oublock);
  119. PHP_RUSAGE_PARA(ru_inblock);
  120. PHP_RUSAGE_PARA(ru_msgsnd);
  121. PHP_RUSAGE_PARA(ru_msgrcv);
  122. PHP_RUSAGE_PARA(ru_maxrss);
  123. PHP_RUSAGE_PARA(ru_ixrss);
  124. PHP_RUSAGE_PARA(ru_idrss);
  125. PHP_RUSAGE_PARA(ru_minflt);
  126. PHP_RUSAGE_PARA(ru_majflt);
  127. PHP_RUSAGE_PARA(ru_nsignals);
  128. PHP_RUSAGE_PARA(ru_nvcsw);
  129. PHP_RUSAGE_PARA(ru_nivcsw);
  130. #endif /*_OSD_POSIX*/
  131. PHP_RUSAGE_PARA(ru_utime.tv_usec);
  132. PHP_RUSAGE_PARA(ru_utime.tv_sec);
  133. PHP_RUSAGE_PARA(ru_stime.tv_usec);
  134. PHP_RUSAGE_PARA(ru_stime.tv_sec);
  135. #undef PHP_RUSAGE_PARA
  136. }
  137. #endif /* HAVE_GETRUSAGE */
  138. /* }}} */
  139. /*
  140. * Local variables:
  141. * tab-width: 4
  142. * c-basic-offset: 4
  143. * End:
  144. * vim600: sw=4 ts=4 fdm=marker
  145. * vim<600: sw=4 ts=4
  146. */