diff --git a/System/Classes/tm_timer.cpp b/System/Classes/tm_timer.cpp new file mode 100644 index 000000000..36208a398 --- /dev/null +++ b/System/Classes/tm_timer.cpp @@ -0,0 +1,123 @@ + +/****************************************************************************** + * MODULE : timer.cpp + * DESCRIPTION: timers + * COPYRIGHT : (C) 1999 Joris van der Hoeven + ******************************************************************************* + * This software falls under the GNU general public license version 3 or later. + * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE + * in the root directory or . + ******************************************************************************/ + +#include "tm_timer.hpp" +#include "basic.hpp" +#include "iterator.hpp" +#include "merge_sort.hpp" +#include "tm_ostream.hpp" + +static hashmap timing_level (0); +static hashmap timing_nr (0); +static hashmap timing_cumul (0); +static hashmap timing_last (0); + +/****************************************************************************** + * Getting the time + ******************************************************************************/ + +time_t +raw_time () { +#ifdef HAVE_GETTIMEOFDAY + struct timeval tp; + gettimeofday (&tp, NULL); + return (time_t) ((tp.tv_sec * 1000) + (tp.tv_usec / 1000)); +#else + timeb tb; + ftime (&tb); + return (time_t) ((tb.time * 1000) + tb.millitm); +#endif +} + +static time_t start_time= raw_time (); + +time_t +texmacs_time () { +#ifdef HAVE_GETTIMEOFDAY + struct timeval tp; + gettimeofday (&tp, NULL); + return ((time_t) ((tp.tv_sec * 1000) + (tp.tv_usec / 1000))) - start_time; +#else + timeb tb; + ftime (&tb); + return ((time_t) ((tb.time * 1000) + tb.millitm)) - start_time; +#endif +} + +/****************************************************************************** + * Routines for benchmarking + ******************************************************************************/ + +void +bench_start (string task) { + // start timer for a given type of task + if (timing_level[task] == 0) timing_last (task)= (int) texmacs_time (); + timing_level (task)++; +} + +void +bench_cumul (string task) { + // end timer for a given type of task, but don't reset timer + timing_level (task)--; + if (timing_level[task] == 0) { + int ms= ((int) texmacs_time ()) - timing_last (task); + timing_nr (task)++; + timing_cumul (task)+= ms; + timing_last->reset (task); + } +} + +void +bench_end (tm_ostream ostream, string task) { + // end timer for a given type of task, print result and reset timer + bench_cumul (task); + bench_print (ostream, task); + bench_reset (task); +} + +void +bench_reset (string task) { + // reset timer for a given type of task + timing_level->reset (task); + timing_nr->reset (task); + timing_cumul->reset (task); + timing_last->reset (task); +} + +void +bench_print (tm_ostream ostream, string task) { + // print timing for a given type of task + if (DEBUG_BENCH) { + int nr= timing_nr[task]; + ostream << "Task '" << task << "' took " << timing_cumul[task] << " ms"; + if (nr > 1) ostream << " (" << nr << " invocations)"; + ostream << LF; + } +} + +static array +collect (hashmap h) { + array a; + iterator it= iterate (h); + while (it->busy ()) + a << it->next (); + merge_sort (a); + return a; +} + +void +bench_print (tm_ostream ostream) { + // print timings for all types of tasks + array a= collect (timing_cumul); + int i, n= N (a); + for (i= 0; i < n; i++) + bench_print (ostream, a[i]); +} diff --git a/System/Classes/tm_timer.hpp b/System/Classes/tm_timer.hpp new file mode 100644 index 000000000..2540bcb8b --- /dev/null +++ b/System/Classes/tm_timer.hpp @@ -0,0 +1,52 @@ + +/****************************************************************************** + * MODULE : tm_timer.hpp + * DESCRIPTION: timers + * COPYRIGHT : (C) 1999 Joris van der Hoeven + ******************************************************************************* + * This software falls under the GNU general public license version 3 or later. + * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE + * in the root directory or . + ******************************************************************************/ + +#ifndef TIMER_H +#define TIMER_H + +#include "string.hpp" +#include "tm_ostream.hpp" + +#ifndef __FreeBSD__ +#ifndef HAVE_TIME_T +#define HAVE_TIME_T +typedef long time_t; +#endif +#else +#include +#endif + +#ifdef OS_SUN +#include +#endif + +#ifdef HAVE_GETTIMEOFDAY +#include +#else +#include +#ifdef OS_SUN +extern "C" { +extern int ftime __P ((struct timeb * __timebuf)); +}; +#endif +#endif + +time_t raw_time (); +time_t texmacs_time (); + +void bench_start (string task); +void bench_cumul (string task); +void bench_end (tm_ostream ostream, string task); +void bench_reset (string task); +void bench_print (tm_ostream ostream, string task); +void bench_print (tm_ostream ostream); + +#endif // defined TIMER_H diff --git a/System/config_l1.h.xmake b/System/config_l1.h.xmake index 1b4e39b4a..817271db4 100644 --- a/System/config_l1.h.xmake +++ b/System/config_l1.h.xmake @@ -1,6 +1,8 @@ ${define HAVE_INTPTR_T} ${define HAVE_STDINT_H} ${define HAVE_INTTYPES_H} +${define HAVE_TIME_T} +${define HAVE_GETTIMEOFDAY} #ifdef HAVE_INTPTR_T #ifdef HAVE_INTTYPES_H diff --git a/xmake.lua b/xmake.lua index 50f0602d8..fc48f9e85 100644 --- a/xmake.lua +++ b/xmake.lua @@ -6,6 +6,7 @@ set_languages("c++17") includes("check_cxxtypes.lua") configvar_check_cxxtypes("HAVE_INTPTR_T", "intptr_t", {includes = {"memory"}}) +configvar_check_cxxtypes("HAVE_TIME_T", "time_t", {includes = {"memory"}}) includes("check_cxxincludes.lua") configvar_check_cxxincludes("HAVE_STDLIB_H", "stdlib.h") @@ -13,6 +14,8 @@ configvar_check_cxxincludes("HAVE_STDINT_H", "stdint.h") configvar_check_cxxincludes("HAVE_INTTYPES_H", "inttypes.h") includes("check_cxxfuncs.lua") +configvar_check_cxxfuncs("HAVE_GETTIMEOFDAY", "gettimeofday", {includes={"sys/time.h"}}) + includes("check_cxxsnippets.lua") configvar_check_cxxsnippets( "CONFIG_LARGE_POINTER", [[ @@ -40,13 +43,14 @@ local l1_files = { "Data/String/**.cpp", } local l1_includedirs = { - "System/IO", - "System/Memory", "Kernel/Abstractions", "Kernel/Algorithms", "Kernel/Containers", "Kernel/Types", "Data/String", + "System/Classes", + "System/IO", + "System/Memory", } target("liblolly") do @@ -90,6 +94,7 @@ target("liblolly") do add_headerfiles("Kernel/Containers/(*hpp)") add_headerfiles("Kernel/Containers/(*.ipp)") add_headerfiles("Kernel/Types/(*hpp)") + add_headerfiles("System/Classes/(*hpp)") add_headerfiles("System/IO/(*hpp)") add_headerfiles("System/Memory/(*hpp)") add_headerfiles("Data/String/(*.hpp)") @@ -159,4 +164,4 @@ if is_mode("profile") then set_symbols("debug") add_cxflags("-pg") add_ldflags("-pg") -end \ No newline at end of file +end