Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6_2] Migrate tm_timer #38

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions System/Classes/tm_timer.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/

#include "tm_timer.hpp"
#include "basic.hpp"
#include "iterator.hpp"
#include "merge_sort.hpp"
#include "tm_ostream.hpp"

static hashmap<string, int> timing_level (0);
static hashmap<string, int> timing_nr (0);
static hashmap<string, int> timing_cumul (0);
static hashmap<string, int> 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<string>
collect (hashmap<string, int> h) {
array<string> a;
iterator<string> 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<string> a= collect (timing_cumul);
int i, n= N (a);
for (i= 0; i < n; i++)
bench_print (ostream, a[i]);
}
52 changes: 52 additions & 0 deletions System/Classes/tm_timer.hpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/

#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 <time.h>
#endif

#ifdef OS_SUN
#include <sys/types.h>
#endif

#ifdef HAVE_GETTIMEOFDAY
#include <sys/time.h>
#else
#include <sys/timeb.h>
#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
2 changes: 2 additions & 0 deletions System/config_l1.h.xmake
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 8 additions & 3 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ 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")
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", [[
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -159,4 +164,4 @@ if is_mode("profile") then
set_symbols("debug")
add_cxflags("-pg")
add_ldflags("-pg")
end
end
Loading