File: microtime.c

package info (click to toggle)
php3 3%3A3.0.18-0potato1.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 17,736 kB
  • ctags: 11,198
  • sloc: ansic: 108,120; sh: 2,512; php: 2,024; yacc: 1,887; makefile: 1,038; perl: 537; pascal: 238; awk: 90; cpp: 28; sql: 11
file content (200 lines) | stat: -rw-r--r-- 6,005 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
   +----------------------------------------------------------------------+
   | PHP HTML Embedded Scripting Language Version 3.0                     |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2000 PHP Development Team (See Credits file)      |
   +----------------------------------------------------------------------+
   | This program is free software; you can redistribute it and/or modify |
   | it under the terms of one of the following licenses:                 |
   |                                                                      |
   |  A) the GNU General Public License as published by the Free Software |
   |     Foundation; either version 2 of the License, or (at your option) |
   |     any later version.                                               |
   |                                                                      |
   |  B) the PHP License as published by the PHP Development Team and     |
   |     included in the distribution in the file: LICENSE                |
   |                                                                      |
   | This program is distributed in the hope that it will be useful,      |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of both licenses referred to here.   |
   | If you did not, or have any questions about PHP licensing, please    |
   | contact core@php.net.                                                |
   +----------------------------------------------------------------------+
   | Authors: Paul Panotzki - Bunyip Information Systems                  |
   +----------------------------------------------------------------------+
 */

/* $Id: microtime.c,v 1.38 2000/02/26 09:03:55 bjh Exp $ */

#include "php.h"
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#include <string.h>
#include <errno.h>

#include "tls.h"
#include "internal_functions.h"
#include "microtime.h"
#include "snprintf.h"

#include <stdio.h>
#if HAVE_GETTIMEOFDAY
#if WIN32
#include "win32/time.h"
#else
#include <sys/time.h>
#endif
#endif

#define NUL  '\0'
#define MICRO_IN_SEC 1000000.00

#if OS2
#define INCL_DOSPROFILE
#include <os2.h>
#endif

/* {{{ proto string microtime(void)
   Returns a string containing the current time in seconds and microseconds */
#ifdef __cplusplus
void php3_microtime(HashTable *)
#else
void php3_microtime(INTERNAL_FUNCTION_PARAMETERS)
#endif
{
#if HAVE_GETTIMEOFDAY
	struct timeval tp;
	char ret[100];

#if OS2
    static ULONG hrtimerHz = 0;
    /* Number of hrtimerHz between 1 Jan 1970 & base of DosTmrQueryTime() */
    static unsigned long long hradjust; 
    QWORD qwNow;
    unsigned long long llNow;

    if (hrtimerHz == 0) {
        DosTmrQueryFreq(&hrtimerHz);
        gettimeofday(&tp, NULL);
        DosTmrQueryTime(&qwNow);
        llNow = ((unsigned long long)qwNow.ulHi << 32) | qwNow.ulLo;
        hradjust = (unsigned long long)tp.tv_sec * hrtimerHz + (tp.tv_usec * hrtimerHz / MICRO_IN_SEC) - llNow;
    }

    DosTmrQueryTime(&qwNow);
    llNow = ((unsigned long long)qwNow.ulHi << 32) | qwNow.ulLo;
    llNow += hradjust;
    snprintf(ret, 100, "%.8f %ld", (double)(llNow % hrtimerHz)/hrtimerHz, llNow / hrtimerHz);
    RETVAL_STRING(ret,1);
#else
	long sec = 0L;
	double msec = 0.0;
	TLS_VARS;
	
	if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) {
		msec = (double) (tp.tv_usec / MICRO_IN_SEC);
		sec = tp.tv_sec;
	}
	if (msec >= 1.0) msec -= (long) msec;
	snprintf(ret, 100, "%.8f %ld", msec, sec);
	RETVAL_STRING(ret,1);
#endif
#endif
}
/* }}} */

/* {{{ proto array gettimeofday(void)
   Returns the current time as array */
void php3_gettimeofday(INTERNAL_FUNCTION_PARAMETERS)
{
	_php3_gettimeofday(return_value);
}

void _php3_gettimeofday(pval *return_value) {
#if HAVE_GETTIMEOFDAY
	struct timeval tp;
	struct timezone tz;
	
	memset(&tp, 0, sizeof(tp));
	memset(&tz, 0, sizeof(tz));
	if(gettimeofday(&tp, &tz) == 0) {
		array_init(return_value);
		add_assoc_long(return_value, "sec", tp.tv_sec);
		add_assoc_long(return_value, "usec", tp.tv_usec);
		add_assoc_long(return_value, "minuteswest", tz.tz_minuteswest);
		add_assoc_long(return_value, "dsttime", tz.tz_dsttime);
	} else {
		RETURN_FALSE;
	}
#endif
}
/* }}} */

/* {{{ proto array getrusage([int who])
   Returns an array of usage statistics */
void php3_getrusage(INTERNAL_FUNCTION_PARAMETERS)
{
#if HAVE_GETRUSAGE
	struct rusage usg;
	int ac = ARG_COUNT(ht);
	pval *pwho;
	int who = RUSAGE_SELF;

	if(ac == 1 &&
		getParameters(ht, ac, &pwho) != FAILURE) {
		convert_to_long(pwho);
		if(pwho->value.lval == 1)
			who = RUSAGE_CHILDREN;
	}

	memset(&usg, 0, sizeof(usg));
	if(getrusage(who, &usg) == -1) {
		RETURN_FALSE;
	}

	array_init(return_value);
#define PHP3_RUSAGE_PARA(a) \
		add_assoc_long(return_value, #a, usg.a)
#ifndef _OSD_POSIX /* BS2000 has only a few fields in the rusage struct */
	PHP3_RUSAGE_PARA(ru_oublock);
	PHP3_RUSAGE_PARA(ru_inblock);
	PHP3_RUSAGE_PARA(ru_msgsnd);
	PHP3_RUSAGE_PARA(ru_msgrcv);
	PHP3_RUSAGE_PARA(ru_maxrss);
	PHP3_RUSAGE_PARA(ru_ixrss);
	PHP3_RUSAGE_PARA(ru_idrss);
	PHP3_RUSAGE_PARA(ru_minflt);
	PHP3_RUSAGE_PARA(ru_majflt);
	PHP3_RUSAGE_PARA(ru_nsignals);
	PHP3_RUSAGE_PARA(ru_nvcsw);
	PHP3_RUSAGE_PARA(ru_nivcsw);
#endif /*_OSD_POSIX*/
	PHP3_RUSAGE_PARA(ru_utime.tv_usec);
	PHP3_RUSAGE_PARA(ru_utime.tv_sec);
	PHP3_RUSAGE_PARA(ru_stime.tv_usec);
	PHP3_RUSAGE_PARA(ru_stime.tv_sec);
#undef PHP3_RUSAGE_PARA
#endif /* HAVE_GETRUSAGE */
}
/* }}} */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */