ping: print short stats summary on SIGQUIT

refs #2782

Change-Id: If0b85dabc7fdee6ea5ab19297122e7f322d55f22
This commit is contained in:
Eric Newberry authored and Junxiao Shi committed 2015-05-07 10:54:52 -07:00
1 parent 6a19af80a2
commit dce5a5386e
3 files changed
+33 -4

No files matched your search

+20 -4
View File
@@ -55,12 +55,15 @@ usage(const boost::program_options::options_description& options)
exit(2);
}
/**
* @brief SIGINT handler: print statistics and exit
*/
static void
signalHandler(Face& face, StatisticsCollector& statisticsCollector)
onSigInt(Face& face, StatisticsCollector& statisticsCollector)
{
face.shutdown();
Statistics statistics = statisticsCollector.computeStatistics();
std::cout << statistics << "\n";
std::cout << statistics << std::endl;
if (statistics.nReceived == statistics.nSent) {
exit(0);
@@ -70,6 +73,16 @@ signalHandler(Face& face, StatisticsCollector& statisticsCollector)
}
}
/**
* @brief SIGQUIT handler: print statistics summary and continue
*/
static void
onSigQuit(StatisticsCollector& statisticsCollector, boost::asio::signal_set& signalSet)
{
statisticsCollector.computeStatistics().printSummary(std::cout);
signalSet.async_wait(bind(&onSigQuit, ref(statisticsCollector), ref(signalSet)));
}
int
main(int argc, char* argv[])
{
@@ -191,8 +204,11 @@ main(int argc, char* argv[])
StatisticsCollector statisticsCollector(ping, options);
Tracer tracer(ping, options);
boost::asio::signal_set signalSet(face.getIoService(), SIGINT);
signalSet.async_wait(bind(&signalHandler, ref(face), ref(statisticsCollector)));
boost::asio::signal_set signalSetInt(face.getIoService(), SIGINT);
signalSetInt.async_wait(bind(&onSigInt, ref(face), ref(statisticsCollector)));
boost::asio::signal_set signalSetQuit(face.getIoService(), SIGQUIT);
signalSetQuit.async_wait(bind(&onSigQuit, ref(statisticsCollector), ref(signalSetQuit)));
std::cout << "PING " << options.prefix << std::endl;
@@ -82,6 +82,16 @@ StatisticsCollector::computeStatistics()
return statistics;
}
std::ostream&
Statistics::printSummary(std::ostream& os) const
{
os << nReceived << "/" << nSent << " packets, " << packetLossRate * 100.0
<< "% loss, min/avg/max/mdev = " << minRtt << "/" << avgRtt << "/" << maxRtt << "/"
<< stdDevRtt << " ms" << std::endl;
return os;
}
std::ostream&
operator<<(std::ostream& os, const Statistics& statistics)
{
@@ -46,6 +46,9 @@ struct Statistics
double sumRtt; //!< sum of round trip times
double avgRtt; //!< average round trip time
double stdDevRtt; //!< std dev of round trip time
std::ostream&
printSummary(std::ostream& os) const;
};
/**