From f0cf65ee965706252c590423f729f82fdcfbfe9e Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Thu, 16 Jan 2025 15:41:14 -0800 Subject: [PATCH] [TSL] Use absl::Time to compute timestamps for logging This allows us to avoid having to handle platform specific details like localtime_r() vs localtime_s() vs localtime(). PiperOrigin-RevId: 716404994 --- xla/tsl/platform/default/BUILD | 2 ++ xla/tsl/platform/default/logging.cc | 35 ++++++++++++++--------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/xla/tsl/platform/default/BUILD b/xla/tsl/platform/default/BUILD index 829cef6d2a1f0..06db906d64bc7 100644 --- a/xla/tsl/platform/default/BUILD +++ b/xla/tsl/platform/default/BUILD @@ -296,6 +296,8 @@ cc_library( "@com_google_absl//absl/base:log_severity", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + "@com_google_absl//absl/time", "@tsl//tsl/platform", "@tsl//tsl/platform:mutex", ], diff --git a/xla/tsl/platform/default/logging.cc b/xla/tsl/platform/default/logging.cc index 9375d85bf97eb..5e6517ad5522e 100644 --- a/xla/tsl/platform/default/logging.cc +++ b/xla/tsl/platform/default/logging.cc @@ -27,6 +27,8 @@ limitations under the License. #include "absl/strings/numbers.h" #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" +#include "absl/time/clock.h" +#include "absl/time/time.h" #include "xla/tsl/platform/env_time.h" #include "xla/tsl/platform/macros.h" #include "tsl/platform/mutex.h" @@ -511,22 +513,19 @@ void TFDefaultLogSink::Send(const TFLogEntry& entry) { #else // PLATFORM_POSIX_ANDROID static const internal::VlogFileMgr vlog_file; static bool log_thread_id = internal::EmitThreadIdFromEnv(); - uint64_t now_micros = EnvTime::NowMicros(); - time_t now_seconds = static_cast(now_micros / 1000000); - int32_t micros_remainder = static_cast(now_micros % 1000000); - const size_t time_buffer_size = 30; - char time_buffer[time_buffer_size]; - struct tm* tp; -#if defined(__linux__) || defined(__APPLE__) - struct tm now_tm; - tp = localtime_r(&now_seconds, &now_tm); -#else - tp = localtime(&now_seconds); // NOLINT(runtime/threadsafe_fn) -#endif - strftime(time_buffer, time_buffer_size, "%Y-%m-%d %H:%M:%S", tp); + constexpr size_t kTimeBufferSize = sizeof("YYYY-MM-DD HH:MM:SS.000000"); + char time_buffer[kTimeBufferSize]; + absl::Time now = absl::Now(); + static const absl::TimeZone* tz = new absl::TimeZone{absl::LocalTimeZone()}; + absl::TimeZone::CivilInfo ci = tz->At(now); + const int64_t usecs = absl::ToInt64Microseconds(ci.subsecond); + absl::SNPrintF(time_buffer, kTimeBufferSize, + "%04d-%02d-%02d %02d:%02d:%02d.%06d", ci.cs.year(), + ci.cs.month(), ci.cs.day(), ci.cs.hour(), ci.cs.minute(), + ci.cs.second(), usecs); uint64_t tid = absl::base_internal::GetTID(); - constexpr size_t kTidBufferSize = - (1 + std::numeric_limits::digits10 + 1); + constexpr size_t kMaxTidDigits = 1 + std::numeric_limits::digits10; + constexpr size_t kTidBufferSize = 1 + kMaxTidDigits + 1; char tid_buffer[kTidBufferSize] = ""; if (log_thread_id) { absl::SNPrintF(tid_buffer, sizeof(tid_buffer), " %7u", tid); @@ -556,9 +555,9 @@ void TFDefaultLogSink::Send(const TFLogEntry& entry) { break; } - absl::FPrintF(vlog_file.FilePtr(), "%s.%06d: %c%s %s:%d] %s\n", time_buffer, - micros_remainder, sev, tid_buffer, entry.FName().c_str(), - entry.Line(), entry.ToString().c_str()); + absl::FPrintF(vlog_file.FilePtr(), "%s: %c%s %s:%d] %s\n", time_buffer, sev, + tid_buffer, entry.FName().c_str(), entry.Line(), + entry.ToString().c_str()); fflush(vlog_file.FilePtr()); // Ensure logs are written immediately. #endif // PLATFORM_POSIX_ANDROID }