Skip to content

[LLDB][NFC] Refactor code extracting timestamp from StructuredData #145954

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

oontvoo
Copy link
Member

@oontvoo oontvoo commented Jun 26, 2025

No description provided.

@oontvoo oontvoo requested a review from labath June 26, 2025 19:20
@oontvoo oontvoo requested a review from JDevlieghere as a code owner June 26, 2025 19:20
@llvmbot llvmbot added the lldb label Jun 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 26, 2025

@llvm/pr-subscribers-lldb

Author: Vy Nguyen (oontvoo)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/145954.diff

1 Files Affected:

  • (modified) lldb/source/Core/Telemetry.cpp (+19-15)
diff --git a/lldb/source/Core/Telemetry.cpp b/lldb/source/Core/Telemetry.cpp
index 6434b11a63ad4..464c1d4071a73 100644
--- a/lldb/source/Core/Telemetry.cpp
+++ b/lldb/source/Core/Telemetry.cpp
@@ -119,6 +119,19 @@ llvm::Error TelemetryManager::preDispatch(TelemetryInfo *entry) {
   return llvm::Error::success();
 }
 
+// Helper for extracting time field from a Dictionary.
+static std::optional<std::chrono::nanoseconds>
+GetAsNanosec(StructuredData::Dictionary *dict, llvm::StringRef key) {
+  auto value = dict->GetValueForKey(key);
+  if (!value->IsValid()) {
+    LLDB_LOG(GetLog(LLDBLog::Object),
+             "Cannot determine {0} from client-telemetry entry", key);
+    return std::nullopt;
+  }
+
+  return std::chrono::nanoseconds(value->GetUnsignedIntegerValue(0));
+}
+
 void TelemetryManager::DispatchClientTelemetry(
     const lldb_private::StructuredDataImpl &entry, Debugger *debugger) {
   if (!m_config->enable_client_telemetry)
@@ -148,23 +161,14 @@ void TelemetryManager::DispatchClientTelemetry(
     LLDB_LOG(GetLog(LLDBLog::Object),
              "Cannot determine client_data from client-telemetry entry");
 
-  int64_t start_time;
-  if (dict->GetValueForKeyAsInteger("start_time", start_time)) {
-    client_info.start_time +=
-        std::chrono::nanoseconds(static_cast<size_t>(start_time));
-  } else {
-    LLDB_LOG(GetLog(LLDBLog::Object),
-             "Cannot determine start-time from client-telemetry entry");
-  }
+  auto start_time = GetAsNanosec(dict, "start_time");
+  if (start_time.has_value())
+    client_info.start_time += start_time.value();
 
-  int64_t end_time;
-  if (dict->GetValueForKeyAsInteger("end_time", end_time)) {
+  auto end_time = GetAsNanosec(dict, "end_time");
+  if (end_time.has_value()) {
     SteadyTimePoint epoch;
-    client_info.end_time =
-        epoch + std::chrono::nanoseconds(static_cast<size_t>(end_time));
-  } else {
-    LLDB_LOG(GetLog(LLDBLog::Object),
-             "Cannot determine end-time from client-telemetry entry");
+    client_info.end_time = epoch + end_time.value();
   }
 
   llvm::StringRef error_msg;

Comment on lines +168 to +171
auto end_time = GetAsNanosec(dict, "end_time");
if (end_time.has_value()) {
SteadyTimePoint epoch;
client_info.end_time =
epoch + std::chrono::nanoseconds(static_cast<size_t>(end_time));
} else {
LLDB_LOG(GetLog(LLDBLog::Object),
"Cannot determine end-time from client-telemetry entry");
client_info.end_time = epoch + end_time.value();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion here.

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- lldb/source/Core/Telemetry.cpp
View the diff from clang-format here.
diff --git a/lldb/source/Core/Telemetry.cpp b/lldb/source/Core/Telemetry.cpp
index 5382c0e09..6d2848f89 100644
--- a/lldb/source/Core/Telemetry.cpp
+++ b/lldb/source/Core/Telemetry.cpp
@@ -162,7 +162,7 @@ void TelemetryManager::DispatchClientTelemetry(
              "Cannot determine client_data from client-telemetry entry");
 
   if (auto maybe_start_time = GetAsNanosec(dict, "start_time"))
-      client_info.start_time += *maybe_start_time;
+    client_info.start_time += *maybe_start_time;
 
   auto end_time = GetAsNanosec(dict, "end_time");
   if (end_time.has_value()) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants