microtime.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Paul Panotzki - Bunyip Information Systems |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #ifdef HAVE_SYS_TYPES_H
  18. #include <sys/types.h>
  19. #endif
  20. #ifdef PHP_WIN32
  21. #include "win32/time.h"
  22. #include "win32/getrusage.h"
  23. #else
  24. #include <sys/time.h>
  25. #endif
  26. #ifdef HAVE_SYS_RESOURCE_H
  27. #include <sys/resource.h>
  28. #endif
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #include "ext/date/php_date.h"
  37. #define NUL '\0'
  38. #define MICRO_IN_SEC 1000000.00
  39. #define SEC_IN_MIN 60
  40. #ifdef HAVE_GETTIMEOFDAY
  41. static void _php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS, int mode)
  42. {
  43. bool get_as_float = 0;
  44. struct timeval tp = {0};
  45. ZEND_PARSE_PARAMETERS_START(0, 1)
  46. Z_PARAM_OPTIONAL
  47. Z_PARAM_BOOL(get_as_float)
  48. ZEND_PARSE_PARAMETERS_END();
  49. if (gettimeofday(&tp, NULL)) {
  50. ZEND_ASSERT(0 && "gettimeofday() can't fail");
  51. }
  52. if (get_as_float) {
  53. RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
  54. }
  55. if (mode) {
  56. timelib_time_offset *offset;
  57. offset = timelib_get_time_zone_info(tp.tv_sec, get_timezone_info());
  58. array_init(return_value);
  59. add_assoc_long(return_value, "sec", tp.tv_sec);
  60. add_assoc_long(return_value, "usec", tp.tv_usec);
  61. add_assoc_long(return_value, "minuteswest", -offset->offset / SEC_IN_MIN);
  62. add_assoc_long(return_value, "dsttime", offset->is_dst);
  63. timelib_time_offset_dtor(offset);
  64. } else {
  65. RETURN_NEW_STR(zend_strpprintf(0, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, (long)tp.tv_sec));
  66. }
  67. }
  68. /* {{{ Returns either a string or a float containing the current time in seconds and microseconds */
  69. PHP_FUNCTION(microtime)
  70. {
  71. _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  72. }
  73. /* }}} */
  74. /* {{{ Returns the current time as array */
  75. PHP_FUNCTION(gettimeofday)
  76. {
  77. _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  78. }
  79. #endif
  80. /* }}} */
  81. #ifdef HAVE_GETRUSAGE
  82. /* {{{ Returns an array of usage statistics */
  83. PHP_FUNCTION(getrusage)
  84. {
  85. struct rusage usg;
  86. zend_long pwho = 0;
  87. int who = RUSAGE_SELF;
  88. ZEND_PARSE_PARAMETERS_START(0, 1)
  89. Z_PARAM_OPTIONAL
  90. Z_PARAM_LONG(pwho)
  91. ZEND_PARSE_PARAMETERS_END();
  92. if (pwho == 1) {
  93. who = RUSAGE_CHILDREN;
  94. }
  95. memset(&usg, 0, sizeof(struct rusage));
  96. if (getrusage(who, &usg) == -1) {
  97. RETURN_FALSE;
  98. }
  99. array_init(return_value);
  100. #define PHP_RUSAGE_PARA(a) \
  101. add_assoc_long(return_value, #a, usg.a)
  102. #ifdef PHP_WIN32 /* Windows only implements a limited amount of fields from the rusage struct */
  103. PHP_RUSAGE_PARA(ru_majflt);
  104. PHP_RUSAGE_PARA(ru_maxrss);
  105. #elif !defined(_OSD_POSIX) && !defined(__HAIKU__)
  106. PHP_RUSAGE_PARA(ru_oublock);
  107. PHP_RUSAGE_PARA(ru_inblock);
  108. PHP_RUSAGE_PARA(ru_msgsnd);
  109. PHP_RUSAGE_PARA(ru_msgrcv);
  110. PHP_RUSAGE_PARA(ru_maxrss);
  111. PHP_RUSAGE_PARA(ru_ixrss);
  112. PHP_RUSAGE_PARA(ru_idrss);
  113. PHP_RUSAGE_PARA(ru_minflt);
  114. PHP_RUSAGE_PARA(ru_majflt);
  115. PHP_RUSAGE_PARA(ru_nsignals);
  116. PHP_RUSAGE_PARA(ru_nvcsw);
  117. PHP_RUSAGE_PARA(ru_nivcsw);
  118. PHP_RUSAGE_PARA(ru_nswap);
  119. #endif /*_OSD_POSIX*/
  120. PHP_RUSAGE_PARA(ru_utime.tv_usec);
  121. PHP_RUSAGE_PARA(ru_utime.tv_sec);
  122. PHP_RUSAGE_PARA(ru_stime.tv_usec);
  123. PHP_RUSAGE_PARA(ru_stime.tv_sec);
  124. #undef PHP_RUSAGE_PARA
  125. }
  126. #endif /* HAVE_GETRUSAGE */
  127. /* }}} */