void SummaryCommand :: RecordSizes( const CSVRow & row, SizeMap & sm ) { for ( unsigned int i = 0; i < row.size(); i++ ) { int sz = row[i].size(); SizeMap::iterator pos = sm.find( i ); if ( pos == sm.end() ) { sm[ i ] = std::make_pair( INT_MAX, 0 ); } sm[i].first = std::min( sm[i].first, sz ); sm[i].second = std::max( sm[i].second, sz ); } }
template<typename samp_type> void recv_to_file( uhd::usrp::multi_usrp::sptr usrp, const std::string &cpu_format, const std::string &wire_format, const std::string &file, size_t samps_per_buff, unsigned long long num_requested_samples, double time_requested = 0.0, bool bw_summary = false, bool stats = false, bool null = false, bool enable_size_map = false, bool continue_on_bad_packet = false ){ unsigned long long num_total_samps = 0; //create a receive streamer uhd::stream_args_t stream_args(cpu_format,wire_format); uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args); uhd::rx_metadata_t md; std::vector<samp_type> buff(samps_per_buff); std::ofstream outfile; if (not null) outfile.open(file.c_str(), std::ofstream::binary); bool overflow_message = true; //setup streaming uhd::stream_cmd_t stream_cmd((num_requested_samples == 0)? uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS: uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE ); stream_cmd.num_samps = num_requested_samples; stream_cmd.stream_now = true; stream_cmd.time_spec = uhd::time_spec_t(); rx_stream->issue_stream_cmd(stream_cmd); boost::system_time start = boost::get_system_time(); unsigned long long ticks_requested = (long)(time_requested * (double)boost::posix_time::time_duration::ticks_per_second()); boost::posix_time::time_duration ticks_diff; boost::system_time last_update = start; unsigned long long last_update_samps = 0; typedef std::map<size_t,size_t> SizeMap; SizeMap mapSizes; while(not stop_signal_called and (num_requested_samples != num_total_samps or num_requested_samples == 0)) { boost::system_time now = boost::get_system_time(); size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md, 3.0, enable_size_map); if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) { std::cout << boost::format("Timeout while streaming") << std::endl; break; } if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW){ if (overflow_message) { overflow_message = false; std::cerr << boost::format( "Got an overflow indication. Please consider the following:\n" " Your write medium must sustain a rate of %fMB/s.\n" " Dropped samples will not be written to the file.\n" " Please modify this example for your purposes.\n" " This message will not appear again.\n" ) % (usrp->get_rx_rate()*sizeof(samp_type)/1e6); } continue; } if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){ std::string error = str(boost::format("Receiver error: %s") % md.strerror()); if (continue_on_bad_packet){ std::cerr << error << std::endl; continue; } else throw std::runtime_error(error); } if (enable_size_map) { SizeMap::iterator it = mapSizes.find(num_rx_samps); if (it == mapSizes.end()) mapSizes[num_rx_samps] = 0; mapSizes[num_rx_samps] += 1; } num_total_samps += num_rx_samps; if (outfile.is_open()) outfile.write((const char*)&buff.front(), num_rx_samps*sizeof(samp_type)); if (bw_summary) { last_update_samps += num_rx_samps; boost::posix_time::time_duration update_diff = now - last_update; if (update_diff.ticks() > boost::posix_time::time_duration::ticks_per_second()) { double t = (double)update_diff.ticks() / (double)boost::posix_time::time_duration::ticks_per_second(); double r = (double)last_update_samps / t; std::cout << boost::format("\t%f Msps") % (r/1e6) << std::endl; last_update_samps = 0; last_update = now; } } ticks_diff = now - start; if (ticks_requested > 0){ if ((unsigned long long)ticks_diff.ticks() > ticks_requested) break; } } stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS; rx_stream->issue_stream_cmd(stream_cmd); if (outfile.is_open()) outfile.close(); if (stats) { std::cout << std::endl; double t = (double)ticks_diff.ticks() / (double)boost::posix_time::time_duration::ticks_per_second(); std::cout << boost::format("Received %d samples in %f seconds") % num_total_samps % t << std::endl; double r = (double)num_total_samps / t; std::cout << boost::format("%f Msps") % (r/1e6) << std::endl; if (enable_size_map) { std::cout << std::endl; std::cout << "Packet size map (bytes: count)" << std::endl; for (SizeMap::iterator it = mapSizes.begin(); it != mapSizes.end(); it++) std::cout << it->first << ":\t" << it->second << std::endl; } } }
void recv_to_file(uhd::rx_streamer::sptr rx_stream, const std::string& file, const size_t samps_per_buff, const double rx_rate, const unsigned long long num_requested_samples, double time_requested = 0.0, bool bw_summary = false, bool stats = false, bool enable_size_map = false, bool continue_on_bad_packet = false) { unsigned long long num_total_samps = 0; uhd::rx_metadata_t md; std::vector<samp_type> buff(samps_per_buff); std::ofstream outfile; if (not file.empty()) { outfile.open(file.c_str(), std::ofstream::binary); } bool overflow_message = true; // setup streaming uhd::stream_cmd_t stream_cmd((num_requested_samples == 0) ? uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS : uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE); stream_cmd.num_samps = size_t(num_requested_samples); stream_cmd.stream_now = true; stream_cmd.time_spec = uhd::time_spec_t(); std::cout << "Issuing stream cmd" << std::endl; rx_stream->issue_stream_cmd(stream_cmd); const auto start_time = std::chrono::steady_clock::now(); const auto stop_time = start_time + std::chrono::milliseconds(int64_t(1000 * time_requested)); // Track time and samps between updating the BW summary auto last_update = start_time; unsigned long long last_update_samps = 0; typedef std::map<size_t, size_t> SizeMap; SizeMap mapSizes; // Run this loop until either time expired (if a duration was given), until // the requested number of samples were collected (if such a number was // given), or until Ctrl-C was pressed. while (not stop_signal_called and (num_requested_samples != num_total_samps or num_requested_samples == 0) and (time_requested == 0.0 or std::chrono::steady_clock::now() <= stop_time)) { const auto now = std::chrono::steady_clock::now(); size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md, 3.0, enable_size_map); if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) { std::cout << boost::format("Timeout while streaming") << std::endl; break; } if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW) { if (overflow_message) { overflow_message = false; std::cerr << boost::format( "Got an overflow indication. Please consider the following:\n" " Your write medium must sustain a rate of %fMB/s.\n" " Dropped samples will not be written to the file.\n" " Please modify this example for your purposes.\n" " This message will not appear again.\n") % (rx_rate * sizeof(samp_type) / 1e6); } continue; } if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE) { std::string error = str(boost::format("Receiver error: %s") % md.strerror()); if (continue_on_bad_packet) { std::cerr << error << std::endl; continue; } else throw std::runtime_error(error); } if (enable_size_map) { SizeMap::iterator it = mapSizes.find(num_rx_samps); if (it == mapSizes.end()) mapSizes[num_rx_samps] = 0; mapSizes[num_rx_samps] += 1; } num_total_samps += num_rx_samps; if (outfile.is_open()) { outfile.write((const char*)&buff.front(), num_rx_samps * sizeof(samp_type)); } if (bw_summary) { last_update_samps += num_rx_samps; const auto time_since_last_update = now - last_update; if (time_since_last_update > std::chrono::seconds(UPDATE_INTERVAL)) { const double time_since_last_update_s = std::chrono::duration<double>(time_since_last_update).count(); const double rate = double(last_update_samps) / time_since_last_update_s; std::cout << "\t" << (rate / 1e6) << " MSps" << std::endl; last_update_samps = 0; last_update = now; } } } const auto actual_stop_time = std::chrono::steady_clock::now(); stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS; std::cout << "Issuing stop stream cmd" << std::endl; rx_stream->issue_stream_cmd(stream_cmd); // Run recv until nothing is left int num_post_samps = 0; do { num_post_samps = rx_stream->recv(&buff.front(), buff.size(), md, 3.0); } while (num_post_samps and md.error_code == uhd::rx_metadata_t::ERROR_CODE_NONE); if (outfile.is_open()) outfile.close(); if (stats) { std::cout << std::endl; const double actual_duration_seconds = std::chrono::duration<float>(actual_stop_time - start_time).count(); std::cout << boost::format("Received %d samples in %f seconds") % num_total_samps % actual_duration_seconds << std::endl; const double rate = (double)num_total_samps / actual_duration_seconds; std::cout << (rate / 1e6) << " MSps" << std::endl; if (enable_size_map) { std::cout << std::endl; std::cout << "Packet size map (bytes: count)" << std::endl; for (SizeMap::iterator it = mapSizes.begin(); it != mapSizes.end(); it++) std::cout << it->first << ":\t" << it->second << std::endl; } } }