profiling: set sample rate to power of 2

For the rules profiling, we really want to limit the performance
impact to the maximum. So let's use an hash size that is a power
of 2. This will allow to not use the modulo operation that is
costly and simply use a single binary operator.

This code is only active for rules profiling so we are backward
compatible.
pull/8879/head
Eric Leblond 5 years ago committed by Victor Julien
parent df88ef0249
commit ea95e85755

@ -1420,7 +1420,7 @@ int SCProfileRuleStopCollection(void)
thread_local int profiling_rules_entered = 0;
int profiling_output_to_file = 0;
static SC_ATOMIC_DECLARE(uint64_t, samples);
static int rate = 1;
static uint64_t rate = 0;
/**
* \brief Initialize profiling.
@ -1432,9 +1432,16 @@ void SCProfilingInit(void)
(void)ConfGetInt("profiling.sample-rate", &rate_v);
if (rate_v > 0 && rate_v < INT_MAX) {
rate = (int)rate_v;
if (rate != 1)
SCLogInfo("profiling runs for every %dth packet", rate);
int literal_rate = (int)rate_v;
for (int i = literal_rate; i >= 1; i--) {
/* If i is a power of 2 */
if ((i & (i - 1)) == 0) {
rate = i - 1;
break;
}
}
if (rate != 0)
SCLogInfo("profiling runs for every %luth packet", rate + 1);
else
SCLogInfo("profiling runs for every packet");
}
@ -1444,7 +1451,7 @@ void SCProfilingInit(void)
int SCProfileRuleStart(Packet *p)
{
uint64_t sample = SC_ATOMIC_ADD(samples, 1);
if (sample % rate == 0) {
if ((sample & rate) == 0) {
p->flags |= PKT_PROFILE;
return 1;
}

@ -1757,9 +1757,9 @@ luajit:
#
profiling:
# Run profiling for every X-th packet. The default is 1, which means we
# profile every packet. If set to 1000, one packet is profiled for every
# 1000 received.
#sample-rate: 1000
# profile every packet. If set to 1024, one packet is profiled for every
# 1024 received. The sample rate must be a power of 2.
#sample-rate: 1024
# rule profiling
rules:

Loading…
Cancel
Save