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
This commit is contained in:
Kalle Sommer Nielsen 2015-05-20 15:45:37 +02:00
parent f7bfeac419
commit 22fc3baa7a
10 changed files with 206 additions and 14 deletions

3
NEWS
View file

@ -119,7 +119,7 @@
. Implement request #67106 (Split main fpm config). (Elan Ruusamäe, Remi)
- FTP:
. Fixed bug #69082 FTPS support on Windows
. Fixed bug #69082 (FTPS support on Windows). (Anatol)
- Intl:
. Removed deprecated aliases datefmt_set_timezone_id() and
@ -223,6 +223,7 @@
(Daniel Lowrey)
. Added preg_replace_callback_array function. (Wei Dai)
. Deprecated salt option to password_hash. (Anthony)
. Added Windows support for getrusage(). (Kalle)
- Streams:
. Fixed bug #68532 (convert.base64-encode omits padding bytes).

View file

@ -25,6 +25,7 @@
#endif
#ifdef PHP_WIN32
#include "win32/time.h"
#include "win32/getrusage.h"
#elif defined(NETWARE)
#include <sys/timeval.h>
#include <sys/time.h>
@ -129,9 +130,14 @@ PHP_FUNCTION(getrusage)
}
array_init(return_value);
#define PHP_RUSAGE_PARA(a) \
add_assoc_long(return_value, #a, usg.a)
#if !defined( _OSD_POSIX) && !defined(__BEOS__) /* BS2000 has only a few fields in the rusage struct */
#ifdef PHP_WIN32 /* Windows only implements a limited amount of fields from the rusage struct */
PHP_RUSAGE_PARA(ru_majflt);
PHP_RUSAGE_PARA(ru_maxrss);
#elif !defined( _OSD_POSIX) && !defined(__BEOS__) /* BS2000 has only a few fields in the rusage struct*/
PHP_RUSAGE_PARA(ru_oublock);
PHP_RUSAGE_PARA(ru_inblock);
PHP_RUSAGE_PARA(ru_msgsnd);
@ -150,6 +156,7 @@ PHP_FUNCTION(getrusage)
PHP_RUSAGE_PARA(ru_utime.tv_sec);
PHP_RUSAGE_PARA(ru_stime.tv_usec);
PHP_RUSAGE_PARA(ru_stime.tv_sec);
#undef PHP_RUSAGE_PARA
}
#endif /* HAVE_GETRUSAGE */

View file

@ -1,10 +1,7 @@
--TEST--
Test getrusage() function: basic test
--SKIPIF--
<?php
if( substr(PHP_OS, 0, 3) == "WIN" )
die("skip.. Do not run on Windows");
?>
<?php if (!function_exists("getrusage")) print "skip"; ?>
--FILE--
<?php
/* Prototype : array getrusage ([ int $who ] )

View file

@ -1,10 +1,7 @@
--TEST--
Test getrusage() function : error conditions - incorrect number of args
--SKIPIF--
<?php
if( substr(PHP_OS, 0, 3) == "WIN" )
die("skip.. Do not run on Windows");
?>
<?php if (!function_exists("getrusage")) print "skip"; ?>
--FILE--
<?php
/* Prototype : array getrusage ([ int $who ] )

View file

@ -3,8 +3,7 @@ Test getrusage() function : usage variation - diff data types as $who arg
--SKIPIF--
<?php
if (PHP_INT_SIZE != 8) die("skip this test is for 64-bit only");
if( substr(PHP_OS, 0, 3) == "WIN" )
die("skip.. Do not run on Windows");
if (!function_exists("getrusage")) die("skip");
?>
--FILE--
<?php

View file

@ -160,7 +160,7 @@ ADD_SOURCES("main/streams", "streams.c cast.c memory.c filter.c plain_wrapper.c
ADD_FLAG("CFLAGS_BD_MAIN_STREAMS", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
ADD_SOURCES("win32", "glob.c readdir.c \
registry.c select.c sendmail.c time.c winutil.c wsyslog.c globals.c");
registry.c select.c sendmail.c time.c winutil.c wsyslog.c globals.c getrusage.c");
ADD_FLAG("CFLAGS_BD_WIN32", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");

View file

@ -181,3 +181,5 @@
#define HAVE_HUGE_VAL_INF 1
#define HAVE_GETRUSAGE

View file

@ -2654,7 +2654,7 @@ function toolset_setup_common_ldlags()
function toolset_setup_common_libs()
{
// urlmon.lib ole32.lib oleaut32.lib uuid.lib gdi32.lib winspool.lib comdlg32.lib
DEFINE("LIBS", "kernel32.lib ole32.lib user32.lib advapi32.lib shell32.lib ws2_32.lib Dnsapi.lib");
DEFINE("LIBS", "kernel32.lib ole32.lib user32.lib advapi32.lib shell32.lib ws2_32.lib Dnsapi.lib psapi.lib");
}
function toolset_setup_build_mode()

75
win32/getrusage.c Normal file
View file

@ -0,0 +1,75 @@
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Kalle Sommer Nielsen <kalle@php.net> |
+----------------------------------------------------------------------+
*/
#include <php.h>
#include <psapi.h>
#include "getrusage.h"
/*
* Parts of this file is based on code from the OpenVSwitch project, that
* is released under the Apache 2.0 license and is copyright 2014 Nicira, Inc.
* and have been modified to work with PHP.
*/
static void usage_to_timeval(FILETIME *ft, struct timeval *tv)
{
ULARGE_INTEGER time;
time.LowPart = ft->dwLowDateTime;
time.HighPart = ft->dwHighDateTime;
tv->tv_sec = (zend_long) (time.QuadPart / 10000000);
tv->tv_usec = (zend_long) ((time.QuadPart % 10000000) / 10);
}
PHPAPI int getrusage(int who, struct rusage *usage)
{
FILETIME ctime, etime, stime, utime;
memset(usage, 0, sizeof(struct rusage));
if (who == RUSAGE_SELF) {
PROCESS_MEMORY_COUNTERS pmc;
HANDLE proc = GetCurrentProcess();
if (!GetProcessTimes(proc, &ctime, &etime, &stime, &utime)) {
return -1;
} else if(!GetProcessMemoryInfo(proc, &pmc, sizeof(pmc))) {
return -1;
}
usage_to_timeval(&stime, &usage->ru_stime);
usage_to_timeval(&utime, &usage->ru_utime);
usage->ru_majflt = pmc.PageFaultCount;
usage->ru_maxrss = pmc.PeakWorkingSetSize / 1024;
return 0;
} else if (who == RUSAGE_THREAD) {
if (!GetThreadTimes(GetCurrentThread(), &ctime, &etime, &stime, &utime)) {
return -1;
}
usage_to_timeval(&stime, &usage->ru_stime);
usage_to_timeval(&utime, &usage->ru_utime);
return 0;
} else {
return -1;
}
}

114
win32/getrusage.h Normal file
View file

@ -0,0 +1,114 @@
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Kalle Sommer Nielsen <kalle@php.net> |
+----------------------------------------------------------------------+
*/
#ifndef HAVE_GETRUSAGE_H
# define HAVE_GETRUSAGE_H
/* Note,
*
* RUSAGE_CHILDREN is not implemented, and the RUSAGE_THREAD will
* instead be used instead.
*/
# define RUSAGE_SELF 0
# define RUSAGE_CHILDREN 1
# define RUSAGE_THREAD RUSAGE_CHILDREN
/*
* Implementation support
*
* RUSAGE_SELF
* ru_utime
* ru_stime
* ru_majflt
* ru_maxrss
*
* RUSAGE_THREAD
* ru_utime
* ru_stime
*
* Not implemented:
* ru_ixrss (unused)
* ru_idrss (unused)
* ru_isrss (unused)
* ru_minflt
* ru_nswap (unused)
* ru_inblock
* ru_oublock
* ru_msgsnd (unused)
* ru_msgrcv (unused)
* ru_nsignals (unused)
* ru_nvcsw
* ru_nivcsw
*/
struct rusage
{
/* User time used */
struct timeval ru_utime;
/* System time used */
struct timeval ru_stime;
/* Integral max resident set size */
long ru_maxrss;
/* Integral shared text memory size */
long ru_ixrss;
/* Integral unshared data size */
long ru_idrss;
/* Integral unshared stack size */
long ru_isrss;
/* Page reclaims */
long ru_minflt;
/* Page faults */
long ru_majflt;
/* Swaps */
long ru_nswap;
/* Block input operations */
long ru_inblock;
/* Block output operations */
long ru_oublock;
/* Messages sent */
long ru_msgsnd;
/* Messages received */
long ru_msgrcv;
/* Signals received */
long ru_nsignals;
/* Voluntary context switches */
long ru_nvcsw;
/* Involuntary context switches */
long ru_nivcsw;
};
PHPAPI int getrusage(int who, struct rusage *usage);
#endif