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

Improve mktime cache #101

Merged
merged 2 commits into from
May 22, 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
37 changes: 24 additions & 13 deletions lib/timeutils/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,31 +305,42 @@ get_cached_realtime_sec(void)
return (time_t) now.tv_sec;
}

static time_t
_calculate_and_adjust_mktime_result_based_on_cache(struct tm *tm)
{
/* our cached value is valid for 1 hour, as we cache the min==sec==0 value
* for every second */
int sec = tm->tm_sec;
int min = tm->tm_min;
*tm = cache.mktime.mutated_key;
tm->tm_sec = sec;
tm->tm_min = min;
return cache.mktime.value + tm->tm_min * 60 + tm->tm_sec;
}

time_t
cached_mktime(struct tm *tm)
{
_validate_timeutils_cache();
if (G_LIKELY(tm->tm_sec == cache.mktime.key.tm_sec &&
tm->tm_min == cache.mktime.key.tm_min &&
tm->tm_hour == cache.mktime.key.tm_hour &&
if (G_LIKELY(tm->tm_hour == cache.mktime.key.tm_hour &&
tm->tm_mday == cache.mktime.key.tm_mday &&
tm->tm_mon == cache.mktime.key.tm_mon &&
tm->tm_year == cache.mktime.key.tm_year &&
tm->tm_isdst == cache.mktime.key.tm_isdst))
{
*tm = cache.mktime.mutated_key;
return cache.mktime.value;
return _calculate_and_adjust_mktime_result_based_on_cache(tm);
}

/* we need to store the incoming value first, as mktime() might change the
* fields in *tm, for instance in the daylight saving transition hour */
cache.mktime.key = *tm;
cache.mktime.value = mktime(tm);
/* we need to store both the incoming value (key) and the one mutated by
* mktime(), as mktime() might change the fields in *tm for instance in
* the daylight saving transition hour */
cache.mktime.key = cache.mktime.mutated_key = *tm;

/* the result we yield consists of both the return value and the mutated
* key, so we need to save both */
cache.mktime.mutated_key = *tm;
return cache.mktime.value;
/* let's cache the top of the hour */
cache.mktime.mutated_key.tm_sec = 0;
cache.mktime.mutated_key.tm_min = 0;
cache.mktime.value = mktime(&cache.mktime.mutated_key);
return _calculate_and_adjust_mktime_result_based_on_cache(tm);
}

void
Expand Down
2 changes: 1 addition & 1 deletion modules/syslogformat/syslog-format.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ _syslog_format_parse_timestamp(LogMessage *msg, UnixTime *stamp,
result = scan_rfc5424_timestamp(data, length, &wct);
}

if ((parse_flags & LP_NO_PARSE_DATE) == 0)
if (result && (parse_flags & LP_NO_PARSE_DATE) == 0)
{
convert_and_normalize_wall_clock_time_to_unix_time_with_tz_hint(&wct, stamp, recv_timezone_ofs);

Expand Down
Loading