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