summaryrefslogtreecommitdiffstats
path: root/host/examples
diff options
context:
space:
mode:
Diffstat (limited to 'host/examples')
-rw-r--r--host/examples/CMakeLists.txt12
-rw-r--r--host/examples/benchmark_rx_rate.cpp143
-rw-r--r--host/examples/rx_timed_samples.cpp33
-rw-r--r--host/examples/tx_timed_samples.cpp12
4 files changed, 170 insertions, 30 deletions
diff --git a/host/examples/CMakeLists.txt b/host/examples/CMakeLists.txt
index a537c0901..5071b114f 100644
--- a/host/examples/CMakeLists.txt
+++ b/host/examples/CMakeLists.txt
@@ -15,10 +15,18 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
+ADD_EXECUTABLE(benchmark_rx_rate benchmark_rx_rate.cpp)
+TARGET_LINK_LIBRARIES(benchmark_rx_rate uhd)
+
ADD_EXECUTABLE(rx_timed_samples rx_timed_samples.cpp)
TARGET_LINK_LIBRARIES(rx_timed_samples uhd)
-INSTALL(TARGETS rx_timed_samples RUNTIME DESTINATION ${PKG_DATA_DIR}/examples)
ADD_EXECUTABLE(tx_timed_samples tx_timed_samples.cpp)
TARGET_LINK_LIBRARIES(tx_timed_samples uhd)
-INSTALL(TARGETS tx_timed_samples RUNTIME DESTINATION ${PKG_DATA_DIR}/examples)
+
+INSTALL(TARGETS
+ benchmark_rx_rate
+ rx_timed_samples
+ tx_timed_samples
+ RUNTIME DESTINATION ${PKG_DATA_DIR}/examples
+)
diff --git a/host/examples/benchmark_rx_rate.cpp b/host/examples/benchmark_rx_rate.cpp
new file mode 100644
index 000000000..a913261eb
--- /dev/null
+++ b/host/examples/benchmark_rx_rate.cpp
@@ -0,0 +1,143 @@
+//
+// Copyright 2010 Ettus Research LLC
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+
+#include <uhd/utils/thread_priority.hpp>
+#include <uhd/utils/safe_main.hpp>
+#include <uhd/usrp/simple_usrp.hpp>
+#include <boost/math/special_functions/round.hpp>
+#include <boost/program_options.hpp>
+#include <boost/format.hpp>
+#include <iostream>
+#include <complex>
+
+namespace po = boost::program_options;
+
+static inline void test_device(
+ uhd::usrp::simple_usrp::sptr sdev,
+ double rx_rate_sps,
+ double duration_secs
+){
+ uhd::device::sptr dev = sdev->get_device();
+ std::cout << boost::format("Testing receive rate %f Msps (%f second run)") % (rx_rate_sps/1e6) % duration_secs << std::endl;
+
+ //allocate recv buffer and metatdata
+ uhd::rx_metadata_t md;
+ std::vector<std::complex<float> > buff(dev->get_max_recv_samps_per_packet());
+
+ //declare status variables
+ bool got_first_packet = false;
+ size_t total_recv_packets = 0;
+ size_t total_lost_samples = 0;
+ size_t total_recv_samples = 0;
+ uhd::time_spec_t initial_time_spec;
+ uhd::time_spec_t next_expected_time_spec;
+
+ sdev->issue_stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
+ do {
+ size_t num_rx_samps = dev->recv(
+ &buff.front(), buff.size(), md,
+ uhd::io_type_t::COMPLEX_FLOAT32,
+ uhd::device::RECV_MODE_ONE_PACKET
+ );
+ if (num_rx_samps == 0){
+ std::cerr << "Unexpected timeout on recv, exit test..." << std::endl;
+ return;
+ }
+ if (not md.has_time_spec){
+ std::cerr << "Metadata missing time spec, exit test..." << std::endl;
+ return;
+ }
+
+ total_recv_samples += num_rx_samps;
+ total_recv_packets++;
+
+ if (not got_first_packet){
+ initial_time_spec = md.time_spec;
+ next_expected_time_spec = initial_time_spec;
+ got_first_packet = true;
+ }
+
+ total_lost_samples += boost::math::iround(rx_rate_sps*(md.time_spec - next_expected_time_spec).get_real_secs());
+ next_expected_time_spec = md.time_spec + uhd::time_spec_t(0, num_rx_samps, rx_rate_sps);
+
+ } while((next_expected_time_spec - initial_time_spec) < uhd::time_spec_t(duration_secs));
+ sdev->issue_stream_cmd(uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS);
+
+ //flush the buffers
+ while(dev->recv(
+ &buff.front(), buff.size(), md,
+ uhd::io_type_t::COMPLEX_FLOAT32,
+ uhd::device::RECV_MODE_ONE_PACKET
+ )){
+ /* NOP */
+ };
+
+ //print a summary
+ std::cout << std::endl; //go to newline, recv may spew SXSYSZ...
+ std::cout << boost::format(" Received packets: %d") % total_recv_packets << std::endl;
+ std::cout << boost::format(" Received samples: %d") % total_recv_samples << std::endl;
+ std::cout << boost::format(" Lost samples: %d") % total_lost_samples << std::endl;
+ size_t packets_lost = boost::math::iround(double(total_lost_samples)/dev->get_max_recv_samps_per_packet());
+ std::cout << boost::format(" Lost packets: %d (approximate)") % packets_lost << std::endl;
+ double actual_rx_rate_sps = (total_recv_samples*rx_rate_sps)/(total_recv_samples+total_lost_samples);
+ std::cout << boost::format(" Sustained receive rate: %f Msps") % (actual_rx_rate_sps/1e6) << std::endl;
+ std::cout << std::endl << std::endl;
+}
+
+int UHD_SAFE_MAIN(int argc, char *argv[]){
+ uhd::set_thread_priority_safe();
+
+ //variables to be set by po
+ std::string args;
+ double duration;
+
+ //setup the program options
+ po::options_description desc("Allowed options");
+ desc.add_options()
+ ("help", "help message")
+ ("args", po::value<std::string>(&args)->default_value(""), "simple uhd device address args")
+ ("duration", po::value<double>(&duration)->default_value(10.0), "duration for each test in seconds")
+ ;
+ po::variables_map vm;
+ po::store(po::parse_command_line(argc, argv, desc), vm);
+ po::notify(vm);
+
+ //print the help message
+ if (vm.count("help")){
+ std::cout << boost::format("UHD Benchmark RX Rate %s") % desc << std::endl;
+ return ~0;
+ }
+
+ //create a usrp device
+ std::cout << std::endl;
+ std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
+ uhd::usrp::simple_usrp::sptr sdev = uhd::usrp::simple_usrp::make(args);
+ std::cout << boost::format("Using Device: %s") % sdev->get_pp_string() << std::endl;
+
+ sdev->set_rx_rate(500e3); //initial rate
+ while(true){
+ double rate = sdev->get_rx_rate();
+ test_device(sdev, rate, duration);
+ sdev->set_rx_rate(rate*2); //double the rate
+ if (sdev->get_rx_rate() == rate) break;
+ }
+
+ //finished
+ std::cout << std::endl << "Done!" << std::endl << std::endl;
+
+ return 0;
+}
diff --git a/host/examples/rx_timed_samples.cpp b/host/examples/rx_timed_samples.cpp
index b4aa7b9e8..9ff8772bc 100644
--- a/host/examples/rx_timed_samples.cpp
+++ b/host/examples/rx_timed_samples.cpp
@@ -21,7 +21,6 @@
#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include <iostream>
-#include <fstream>
#include <complex>
namespace po = boost::program_options;
@@ -30,8 +29,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
uhd::set_thread_priority_safe();
//variables to be set by po
- std::string args, file_path;
- int seconds_in_future;
+ std::string args;
+ time_t seconds_in_future;
size_t total_num_samps;
double rx_rate, freq;
@@ -40,11 +39,10 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
desc.add_options()
("help", "help message")
("args", po::value<std::string>(&args)->default_value(""), "simple uhd device address args")
- ("secs", po::value<int>(&seconds_in_future)->default_value(3), "number of seconds in the future to receive")
+ ("secs", po::value<time_t>(&seconds_in_future)->default_value(3), "number of seconds in the future to receive")
("nsamps", po::value<size_t>(&total_num_samps)->default_value(1000), "total number of samples to receive")
("rxrate", po::value<double>(&rx_rate)->default_value(100e6/16), "rate of incoming samples")
("freq", po::value<double>(&freq)->default_value(0), "rf center frequency in Hz")
- ("file", po::value<std::string>(&file_path)->default_value(""), "write samps to output file")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
@@ -61,7 +59,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
uhd::usrp::simple_usrp::sptr sdev = uhd::usrp::simple_usrp::make(args);
uhd::device::sptr dev = sdev->get_device();
- std::cout << boost::format("Using Device: %s") % sdev->get_name() << std::endl;
+ std::cout << boost::format("Using Device: %s") % sdev->get_pp_string() << std::endl;
//set properties on the device
std::cout << boost::format("Setting RX Rate: %f Msps...") % (rx_rate/1e6) << std::endl;
@@ -69,7 +67,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
std::cout << boost::format("Actual RX Rate: %f Msps...") % (sdev->get_rx_rate()/1e6) << std::endl;
std::cout << boost::format("Setting device timestamp to 0...") << std::endl;
sdev->set_rx_freq(freq);
- sdev->set_time_now(uhd::time_spec_t(0));
+ sdev->set_time_now(uhd::time_spec_t(0.0));
//setup streaming
std::cout << std::endl;
@@ -81,20 +79,14 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
stream_cmd.time_spec = uhd::time_spec_t(seconds_in_future);
sdev->issue_stream_cmd(stream_cmd);
- std::ofstream outfile;
- if(file_path != "") outfile.open (file_path.c_str(), std::ios::out | std::ios::binary);
-
- //setup recv buffer, io type, and metadata for recv
- uhd::rx_metadata_t md;
- uhd::io_type_t io_type(uhd::io_type_t::COMPLEX_FLOAT32);
- std::vector<std::complex<float> > recv_mem(dev->get_max_recv_samps_per_packet());
- boost::asio::mutable_buffer buff(boost::asio::buffer(recv_mem));
-
//loop until total number of samples reached
size_t num_acc_samps = 0; //number of accumulated samples
while(num_acc_samps < total_num_samps){
+ uhd::rx_metadata_t md;
+ std::vector<std::complex<float> > buff(dev->get_max_recv_samps_per_packet());
size_t num_rx_samps = dev->recv(
- buff, md, io_type,
+ &buff.front(), buff.size(), md,
+ uhd::io_type_t::COMPLEX_FLOAT32,
uhd::device::RECV_MODE_ONE_PACKET
);
if (num_rx_samps == 0 and num_acc_samps > 0){
@@ -103,17 +95,14 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
}
if (num_rx_samps == 0) continue; //wait for packets with contents
- std::cout << boost::format("Got packet: %u samples, %u secs, %u nsecs")
- % num_rx_samps % md.time_spec.secs % md.time_spec.nsecs << std::endl;
-
- if(file_path != "") outfile.write(boost::asio::buffer_cast<const char *>(buff), num_rx_samps*io_type.size);
+ std::cout << boost::format("Got packet: %u samples, %u full secs, %f frac secs")
+ % num_rx_samps % md.time_spec.get_full_secs() % md.time_spec.get_frac_secs() << std::endl;
num_acc_samps += num_rx_samps;
}
//finished
std::cout << std::endl << "Done!" << std::endl << std::endl;
- if(file_path != "") outfile.close();
return 0;
}
diff --git a/host/examples/tx_timed_samples.cpp b/host/examples/tx_timed_samples.cpp
index 28fd2ee67..4226aa4c8 100644
--- a/host/examples/tx_timed_samples.cpp
+++ b/host/examples/tx_timed_samples.cpp
@@ -30,7 +30,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
//variables to be set by po
std::string args;
- int seconds_in_future;
+ time_t seconds_in_future;
size_t total_num_samps;
double tx_rate, freq;
float ampl;
@@ -40,7 +40,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
desc.add_options()
("help", "help message")
("args", po::value<std::string>(&args)->default_value(""), "simple uhd device address args")
- ("secs", po::value<int>(&seconds_in_future)->default_value(3), "number of seconds in the future to transmit")
+ ("secs", po::value<time_t>(&seconds_in_future)->default_value(3), "number of seconds in the future to transmit")
("nsamps", po::value<size_t>(&total_num_samps)->default_value(1000), "total number of samples to transmit")
("txrate", po::value<double>(&tx_rate)->default_value(100e6/16), "rate of outgoing samples")
("freq", po::value<double>(&freq)->default_value(0), "rf center frequency in Hz")
@@ -61,7 +61,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
uhd::usrp::simple_usrp::sptr sdev = uhd::usrp::simple_usrp::make(args);
uhd::device::sptr dev = sdev->get_device();
- std::cout << boost::format("Using Device: %s") % sdev->get_name() << std::endl;
+ std::cout << boost::format("Using Device: %s") % sdev->get_pp_string() << std::endl;
//set properties on the device
std::cout << boost::format("Setting TX Rate: %f Msps...") % (tx_rate/1e6) << std::endl;
@@ -69,7 +69,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
std::cout << boost::format("Actual TX Rate: %f Msps...") % (sdev->get_tx_rate()/1e6) << std::endl;
std::cout << boost::format("Setting device timestamp to 0...") << std::endl;
sdev->set_tx_freq(freq);
- sdev->set_time_now(uhd::time_spec_t(0));
+ sdev->set_time_now(uhd::time_spec_t(0.0));
//data to send
std::vector<std::complex<float> > buff(total_num_samps, std::complex<float>(ampl, ampl));
@@ -81,8 +81,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
//send the entire buffer, let the driver handle fragmentation
size_t num_tx_samps = dev->send(
- boost::asio::buffer(buff),
- md, uhd::io_type_t::COMPLEX_FLOAT32,
+ &buff.front(), buff.size(), md,
+ uhd::io_type_t::COMPLEX_FLOAT32,
uhd::device::SEND_MODE_FULL_BUFF
);
std::cout << std::endl << boost::format("Sent %d samples") % num_tx_samps << std::endl;