]> granicus.if.org Git - php/commitdiff
Windows support for getrusage()
authorKalle Sommer Nielsen <kalle@php.net>
Wed, 20 May 2015 13:45:37 +0000 (15:45 +0200)
committerKalle Sommer Nielsen <kalle@php.net>
Wed, 20 May 2015 13:45:37 +0000 (15:45 +0200)
* 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

NEWS
ext/standard/microtime.c
ext/standard/tests/general_functions/getrusage_basic.phpt
ext/standard/tests/general_functions/getrusage_error.phpt
ext/standard/tests/general_functions/getrusage_variation1.phpt
win32/build/config.w32
win32/build/config.w32.h.in
win32/build/confutils.js
win32/getrusage.c [new file with mode: 0644]
win32/getrusage.h [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 7ca0e337480d4cb368a31be653ecaf5f28e1b143..0018b8e5aff29fba88199c27b759fc4d74bd217a 100644 (file)
--- a/NEWS
+++ b/NEWS
   . 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
     (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).
index 6d5b8cc695f7ab029e43932e0999641b5ac91bca..4b391bbb7d858a24c6921cbc7cd3d70a679f5c5c 100644 (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 */
index 0e5b42c420c7190ed52dfe027dd7fa87baa8687f..b2379a76630166bd24da0d57713828f727c72fd9 100644 (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  ] )
index f00627a76678197150f27538585258603c45ce3d..7ac99655b8e2c472334ff4d45332f289ad44ac37 100644 (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  ] )
index ae2b15054857380fe3ade8f0b7c4c5ec36f54946..568c9c73835f2db16002ed9a4edee6e309a1d868 100644 (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
index 3c2ff7a0a9bfcf99d6e918b5e5b07bfd52b8270c..70aef37799071a44aa0bd4b24bdfd0b3694ec5e6 100644 (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");
 
index 180f470021146986a47176c99ad88ef3e7e5a0ed..3276fea705069b3e86181660bc526f6e08d99a7e 100644 (file)
 
 #define HAVE_HUGE_VAL_INF 1
 
+#define HAVE_GETRUSAGE
+
index 2005571f360df6469701563943ffd6693a071501..9a7b3fa5ac990899189d537f8295a98be3b7f89c 100644 (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()
diff --git a/win32/getrusage.c b/win32/getrusage.c
new file mode 100644 (file)
index 0000000..53861cb
--- /dev/null
@@ -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;
+       }
+}
\ No newline at end of file
diff --git a/win32/getrusage.h b/win32/getrusage.h
new file mode 100644 (file)
index 0000000..dd58d6e
--- /dev/null
@@ -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
\ No newline at end of file