summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2010-09-26 03:05:06 -0400
committerTavian Barnes <tavianator@gmail.com>2010-09-26 03:05:06 -0400
commitafb8dbf0b074b16382e23c1ecabcbf5b650e1dab (patch)
tree924d2c83f20a7a1d350d13c993776ad8e593442e
parentbac926b1e54696beee1d84834876cfadd9939132 (diff)
downloaddimension-afb8dbf0b074b16382e23c1ecabcbf5b650e1dab.tar.xz
Add a timer API to libdimension.
-rw-r--r--dimension/main.c71
-rw-r--r--libdimension/Makefile.am6
-rw-r--r--libdimension/dimension.h1
-rw-r--r--libdimension/dimension/scene.h4
-rw-r--r--libdimension/dimension/timer.h34
-rw-r--r--libdimension/raytrace.c44
-rw-r--r--libdimension/scene.c5
-rw-r--r--libdimension/timer.c64
8 files changed, 185 insertions, 44 deletions
diff --git a/dimension/main.c b/dimension/main.c
index bb44df5..5274bdf 100644
--- a/dimension/main.c
+++ b/dimension/main.c
@@ -233,14 +233,19 @@ main(int argc, char **argv)
}
/* Realize the input */
+
printf("Parsing scene ...\n");
- dmnsn_scene *scene = dmnsn_realize(input_file, symtable);
- if (!scene) {
- fprintf(stderr, "Error realizing input file!\n");
- dmnsn_delete_symbol_table(symtable);
- fclose(input_file);
- return EXIT_FAILURE;
- }
+ /* Time the parser */
+ dmnsn_timer *parse_timer = dmnsn_new_timer();
+ dmnsn_scene *scene = dmnsn_realize(input_file, symtable);
+ if (!scene) {
+ fprintf(stderr, "Error realizing input file!\n");
+ dmnsn_delete_timer(parse_timer);
+ dmnsn_delete_symbol_table(symtable);
+ fclose(input_file);
+ return EXIT_FAILURE;
+ }
+ dmnsn_complete_timer(parse_timer);
dmnsn_delete_symbol_table(symtable);
fclose(input_file);
@@ -285,6 +290,7 @@ main(int argc, char **argv)
if (free_output)
dmnsn_free(output);
if (!output_file) {
+ dmnsn_delete_timer(parse_timer);
fprintf(stderr, "Couldn't open output file!");
return EXIT_FAILURE;
}
@@ -301,30 +307,49 @@ main(int argc, char **argv)
scene->nthreads);
if (dmnsn_finish_progress(render_progress) != 0) {
+ dmnsn_delete_timer(parse_timer);
dmnsn_delete_scene(scene);
fprintf(stderr, "Error rendering scene!\n");
return EXIT_FAILURE;
}
- dmnsn_progress *output_progress
- = dmnsn_png_write_canvas_async(scene->canvas, output_file);
- if (!output_progress) {
- fclose(output_file);
- dmnsn_delete_scene(scene);
- fprintf(stderr, "Couldn't initialize PNG export!\n");
- return EXIT_FAILURE;
- }
+ /* Time the export */
+ dmnsn_timer *export_timer = dmnsn_new_timer();
+ dmnsn_progress *output_progress
+ = dmnsn_png_write_canvas_async(scene->canvas, output_file);
+ if (!output_progress) {
+ dmnsn_delete_timer(parse_timer);
+ fclose(output_file);
+ dmnsn_delete_scene(scene);
+ fprintf(stderr, "Couldn't initialize PNG export!\n");
+ return EXIT_FAILURE;
+ }
- dmnsn_progressbar("Writing PNG", output_progress);
+ dmnsn_progressbar("Writing PNG", output_progress);
- if (dmnsn_finish_progress(output_progress) != 0) {
+ if (dmnsn_finish_progress(output_progress) != 0) {
+ dmnsn_delete_timer(export_timer);
+ dmnsn_delete_timer(parse_timer);
+ fclose(output_file);
+ dmnsn_delete_scene(scene);
+ fprintf(stderr, "Couldn't write output!\n");
+ return EXIT_FAILURE;
+ }
fclose(output_file);
- dmnsn_delete_scene(scene);
- fprintf(stderr, "Couldn't write output!\n");
- return EXIT_FAILURE;
- }
- fclose(output_file);
-
+ dmnsn_complete_timer(export_timer);
+
+ printf("\n"
+ " Parse time: " DMNSN_TIMER_FORMAT "\n"
+ " Bounding time: " DMNSN_TIMER_FORMAT "\n"
+ " Render time: " DMNSN_TIMER_FORMAT "\n"
+ " Export time: " DMNSN_TIMER_FORMAT "\n",
+ DMNSN_TIMER_PRINTF(parse_timer),
+ DMNSN_TIMER_PRINTF(scene->bounding_timer),
+ DMNSN_TIMER_PRINTF(scene->render_timer),
+ DMNSN_TIMER_PRINTF(export_timer));
+
+ dmnsn_delete_timer(export_timer);
+ dmnsn_delete_timer(parse_timer);
dmnsn_delete_scene(scene);
return EXIT_SUCCESS;
}
diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am
index 16ab286..73fb010 100644
--- a/libdimension/Makefile.am
+++ b/libdimension/Makefile.am
@@ -43,7 +43,8 @@ nobase_include_HEADERS = dimension.h \
dimension/progress.h \
dimension/raytrace.h \
dimension/scene.h \
- dimension/texture.h
+ dimension/texture.h \
+ dimension/timer.h
lib_LTLIBRARIES = libdimension.la
@@ -84,7 +85,8 @@ libdimension_la_SOURCES = $(nobase_include_HEADERS) \
sphere.c \
texture.c \
threads.c \
- threads.h
+ threads.h \
+ timer.c
libdimension_la_CFLAGS = $(AM_CFLAGS) -pthread
libdimension_la_LDFLAGS = -version-info 0:0:0 $(AM_LDFLAGS)
libdimension_la_LIBADD = -lm
diff --git a/libdimension/dimension.h b/libdimension/dimension.h
index 57e7c90..c58eb0e 100644
--- a/libdimension/dimension.h
+++ b/libdimension/dimension.h
@@ -65,6 +65,7 @@ typedef void dmnsn_free_fn(void *ptr);
#include <dimension/array.h>
#include <dimension/list.h>
#include <dimension/progress.h>
+#include <dimension/timer.h>
#include <dimension/geometry.h>
#include <dimension/color.h>
#include <dimension/canvas.h>
diff --git a/libdimension/dimension/scene.h b/libdimension/dimension/scene.h
index 7c53991..7e52859 100644
--- a/libdimension/dimension/scene.h
+++ b/libdimension/dimension/scene.h
@@ -64,6 +64,10 @@ typedef struct {
/* Number of parallel threads */
unsigned int nthreads;
+
+ /* Timers */
+ dmnsn_timer *bounding_timer;
+ dmnsn_timer *render_timer;
} dmnsn_scene;
/* Create a scene */
diff --git a/libdimension/dimension/timer.h b/libdimension/dimension/timer.h
new file mode 100644
index 0000000..113cb5f
--- /dev/null
+++ b/libdimension/dimension/timer.h
@@ -0,0 +1,34 @@
+/*************************************************************************
+ * Copyright (C) 2010 Tavian Barnes <tavianator@gmail.com> *
+ * *
+ * This file is part of The Dimension Library. *
+ * *
+ * The Dimension Library is free software; you can redistribute it and/ *
+ * or modify it under the terms of the GNU Lesser General Public License *
+ * as published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Library is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this program. If not, see *
+ * <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+/*
+ * A platform-agnostic timer abstraction
+ */
+
+typedef struct dmnsn_timer {
+ double real, user, system;
+} dmnsn_timer;
+
+#define DMNSN_TIMER_FORMAT "%.2fs (user: %.2fs; system: %.2fs)"
+#define DMNSN_TIMER_PRINTF(t) (t)->real, (t)->user, (t)->system
+
+dmnsn_timer *dmnsn_new_timer();
+void dmnsn_complete_timer(dmnsn_timer *timer);
+void dmnsn_delete_timer(dmnsn_timer *timer);
diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c
index 3430678..9822c8c 100644
--- a/libdimension/raytrace.c
+++ b/libdimension/raytrace.c
@@ -76,12 +76,15 @@ dmnsn_raytrace_scene_thread(void *ptr)
{
dmnsn_raytrace_payload *payload = ptr;
- /* Pre-calculate bounding box transformations, etc. */
- DMNSN_ARRAY_FOREACH (dmnsn_object **, object, payload->scene->objects) {
- dmnsn_object_init(*object);
- }
+ /* Time the bounding tree construction */
+ payload->scene->bounding_timer = dmnsn_new_timer();
+ /* Pre-calculate bounding box transformations, etc. */
+ DMNSN_ARRAY_FOREACH (dmnsn_object **, object, payload->scene->objects) {
+ dmnsn_object_init(*object);
+ }
- payload->prtree = dmnsn_new_prtree(payload->scene->objects);
+ payload->prtree = dmnsn_new_prtree(payload->scene->objects);
+ dmnsn_complete_timer(payload->scene->bounding_timer);
dmnsn_raytrace_payload *payloads;
pthread_t *threads;
@@ -105,23 +108,26 @@ dmnsn_raytrace_scene_thread(void *ptr)
payloads[i].threads = nthreads;
}
- /* Create the threads */
- for (int i = 0; i < nthreads; ++i) {
- if (pthread_create(&threads[i], NULL,
- &dmnsn_raytrace_scene_multithread_thread,
- &payloads[i]) != 0)
- {
- dmnsn_error(DMNSN_SEVERITY_HIGH,
- "Couldn't start worker thread in raytrace engine.");
+ /* Time the render itself */
+ payload->scene->render_timer = dmnsn_new_timer();
+ /* Create the threads */
+ for (int i = 0; i < nthreads; ++i) {
+ if (pthread_create(&threads[i], NULL,
+ &dmnsn_raytrace_scene_multithread_thread,
+ &payloads[i]) != 0)
+ {
+ dmnsn_error(DMNSN_SEVERITY_HIGH,
+ "Couldn't start worker thread in raytrace engine.");
+ }
}
- }
- for (int i = 0; i < nthreads; ++i) {
- if (pthread_join(threads[i], NULL)) {
- dmnsn_error(DMNSN_SEVERITY_MEDIUM,
- "Couldn't join worker thread in raytrace engine.");
+ for (int i = 0; i < nthreads; ++i) {
+ if (pthread_join(threads[i], NULL)) {
+ dmnsn_error(DMNSN_SEVERITY_MEDIUM,
+ "Couldn't join worker thread in raytrace engine.");
+ }
}
- }
+ dmnsn_complete_timer(payload->scene->render_timer);
dmnsn_free(threads);
dmnsn_free(payloads);
diff --git a/libdimension/scene.c b/libdimension/scene.c
index 6d17cea..f56236f 100644
--- a/libdimension/scene.c
+++ b/libdimension/scene.c
@@ -35,6 +35,8 @@ dmnsn_new_scene()
scene->quality = DMNSN_RENDER_FULL;
scene->reclimit = 5;
scene->nthreads = dmnsn_ncpus();
+ scene->bounding_timer = NULL;
+ scene->render_timer = NULL;
return scene;
}
@@ -44,6 +46,9 @@ void
dmnsn_delete_scene(dmnsn_scene *scene)
{
if (scene) {
+ dmnsn_delete_timer(scene->render_timer);
+ dmnsn_delete_timer(scene->bounding_timer);
+
DMNSN_ARRAY_FOREACH (dmnsn_light **, light, scene->lights) {
dmnsn_delete_light(*light);
}
diff --git a/libdimension/timer.c b/libdimension/timer.c
new file mode 100644
index 0000000..eb15357
--- /dev/null
+++ b/libdimension/timer.c
@@ -0,0 +1,64 @@
+/*************************************************************************
+ * Copyright (C) 2009-2010 Tavian Barnes <tavianator@gmail.com> *
+ * *
+ * This file is part of The Dimension Library. *
+ * *
+ * The Dimension Library is free software; you can redistribute it and/ *
+ * or modify it under the terms of the GNU Lesser General Public License *
+ * as published by the Free Software Foundation; either version 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Library is distributed in the hope that it will be *
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this program. If not, see *
+ * <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+#include "dimension.h"
+#include <sys/times.h>
+#include <unistd.h>
+
+static long clk_tck = 0;
+
+dmnsn_timer *
+dmnsn_new_timer()
+{
+ /* Figure out the clock ticks per second */
+ if (!clk_tck) {
+ clk_tck = sysconf(_SC_CLK_TCK);
+ if (clk_tck == -1) {
+ dmnsn_error(DMNSN_SEVERITY_MEDIUM, "sysconf(_SC_CLK_TCK) failed.");
+ clk_tck = 1000000L;
+ }
+ }
+
+ dmnsn_timer *timer = dmnsn_malloc(sizeof(dmnsn_timer));
+
+ struct tms buf;
+ clock_t real = times(&buf);
+ timer->real = (double)real/clk_tck;
+ timer->user = (double)buf.tms_utime/clk_tck;
+ timer->system = (double)buf.tms_stime/clk_tck;
+
+ return timer;
+}
+
+void
+dmnsn_complete_timer(dmnsn_timer *timer)
+{
+ struct tms buf;
+ clock_t real = times(&buf);
+ timer->real = (double)real/clk_tck - timer->real;
+ timer->user = (double)buf.tms_utime/clk_tck - timer->user;
+ timer->system = (double)buf.tms_stime/clk_tck - timer->system;
+}
+
+void
+dmnsn_delete_timer(dmnsn_timer *timer)
+{
+ dmnsn_free(timer);
+}