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
Open
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
33 changes: 18 additions & 15 deletions lldb/source/Core/Telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -148,23 +161,13 @@ 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");
}
if (auto maybe_start_time = GetAsNanosec(dict, "start_time"))
client_info.start_time += *maybe_start_time;

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();
Comment on lines +167 to +170
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.

}

llvm::StringRef error_msg;
Expand Down
Loading