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