diff --git a/server/driver/video_encoder.cpp b/server/driver/video_encoder.cpp index c8fb5781..989f2307 100644 --- a/server/driver/video_encoder.cpp +++ b/server/driver/video_encoder.cpp @@ -86,9 +86,11 @@ void VideoEncoder::Encode(wivrn_session & cnx, this->cnx = &cnx; this->frame_idx = frame_index; auto target_timestamp = std::chrono::steady_clock::time_point(std::chrono::nanoseconds(view_info.display_time)); + cnx.dump_time("encode_begin", frame_index, os_monotonic_get_ns(), stream_idx); shards.clear(); Encode(index, idr, target_timestamp); + cnx.dump_time("encode_end", frame_index, os_monotonic_get_ns(), stream_idx); if (shards.empty()) return; const size_t view_info_size = sizeof(to_headset::video_stream_data_shard::view_info_t); @@ -154,6 +156,7 @@ void VideoEncoder::Encode(wivrn_session & cnx, { U_LOG_W("failed to setup reed_solomon encoder with %ld data shards", data_shards.size()); } + cnx.dump_time("rs_end", frame_index, os_monotonic_get_ns(), stream_idx); } void VideoEncoder::SendData(std::vector && data) @@ -185,6 +188,8 @@ void VideoEncoder::PushShard(std::vector && payload, uint8_t flags) if (not shards.empty()) { cnx->send_stream(shards.back()); + } else { + cnx->dump_time("send_start", frame_idx, os_monotonic_get_ns(), stream_idx); } to_headset::video_stream_data_shard shard; shard.stream_item_idx = stream_idx; diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index abeac6f5..e70d1d04 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -679,12 +679,15 @@ static void comp_wivrn_mark_timing_point(struct comp_target * ct, switch (point) { case COMP_TARGET_TIMING_POINT_WAKE_UP: + cn->cnx->dump_time("wake_up", cn->frame_index + 1, when_ns); u_pc_mark_point(cn->upc, U_TIMING_POINT_WAKE_UP, cn->current_frame_id, when_ns); break; case COMP_TARGET_TIMING_POINT_BEGIN: + cn->cnx->dump_time("begin", cn->frame_index + 1, when_ns); u_pc_mark_point(cn->upc, U_TIMING_POINT_BEGIN, cn->current_frame_id, when_ns); break; case COMP_TARGET_TIMING_POINT_SUBMIT: + cn->cnx->dump_time("submit", cn->frame_index + 1, when_ns); u_pc_mark_point(cn->upc, U_TIMING_POINT_SUBMIT, cn->current_frame_id, when_ns); break; default: diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index b5facfe8..bdd7a7d2 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -72,10 +72,11 @@ xrt_system_devices * xrt::drivers::wivrn::wivrn_session::create_session(xrt::dri // TODO }; -#if 0 - // For debugging purposes - self->feedback_csv.open("feedback.csv"); -#endif + auto dump_file = std::getenv("WIVRN_DUMP_TIMINGS"); + if (dump_file) + { + self->feedback_csv.open(dump_file); + } self->thread = std::thread(&wivrn_session::run, self); return devices; @@ -116,36 +117,27 @@ void wivrn_session::operator()(from_headset::timesync_response && timesync) void wivrn_session::operator()(from_headset::feedback && feedback) { - if (feedback_csv) + + clock_offset o; { - feedback_csv << feedback.frame_index << "," << feedback.received_first_packet << "," - << feedback.received_last_packet << "," << feedback.reconstructed << "," - << feedback.sent_to_decoder << "," << feedback.received_from_decoder << "," - << feedback.blitted << "," << feedback.displayed << ","; - - XrQuaternionf q; - XrVector3f x; - - q = feedback.received_pose[0].orientation; - feedback_csv << q.x << "," << q.y << "," << q.z << "," << q.w << ","; - x = feedback.received_pose[0].position; - feedback_csv << x.x << "," << x.y << "," << x.z << ","; - - q = feedback.received_pose[1].orientation; - feedback_csv << q.x << "," << q.y << "," << q.z << "," << q.w << ","; - x = feedback.received_pose[1].position; - feedback_csv << x.x << "," << x.y << "," << x.z << ","; - - q = feedback.real_pose[0].orientation; - feedback_csv << q.x << "," << q.y << "," << q.z << "," << q.w << ","; - x = feedback.real_pose[0].position; - feedback_csv << x.x << "," << x.y << "," << x.z << ","; - - q = feedback.real_pose[1].orientation; - feedback_csv << q.x << "," << q.y << "," << q.z << "," << q.w << ","; - x = feedback.real_pose[1].position; - feedback_csv << x.x << "," << x.y << "," << x.z << std::endl; + std::lock_guard lock(mutex); + o = offset; } + + if (feedback.received_first_packet) + dump_time("receive_start", feedback.frame_index, o.from_headset(feedback.received_first_packet), feedback.stream_index); + if (feedback.received_last_packet) + dump_time("receive_end", feedback.frame_index, o.from_headset(feedback.received_last_packet), feedback.stream_index); + if (feedback.reconstructed) + dump_time("reconstructed", feedback.frame_index, o.from_headset(feedback.reconstructed), feedback.stream_index); + if (feedback.sent_to_decoder) + dump_time("decode_start", feedback.frame_index, o.from_headset(feedback.sent_to_decoder), feedback.stream_index); + if (feedback.received_from_decoder) + dump_time("decode_end", feedback.frame_index, o.from_headset(feedback.received_from_decoder), feedback.stream_index); + if (feedback.blitted) + dump_time("blit", feedback.frame_index, o.from_headset(feedback.blitted), feedback.stream_index); + if (feedback.displayed) + dump_time("display", feedback.frame_index, o.from_headset(feedback.displayed), feedback.stream_index); } uint64_t clock_offset::from_headset(uint64_t ts) const @@ -195,3 +187,12 @@ void wivrn_session::run(std::weak_ptr weak_self) } } } + +void wivrn_session::dump_time(const std::string &event, uint64_t frame, uint64_t time, uint8_t stream) +{ + if (feedback_csv) + { + std::lock_guard lock(csv_mutex); + feedback_csv << std::quoted(event) << "," << frame << "," << time << "," << (int)stream << std::endl; + } +} diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 637ede46..8a40c1eb 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -59,6 +59,7 @@ class wivrn_session : public std::enable_shared_from_this clock_offset offset; std::chrono::steady_clock::time_point offset_age{}; + std::mutex csv_mutex; std::ofstream feedback_csv; wivrn_session(TCP && tcp, in6_addr & address); @@ -88,6 +89,8 @@ public: connection.send_control(packet); } + void dump_time(const std::string& event, uint64_t frame, uint64_t time, uint8_t stream = -1); + private: static void run(std::weak_ptr); }; diff --git a/tools/process_timings.py b/tools/process_timings.py new file mode 100755 index 00000000..d11e004d --- /dev/null +++ b/tools/process_timings.py @@ -0,0 +1,165 @@ +#!/usr/bin/env python3 + +import csv +import argparse + +PIXEL_SCALE = 10/1_000_000 # 10px/ms +LINE_HEIGHT = 10 + +COLOURS = ["#ea5545", "#f46a9b", "#ef9b20", "#edbf33", "#ede15b", "#bdcf32", "#87bc45", "#27aeef", "#b33dc6"] + +class Frame: + def __init__(self, num): + self.num = num + self.events = dict() + self.streams = dict() + + def set(self, event, timestamp, stream): + if stream == 255: + self.events[event] = timestamp + else: + if stream not in self.streams: + self.streams[stream] = dict() + self.streams[stream][event] = timestamp + + + def draw(self, out): + out.write("\n".format(self.num)) + if "wake_up" in self.events and "submit" in self.events: + wake_up = self.events["wake_up"] + submit = self.events["submit"] + out.write('\n'.format( + x = wake_up * PIXEL_SCALE, + w = (submit - wake_up) * PIXEL_SCALE, + y = 0, + h = LINE_HEIGHT, + fill = COLOURS[0] + )) + if "begin" in self.events: + begin = self.events["begin"] + out.write('\n'.format( + x = begin * PIXEL_SCALE, + y = 0, + y2 = LINE_HEIGHT + )) + + for stream, events in self.streams.items(): + y_offset = LINE_HEIGHT * (1 + stream * 2) + if "encode_begin" in events and "rs_end" in events: + begin = events["encode_begin"] + end = events["encode_end"] + send_start = events["send_start"] + rs_end = events["rs_end"] + out.write('\n'.format( + x = begin * PIXEL_SCALE, + y = y_offset, + h = LINE_HEIGHT, + w = (end - begin) * PIXEL_SCALE, + fill = COLOURS[1] + )) + out.write('\n'.format( + x = send_start * PIXEL_SCALE, + y = y_offset + 0.2 * LINE_HEIGHT, + h = 0.6 * LINE_HEIGHT, + w = (rs_end - send_start) * PIXEL_SCALE, + fill = COLOURS[2] + )) + + y_offset = LINE_HEIGHT * (2 + stream * 2) + + if "receive_start" in events and "receive_end" in events: + begin = events["receive_start"] + end = events["receive_end"] + out.write('\n'.format( + x = begin * PIXEL_SCALE, + y = y_offset, + h = LINE_HEIGHT, + w = (end - begin) * PIXEL_SCALE, + fill = COLOURS[3] + )) + + if "reconstructed" in events: + t = events["reconstructed"] + out.write('\n'.format( + x = t * PIXEL_SCALE, + y = y_offset, + y2 = y_offset + LINE_HEIGHT + )) + + if "decode_start" in events and "decode_end" in events: + begin = events["decode_start"] + end = events["decode_end"] + out.write('\n'.format( + x = begin * PIXEL_SCALE, + y = y_offset, + h = LINE_HEIGHT, + w = (end - begin) * PIXEL_SCALE, + fill = COLOURS[4] + )) + + if "blit" in events: + t = events["blit"] + out.write('\n'.format( + x = t * PIXEL_SCALE, + y = y_offset, + y2 = y_offset + LINE_HEIGHT, + stroke = COLOURS[0] + )) + + if "display" in events: + t = events["display"] + out.write('\n'.format( + x = t * PIXEL_SCALE, + y = y_offset, + y2 = y_offset + LINE_HEIGHT, + stroke = COLOURS[1] + )) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser("Parser for WiVRn timing dumps") + parser.add_argument("CSV", type=argparse.FileType("r")) + parser.add_argument("--out", type=argparse.FileType("w"), default="-") + parser.add_argument("--skip", type=int, default=0) + parser.add_argument("--duration_ms", type=int, default=100) + + args = parser.parse_args() + + timings = csv.reader(args.CSV) + + origin = None + frames = [] + for event, frame, timestamp, stream in timings: + frame = int(frame) + + if frame < args.skip: + continue + timestamp = int(timestamp) + stream = int(stream) + + if origin is None: + origin = timestamp + + if not timestamp: + continue + + timestamp -= origin + + if timestamp > args.duration_ms * 1_000_000: + continue + + while len(frames) < frame + 1: + frames.append(Frame(len(frames))) + + frames[frame].set(event, timestamp, stream) + + out = args.out + out.write(''' + + +''') + + for frame in reversed(frames): + frame.draw(out) + + out.write("")