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

Don't cache time on Open BSD #17

Closed
wants to merge 1 commit into from
Closed
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
55 changes: 42 additions & 13 deletions src/util-time.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ static struct timeval current_time = { 0, 0 };
static SCSpinlock current_time_spinlock;
static char live = TRUE;

/* Values for caching in CreateTimeString */
#define MAX_LOCAL_TIME_STRING 128
static __thread int mru_time_slot; /* Most recently used cached value */
static __thread time_t last_local_time[2];
static __thread short int cached_local_time_len[2];
static __thread char cached_local_time[2][MAX_LOCAL_TIME_STRING];

/* Values for caching SCLocalTime() These cached values are
* independent from the CreateTimeString cached values. */
static __thread int mru_tm_slot; /* Most recently used local tm */
static __thread time_t cached_minute_start[2];
static __thread struct tm cached_local_tm[2];

void TimeInit(void)
{
SCSpinInit(&current_time_spinlock, 0);
Expand Down Expand Up @@ -133,6 +120,46 @@ void TimeSetIncrementTime(uint32_t tv_sec)
TimeSet(&tv);
}

/*
* Time Caching code
*/

#if defined(__OpenBSD__)
/* OpenBSD does not support __thread, so don't use time caching on BSD
*/
struct tm *SCLocalTime(time_t timep, struct tm *result)
{
return localtime_r(&timep, result);
}

void CreateTimeString (const struct timeval *ts, char *str, size_t size)
{
time_t time = ts->tv_sec;
struct tm local_tm;
struct tm *t = (struct tm*)SCLocalTime(time, &local_tm);

snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u",
t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour,
t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec);
}

#else

/* On systems supporting __thread, use Per-thread values for caching
* in CreateTimeString */

#define MAX_LOCAL_TIME_STRING 128
static __thread int mru_time_slot; /* Most recently used cached value */
static __thread time_t last_local_time[2];
static __thread short int cached_local_time_len[2];
static __thread char cached_local_time[2][MAX_LOCAL_TIME_STRING];

/* Per-thread values for caching SCLocalTime() These cached values are
* independent from the CreateTimeString cached values. */
static __thread int mru_tm_slot; /* Most recently used local tm */
static __thread time_t cached_minute_start[2];
static __thread struct tm cached_local_tm[2];

/** \brief Convert time_t into Year, month, day, hour and minutes.
* \param timep Time in seconds since defined date.
* \param result The structure into which the broken down time it put.
Expand Down Expand Up @@ -245,3 +272,5 @@ void CreateTimeString (const struct timeval *ts, char *str, size_t size)
"%02d.%06u",
seconds, (uint32_t) ts->tv_usec);
}

#endif /* defined(__OpenBSD__) */