perf: Don't require a separate counter from the timer for perf loops.

This commit is contained in:
Carl Worth 2006-08-31 11:02:20 -07:00
parent 13bcba68ae
commit b9f629d542
6 changed files with 49 additions and 57 deletions

View file

@ -29,7 +29,7 @@
int cairo_perf_duration = -1;
int alarm_expired = 0;
int cairo_perf_alarm_expired = 0;
typedef struct _cairo_perf {
const char *name;
@ -81,27 +81,21 @@ target_is_measurable (cairo_test_target_t *target)
}
void
start_timing (bench_timer_t *tr, long *count) {
start_timing (bench_timer_t *tr) {
if (cairo_perf_duration == -1) {
if (getenv("CAIRO_PERF_DURATION"))
cairo_perf_duration = strtol(getenv("CAIRO_PERF_DURATION"), NULL, 0);
else
cairo_perf_duration = 5;
}
*count = 0;
tr->count = 0;
timer_start (tr);
set_alarm (cairo_perf_duration);
}
void
stop_timing (bench_timer_t *tr, long count) {
stop_timing (bench_timer_t *tr) {
timer_stop (tr);
tr->count = count;
}
double
timing_result (bench_timer_t *tr) {
return tr->count / timer_elapsed (tr);
}
int

View file

@ -30,42 +30,52 @@
#include "cairo-boilerplate.h"
typedef struct {
#ifdef USE_WINAPI
LARGE_INTEGER start;
LARGE_INTEGER stop;
#else
struct timeval start;
struct timeval stop;
#endif
long count;
} bench_timer_t;
#include "timer-alarm.h"
void
start_timing (bench_timer_t *tr);
void
stop_timing (bench_timer_t *tr);
extern int cairo_perf_duration;
extern int alarm_expired;
void
start_timing (bench_timer_t *tr, long *count);
void
stop_timing (bench_timer_t *tr, long count);
double
timing_result (bench_timer_t *tr);
extern int cairo_perf_alarm_expired;
#if CAIRO_HAS_WIN32_SURFACE
// Windows needs a SleepEx to put the thread into an alertable state,
// such that the timer expiration callback can fire. I can't figure
// out how to do an async timer. On a quiet system, this doesn't
// seem to significantly affect the results.
# define PERF_LOOP_INIT(timervar,countvar) do { \
countvar = 0; \
start_timing(&(timervar), &(countvar)); \
while (!alarm_expired) { \
SleepEx(0, TRUE);
/* Windows needs a SleepEx to put the thread into an alertable state,
* such that the timer expiration callback can fire. I can't figure
* out how to do an async timer. On a quiet system, this doesn't
* seem to significantly affect the results.
*/
# define PERF_LOOP_INIT(timervar) do { \
start_timing(&(timervar)); \
while (! cairo_perf_alarm_expired) { \
SleepEx(0, TRUE)
#else
# define PERF_LOOP_INIT(timervar,countvar) do { \
countvar = 0; \
start_timing(&(timervar), &(countvar)); \
while (!alarm_expired) {
# define PERF_LOOP_INIT(timervar) do { \
start_timing(&(timervar)); \
while (! cairo_perf_alarm_expired) {
#endif
#define PERF_LOOP_FINI(timervar,countvar) \
(countvar)++; \
#define PERF_LOOP_FINI(timervar) \
(timervar).count++; \
} \
stop_timing (&(timervar), (countvar)); \
} while (0);
stop_timing (&(timervar)); \
} while (0)
#define PERF_LOOP_RATE(timervar) \
((timervar).count) / timer_elapsed (&(timervar))
typedef void (*cairo_perf_func_t) (cairo_t *cr, int width, int height);

View file

@ -41,13 +41,12 @@ void
paint (cairo_t *cr, int width, int height)
{
bench_timer_t timer;
long count;
PERF_LOOP_INIT (timer, count);
PERF_LOOP_INIT (timer);
{
cairo_paint (cr);
}
PERF_LOOP_FINI (timer, count);
PERF_LOOP_FINI (timer);
printf ("Rate: %g\n", timing_result (&timer));
printf ("Rate: %g\n", PERF_LOOP_RATE (timer));
}

View file

@ -58,13 +58,13 @@ timer_elapsed (bench_timer_t *tr) {
void
alarm_handler (int signal) {
if (signal == SIGALRM) {
alarm_expired = 1;
cairo_perf_alarm_expired = 1;
}
}
void
set_alarm (int seconds) {
alarm_expired = 0;
cairo_perf_alarm_expired = 0;
signal (SIGALRM, alarm_handler);
alarm (seconds);
}

View file

@ -59,7 +59,7 @@ timer_elapsed (bench_timer_t *tr) {
void CALLBACK
alarm_handler (void *closure, DWORD dwTimerLowValue, DWORD dwTimerHighValue) {
alarm_expired = 1;
cairo_perf_alarm_expired = 1;
}
HANDLE hTimer = NULL;
@ -67,10 +67,10 @@ void
set_alarm (int seconds) {
if (hTimer == NULL)
hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
alarm_expired = 0;
cairo_perf_alarm_expired = 0;
LARGE_INTEGER expTime;
expTime.QuadPart = - (seconds * 10000000);
if (!SetWaitableTimer (hTimer, &expTime, 0, alarm_handler, &alarm_expired, FALSE))
if (!SetWaitableTimer (hTimer, &expTime, 0, alarm_handler, &cairo_perf_alarm_expired, FALSE))
fprintf (stderr, "SetWaitableTimer failed!\n");
}

View file

@ -32,17 +32,6 @@
/* timers */
typedef struct {
#ifdef USE_WINAPI
LARGE_INTEGER start;
LARGE_INTEGER stop;
#else
struct timeval start;
struct timeval stop;
#endif
long count;
} bench_timer_t;
extern int alarm_expired;
void