3232
3333#include " rogue/utilities/fileio/StreamWriter.h"
3434
35+ #include < chrono>
3536#include < fcntl.h>
3637#include < inttypes.h>
3738#include < stdint.h>
@@ -79,6 +80,7 @@ void ruf::StreamWriter::setup_python() {
7980 .def (" getChannel" , &ruf::StreamWriter::getChannel)
8081 .def (" getTotalSize" , &ruf::StreamWriter::getTotalSize)
8182 .def (" getCurrentSize" , &ruf::StreamWriter::getCurrentSize)
83+ .def (" getBandwidth" , &ruf::StreamWriter::getBandwidth)
8284 .def (" getFrameCount" , &ruf::StreamWriter::getFrameCount)
8385 .def (" waitFrameCount" , &ruf::StreamWriter::waitFrameCount);
8486#endif
@@ -94,6 +96,7 @@ ruf::StreamWriter::StreamWriter() {
9496 totSize_ = 0 ;
9597 buffer_ = NULL ;
9698 frameCount_ = 0 ;
99+ bandwidthBytes_ = 0 ;
97100 currBuffer_ = 0 ;
98101 dropErrors_ = false ;
99102 isOpen_ = false ;
@@ -134,6 +137,8 @@ void ruf::StreamWriter::open(std::string file) {
134137 totSize_ = 0 ;
135138 currSize_ = 0 ;
136139 frameCount_ = 0 ;
140+ bandwidthBytes_ = 0 ;
141+ bandwidthHistory_.clear ();
137142 currBuffer_ = 0 ;
138143
139144 // Iterate over all channels and reset their frame counts
@@ -150,6 +155,8 @@ void ruf::StreamWriter::close() {
150155 std::lock_guard<std::mutex> lock (mtx_);
151156 isOpen_ = false ;
152157 flush ();
158+ bandwidthBytes_ = 0 ;
159+ bandwidthHistory_.clear ();
153160 if (fd_ >= 0 ) ::close (fd_);
154161 fd_ = -1 ;
155162}
@@ -231,6 +238,22 @@ uint64_t ruf::StreamWriter::getCurrentSize() {
231238 return (currSize_ + currBuffer_);
232239}
233240
241+ // ! Get instantaneous bandwidth in bytes per second over the last second
242+ double ruf::StreamWriter::getBandwidth () {
243+ rogue::GilRelease noGil;
244+ std::lock_guard<std::mutex> lock (mtx_);
245+
246+ auto now = std::chrono::steady_clock::now ();
247+ pruneBandwidth (now);
248+
249+ if (bandwidthHistory_.empty ()) return (0.0 );
250+
251+ auto windowStart = bandwidthHistory_.front ().first ;
252+ double seconds = std::chrono::duration<double >(now - windowStart).count ();
253+ if (seconds <= 0.0 ) return (static_cast <double >(bandwidthBytes_));
254+ return (static_cast <double >(bandwidthBytes_) / seconds);
255+ }
256+
234257// ! Get current frame count
235258uint32_t ruf::StreamWriter::getFrameCount () {
236259 return (frameCount_);
@@ -266,6 +289,22 @@ bool ruf::StreamWriter::waitFrameCount(uint32_t count, uint64_t timeout) {
266289 return (frameCount_ >= count);
267290}
268291
292+ void ruf::StreamWriter::pruneBandwidth (std::chrono::steady_clock::time_point now) {
293+ auto cutoff = now - std::chrono::seconds (1 );
294+ while (!bandwidthHistory_.empty () && bandwidthHistory_.front ().first < cutoff) {
295+ bandwidthBytes_ -= bandwidthHistory_.front ().second ;
296+ bandwidthHistory_.pop_front ();
297+ }
298+ }
299+
300+ void ruf::StreamWriter::recordBandwidth (uint32_t size) {
301+ if (size == 0 ) return ;
302+ auto now = std::chrono::steady_clock::now ();
303+ pruneBandwidth (now);
304+ bandwidthHistory_.emplace_back (now, size);
305+ bandwidthBytes_ += size;
306+ }
307+
269308// ! Write data to file. Called from StreamWriterChannel
270309void ruf::StreamWriter::writeFile (uint8_t channel, std::shared_ptr<rogue::interfaces::stream::Frame> frame) {
271310 ris::Frame::BufferIterator it;
@@ -333,6 +372,8 @@ void ruf::StreamWriter::intWrite(void* data, uint32_t size) {
333372 std::memcpy (buffer_ + currBuffer_, data, size);
334373 currBuffer_ += size;
335374 }
375+
376+ recordBandwidth (size);
336377}
337378
338379// ! Check file size for next write
0 commit comments