Author: eb Date: 2008-01-12 21:03:04 -0700 (Sat, 12 Jan 2008) New Revision: 7412
Added: usrp2/trunk/host/lib/gri_if_stats.cc usrp2/trunk/host/lib/gri_if_stats.h Modified: usrp2/trunk/host/Makefile.common usrp2/trunk/host/apps/Makefile.am usrp2/trunk/host/apps/rx_samples.cc usrp2/trunk/host/lib/Makefile.am Log: collect ethernet interface stats and compare to what we see in user space Modified: usrp2/trunk/host/Makefile.common =================================================================== --- usrp2/trunk/host/Makefile.common 2008-01-13 03:57:47 UTC (rev 7411) +++ usrp2/trunk/host/Makefile.common 2008-01-13 04:03:04 UTC (rev 7412) @@ -16,6 +16,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +AM_CXXFLAGS = @autoconf_default_CXXFLAGS@ + STD_DEFINES_AND_INCLUDES = \ -I$(top_srcdir)/../firmware/include \ -I$(top_srcdir)/lib Modified: usrp2/trunk/host/apps/Makefile.am =================================================================== --- usrp2/trunk/host/apps/Makefile.am 2008-01-13 03:57:47 UTC (rev 7411) +++ usrp2/trunk/host/apps/Makefile.am 2008-01-13 04:03:04 UTC (rev 7412) @@ -36,3 +36,4 @@ + Modified: usrp2/trunk/host/apps/rx_samples.cc =================================================================== --- usrp2/trunk/host/apps/rx_samples.cc 2008-01-13 03:57:47 UTC (rev 7411) +++ usrp2/trunk/host/apps/rx_samples.cc 2008-01-13 04:03:04 UTC (rev 7412) @@ -25,11 +25,38 @@ #include <getopt.h> #include <string.h> #include "strtod_si.h" +#include <signal.h> +#include <stdexcept> +#include "gri_if_stats.h" - typedef std::complex<float> fcomplex; +static volatile bool signaled = false; +static void +sig_handler(int sig) +{ + signaled = true; +} + +static void +install_sig_handler(int signum, + void (*new_handler)(int)) +{ + struct sigaction new_action; + memset (&new_action, 0, sizeof (new_action)); + + new_action.sa_handler = new_handler; + sigemptyset (&new_action.sa_mask); + new_action.sa_flags = 0; + + if (sigaction (signum, &new_action, 0) < 0){ + perror ("sigaction (install new)"); + throw std::runtime_error ("sigaction"); + } +} + + /* * Vectorize me! */ @@ -62,7 +89,7 @@ fprintf(stderr, " -h show this message and exit\n"); fprintf(stderr, " -e ETH_INTERFACE specify ethernet interface [default=eth0]\n"); fprintf(stderr, " -m MAC_ADDR mac address of USRP2 HH:HH [default=first one found]\n"); - fprintf(stderr, " -o OUTPUT_FILE set output filename [default=samples.dat]\n"); + fprintf(stderr, " -o OUTPUT_FILE set output filename [default=NONE]\n"); fprintf(stderr, " -f FREQ set frequency to FREQ [default=0]\n"); fprintf(stderr, " -d DECIM set decimation rate to DECIM [default=32]\n"); fprintf(stderr, " -N NSAMPLES total number of samples to receive [default=2.5e6]\n"); @@ -77,7 +104,7 @@ // options and their defaults const char *interface = "eth0"; const char *mac_addr_str = 0; - const char *output_filename = "samples.dat"; + const char *output_filename = 0; double freq = 0; int32_t decim = 32; int32_t nsamples = static_cast<int32_t>(2.5e6); @@ -154,7 +181,9 @@ exit(1); } - FILE *of = fopen(output_filename, "wb"); + FILE *of = 0; + if (output_filename) + of = fopen(output_filename, "wb"); usrp2_basic *u2 = new usrp2_basic(); @@ -163,6 +192,14 @@ return 0; } + + install_sig_handler(SIGINT, sig_handler); + if (1){ + install_sig_handler(SIGALRM, sig_handler); + alarm(5); + } + + std::vector<op_id_reply_t> r = u2->find_usrps(); for (size_t i = 0; i < r.size(); i++){ @@ -176,12 +213,20 @@ u2_mac_addr_t which = r[0].addr; // pick the first one + + gri_if_stats start, stop; + gri_get_if_stats(interface, &start); + if (!u2->start_rx(which, freq, decim, nsamples, samples_per_frame, scale, scale)){ std::cerr << "start_rx failed\n"; return 1; } - while (1){ + + long total_samples_recvd = 0; + long total_pkts_recvd = 0; + + while (!signaled && total_samples_recvd < nsamples){ u2_eth_samples_t pkt; // fcomplex c_samples[U2_MAX_SAMPLES]; @@ -190,19 +235,37 @@ if (n <= 0) break; - printf("%3d %8d\n", n, u2p_timestamp(&pkt.fixed)); + total_pkts_recvd++; + total_samples_recvd += n; + printf("%3d %8d %8ld %8ld\n", n, u2p_timestamp(&pkt.fixed), + total_pkts_recvd, total_samples_recvd); // convert_samples_to_complex(n, pkt.samples, c_samples); // size_t r = fwrite(c_samples, sizeof(fcomplex), n, of); - size_t r = fwrite(pkt.samples, sizeof(uint32_t), n, of); - fflush(of); + if (of){ + fwrite(pkt.samples, sizeof(uint32_t), n, of); + fflush(of); + } } + gri_get_if_stats(interface, &stop); + if (!u2->stop_rx(which)){ std::cerr << "stop_rx failed\n"; return 1; } + printf("\nInterface statistics:\n"); + printf(" rx_bytes: %8Ld\n", stop.rx_bytes - start.rx_bytes); + printf(" rx_packets: %8Ld\n", stop.rx_packets - start.rx_packets); + printf(" rx_errs: %8Ld\n", stop.rx_errs - start.rx_errs); + printf(" rx_drop: %8Ld\n", stop.rx_drop - start.rx_drop); + printf(" tx_bytes: %8Ld\n", stop.tx_bytes - start.tx_bytes); + printf(" tx_packets: %8Ld\n", stop.tx_packets - start.tx_packets); + printf(" tx_errs: %8Ld\n", stop.tx_errs - start.tx_errs); + printf(" tx_drop: %8Ld\n", stop.tx_drop - start.tx_drop); + + return 0; } Modified: usrp2/trunk/host/lib/Makefile.am =================================================================== --- usrp2/trunk/host/lib/Makefile.am 2008-01-13 03:57:47 UTC (rev 7411) +++ usrp2/trunk/host/lib/Makefile.am 2008-01-13 04:03:04 UTC (rev 7412) @@ -23,6 +23,7 @@ libusrp2_la_SOURCES = \ gri_ethernet.cc \ + gri_if_stats.cc \ gri_pktfilter.cc \ strtod_si.c \ usrp2_basic.cc @@ -30,6 +31,7 @@ include_HEADERS = \ gri_ethernet.h \ + gri_if_stats.h \ gri_pktfilter.h \ strtod_si.h \ usrp2_basic.h Added: usrp2/trunk/host/lib/gri_if_stats.cc =================================================================== --- usrp2/trunk/host/lib/gri_if_stats.cc (rev 0) +++ usrp2/trunk/host/lib/gri_if_stats.cc 2008-01-13 04:03:04 UTC (rev 7412) @@ -0,0 +1,76 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008 Free Software Foundation, Inc. + * + * 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/>. + */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gri_if_stats.h" +#include <stdio.h> + + +/* Very fragile. Works on my machine. C++ is way too low-level for this kind of thing */ + +static const char *stats_filename = "/proc/net/dev"; + +std::vector<gri_if_stats> +gri_get_all_if_stats() +{ + std::vector<gri_if_stats> r; + char line[1024]; + + FILE *fp = fopen(stats_filename, "r"); + if (fp == 0){ + perror(stats_filename); + return r; + } + + fgets(line, sizeof(line), fp); // read and ignore 1st line + fgets(line, sizeof(line), fp); // read and ignore 2nd line + + while (fgets(line, sizeof(line), fp) != 0){ + gri_if_stats s; + char name[16]; + const char *fmt = " %15[^:]: %Ld %Ld %Ld %Ld %*Ld %*Ld %*Ld %*Ld %Ld %Ld %Ld %Ld"; + int n = sscanf(line, fmt, + name, + &s.rx_bytes, &s.rx_packets, &s.rx_errs, &s.rx_drop, + &s.tx_bytes, &s.tx_packets, &s.tx_errs, &s.tx_drop); + name[15] = 0; + if (n != 9){ + fprintf(stderr, "gri_get_if_stats: unexpected input: n = %d\n%s\n", n, line); + continue; + } + s.if_name = name; + r.push_back(s); + } + + return r; +} + +bool +gri_get_if_stats(const std::string &if_name, gri_if_stats *s) +{ + std::vector<gri_if_stats> all = gri_get_all_if_stats(); + for (size_t i = 0; i < all.size(); i++){ + if (all[i].if_name == if_name){ + *s = all[i]; + return true; + } + } + return false; +} Property changes on: usrp2/trunk/host/lib/gri_if_stats.cc ___________________________________________________________________ Name: svn:eol-style + native Added: usrp2/trunk/host/lib/gri_if_stats.h =================================================================== --- usrp2/trunk/host/lib/gri_if_stats.h (rev 0) +++ usrp2/trunk/host/lib/gri_if_stats.h 2008-01-13 04:03:04 UTC (rev 7412) @@ -0,0 +1,46 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008 Free Software Foundation, Inc. + * + * 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/>. + */ +#ifndef INCLUDED_GRI_IF_STATS_H +#define INCLUDED_GRI_IF_STATS_H + +#include <string> +#include <vector> + +struct gri_if_stats { + std::string if_name; + + long long rx_bytes; + long long rx_packets; + long long rx_errs; + long long rx_drop; + + long long tx_bytes; + long long tx_packets; + long long tx_errs; + long long tx_drop; +}; + + +/*! + * Retrieve network interface stats (by reading /proc/net/dev) + */ +std::vector<gri_if_stats> gri_get_all_if_stats(); + +bool gri_get_if_stats(const std::string &if_name, gri_if_stats *s); + +#endif /* INCLUDED_GRI_IF_STATS_H */ Property changes on: usrp2/trunk/host/lib/gri_if_stats.h ___________________________________________________________________ Name: svn:eol-style + native _______________________________________________ Commit-gnuradio mailing list [email protected] http://lists.gnu.org/mailman/listinfo/commit-gnuradio
