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
+ }
0 commit comments