xref: /PHP-7.4/ext/standard/microtime.c (revision 974e77b3)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 
19 #include "php.h"
20 
21 #ifdef HAVE_SYS_TYPES_H
22 #include <sys/types.h>
23 #endif
24 #ifdef PHP_WIN32
25 #include "win32/time.h"
26 #include "win32/getrusage.h"
27 #else
28 #include <sys/time.h>
29 #endif
30 #ifdef HAVE_SYS_RESOURCE_H
31 #include <sys/resource.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <errno.h>
40 
41 #include "microtime.h"
42 #include "ext/date/php_date.h"
43 
44 #define NUL  '\0'
45 #define MICRO_IN_SEC 1000000.00
46 #define SEC_IN_MIN 60
47 
48 #ifdef HAVE_GETTIMEOFDAY
_php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS,int mode)49 static void _php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS, int mode)
50 {
51 	zend_bool get_as_float = 0;
52 	struct timeval tp = {0};
53 
54 	ZEND_PARSE_PARAMETERS_START(0, 1)
55 		Z_PARAM_OPTIONAL
56 		Z_PARAM_BOOL(get_as_float)
57 	ZEND_PARSE_PARAMETERS_END();
58 
59 	if (gettimeofday(&tp, NULL)) {
60 		ZEND_ASSERT(0 && "gettimeofday() can't fail");
61 	}
62 
63 	if (get_as_float) {
64 		RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
65 	}
66 
67 	if (mode) {
68 		timelib_time_offset *offset;
69 
70 		offset = timelib_get_time_zone_info(tp.tv_sec, get_timezone_info());
71 
72 		array_init(return_value);
73 		add_assoc_long(return_value, "sec", tp.tv_sec);
74 		add_assoc_long(return_value, "usec", tp.tv_usec);
75 
76 		add_assoc_long(return_value, "minuteswest", -offset->offset / SEC_IN_MIN);
77 		add_assoc_long(return_value, "dsttime", offset->is_dst);
78 
79 		timelib_time_offset_dtor(offset);
80 	} else {
81 		RETURN_NEW_STR(zend_strpprintf(0, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, (long)tp.tv_sec));
82 	}
83 }
84 
85 /* {{{ proto mixed microtime([bool get_as_float])
86    Returns either a string or a float containing the current time in seconds and microseconds */
PHP_FUNCTION(microtime)87 PHP_FUNCTION(microtime)
88 {
89 	_php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
90 }
91 /* }}} */
92 
93 /* {{{ proto array gettimeofday([bool get_as_float])
94    Returns the current time as array */
PHP_FUNCTION(gettimeofday)95 PHP_FUNCTION(gettimeofday)
96 {
97 	_php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
98 }
99 #endif
100 /* }}} */
101 
102 #ifdef HAVE_GETRUSAGE
103 /* {{{ proto array getrusage([int who])
104    Returns an array of usage statistics */
PHP_FUNCTION(getrusage)105 PHP_FUNCTION(getrusage)
106 {
107 	struct rusage usg;
108 	zend_long pwho = 0;
109 	int who = RUSAGE_SELF;
110 
111 	ZEND_PARSE_PARAMETERS_START(0, 1)
112 		Z_PARAM_OPTIONAL
113 		Z_PARAM_LONG(pwho)
114 	ZEND_PARSE_PARAMETERS_END();
115 
116 	if (pwho == 1) {
117 		who = RUSAGE_CHILDREN;
118 	}
119 
120 	memset(&usg, 0, sizeof(struct rusage));
121 
122 	if (getrusage(who, &usg) == -1) {
123 		RETURN_FALSE;
124 	}
125 
126 	array_init(return_value);
127 
128 #define PHP_RUSAGE_PARA(a) \
129 		add_assoc_long(return_value, #a, usg.a)
130 
131 #ifdef PHP_WIN32 /* Windows only implements a limited amount of fields from the rusage struct */
132 	PHP_RUSAGE_PARA(ru_majflt);
133 	PHP_RUSAGE_PARA(ru_maxrss);
134 #elif !defined(_OSD_POSIX)
135 	PHP_RUSAGE_PARA(ru_oublock);
136 	PHP_RUSAGE_PARA(ru_inblock);
137 	PHP_RUSAGE_PARA(ru_msgsnd);
138 	PHP_RUSAGE_PARA(ru_msgrcv);
139 	PHP_RUSAGE_PARA(ru_maxrss);
140 	PHP_RUSAGE_PARA(ru_ixrss);
141 	PHP_RUSAGE_PARA(ru_idrss);
142 	PHP_RUSAGE_PARA(ru_minflt);
143 	PHP_RUSAGE_PARA(ru_majflt);
144 	PHP_RUSAGE_PARA(ru_nsignals);
145 	PHP_RUSAGE_PARA(ru_nvcsw);
146 	PHP_RUSAGE_PARA(ru_nivcsw);
147 	PHP_RUSAGE_PARA(ru_nswap);
148 #endif /*_OSD_POSIX*/
149 	PHP_RUSAGE_PARA(ru_utime.tv_usec);
150 	PHP_RUSAGE_PARA(ru_utime.tv_sec);
151 	PHP_RUSAGE_PARA(ru_stime.tv_usec);
152 	PHP_RUSAGE_PARA(ru_stime.tv_sec);
153 
154 #undef PHP_RUSAGE_PARA
155 }
156 #endif /* HAVE_GETRUSAGE */
157 
158 /* }}} */
159