Skip to content

Commit 376adc4

Browse files
committedNov 22, 2011
emulate getrusage(2) on windows (borought from PostgreSQL)
1 parent ae0771e commit 376adc4

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed
 

‎tests/bench/qgsbench.cpp

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
* (at your option) any later version. *
1515
* *
1616
***************************************************************************/
17+
#include <QtGlobal>
18+
1719
#include <cmath>
1820
#include <ctime>
1921
#include <iostream>
2022
#include <stdio.h>
23+
#ifndef Q_OS_WIN
2124
#include <sys/resource.h>
25+
#endif
2226
#include <time.h>
2327
#include <math.h>
2428

@@ -35,6 +39,79 @@
3539
#include "qgsmaplayerregistry.h"
3640
#include "qgsproject.h"
3741

42+
#ifdef Q_OS_WIN
43+
// slightly adapted from http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/port/getrusage.c?rev=1.18;content-type=text%2Fplain
44+
45+
#include <winsock2.h>
46+
#include <errno.h>
47+
48+
#define RUSAGE_SELF 0
49+
50+
struct rusage
51+
{
52+
struct timeval ru_utime; /* user time used */
53+
struct timeval ru_stime; /* system time used */
54+
};
55+
56+
/*-------------------------------------------------------------------------
57+
*
58+
* getrusage.c
59+
* get information about resource utilisation
60+
*
61+
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
62+
* Portions Copyright (c) 1994, Regents of the University of California
63+
*
64+
*
65+
* IDENTIFICATION
66+
* $PostgreSQL: pgsql/src/port/getrusage.c,v 1.18 2010-01-02 16:58:13 momjian Exp $
67+
*
68+
*-------------------------------------------------------------------------
69+
*/
70+
71+
72+
int getrusage( int who, struct rusage * rusage )
73+
{
74+
FILETIME starttime;
75+
FILETIME exittime;
76+
FILETIME kerneltime;
77+
FILETIME usertime;
78+
ULARGE_INTEGER li;
79+
80+
if ( who != RUSAGE_SELF )
81+
{
82+
/* Only RUSAGE_SELF is supported in this implementation for now */
83+
errno = EINVAL;
84+
return -1;
85+
}
86+
87+
if ( rusage == ( struct rusage * ) NULL )
88+
{
89+
errno = EFAULT;
90+
return -1;
91+
}
92+
memset( rusage, 0, sizeof( struct rusage ) );
93+
if ( GetProcessTimes( GetCurrentProcess(),
94+
&starttime, &exittime, &kerneltime, &usertime ) == 0 )
95+
{
96+
// _dosmaperr(GetLastError());
97+
return -1;
98+
}
99+
100+
/* Convert FILETIMEs (0.1 us) to struct timeval */
101+
memcpy( &li, &kerneltime, sizeof( FILETIME ) );
102+
li.QuadPart /= 10L; /* Convert to microseconds */
103+
rusage->ru_stime.tv_sec = li.QuadPart / 1000000L;
104+
rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
105+
106+
memcpy( &li, &usertime, sizeof( FILETIME ) );
107+
li.QuadPart /= 10L; /* Convert to microseconds */
108+
rusage->ru_utime.tv_sec = li.QuadPart / 1000000L;
109+
rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
110+
111+
return 0;
112+
}
113+
#endif
114+
38115
QgsBench::QgsBench( int theWidth, int theHeight, int theIterations )
39116
: QObject(), mWidth( theWidth ), mHeight( theHeight ), mIterations( theIterations ), mSetExtent( false )
40117
{
@@ -260,4 +337,3 @@ void QgsBench::elapsed()
260337
t[2] = t[0] + t[1];
261338
mTimes.append( t );
262339
}
263-

0 commit comments

Comments
 (0)
Please sign in to comment.