]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/squidclient/Ping.cc
43eae739a1e13878efe5a9f10f9b1eb25449653d
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "SquidTime.h"
11 #include "tools/squidclient/Parameters.h"
12 #include "tools/squidclient/Ping.h"
24 Ping::TheConfig Config
;
26 /// measurements collected by the squidclient ping mode logics
30 pingStats_() {memset(this, 0, sizeof(pingStats_
));}
32 long counted
; ///< number of transactions which have so far been measured
33 long pMin
; ///< shortest transaction time seen
34 long pMax
; ///< longest transaction time seen
35 long sum
; ///< total time so far spent waiting on transactions
42 * Signal interrupt handler for squidclient ping.
43 * Displays final statistics and disables further pings.
49 Ping::Config
.enable
= false;
50 std::cerr
<< "SIGNAL " << sig
<< " Interrupted." << std::endl
;
56 if (Ping::Config
.enable
) {
58 struct sigaction sa
, osa
;
59 if (sigaction(SIGINT
, NULL
, &osa
) == 0 && osa
.sa_handler
== SIG_DFL
) {
60 sa
.sa_handler
= catchSignal
;
62 sigemptyset(&sa
.sa_mask
);
63 (void) sigaction(SIGINT
, &sa
, NULL
);
67 if ((osig
= signal(SIGINT
, catchSignal
)) != SIG_DFL
)
68 (void) signal(SIGINT
, osig
);
70 return Ping::Config
.count
;
76 static struct timeval tv1
, tv2
;
81 if (!Ping::Config
.enable
)
84 #if GETTIMEOFDAY_NO_TZP
85 (void)gettimeofday(&tv1
);
87 (void)gettimeofday(&tv1
, NULL
);
92 Ping::TimerStop(size_t fsize
)
94 if (!Ping::Config
.enable
)
101 #if GETTIMEOFDAY_NO_TZP
102 (void)gettimeofday(&tv2
);
104 (void)gettimeofday(&tv2
, NULL
);
107 elapsed_msec
= tvSubMsec(tv1
, tv2
);
109 tmp
= localtime(&t2s
);
111 snprintf(tbuf
, sizeof(tbuf
)-1, "%d-%02d-%02d %02d:%02d:%02d [%ld]: %ld.%03ld secs, %f KB/s",
112 tmp
->tm_year
+ 1900, tmp
->tm_mon
+ 1, tmp
->tm_mday
,
113 tmp
->tm_hour
, tmp
->tm_min
, tmp
->tm_sec
, stats
.counted
+ 1,
114 elapsed_msec
/ 1000, elapsed_msec
% 1000,
115 elapsed_msec
? (double) fsize
/ elapsed_msec
: -1.0);
116 std::cerr
<< tbuf
<< std::endl
;
118 if (!stats
.counted
|| elapsed_msec
< stats
.pMin
)
119 stats
.pMin
= elapsed_msec
;
121 if (!stats
.counted
|| elapsed_msec
> stats
.pMax
)
122 stats
.pMax
= elapsed_msec
;
124 stats
.sum
+= elapsed_msec
;
128 /* Delay until next "ping.interval" boundary */
129 if (!LoopDone(stats
.counted
) && elapsed_msec
< Ping::Config
.interval
) {
132 long msec_left
= Ping::Config
.interval
- elapsed_msec
;
134 tvs
.tv_sec
= msec_left
/ 1000;
135 tvs
.tv_usec
= (msec_left
% 1000) * 1000;
136 select(0, NULL
, NULL
, NULL
, &tvs
);
143 if (Ping::Config
.enable
&& stats
.counted
) {
144 long mean
= stats
.sum
/ stats
.counted
;
145 std::cerr
<< std::endl
146 << stats
.counted
<< " requests, round-trip (secs) min/avg/max = "
147 << (stats
.pMin
/1000) << "." << (stats
.pMin
%1000)
148 << "/" << (mean
/1000) << "." << (mean
%1000)
149 << "/" << (stats
.pMax
/1000) << "." << (stats
.pMax
%1000)
155 Ping::TheConfig::usage()
157 std::cerr
<< "Ping Mode" << std::endl
158 << " --ping [options] Enable ping mode." << std::endl
160 << " options:" << std::endl
161 << " -g count Ping iteration count (default, loop until interrupted)." << std::endl
162 << " -I interval Ping interval in seconds (default 1 second)." << std::endl
167 Ping::TheConfig::parseCommandOpts(int argc
, char *argv
[], int c
, int &optIndex
)
169 // to get here --ping was seen
171 count
= 0; // default is infinite loop
172 interval
= 1 * 1000; // default is 1s intervals
174 const char *shortOpStr
= "g:I:?";
176 // options for controlling squidclient ping mode
177 static struct option pingOptions
[] = {
178 {"count", no_argument
, 0, 'g'},
179 {"interval", no_argument
, 0, 'I'},
183 int saved_opterr
= opterr
;
184 opterr
= 0; // suppress errors from getopt
185 while ((c
= getopt_long(argc
, argv
, shortOpStr
, pingOptions
, &optIndex
)) != -1) {
189 count
= atoi(optarg
);
191 std::cerr
<< "ERROR: -g ping count missing parameter." << std::endl
;
198 std::cerr
<< "ERROR: -I ping interval missing parameter." << std::endl
;
200 } else if ((interval
= atoi(optarg
) * 1000) <= 0) {
201 std::cerr
<< "ERROR: -I ping interval out of range (0-" << (INT_MAX
/1000) << ")." << std::endl
;
207 // rewind and let the caller handle unknown options
209 opterr
= saved_opterr
;
214 opterr
= saved_opterr
;