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