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

Implement the Timestamp class to ease the usage of time_point. #134

Merged
merged 1 commit into from Nov 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions layer/support/csv_logging.cc
Expand Up @@ -46,8 +46,8 @@ std::string ValueToCSVString(Duration value) {
return std::to_string(value.ToNanoseconds());
}

std::string ValueToCSVString(TimestampClock::time_point value) {
return std::to_string(ToUnixNanos(value));
std::string ValueToCSVString(Timestamp value) {
return std::to_string(value.ToNanoseconds());
}

// Takes an `Event` instance as an input and generates a csv string containing
Expand Down
6 changes: 3 additions & 3 deletions layer/support/csv_logging.h
Expand Up @@ -29,11 +29,11 @@ std::string ValueToCSVString(const int64_t value);

std::string ValueToCSVString(const std::vector<int64_t> &values);

// Converts a `DurationClock::duration` to its nanoseconds representation.
// Converts a `Duration` to its nanoseconds representation.
std::string ValueToCSVString(Duration value);

// Converts a `TimestampClock::time_point` to its nanoseconds representation.
std::string ValueToCSVString(TimestampClock::time_point value);
// Converts a `Timestamp` to its nanoseconds representation.
std::string ValueToCSVString(Timestamp value);

// Takes an `Event` instance as an input and generates a csv string containing
// `event`'s name and attribute values. The duration values will be logged in
Expand Down
12 changes: 5 additions & 7 deletions layer/support/event_logging.h
Expand Up @@ -123,13 +123,13 @@ class TimestampAttr : public Attribute {
public:
static constexpr ValueType id_ = ValueType::kTimestamp;

TimestampAttr(const char *name, const TimestampClock::time_point &value)
TimestampAttr(const char *name, const Timestamp &value)
: Attribute(name, ValueType::kTimestamp), value_(value) {}

TimestampClock::time_point GetValue() const { return value_; };
Timestamp GetValue() const { return value_; };

private:
TimestampClock::time_point value_;
Timestamp value_;
};

using BoolAttr = AttributeImpl<bool, ValueType::kBool>;
Expand Down Expand Up @@ -219,14 +219,12 @@ class Event {
log_level_(log_level),
creation_time_({"timestamp", GetTimestamp()}) {}

Event(const char *name, int64_t timestamp,
Event(const char *name, int64_t timestamp_ns,
LogLevel log_level = LogLevel::kLow)
: name_(name),
log_level_(log_level),
creation_time_(
{"timestamp",
TimestampClock::time_point(TimestampClock::duration(timestamp))}) {
}
{"timestamp", Timestamp::FromNanoseconds(timestamp_ns)}) {}

virtual ~Event() = default;

Expand Down
9 changes: 0 additions & 9 deletions layer/support/layer_utils.cc
Expand Up @@ -21,15 +21,6 @@ TimestampClock::time_point GetTimestamp() { return TimestampClock::now(); }

DurationClock::time_point Now() { return DurationClock::now(); }

int64_t ToUnixNanos(TimestampClock::time_point time) {
return std::chrono::nanoseconds(time.time_since_epoch()).count();
}

double ToUnixMillis(TimestampClock::time_point time) {
return std::chrono::duration<double, std::milli>(time.time_since_epoch())
.count();
}

FunctionInterceptor::FunctionInterceptor(
InterceptedVulkanFunc intercepted_function) {
FunctionNameToPtr& registered_functions = GetInterceptedFunctions();
Expand Down
36 changes: 30 additions & 6 deletions layer/support/layer_utils.h
Expand Up @@ -74,12 +74,6 @@ TimestampClock::time_point GetTimestamp();
// Returns a monotonic time_point to be used for measuring duration.
DurationClock::time_point Now();

// Converts a chrono time_point to a Unix int64 nanoseconds representation.
int64_t ToUnixNanos(TimestampClock::time_point time);

// Converts a chrono time_point to a Unix int64 milliseconds representation.
double ToUnixMillis(TimestampClock::time_point time);

// A wrapper around `DurationClock::duration` to keep track of the time unit.
// When the `Duration` is used, we know it's either created from a
// `DurationClock::duration` that has nanosecond-level precision or an int that
Expand All @@ -104,6 +98,36 @@ class Duration {
DurationClock::duration duration_;
};

// A wrapper around `TimestampClock::time_point` to keep track of the time unit.
// When the `Timestamp` is used, we know it's either created from a
// `GetTimestamp()` that has nanosecond-level precision or an int that
// represents duration in nanoseconds.
class Timestamp {
public:
// A `time_point` is a point in time offset to a "zero" epoch. The given input
// has no epoch information. To create a time_point from it, it should be
// converted into a duration representing the offset from the zero epoch.
static Timestamp FromNanoseconds(int64_t nanos) {
return Timestamp(
TimestampClock::time_point(TimestampClock::duration(nanos)));
}

Timestamp(TimestampClock::time_point timestamp) : timestamp_{timestamp} {}

int64_t ToNanoseconds() const {
return std::chrono::nanoseconds(timestamp_.time_since_epoch()).count();
}

double ToMilliseconds() const {
return std::chrono::duration<double, std::milli>(
timestamp_.time_since_epoch())
.count();
}

private:
TimestampClock::time_point timestamp_;
};

// Represents a type-erased layer function pointer intercepting a known
// Vulkan function. Should be constructed with the type safe |Create|
// function.
Expand Down
4 changes: 2 additions & 2 deletions layer/unittest/common_log_tests.cc
Expand Up @@ -30,7 +30,7 @@ TEST(CommonLogger, MethodCheck) {
CommonLogger logger(&out);
VectorInt64Attr hashes("hashes", {2, 3});
CreateGraphicsPipelinesEvent pipeline_event(
"create_graphics_pipeline", hashes, DurationClock::duration(4),
"create_graphics_pipeline", hashes, Duration::FromNanoseconds(4),
LogLevel::kHigh);
logger.StartLog();
logger.AddEvent(&pipeline_event);
Expand All @@ -41,7 +41,7 @@ TEST(CommonLogger, MethodCheck) {

std::stringstream event_str;
int64_t timestamp_in_ns =
ToUnixNanos(pipeline_event.GetCreationTime().GetValue());
pipeline_event.GetCreationTime().GetValue().ToNanoseconds();
event_str << "create_graphics_pipeline,timestamp:" << timestamp_in_ns
<< ",hashes:\"[0x2,0x3]\",duration:4";
EXPECT_THAT(out.GetLog(), ElementsAre(event_str.str()));
Expand Down
6 changes: 3 additions & 3 deletions layer/unittest/trace_event_log_tests.cc
Expand Up @@ -56,8 +56,8 @@ std::string ValueToJsonString(Duration value) {

// Converts the timestamp to milliseconds, the defualt time unit in the `Trace
// Event` format.
std::string ValueToJsonString(TimestampClock::time_point value) {
return std::to_string(ToUnixMillis(value));
std::string ValueToJsonString(Timestamp value) {
return std::to_string(value.ToMilliseconds());
}

// Appends all given attributes, including the type-specific attribute, to the
Expand Down Expand Up @@ -117,7 +117,7 @@ void AppendCompleteEvent(TimestampAttr timestamp,
assert(duration_attr && "Duration not found.");

double duration_ms = duration_attr->GetValue().ToMilliseconds();
double end_timestamp_ms = ToUnixMillis(timestamp.GetValue());
double end_timestamp_ms = timestamp.GetValue().ToMilliseconds();
double start_timestamp_ms = end_timestamp_ms - duration_ms;
json_stream << ", " << std::quoted("ts") << " : " << std::fixed
<< start_timestamp_ms << ", " << std::quoted("dur") << " : "
Expand Down