microtime.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.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. #include "php.h"
  19. #ifdef HAVE_SYS_TYPES_H
  20. #include <sys/types.h>
  21. #endif
  22. #ifdef PHP_WIN32
  23. #include "win32/time.h"
  24. #include "win32/getrusage.h"
  25. #else
  26. #include <sys/time.h>
  27. #endif
  28. #ifdef HAVE_SYS_RESOURCE_H
  29. #include <sys/resource.h>
  30. #endif
  31. #ifdef HAVE_UNISTD_H
  32. #include <unistd.h>
  33. #endif
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <errno.h>
  38. #include "microtime.h"
  39. #include "ext/date/php_date.h"
  40. #define NUL '\0'
  41. #define MICRO_IN_SEC 1000000.00
  42. #define SEC_IN_MIN 60
  43. #ifdef HAVE_GETTIMEOFDAY
  44. static void _php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS, int mode)
  45. {
  46. zend_bool get_as_float = 0;
  47. struct timeval tp = {0};
  48. ZEND_PARSE_PARAMETERS_START(0, 1)
  49. Z_PARAM_OPTIONAL
  50. Z_PARAM_BOOL(get_as_float)
  51. ZEND_PARSE_PARAMETERS_END();
  52. if (gettimeofday(&tp, NULL)) {
  53. RETURN_FALSE;
  54. }
  55. if (get_as_float) {
  56. RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
  57. }
  58. if (mode) {
  59. timelib_time_offset *offset;
  60. offset = timelib_get_time_zone_info(tp.tv_sec, get_timezone_info());
  61. array_init(return_value);
  62. add_assoc_long(return_value, "sec", tp.tv_sec);
  63. add_assoc_long(return_value, "usec", tp.tv_usec);
  64. add_assoc_long(return_value, "minuteswest", -offset->offset / SEC_IN_MIN);
  65. add_assoc_long(return_value, "dsttime", offset->is_dst);
  66. timelib_time_offset_dtor(offset);
  67. } else {
  68. RETURN_NEW_STR(zend_strpprintf(0, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, (long)tp.tv_sec));
  69. }
  70. }
  71. /* {{{ proto mixed microtime([bool get_as_float])
  72. Returns either a string or a float containing the current time in seconds and microseconds */
  73. PHP_FUNCTION(microtime)
  74. {
  75. _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  76. }
  77. /* }}} */
  78. /* {{{ proto array gettimeofday([bool get_as_float])
  79. Returns the current time as array */
  80. PHP_FUNCTION(gettimeofday)
  81. {
  82. _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  83. }
  84. #endif
  85. /* }}} */
  86. #ifdef HAVE_GETRUSAGE
  87. /* {{{ proto array getrusage([int who])
  88. Returns an array of usage statistics */
  89. PHP_FUNCTION(getrusage)
  90. {
  91. struct rusage usg;
  92. zend_long pwho = 0;
  93. int who = RUSAGE_SELF;
  94. ZEND_PARSE_PARAMETERS_START(0, 1)
  95. Z_PARAM_OPTIONAL
  96. Z_PARAM_LONG(pwho)
  97. ZEND_PARSE_PARAMETERS_END();
  98. if (pwho == 1) {
  99. who = RUSAGE_CHILDREN;
  100. }
  101. memset(&usg, 0, sizeof(struct rusage));
  102. if (getrusage(who, &usg) == -1) {
  103. RETURN_FALSE;
  104. }
  105. array_init(return_value);
  106. #define PHP_RUSAGE_PARA(a) \
  107. add_assoc_long(return_value, #a, usg.a)
  108. #ifdef PHP_WIN32 /* Windows only implements a limited amount of fields from the rusage struct */
  109. PHP_RUSAGE_PARA(ru_majflt);
  110. PHP_RUSAGE_PARA(ru_maxrss);
  111. #elif !defined(_OSD_POSIX)
  112. PHP_RUSAGE_PARA(ru_oublock);
  113. PHP_RUSAGE_PARA(ru_inblock);
  114. PHP_RUSAGE_PARA(ru_msgsnd);
  115. PHP_RUSAGE_PARA(ru_msgrcv);
  116. PHP_RUSAGE_PARA(ru_maxrss);
  117. PHP_RUSAGE_PARA(ru_ixrss);
  118. PHP_RUSAGE_PARA(ru_idrss);
  119. PHP_RUSAGE_PARA(ru_minflt);
  120. PHP_RUSAGE_PARA(ru_majflt);
  121. PHP_RUSAGE_PARA(ru_nsignals);
  122. PHP_RUSAGE_PARA(ru_nvcsw);
  123. PHP_RUSAGE_PARA(ru_nivcsw);
  124. PHP_RUSAGE_PARA(ru_nswap);
  125. #endif /*_OSD_POSIX*/
  126. PHP_RUSAGE_PARA(ru_utime.tv_usec);
  127. PHP_RUSAGE_PARA(ru_utime.tv_sec);
  128. PHP_RUSAGE_PARA(ru_stime.tv_usec);
  129. PHP_RUSAGE_PARA(ru_stime.tv_sec);
  130. #undef PHP_RUSAGE_PARA
  131. }
  132. #endif /* HAVE_GETRUSAGE */
  133. /* }}} */
  134. /*
  135. * Local variables:
  136. * tab-width: 4
  137. * c-basic-offset: 4
  138. * End:
  139. * vim600: sw=4 ts=4 fdm=marker
  140. * vim<600: sw=4 ts=4
  141. */