Skip to content

Commit

Permalink
Implement the Timestamp class to ease the usage of time_point. (#134
Browse files Browse the repository at this point in the history
)

- Allows creating `time_point`s from int values.
- Keeps track of timestamps' time unit.
  • Loading branch information
miladHakimi committed Nov 24, 2022
1 parent f57d42d commit c562408
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 32 deletions.
4 changes: 2 additions & 2 deletions layer/support/csv_logging.cc
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit c562408

Please sign in to comment.