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.
(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");
}
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;
}
#
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: