Skip to content

Commit 7b20a38

Browse files
committed
Minor efficiencies: avoid multiple expansions of function calls in TIMEVAL_* macros
1 parent 136e1c6 commit 7b20a38

File tree

5 files changed

+44
-26
lines changed

5 files changed

+44
-26
lines changed

FPEngine.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,8 @@ int FPHost6::schedule() {
20412041
}
20422042

20432043
/* Check if the probe timedout */
2044-
if (TIMEVAL_SUBTRACT(now, this->fp_probes[i].getTimeSent()) >= this->rto) {
2044+
struct timeval sent = this->fp_probes[i].getTimeSent();
2045+
if (TIMEVAL_SUBTRACT(now, sent) >= this->rto) {
20452046

20462047
/* If we have reached the maximum number of retransmissions, mark the
20472048
* probe as failed. Otherwise, schedule its transmission. */
@@ -2099,10 +2100,11 @@ int FPHost6::schedule() {
20992100
continue;
21002101
}
21012102

2103+
struct timeval sent = this->fp_probes[i].getTimeSent();
21022104
/* If there is some timed probe for which we have already scheduled its
21032105
* retransmission but it hasn't been sent yet, break the loop. We don't
21042106
* have to worry about retransmitting these probes yet.*/
2105-
if (this->fp_probes[i].getTimeSent().tv_sec == 0)
2107+
if (sent.tv_sec == 0)
21062108
return OP_SUCCESS;
21072109

21082110
/* If we got a total timeout for any of the timed probes, we shouldn't
@@ -2118,7 +2120,7 @@ int FPHost6::schedule() {
21182120
* time out (max retransmissions done and still no answer) then mark
21192121
* it as such. Otherwise, count it so we can retransmit the whole
21202122
* group of timed probes later if appropriate. */
2121-
if (TIMEVAL_SUBTRACT(now, this->fp_probes[i].getTimeSent()) >= this->rto) {
2123+
if (TIMEVAL_SUBTRACT(now, sent) >= this->rto) {
21222124
if (o.debugging > 3) {
21232125
log_write(LOG_PLAIN, "[%s] timed probe %d (%s) timedout\n",
21242126
this->target_host->targetipstr(), i, this->fp_probes[i].getProbeID());
@@ -2318,11 +2320,13 @@ int FPHost6::callback(const u8 *pkt, size_t pkt_len, const struct timeval *tv) {
23182320

23192321
/* See if the received packet is a response to a probe */
23202322
if (this->fp_probes[i].isResponse(rcvd)) {
2321-
struct timeval now, time_sent;
2323+
struct timeval time_sent = this->fp_probes[i].getTimeSent();
2324+
assert(time_sent.tv_sec > 0);
2325+
struct timeval now;
23222326

23232327
gettimeofday(&now, NULL);
23242328
this->fp_responses[i] = new FPResponse(this->fp_probes[i].getProbeID(),
2325-
pkt, pkt_len, fp_probes[i].getTimeSent(), *tv);
2329+
pkt, pkt_len, time_sent, *tv);
23262330
this->fp_probes[i].incrementReplies();
23272331
match_found = true;
23282332

@@ -2341,8 +2345,6 @@ int FPHost6::callback(const u8 *pkt, size_t pkt_len, const struct timeval *tv) {
23412345
}
23422346
this->probes_answered++;
23432347
/* Recompute the Retransmission Timeout based on this new RTT observation. */
2344-
time_sent = this->fp_probes[i].getTimeSent();
2345-
assert(time_sent.tv_sec > 0);
23462348
this->update_RTO(TIMEVAL_SUBTRACT(now, time_sent), this->fp_probes[i].getRetransmissions() != 0);
23472349
break;
23482350
}

idle_scan.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,12 +1058,18 @@ static int idlescan_countopen2(struct idle_proxy_info *proxy,
10581058

10591059
openports = -1;
10601060
tries = 0;
1061-
TIMEVAL_MSEC_ADD(probe_times[0], start, MAX(50, (target->to.srtt * 3 / 4) / 1000));
1062-
TIMEVAL_MSEC_ADD(probe_times[1], start, target->to.srtt / 1000 );
1063-
TIMEVAL_MSEC_ADD(probe_times[2], end, MAX(75, (2 * target->to.srtt +
1064-
target->to.rttvar) / 1000));
1065-
TIMEVAL_MSEC_ADD(probe_times[3], end, MIN(4000, (2 * target->to.srtt +
1066-
(target->to.rttvar << 2 )) / 1000));
1061+
1062+
int tmp = (target->to.srtt * 3) / (4 * 1000);
1063+
tmp = MAX(50, tmp);
1064+
TIMEVAL_MSEC_ADD(probe_times[0], start, tmp);
1065+
tmp = target->to.srtt / 1000;
1066+
TIMEVAL_MSEC_ADD(probe_times[1], start, tmp);
1067+
tmp = (2 * target->to.srtt + target->to.rttvar) / 1000;
1068+
tmp = MAX(75, tmp);
1069+
TIMEVAL_MSEC_ADD(probe_times[2], end, tmp);
1070+
tmp = (2 * target->to.srtt + (target->to.rttvar << 2 )) / 1000;
1071+
tmp = MIN(4000, tmp);
1072+
TIMEVAL_MSEC_ADD(probe_times[3], end, tmp);
10671073

10681074
do {
10691075
if (tries == 2)

nmap_tty.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,18 @@ bool keyWasPressed()
301301
option. */
302302
if (o.stats_interval != 0.0) {
303303
struct timeval now;
304+
time_t usec_interval = o.stats_interval * 1000000;
304305

305306
gettimeofday(&now, NULL);
306307
if (stats_time.tv_sec == 0) {
307308
/* Initialize the scheduled stats time. */
308309
stats_time = *o.getStartTime();
309-
TIMEVAL_ADD(stats_time, stats_time, (time_t) (o.stats_interval * 1000000));
310+
TIMEVAL_ADD(stats_time, stats_time, usec_interval);
310311
}
311312

312313
if (TIMEVAL_AFTER(now, stats_time)) {
313314
/* Advance to the next print time. */
314-
TIMEVAL_ADD(stats_time, stats_time, (time_t) (o.stats_interval * 1000000));
315+
TIMEVAL_ADD(stats_time, stats_time, usec_interval);
315316
/* If it's still in the past, catch it up to the present,
316317
* plus half a second to avoid double-printing without any progress. */
317318
if (TIMEVAL_AFTER(now, stats_time))

osscan2.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,8 +1276,9 @@ bool HostOsScan::nextTimeout(HostOsScanStats *hss, struct timeval *when) const {
12761276
memset(&probe_to, 0, sizeof(probe_to));
12771277
memset(&earliest_to, 0, sizeof(earliest_to));
12781278

1279+
unsigned long usec_to = timeProbeTimeout(hss);
12791280
for (probeI = hss->probesActive.begin(); probeI != hss->probesActive.end(); probeI++) {
1280-
TIMEVAL_ADD(probe_to, (*probeI)->sent, timeProbeTimeout(hss));
1281+
TIMEVAL_ADD(probe_to, (*probeI)->sent, usec_to);
12811282
if (firstgood || TIMEVAL_BEFORE(probe_to, earliest_to)) {
12821283
earliest_to = probe_to;
12831284
firstgood = false;
@@ -1435,14 +1436,15 @@ void HostOsScan::updateActiveSeqProbes(HostOsScanStats *hss) {
14351436
assert(hss);
14361437
std::list<OFProbe *>::iterator probeI, nxt;
14371438
OFProbe *probe = NULL;
1439+
long usec_to = timeProbeTimeout(hss);
14381440

14391441
for (probeI = hss->probesActive.begin(); probeI != hss->probesActive.end(); probeI = nxt) {
14401442
nxt = probeI;
14411443
nxt++;
14421444
probe = *probeI;
14431445

14441446
/* Is the probe timedout? */
1445-
if (TIMEVAL_SUBTRACT(now, probe->sent) > (long) timeProbeTimeout(hss)) {
1447+
if (TIMEVAL_SUBTRACT(now, probe->sent) > usec_to) {
14461448
hss->removeActiveProbe(probeI);
14471449
assert(stats->num_probes_active > 0);
14481450
stats->num_probes_active--;
@@ -1517,13 +1519,14 @@ void HostOsScan::updateActiveTUIProbes(HostOsScanStats *hss) {
15171519
assert(hss);
15181520
std::list<OFProbe *>::iterator probeI, nxt;
15191521
OFProbe *probe = NULL;
1522+
long usec_to = timeProbeTimeout(hss);
15201523

15211524
for (probeI = hss->probesActive.begin(); probeI != hss->probesActive.end(); probeI = nxt) {
15221525
nxt = probeI;
15231526
nxt++;
15241527
probe = *probeI;
15251528

1526-
if (TIMEVAL_SUBTRACT(now, probe->sent) > (long) timeProbeTimeout(hss)) {
1529+
if (TIMEVAL_SUBTRACT(now, probe->sent) > usec_to) {
15271530
if (probe->tryno >= 3) {
15281531
/* The probe is expired. */
15291532
hss->removeActiveProbe(probeI);
@@ -1579,8 +1582,9 @@ bool HostOsScan::hostSendOK(HostOsScanStats *hss, struct timeval *when) const {
15791582
TIMEVAL_MSEC_ADD(earliest_to, now, 10000);
15801583

15811584
/* Any timeouts coming up? */
1585+
unsigned long msec_to = timeProbeTimeout(hss) / 1000;
15821586
for (probeI = hss->probesActive.begin(); probeI != hss->probesActive.end(); probeI++) {
1583-
TIMEVAL_MSEC_ADD(probe_to, (*probeI)->sent, timeProbeTimeout(hss) / 1000);
1587+
TIMEVAL_MSEC_ADD(probe_to, (*probeI)->sent, msec_to);
15841588
if (TIMEVAL_BEFORE(probe_to, earliest_to)) {
15851589
earliest_to = probe_to;
15861590
}
@@ -1652,8 +1656,9 @@ bool HostOsScan::hostSeqSendOK(HostOsScanStats *hss, struct timeval *when) const
16521656
TIMEVAL_MSEC_ADD(earliest_to, now, 10000);
16531657

16541658
/* Any timeouts coming up? */
1659+
unsigned long msec_to = timeProbeTimeout(hss) / 1000;
16551660
for (probeI = hss->probesActive.begin(); probeI != hss->probesActive.end(); probeI++) {
1656-
TIMEVAL_MSEC_ADD(probe_to, (*probeI)->sent, timeProbeTimeout(hss) / 1000);
1661+
TIMEVAL_MSEC_ADD(probe_to, (*probeI)->sent, msec_to);
16571662
if (TIMEVAL_BEFORE(probe_to, earliest_to)) {
16581663
earliest_to = probe_to;
16591664
}

scan_engine.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,13 @@ void GroupScanStats::probeSent(unsigned int nbytes) {
298298
Recall that these have effect only when --min-rate or --max-rate is
299299
given. */
300300

301+
static time_t max_rate_add = o.max_packet_send_rate != 0.0 ?
302+
(1000000.0 / o.max_packet_send_rate) : 0;
303+
static time_t min_rate_add = o.min_packet_send_rate != 0.0 ?
304+
(1000000.0 / o.min_packet_send_rate) : 0;
305+
301306
if (o.max_packet_send_rate != 0.0)
302-
TIMEVAL_ADD(send_no_earlier_than, send_no_earlier_than,
303-
(time_t) (1000000.0 / o.max_packet_send_rate));
307+
TIMEVAL_ADD(send_no_earlier_than, send_no_earlier_than, max_rate_add);
304308
/* Allow send_no_earlier_than to slip into the past. This allows the sending
305309
scheduler to catch up and make up for delays in other parts of the scan
306310
engine. If we were to update send_no_earlier_than to the present the
@@ -314,8 +318,7 @@ void GroupScanStats::probeSent(unsigned int nbytes) {
314318
present to prevent that. */
315319
send_no_later_than = USI->now;
316320
}
317-
TIMEVAL_ADD(send_no_later_than, send_no_later_than,
318-
(time_t) (1000000.0 / o.min_packet_send_rate));
321+
TIMEVAL_ADD(send_no_later_than, send_no_later_than, min_rate_add);
319322
}
320323
}
321324

@@ -338,7 +341,7 @@ bool GroupScanStats::sendOK(struct timeval *when) const {
338341
recentsends = USI->gstats->probes_sent - USI->gstats->probes_sent_at_last_wait;
339342
if (recentsends > 0 &&
340343
(USI->scantype == CONNECT_SCAN || USI->ptech.connecttcpscan || !pcap_recv_timeval_valid())) {
341-
int to_ms = (int) MAX(to.srtt * .75 / 1000, 50);
344+
int to_ms = MAX(to.srtt * 3 / 4000, 50);
342345
if (TIMEVAL_MSEC_SUBTRACT(USI->now, last_wait) > to_ms)
343346
return false;
344347
}
@@ -592,10 +595,11 @@ bool HostScanStats::sendOK(struct timeval *when) const {
592595
TIMEVAL_MSEC_ADD(earliest_to, USI->now, 10000);
593596

594597
// Any timeouts coming up?
598+
unsigned long msec_to = probeTimeout() / 1000;
595599
for (probeI = probes_outstanding.begin(); probeI != probes_outstanding.end();
596600
probeI++) {
597601
if (!(*probeI)->timedout) {
598-
TIMEVAL_MSEC_ADD(probe_to, (*probeI)->sent, probeTimeout() / 1000);
602+
TIMEVAL_MSEC_ADD(probe_to, (*probeI)->sent, msec_to);
599603
if (TIMEVAL_BEFORE(probe_to, earliest_to)) {
600604
earliest_to = probe_to;
601605
}

0 commit comments

Comments
 (0)