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

Moved timer functionality to (new) H5timer.h #4970

Merged
merged 1 commit into from
Oct 18, 2024
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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ set (H5_PUBLIC_HEADERS

set (H5_PRIVATE_HEADERS
${HDF5_SRC_DIR}/H5private.h
${HDF5_SRC_DIR}/H5timer.h

${HDF5_SRC_DIR}/H5Apkg.h
${HDF5_SRC_DIR}/H5Aprivate.h
Expand Down
41 changes: 3 additions & 38 deletions src/H5private.h
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,6 @@
#endif
#endif

/* KiB, MiB, GiB, TiB, PiB, EiB - Used in profiling and timing code */
#define H5_KB (1024.0F)
#define H5_MB (1024.0F * 1024.0F)
#define H5_GB (1024.0F * 1024.0F * 1024.0F)
#define H5_TB (1024.0F * 1024.0F * 1024.0F * 1024.0F)
#define H5_PB (1024.0F * 1024.0F * 1024.0F * 1024.0F * 1024.0F)
#define H5_EB (1024.0F * 1024.0F * 1024.0F * 1024.0F * 1024.0F * 1024.0F)

#ifndef H5_HAVE_FLOCK
/* flock() operations. Used in the source so we have to define them when
* the call is not available (e.g.: Windows). These should NOT be used
Expand Down Expand Up @@ -617,37 +609,10 @@ typedef _Float16 H5__Float16;
typedef int (*H5_sort_func_cb_t)(const void *, const void *);

/* Typedefs and functions for timing certain parts of the library. */
#include "H5timer.h"

/* A set of elapsed/user/system times emitted as a time point by the
* platform-independent timers.
*/
typedef struct {
double user; /* User time in seconds */
double system; /* System time in seconds */
double elapsed; /* Elapsed (wall clock) time in seconds */
} H5_timevals_t;

/* Timer structure for platform-independent timers */
typedef struct {
H5_timevals_t initial; /* Current interval start time */
H5_timevals_t final_interval; /* Last interval elapsed time */
H5_timevals_t total; /* Total elapsed time for all intervals */
bool is_running; /* Whether timer is running */
} H5_timer_t;

/* Returns library bandwidth as a pretty string */
H5_DLL void H5_bandwidth(char *buf /*out*/, size_t bufsize, double nbytes, double nseconds);

/* Timer functionality */
H5_DLL time_t H5_now(void);
H5_DLL uint64_t H5_now_usec(void);
H5_DLL herr_t H5_timer_init(H5_timer_t *timer /*in,out*/);
H5_DLL herr_t H5_timer_start(H5_timer_t *timer /*in,out*/);
H5_DLL herr_t H5_timer_stop(H5_timer_t *timer /*in,out*/);
H5_DLL herr_t H5_timer_get_times(H5_timer_t timer, H5_timevals_t *times /*in,out*/);
H5_DLL herr_t H5_timer_get_total_times(H5_timer_t timer, H5_timevals_t *times /*in,out*/);
H5_DLL char *H5_timer_get_time_string(double seconds);
H5_DLL char *H5_strcasestr(const char *haystack, const char *needle);
/* Substitute for strcasestr() when that doesn't exist on the platform */
H5_DLL char *H5_strcasestr(const char *haystack, const char *needle);

/* Depth of object copy */
typedef enum {
Expand Down
86 changes: 17 additions & 69 deletions src/H5timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,15 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
* Created: H5timer.c
*
* Purpose: Internal, platform-independent 'timer' support routines.
* H5timer.c
*
* Internal, platform-independent 'timer' support routines
*-------------------------------------------------------------------------
*/

/****************/
/* Module Setup */
/****************/
#include "H5module.h" /* This source code file is part of the H5 module */

/***********/
/* Headers */
/***********/
#include "H5private.h" /* Generic Functions */

/****************/
/* Local Macros */
/****************/
#include "H5private.h"

/* Size of a generated time string.
* Most time strings should be < 20 or so characters (max!) so this should be a
Expand All @@ -43,30 +32,6 @@
#define H5_SEC_PER_HOUR (60.0 * 60.0)
#define H5_SEC_PER_MIN (60.0)

/******************/
/* Local Typedefs */
/******************/

/********************/
/* Package Typedefs */
/********************/

/********************/
/* Local Prototypes */
/********************/

/*********************/
/* Package Variables */
/*********************/

/*****************************/
/* Library Private Variables */
/*****************************/

/*******************/
/* Local Variables */
/*******************/

/*-------------------------------------------------------------------------
* Function: H5_bandwidth
*
Expand All @@ -86,7 +51,6 @@
* 6.678e+106 For really big values
*
* Return: void
*
*-------------------------------------------------------------------------
*/
void
Expand Down Expand Up @@ -130,8 +94,8 @@ H5_bandwidth(char *buf /*out*/, size_t bufsize, double nbytes, double nseconds)
snprintf(buf, bufsize, "%10.4e", bw);
if (strlen(buf) > 10)
snprintf(buf, bufsize, "%10.3e", bw);
} /* end else-if */
} /* end else */
}
}
} /* end H5_bandwidth() */

/*-------------------------------------------------------------------------
Expand All @@ -140,7 +104,6 @@ H5_bandwidth(char *buf /*out*/, size_t bufsize, double nbytes, double nseconds)
* Purpose: Retrieves the current time, as seconds after the UNIX epoch.
*
* Return: # of seconds from the epoch (can't fail)
*
*-------------------------------------------------------------------------
*/
time_t
Expand All @@ -155,11 +118,11 @@ H5_now(void)
HDgettimeofday(&now_tv, NULL);
now = now_tv.tv_sec;
}
#else /* H5_HAVE_GETTIMEOFDAY */
#else
now = time(NULL);
#endif /* H5_HAVE_GETTIMEOFDAY */
#endif

return (now);
return now;
} /* end H5_now() */

/*-------------------------------------------------------------------------
Expand All @@ -168,7 +131,6 @@ H5_now(void)
* Purpose: Retrieves the current time, as microseconds after the UNIX epoch.
*
* Return: # of microseconds from the epoch (can't fail)
*
*-------------------------------------------------------------------------
*/
uint64_t
Expand Down Expand Up @@ -197,13 +159,13 @@ H5_now_usec(void)
* calculations are done in 64 bit, to prevent overflow */
now = ((uint64_t)now_tv.tv_sec * ((uint64_t)1000 * (uint64_t)1000)) + (uint64_t)now_tv.tv_usec;
}
#else /* H5_HAVE_GETTIMEOFDAY */
#else
/* Cast all values in this expression to uint64_t to ensure that all intermediate calculations
* are done in 64 bit, to prevent overflow */
now = ((uint64_t)time(NULL) * ((uint64_t)1000 * (uint64_t)1000));
#endif /* H5_HAVE_GETTIMEOFDAY */
#endif

return (now);
return now;
} /* end H5_now_usec() */

/*--------------------------------------------------------------------------
Expand All @@ -213,7 +175,6 @@ H5_now_usec(void)
*
* Return: Success: A non-negative time value
* Failure: -1.0 (in theory, can't currently fail)
*
*--------------------------------------------------------------------------
*/
double
Expand Down Expand Up @@ -252,13 +213,11 @@ H5_get_time(void)
*
* Return: Success: 0
* Failure: -1
*
*-------------------------------------------------------------------------
*/
static herr_t
H5__timer_get_timevals(H5_timevals_t *times /*in,out*/)
{
/* Sanity check */
assert(times);

/* Windows call handles both system/user and elapsed times */
Expand All @@ -269,7 +228,7 @@ H5__timer_get_timevals(H5_timevals_t *times /*in,out*/)
times->user = -1.0;

return -1;
} /* end if */
}
#else /* H5_HAVE_WIN32_API */

/*************************
Expand Down Expand Up @@ -349,13 +308,11 @@ H5__timer_get_timevals(H5_timevals_t *times /*in,out*/)
*
* Return: Success: 0
* Failure: -1
*
*-------------------------------------------------------------------------
*/
herr_t
H5_timer_init(H5_timer_t *timer /*in,out*/)
{
/* Sanity check */
assert(timer);

/* Initialize everything */
Expand All @@ -371,13 +328,11 @@ H5_timer_init(H5_timer_t *timer /*in,out*/)
*
* Return: Success: 0
* Failure: -1
*
*-------------------------------------------------------------------------
*/
herr_t
H5_timer_start(H5_timer_t *timer /*in,out*/)
{
/* Sanity check */
assert(timer);

/* Start the timer
Expand All @@ -398,13 +353,11 @@ H5_timer_start(H5_timer_t *timer /*in,out*/)
*
* Return: Success: 0
* Failure: -1
*
*-------------------------------------------------------------------------
*/
herr_t
H5_timer_stop(H5_timer_t *timer /*in,out*/)
{
/* Sanity check */
assert(timer);

/* Stop the timer */
Expand Down Expand Up @@ -446,13 +399,11 @@ H5_timer_stop(H5_timer_t *timer /*in,out*/)
*
* Return: Success: 0
* Failure: -1
*
*-------------------------------------------------------------------------
*/
herr_t
H5_timer_get_times(H5_timer_t timer, H5_timevals_t *times /*in,out*/)
{
/* Sanity check */
assert(times);

if (timer.is_running) {
Expand All @@ -467,12 +418,12 @@ H5_timer_get_times(H5_timer_t timer, H5_timevals_t *times /*in,out*/)
times->elapsed = now.elapsed - timer.initial.elapsed;
times->system = now.system - timer.initial.system;
times->user = now.user - timer.initial.user;
} /* end if */
}
else {
times->elapsed = timer.final_interval.elapsed;
times->system = timer.final_interval.system;
times->user = timer.final_interval.user;
} /* end else */
}

return 0;
} /* end H5_timer_get_times() */
Expand All @@ -498,13 +449,11 @@ H5_timer_get_times(H5_timer_t timer, H5_timevals_t *times /*in,out*/)
*
* Return: Success: 0
* Failure: -1
*
*-------------------------------------------------------------------------
*/
herr_t
H5_timer_get_total_times(H5_timer_t timer, H5_timevals_t *times /*in,out*/)
{
/* Sanity check */
assert(times);

if (timer.is_running) {
Expand All @@ -519,12 +468,12 @@ H5_timer_get_total_times(H5_timer_t timer, H5_timevals_t *times /*in,out*/)
times->elapsed = timer.total.elapsed + (now.elapsed - timer.initial.elapsed);
times->system = timer.total.system + (now.system - timer.initial.system);
times->user = timer.total.user + (now.user - timer.initial.user);
} /* end if */
}
else {
times->elapsed = timer.total.elapsed;
times->system = timer.total.system;
times->user = timer.total.user;
} /* end else */
}

return 0;
} /* end H5_timer_get_total_times() */
Expand All @@ -548,7 +497,6 @@ H5_timer_get_total_times(H5_timer_t timer, H5_timevals_t *times /*in,out*/)
* "%.f h %.f m %.f s" longer times
*
* Failure: NULL
*
*-------------------------------------------------------------------------
*/
char *
Expand Down Expand Up @@ -580,7 +528,7 @@ H5_timer_get_time_string(double seconds)
remainder_sec -= (minutes * H5_SEC_PER_MIN);

/* The # of seconds left is in remainder_sec */
} /* end if */
}

/* Allocate */
if (NULL == (s = (char *)calloc(H5TIMER_TIME_STRING_LEN, sizeof(char))))
Expand Down
Loading
Loading