[cairo-commit] 2 commits - boilerplate/cairo-boilerplate.h perf/cairo-perf.h perf/Makefile.am perf/paint.c perf/timer-alarm.h perf/timer-alarm-posix.c perf/timer-alarm-win32.c perf/timing.c perf/timing.h perf/util.c

Carl Worth cworth at kemper.freedesktop.org
Thu Aug 31 10:30:40 PDT 2006


 boilerplate/cairo-boilerplate.h |    1 
 perf/Makefile.am                |    9 ++
 perf/cairo-perf.h               |    6 -
 perf/paint.c                    |    9 +-
 perf/timer-alarm-posix.c        |   70 +++++++++++++++
 perf/timer-alarm-win32.c        |   79 +++++++++++++++++
 perf/timer-alarm.h              |   65 ++++++++++++++
 perf/timing.c                   |   56 ++++++++++++
 perf/timing.h                   |   68 +++++++++++++++
 perf/util.c                     |  177 ++++++++++++++++++++++++++++++++++++++++
 10 files changed, 534 insertions(+), 6 deletions(-)

New commits:
diff-tree 739c4767673ace4f566d395d770924617ddebaff (from 3d279da6214d43de05eba4af381e451cec3cfd72)
Author: Carl Worth <cworth at cworth.org>
Date:   Thu Aug 31 10:27:20 2006 -0700

    Initial hookup of Vlad's timer/alarm code

diff --git a/boilerplate/cairo-boilerplate.h b/boilerplate/cairo-boilerplate.h
index 7f8f0ed..4810353 100644
--- a/boilerplate/cairo-boilerplate.h
+++ b/boilerplate/cairo-boilerplate.h
@@ -31,6 +31,7 @@
 #endif
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <math.h>
 #include <cairo.h>
 #include <string.h>
diff --git a/perf/Makefile.am b/perf/Makefile.am
index 08223ca..f9124c5 100644
--- a/perf/Makefile.am
+++ b/perf/Makefile.am
@@ -16,8 +16,17 @@ noinst_PROGRAMS = cairo-perf
 cairo_perf_SOURCES =	\
 	cairo-perf.c	\
 	cairo-perf.h	\
+	timing.c	\
+	timing.h	\
+	timer-alarm.h	\
 	paint.c
 
+if CAIRO_HAS_WIN32_SURFACE
+cairo_perf_SOURCES += timer-alarm-win32.c
+else
+cairo_perf_SOURCES += timer-alarm-posix.c
+endif
+
 LDADD = $(top_builddir)/boilerplate/libcairoboilerplate.la \
 	$(top_builddir)/src/libcairo.la
 
diff --git a/perf/cairo-bench.h b/perf/cairo-bench.h
deleted file mode 100644
index 076b7c1..0000000
--- a/perf/cairo-bench.h
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * Copyright © 2006 Mozilla Corporation
- *
- * Permission to use, copy, modify, distribute, and sell this software
- * and its documentation for any purpose is hereby granted without
- * fee, provided that the above copyright notice appear in all copies
- * and that both that copyright notice and this permission notice
- * appear in supporting documentation, and that the name of
- * Mozilla Corporation not be used in advertising or publicity pertaining to
- * distribution of the software without specific, written prior
- * permission. Mozilla Corporation makes no representations about the
- * suitability of this software for any purpose.  It is provided "as
- * is" without express or implied warranty.
- *
- * MOZILLA CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
- * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS, IN NO EVENT SHALL MOZILLA CORPORATION BE LIABLE FOR ANY SPECIAL,
- * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
- * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
- * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
- * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- * Author: Vladimir Vukicevic <vladimir at pobox.com>
- */
-
-#ifndef CAIRO_BENCH_H_
-#define CAIRO_BENCH_H_
-
-#ifndef USE_WINAPI
-#include <sys/time.h>
-#endif
-
-#include <cairo.h>
-
-#include "surface-boilerplate.h"
-
-extern int num_benchmarks;
-
-char *content_name (cairo_content_t content);
-cairo_content_t content_for_name (const char *content);
-
-/* results */
-
-typedef struct _bench_result_t bench_result_t;
-
-struct _bench_result_t {
-    cairo_test_target_t *target;
-    double *results;
-
-    bench_result_t *next;
-};
-
-/* 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 timer_start (bench_timer_t *tr);
-void timer_stop (bench_timer_t *tr);
-double timer_elapsed (bench_timer_t *tr);
-
-void set_alarm (int seconds);
-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);
-
-#ifdef USE_WINAPI
-// 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 BEGIN_TIMING_LOOP(timervar,countvar)  do {   \
-    countvar = 0;                                    \
-    start_timing(&(timervar), &(countvar));          \
-    while (!alarm_expired) {                         \
-        SleepEx(0, TRUE);
-
-#else
-
-#define BEGIN_TIMING_LOOP(timervar,countvar)  do {   \
-    countvar = 0;                                    \
-    start_timing(&(timervar), &(countvar));          \
-    while (!alarm_expired) {
-
-#endif
-
-#define END_TIMING_LOOP(timervar,countvar)      \
-    (countvar)++;                               \
-    }                                           \
-    stop_timing (&(timervar), (countvar));      \
-    } while (0);
-
-/* arg parsing */
-int parse_args (int argc, char **argv, int **tests, cairo_test_target_t ***targets);
-
-#ifndef CAIRO_HAS_PNG_FUNCTIONS
-cairo_status_t cairo_surface_write_to_png (cairo_surface_t *surface, const char *filename);
-#endif
-
-#endif /* CAIRO_BENCH_H_ */
diff --git a/perf/cairo-perf.h b/perf/cairo-perf.h
index b5d9b7e..3fc235f 100644
--- a/perf/cairo-perf.h
+++ b/perf/cairo-perf.h
@@ -28,6 +28,8 @@
 
 #include "cairo-boilerplate.h"
 
+#include "timing.h"
+
 extern unsigned int iterations;
 
 typedef void (*cairo_perf_func_t) (cairo_t *cr, int width, int height);
@@ -38,8 +40,4 @@ DECL_PERF_FUNC (paint_setup);
 DECL_PERF_FUNC (paint_alpha_setup);
 DECL_PERF_FUNC (paint);
 
-/* XXX: Obviously bogus as we bring up the infrastructure. */
-#define PERF_LOOP_INIT do {
-#define PERF_LOOP_FINI } while (0);
-
 #endif
diff --git a/perf/paint.c b/perf/paint.c
index 5c00e5c..c4233df 100644
--- a/perf/paint.c
+++ b/perf/paint.c
@@ -40,10 +40,15 @@ paint_alpha_setup (cairo_t *cr, int widt
 void
 paint (cairo_t *cr, int width, int height)
 {
-    PERF_LOOP_INIT;
+    bench_timer_t timer;
+    long count;
+
+    PERF_LOOP_INIT (timer, count);
     {
 	cairo_paint (cr);
 	iterations++;
     }
-    PERF_LOOP_FINI;
+    PERF_LOOP_FINI (timer, count);
+
+    printf ("Rate: %g\n", timing_result (&timer));
 }
diff --git a/perf/timer-alarm-posix.c b/perf/timer-alarm-posix.c
new file mode 100644
index 0000000..161d1be
--- /dev/null
+++ b/perf/timer-alarm-posix.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright © 2006 Mozilla Corporation
+ * Copyright © 2006 Red Hat, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * the authors not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. The authors make no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Authors: Vladimir Vukicevic <vladimir at pobox.com>
+ *          Carl Worth <cworth at cworth.org>
+ */
+
+#include <signal.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include "timing.h"
+
+/* timers */
+
+void
+timer_start (bench_timer_t *tr) {
+    gettimeofday (&tr->start, NULL);
+}
+
+void
+timer_stop (bench_timer_t *tr) {
+    gettimeofday (&tr->stop, NULL);
+}
+
+double
+timer_elapsed (bench_timer_t *tr) {
+    double d;
+
+    d = tr->stop.tv_sec - tr->start.tv_sec;
+    d += (tr->stop.tv_usec - tr->start.tv_usec) / 1000000.0;
+
+    return d;
+}
+
+/* alarms */
+
+void
+alarm_handler (int signal) {
+    if (signal == SIGALRM) {
+        alarm_expired = 1;
+    }
+}
+
+void
+set_alarm (int seconds) {
+    alarm_expired = 0;
+    signal (SIGALRM, alarm_handler);
+    alarm (seconds);
+}
diff --git a/perf/timer-alarm-win32.c b/perf/timer-alarm-win32.c
new file mode 100644
index 0000000..70fc8a9
--- /dev/null
+++ b/perf/timer-alarm-win32.c
@@ -0,0 +1,79 @@
+/*
+ * Copyright © 2006 Mozilla Corporation
+ * Copyright © 2006 Red Hat, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * the authors not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. The authors make no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Authors: Vladimir Vukicevic <vladimir at pobox.com>
+ *          Carl Worth <cworth at cworth.org>
+ */
+
+#define USE_WINAPI
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+#include "cairo-perf.h"
+
+/* timers */
+
+void
+timer_start (bench_timer_t *tr) {
+    QueryPerformanceCounter(&tr->start);
+}
+
+void
+timer_stop (bench_timer_t *tr) {
+    QueryPerformanceCounter(&tr->stop);
+}
+
+double
+timer_elapsed (bench_timer_t *tr) {
+    double d;
+    LARGE_INTEGER freq;
+
+    QueryPerformanceFrequency(&freq);
+
+    d = (tr->stop.QuadPart - tr->start.QuadPart) / (double) freq.QuadPart;
+    return d;
+}
+
+/* alarms */
+int test_seconds = -1;
+
+int alarm_expired = 0;
+
+void CALLBACK
+alarm_handler (void *closure, DWORD dwTimerLowValue, DWORD dwTimerHighValue) {
+    alarm_expired = 1;
+}
+
+HANDLE hTimer = NULL;
+void
+set_alarm (int seconds) {
+    if (hTimer == NULL)
+        hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
+    alarm_expired = 0;
+
+    LARGE_INTEGER expTime;
+    expTime.QuadPart = - (seconds * 10000000);
+    if (!SetWaitableTimer (hTimer, &expTime, 0, alarm_handler, &alarm_expired, FALSE))
+        fprintf (stderr, "SetWaitableTimer failed!\n");
+}
diff --git a/perf/timer-alarm.h b/perf/timer-alarm.h
new file mode 100644
index 0000000..942bc28
--- /dev/null
+++ b/perf/timer-alarm.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright © 2006 Mozilla Corporation
+ * Copyright © 2006 Red Hat, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * the authors not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. The authors make no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Authors: Vladimir Vukicevic <vladimir at pobox.com>
+ *          Carl Worth <cworth at cworth.org>
+ */
+
+#ifndef _TIMER_ALARM_H_
+#define _TIMER_ALARM_H_
+
+#include "cairo-perf.h"
+
+/* 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
+timer_start (bench_timer_t *tr);
+
+void
+timer_stop (bench_timer_t *tr);
+
+double
+timer_elapsed (bench_timer_t *tr);
+
+/* alarms */
+
+void
+alarm_handler (int signal);
+
+void
+set_alarm (int seconds);
+
+#endif
diff --git a/perf/timing.c b/perf/timing.c
new file mode 100644
index 0000000..20318d8
--- /dev/null
+++ b/perf/timing.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright © 2006 Mozilla Corporation
+ * Copyright © 2006 Red Hat, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * the authors not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. The authors make no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Authors: Vladimir Vukicevic <vladimir at pobox.com>
+ *          Carl Worth <cworth at cworth.org>
+ */
+
+#include "timing.h"
+
+int cairo_perf_duration = -1;
+
+int alarm_expired = 0;
+
+void
+start_timing (bench_timer_t *tr, long *count) {
+    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;
+    timer_start (tr);
+    set_alarm (cairo_perf_duration);
+}
+
+void
+stop_timing (bench_timer_t *tr, long count) {
+    timer_stop (tr);
+    tr->count = count;
+}
+
+double
+timing_result (bench_timer_t *tr) {
+    return tr->count / timer_elapsed (tr);
+}
diff --git a/perf/timing.h b/perf/timing.h
new file mode 100644
index 0000000..9b73438
--- /dev/null
+++ b/perf/timing.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright © 2006 Mozilla Corporation
+ * Copyright © 2006 Red Hat, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * the authors not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. The authors make no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Authors: Vladimir Vukicevic <vladimir at pobox.com>
+ *          Carl Worth <cworth at cworth.org>
+ */
+
+#ifndef _TIMING_H_
+#define _TIMING_H_
+
+#include "timer-alarm.h"
+
+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);
+
+#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);
+#else
+# define PERF_LOOP_INIT(timervar,countvar)  do {     \
+    countvar = 0;                                    \
+    start_timing(&(timervar), &(countvar));          \
+    while (!alarm_expired) {
+#endif
+
+#define PERF_LOOP_FINI(timervar,countvar)       \
+    (countvar)++;                               \
+    }                                           \
+    stop_timing (&(timervar), (countvar));      \
+    } while (0);
+
+#endif
diff --git a/perf/util.c b/perf/util.c
index a786df7..af1a2ef 100644
--- a/perf/util.c
+++ b/perf/util.c
@@ -1,26 +1,28 @@
 /*
  * Copyright © 2006 Mozilla Corporation
+ * Copyright © 2006 Red Hat, Inc.
  *
  * Permission to use, copy, modify, distribute, and sell this software
  * and its documentation for any purpose is hereby granted without
  * fee, provided that the above copyright notice appear in all copies
  * and that both that copyright notice and this permission notice
  * appear in supporting documentation, and that the name of
- * Mozilla Corporation not be used in advertising or publicity pertaining to
+ * the authors not be used in advertising or publicity pertaining to
  * distribution of the software without specific, written prior
- * permission. Mozilla Corporation makes no representations about the
+ * permission. The authors make no representations about the
  * suitability of this software for any purpose.  It is provided "as
  * is" without express or implied warranty.
  *
- * MOZILLA CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS, IN NO EVENT SHALL MOZILLA CORPORATION BE LIABLE FOR ANY SPECIAL,
+ * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * Author: Vladimir Vukicevic <vladimir at pobox.com>
+ * Authors: Vladimir Vukicevic <vladimir at pobox.com>
+ *          Carl Worth <cworth at cworth.org>
  */
 
 #define _GNU_SOURCE
diff-tree 3d279da6214d43de05eba4af381e451cec3cfd72 (from 851dd63719d51f9b839784ba2761a1e2b2ecfbe5)
Author: Vladimir Vukicevic <vladimir at pobox.com>
Date:   Thu Aug 31 09:01:23 2006 -0700

    Add useful pieces from Vladimir's cairo-bench

diff --git a/perf/cairo-bench.h b/perf/cairo-bench.h
new file mode 100644
index 0000000..076b7c1
--- /dev/null
+++ b/perf/cairo-bench.h
@@ -0,0 +1,110 @@
+/*
+ * Copyright © 2006 Mozilla Corporation
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * Mozilla Corporation not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Mozilla Corporation makes no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * MOZILLA CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL MOZILLA CORPORATION BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Author: Vladimir Vukicevic <vladimir at pobox.com>
+ */
+
+#ifndef CAIRO_BENCH_H_
+#define CAIRO_BENCH_H_
+
+#ifndef USE_WINAPI
+#include <sys/time.h>
+#endif
+
+#include <cairo.h>
+
+#include "surface-boilerplate.h"
+
+extern int num_benchmarks;
+
+char *content_name (cairo_content_t content);
+cairo_content_t content_for_name (const char *content);
+
+/* results */
+
+typedef struct _bench_result_t bench_result_t;
+
+struct _bench_result_t {
+    cairo_test_target_t *target;
+    double *results;
+
+    bench_result_t *next;
+};
+
+/* 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 timer_start (bench_timer_t *tr);
+void timer_stop (bench_timer_t *tr);
+double timer_elapsed (bench_timer_t *tr);
+
+void set_alarm (int seconds);
+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);
+
+#ifdef USE_WINAPI
+// 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 BEGIN_TIMING_LOOP(timervar,countvar)  do {   \
+    countvar = 0;                                    \
+    start_timing(&(timervar), &(countvar));          \
+    while (!alarm_expired) {                         \
+        SleepEx(0, TRUE);
+
+#else
+
+#define BEGIN_TIMING_LOOP(timervar,countvar)  do {   \
+    countvar = 0;                                    \
+    start_timing(&(timervar), &(countvar));          \
+    while (!alarm_expired) {
+
+#endif
+
+#define END_TIMING_LOOP(timervar,countvar)      \
+    (countvar)++;                               \
+    }                                           \
+    stop_timing (&(timervar), (countvar));      \
+    } while (0);
+
+/* arg parsing */
+int parse_args (int argc, char **argv, int **tests, cairo_test_target_t ***targets);
+
+#ifndef CAIRO_HAS_PNG_FUNCTIONS
+cairo_status_t cairo_surface_write_to_png (cairo_surface_t *surface, const char *filename);
+#endif
+
+#endif /* CAIRO_BENCH_H_ */
diff --git a/perf/util.c b/perf/util.c
new file mode 100644
index 0000000..a786df7
--- /dev/null
+++ b/perf/util.c
@@ -0,0 +1,175 @@
+/*
+ * Copyright © 2006 Mozilla Corporation
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * Mozilla Corporation not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Mozilla Corporation makes no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * MOZILLA CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL MOZILLA CORPORATION BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Author: Vladimir Vukicevic <vladimir at pobox.com>
+ */
+
+#define _GNU_SOURCE
+
+#ifdef USE_WINAPI
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#endif
+
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+#ifndef USE_WINAPI
+#include <sys/time.h>
+#include <unistd.h>
+#endif
+
+#include "cairo-bench.h"
+
+/* helpers */
+
+char *
+content_name (cairo_content_t content)
+{
+    if (content == CAIRO_CONTENT_COLOR) return "rgb";
+    if (content == CAIRO_CONTENT_COLOR_ALPHA) return "argb";
+    if (content == CAIRO_CONTENT_ALPHA) return "a8";
+    assert (0);
+    return NULL;
+}
+
+cairo_content_t
+content_for_name (const char *content)
+{
+    if (strcmp(content, "rgb") == 0) return CAIRO_CONTENT_COLOR;
+    if (strcmp(content, "argb") == 0) return CAIRO_CONTENT_COLOR_ALPHA;
+    if (strcmp(content, "a8") == 0) return CAIRO_CONTENT_ALPHA;
+    return (cairo_content_t) -1;
+}
+
+/* timers */
+
+#ifdef USE_WINAPI
+void
+timer_start (bench_timer_t *tr) {
+    QueryPerformanceCounter(&tr->start);
+}
+
+void
+timer_stop (bench_timer_t *tr) {
+    QueryPerformanceCounter(&tr->stop);
+}
+
+double
+timer_elapsed (bench_timer_t *tr) {
+    double d;
+    LARGE_INTEGER freq;
+
+    QueryPerformanceFrequency(&freq);
+
+    d = (tr->stop.QuadPart - tr->start.QuadPart) / (double) freq.QuadPart;
+    return d;
+}
+#else
+void
+timer_start (bench_timer_t *tr) {
+    gettimeofday (&tr->start, NULL);
+}
+
+void
+timer_stop (bench_timer_t *tr) {
+    gettimeofday (&tr->stop, NULL);
+}
+
+double
+timer_elapsed (bench_timer_t *tr) {
+    double d;
+
+    d = tr->stop.tv_sec - tr->start.tv_sec;
+    d += (tr->stop.tv_usec - tr->start.tv_usec) / 1000000.0;
+
+    return d;
+}
+#endif
+
+/* alarms */
+int test_seconds = -1;
+
+int alarm_expired = 0;
+
+#ifdef USE_WINAPI
+void CALLBACK
+alarm_handler (void *closure, DWORD dwTimerLowValue, DWORD dwTimerHighValue) {
+    alarm_expired = 1;
+}
+
+HANDLE hTimer = NULL;
+void
+set_alarm (int seconds) {
+    if (hTimer == NULL)
+        hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
+    alarm_expired = 0;
+
+    LARGE_INTEGER expTime;
+    expTime.QuadPart = - (seconds * 10000000);
+    if (!SetWaitableTimer (hTimer, &expTime, 0, alarm_handler, &alarm_expired, FALSE))
+        fprintf (stderr, "SetWaitableTimer failed!\n");
+}
+#else
+void
+alarm_handler (int signal) {
+    if (signal == SIGALRM) {
+        alarm_expired = 1;
+    }
+}
+
+void
+set_alarm (int seconds) {
+    alarm_expired = 0;
+    signal (SIGALRM, alarm_handler);
+    alarm (seconds);
+}
+#endif
+
+/* timers + alarms! */
+
+void
+start_timing (bench_timer_t *tr, long *count) {
+    if (test_seconds == -1) {
+        if (getenv("TEST_SECONDS"))
+            test_seconds = strtol(getenv("TEST_SECONDS"), NULL, 0);
+        else
+            test_seconds = 5;
+    }
+    *count = 0;
+    timer_start (tr);
+    set_alarm (test_seconds);
+}
+
+void
+stop_timing (bench_timer_t *tr, long count) {
+    timer_stop (tr);
+    tr->count = count;
+}
+
+double
+timing_result (bench_timer_t *tr) {
+    return tr->count / timer_elapsed (tr);
+}


More information about the cairo-commit mailing list