Skip to content

Commit 22fc3ba

Browse files
committed
Windows support for getrusage()
* See getrusage.c/h for implementation details and limitations * Tests passes and have had their SKIPIF updated * psapi.lib is now linked to by default
1 parent f7bfeac commit 22fc3ba

File tree

10 files changed

+206
-14
lines changed

10 files changed

+206
-14
lines changed

NEWS

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
. Implement request #67106 (Split main fpm config). (Elan Ruusamäe, Remi)
120120

121121
- FTP:
122-
. Fixed bug #69082 FTPS support on Windows
122+
. Fixed bug #69082 (FTPS support on Windows). (Anatol)
123123

124124
- Intl:
125125
. Removed deprecated aliases datefmt_set_timezone_id() and
@@ -223,6 +223,7 @@
223223
(Daniel Lowrey)
224224
. Added preg_replace_callback_array function. (Wei Dai)
225225
. Deprecated salt option to password_hash. (Anthony)
226+
. Added Windows support for getrusage(). (Kalle)
226227

227228
- Streams:
228229
. Fixed bug #68532 (convert.base64-encode omits padding bytes).

ext/standard/microtime.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#endif
2626
#ifdef PHP_WIN32
2727
#include "win32/time.h"
28+
#include "win32/getrusage.h"
2829
#elif defined(NETWARE)
2930
#include <sys/timeval.h>
3031
#include <sys/time.h>
@@ -129,9 +130,14 @@ PHP_FUNCTION(getrusage)
129130
}
130131

131132
array_init(return_value);
133+
132134
#define PHP_RUSAGE_PARA(a) \
133135
add_assoc_long(return_value, #a, usg.a)
134-
#if !defined( _OSD_POSIX) && !defined(__BEOS__) /* BS2000 has only a few fields in the rusage struct */
136+
137+
#ifdef PHP_WIN32 /* Windows only implements a limited amount of fields from the rusage struct */
138+
PHP_RUSAGE_PARA(ru_majflt);
139+
PHP_RUSAGE_PARA(ru_maxrss);
140+
#elif !defined( _OSD_POSIX) && !defined(__BEOS__) /* BS2000 has only a few fields in the rusage struct*/
135141
PHP_RUSAGE_PARA(ru_oublock);
136142
PHP_RUSAGE_PARA(ru_inblock);
137143
PHP_RUSAGE_PARA(ru_msgsnd);
@@ -150,6 +156,7 @@ PHP_FUNCTION(getrusage)
150156
PHP_RUSAGE_PARA(ru_utime.tv_sec);
151157
PHP_RUSAGE_PARA(ru_stime.tv_usec);
152158
PHP_RUSAGE_PARA(ru_stime.tv_sec);
159+
153160
#undef PHP_RUSAGE_PARA
154161
}
155162
#endif /* HAVE_GETRUSAGE */

ext/standard/tests/general_functions/getrusage_basic.phpt

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
--TEST--
22
Test getrusage() function: basic test
33
--SKIPIF--
4-
<?php
5-
if( substr(PHP_OS, 0, 3) == "WIN" )
6-
die("skip.. Do not run on Windows");
7-
?>
4+
<?php if (!function_exists("getrusage")) print "skip"; ?>
85
--FILE--
96
<?php
107
/* Prototype : array getrusage ([ int $who ] )

ext/standard/tests/general_functions/getrusage_error.phpt

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
--TEST--
22
Test getrusage() function : error conditions - incorrect number of args
33
--SKIPIF--
4-
<?php
5-
if( substr(PHP_OS, 0, 3) == "WIN" )
6-
die("skip.. Do not run on Windows");
7-
?>
4+
<?php if (!function_exists("getrusage")) print "skip"; ?>
85
--FILE--
96
<?php
107
/* Prototype : array getrusage ([ int $who ] )

ext/standard/tests/general_functions/getrusage_variation1.phpt

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ Test getrusage() function : usage variation - diff data types as $who arg
33
--SKIPIF--
44
<?php
55
if (PHP_INT_SIZE != 8) die("skip this test is for 64-bit only");
6-
if( substr(PHP_OS, 0, 3) == "WIN" )
7-
die("skip.. Do not run on Windows");
6+
if (!function_exists("getrusage")) die("skip");
87
?>
98
--FILE--
109
<?php

win32/build/config.w32

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ ADD_SOURCES("main/streams", "streams.c cast.c memory.c filter.c plain_wrapper.c
160160
ADD_FLAG("CFLAGS_BD_MAIN_STREAMS", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
161161

162162
ADD_SOURCES("win32", "glob.c readdir.c \
163-
registry.c select.c sendmail.c time.c winutil.c wsyslog.c globals.c");
163+
registry.c select.c sendmail.c time.c winutil.c wsyslog.c globals.c getrusage.c");
164164

165165
ADD_FLAG("CFLAGS_BD_WIN32", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
166166

win32/build/config.w32.h.in

+2
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,5 @@
181181

182182
#define HAVE_HUGE_VAL_INF 1
183183

184+
#define HAVE_GETRUSAGE
185+

win32/build/confutils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2654,7 +2654,7 @@ function toolset_setup_common_ldlags()
26542654
function toolset_setup_common_libs()
26552655
{
26562656
// urlmon.lib ole32.lib oleaut32.lib uuid.lib gdi32.lib winspool.lib comdlg32.lib
2657-
DEFINE("LIBS", "kernel32.lib ole32.lib user32.lib advapi32.lib shell32.lib ws2_32.lib Dnsapi.lib");
2657+
DEFINE("LIBS", "kernel32.lib ole32.lib user32.lib advapi32.lib shell32.lib ws2_32.lib Dnsapi.lib psapi.lib");
26582658
}
26592659

26602660
function toolset_setup_build_mode()

win32/getrusage.c

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 7 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2015 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+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Authors: Kalle Sommer Nielsen <[email protected]> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
#include <php.h>
20+
#include <psapi.h>
21+
#include "getrusage.h"
22+
23+
/*
24+
* Parts of this file is based on code from the OpenVSwitch project, that
25+
* is released under the Apache 2.0 license and is copyright 2014 Nicira, Inc.
26+
* and have been modified to work with PHP.
27+
*/
28+
29+
static void usage_to_timeval(FILETIME *ft, struct timeval *tv)
30+
{
31+
ULARGE_INTEGER time;
32+
33+
time.LowPart = ft->dwLowDateTime;
34+
time.HighPart = ft->dwHighDateTime;
35+
36+
tv->tv_sec = (zend_long) (time.QuadPart / 10000000);
37+
tv->tv_usec = (zend_long) ((time.QuadPart % 10000000) / 10);
38+
}
39+
40+
PHPAPI int getrusage(int who, struct rusage *usage)
41+
{
42+
FILETIME ctime, etime, stime, utime;
43+
44+
memset(usage, 0, sizeof(struct rusage));
45+
46+
if (who == RUSAGE_SELF) {
47+
PROCESS_MEMORY_COUNTERS pmc;
48+
HANDLE proc = GetCurrentProcess();
49+
50+
if (!GetProcessTimes(proc, &ctime, &etime, &stime, &utime)) {
51+
return -1;
52+
} else if(!GetProcessMemoryInfo(proc, &pmc, sizeof(pmc))) {
53+
return -1;
54+
}
55+
56+
usage_to_timeval(&stime, &usage->ru_stime);
57+
usage_to_timeval(&utime, &usage->ru_utime);
58+
59+
usage->ru_majflt = pmc.PageFaultCount;
60+
usage->ru_maxrss = pmc.PeakWorkingSetSize / 1024;
61+
62+
return 0;
63+
} else if (who == RUSAGE_THREAD) {
64+
if (!GetThreadTimes(GetCurrentThread(), &ctime, &etime, &stime, &utime)) {
65+
return -1;
66+
}
67+
68+
usage_to_timeval(&stime, &usage->ru_stime);
69+
usage_to_timeval(&utime, &usage->ru_utime);
70+
71+
return 0;
72+
} else {
73+
return -1;
74+
}
75+
}

win32/getrusage.h

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 7 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2015 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+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Authors: Kalle Sommer Nielsen <[email protected]> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
#ifndef HAVE_GETRUSAGE_H
20+
# define HAVE_GETRUSAGE_H
21+
22+
/* Note,
23+
*
24+
* RUSAGE_CHILDREN is not implemented, and the RUSAGE_THREAD will
25+
* instead be used instead.
26+
*/
27+
28+
# define RUSAGE_SELF 0
29+
# define RUSAGE_CHILDREN 1
30+
31+
# define RUSAGE_THREAD RUSAGE_CHILDREN
32+
33+
/*
34+
* Implementation support
35+
*
36+
* RUSAGE_SELF
37+
* ru_utime
38+
* ru_stime
39+
* ru_majflt
40+
* ru_maxrss
41+
*
42+
* RUSAGE_THREAD
43+
* ru_utime
44+
* ru_stime
45+
*
46+
* Not implemented:
47+
* ru_ixrss (unused)
48+
* ru_idrss (unused)
49+
* ru_isrss (unused)
50+
* ru_minflt
51+
* ru_nswap (unused)
52+
* ru_inblock
53+
* ru_oublock
54+
* ru_msgsnd (unused)
55+
* ru_msgrcv (unused)
56+
* ru_nsignals (unused)
57+
* ru_nvcsw
58+
* ru_nivcsw
59+
*/
60+
61+
struct rusage
62+
{
63+
/* User time used */
64+
struct timeval ru_utime;
65+
66+
/* System time used */
67+
struct timeval ru_stime;
68+
69+
/* Integral max resident set size */
70+
long ru_maxrss;
71+
72+
/* Integral shared text memory size */
73+
long ru_ixrss;
74+
75+
/* Integral unshared data size */
76+
long ru_idrss;
77+
78+
/* Integral unshared stack size */
79+
long ru_isrss;
80+
81+
/* Page reclaims */
82+
long ru_minflt;
83+
84+
/* Page faults */
85+
long ru_majflt;
86+
87+
/* Swaps */
88+
long ru_nswap;
89+
90+
/* Block input operations */
91+
long ru_inblock;
92+
93+
/* Block output operations */
94+
long ru_oublock;
95+
96+
/* Messages sent */
97+
long ru_msgsnd;
98+
99+
/* Messages received */
100+
long ru_msgrcv;
101+
102+
/* Signals received */
103+
long ru_nsignals;
104+
105+
/* Voluntary context switches */
106+
long ru_nvcsw;
107+
108+
/* Involuntary context switches */
109+
long ru_nivcsw;
110+
};
111+
112+
PHPAPI int getrusage(int who, struct rusage *usage);
113+
114+
#endif

0 commit comments

Comments
 (0)