counters: regular counter get local API update

This commit is contained in:
Victor Julien 2025-11-27 13:06:46 +01:00
parent 9956f1f3c0
commit 77ddbbb9c8
8 changed files with 32 additions and 35 deletions

View file

@ -151,8 +151,8 @@ static inline void PfringDumpCounters(PfringThreadVars *ptv)
* So to get the number of packet on the interface we can add
* the newly seen packets and drops for this thread and add it
* to the interface counter */
uint64_t th_pkts = StatsGetLocalCounterValue(ptv->tv, ptv->capture_kernel_packets);
uint64_t th_drops = StatsGetLocalCounterValue(ptv->tv, ptv->capture_kernel_drops);
uint64_t th_pkts = StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_kernel_packets);
uint64_t th_drops = StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_kernel_drops);
LiveDevicePktsAdd(ptv->livedev, pfring_s.recv - th_pkts);
LiveDeviceDropAdd(ptv->livedev, pfring_s.drop - th_drops);
StatsCounterSetI64(&ptv->tv->stats, ptv->capture_kernel_packets, pfring_s.recv);
@ -160,7 +160,8 @@ static inline void PfringDumpCounters(PfringThreadVars *ptv)
#ifdef HAVE_PF_RING_FLOW_OFFLOAD
if (ptv->flags & PFRING_FLAGS_BYPASS) {
uint64_t th_bypassed = StatsGetLocalCounterValue(ptv->tv, ptv->capture_bypassed);
uint64_t th_bypassed =
StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_bypassed);
LiveDeviceBypassedAdd(ptv->livedev, pfring_s.shunt - th_bypassed);
StatsCounterSetI64(&ptv->tv->stats, ptv->capture_bypassed, pfring_s.shunt);
}
@ -620,14 +621,14 @@ void ReceivePfringThreadExitStats(ThreadVars *tv, void *data)
PfringThreadVars *ptv = (PfringThreadVars *)data;
PfringDumpCounters(ptv);
SCLogPerf("(%s) Kernel: Packets %" PRIu64 ", dropped %" PRIu64 "", tv->name,
StatsGetLocalCounterValue(tv, ptv->capture_kernel_packets),
StatsGetLocalCounterValue(tv, ptv->capture_kernel_drops));
SCLogPerf("(%s) Kernel: Packets %" PRIi64 ", dropped %" PRIi64 "", tv->name,
StatsCounterGetLocalValue(&tv->stats, ptv->capture_kernel_packets),
StatsCounterGetLocalValue(&tv->stats, ptv->capture_kernel_drops));
SCLogPerf("(%s) Packets %" PRIu64 ", bytes %" PRIu64 "", tv->name, ptv->pkts, ptv->bytes);
#ifdef HAVE_PF_RING_FLOW_OFFLOAD
if (ptv->flags & PFRING_FLAGS_BYPASS) {
SCLogPerf("(%s) Bypass: Packets %" PRIu64 "", tv->name,
StatsGetLocalCounterValue(tv, ptv->capture_bypassed));
SCLogPerf("(%s) Bypass: Packets %" PRIi64 "", tv->name,
StatsCounterGetLocalValue(&tv->stats, ptv->capture_bypassed));
}
#endif
}

View file

@ -1308,9 +1308,9 @@ static int StatsUpdateCounterArray(StatsPrivateThreadContext *pca, StatsPublicTh
* \retval 0 on success.
* \retval -1 on error.
*/
uint64_t StatsGetLocalCounterValue(ThreadVars *tv, StatsCounterId id)
int64_t StatsCounterGetLocalValue(StatsThreadContext *stats, StatsCounterId id)
{
StatsPrivateThreadContext *pca = &tv->stats.priv;
StatsPrivateThreadContext *pca = &stats->priv;
#ifdef DEBUG
BUG_ON((id.id < 1) || (id.id > pca->size));
#endif

View file

@ -148,7 +148,7 @@ void StatsCounterMaxUpdateI64(StatsThreadContext *, StatsCounterMaxId id, int64_
void StatsCounterAvgAddI64(StatsThreadContext *, StatsCounterAvgId id, int64_t x);
/* utility functions */
uint64_t StatsGetLocalCounterValue(struct ThreadVars_ *, StatsCounterId);
int64_t StatsCounterGetLocalValue(StatsThreadContext *, StatsCounterId);
void StatsThreadInit(StatsThreadContext *);
int StatsSetupPrivate(StatsThreadContext *, const char *);
void StatsThreadCleanup(StatsThreadContext *);

View file

@ -2696,8 +2696,8 @@ void ReceiveAFPThreadExitStats(ThreadVars *tv, void *data)
#ifdef PACKET_STATISTICS
AFPDumpCounters(ptv);
SCLogPerf("%s: (%s) kernel: Packets %" PRIu64 ", dropped %" PRIu64 "", ptv->iface, tv->name,
StatsGetLocalCounterValue(tv, ptv->capture_kernel_packets),
StatsGetLocalCounterValue(tv, ptv->capture_kernel_drops));
StatsCounterGetLocalValue(&tv->stats, ptv->capture_kernel_packets),
StatsCounterGetLocalValue(&tv->stats, ptv->capture_kernel_drops));
#endif
}

View file

@ -249,15 +249,16 @@ static inline void AFXDPDumpCounters(AFXDPThreadVars *ptv)
uint64_t rx_dropped = stats.rx_dropped + stats.rx_invalid_descs + stats.rx_ring_full;
StatsCounterAddI64(&ptv->tv->stats, ptv->capture_kernel_drops,
rx_dropped - StatsGetLocalCounterValue(ptv->tv, ptv->capture_kernel_drops));
rx_dropped - StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_kernel_drops));
StatsCounterAddI64(&ptv->tv->stats, ptv->capture_afxdp_packets, ptv->pkts);
(void)SC_ATOMIC_SET(ptv->livedev->drop, rx_dropped);
(void)SC_ATOMIC_ADD(ptv->livedev->pkts, ptv->pkts);
SCLogDebug("(%s) Kernel: Packets %" PRIu64 ", bytes %" PRIu64 ", dropped %" PRIu64 "",
ptv->tv->name, StatsGetLocalCounterValue(ptv->tv, ptv->capture_afxdp_packets),
ptv->bytes, StatsGetLocalCounterValue(ptv->tv, ptv->capture_kernel_drops));
ptv->tv->name,
StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_afxdp_packets), ptv->bytes,
StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_kernel_drops));
ptv->pkts = 0;
}
@ -904,8 +905,8 @@ static void ReceiveAFXDPThreadExitStats(ThreadVars *tv, void *data)
AFXDPDumpCounters(ptv);
SCLogPerf("(%s) Kernel: Packets %" PRIu64 ", bytes %" PRIu64 ", dropped %" PRIu64 "", tv->name,
StatsGetLocalCounterValue(tv, ptv->capture_afxdp_packets), ptv->bytes,
StatsGetLocalCounterValue(tv, ptv->capture_kernel_drops));
StatsCounterGetLocalValue(&tv->stats, ptv->capture_afxdp_packets), ptv->bytes,
StatsCounterGetLocalValue(&tv->stats, ptv->capture_kernel_drops));
}
/**

View file

@ -534,17 +534,12 @@ ReceiveErfDagThreadExitStats(ThreadVars *tv, void *data)
{
ErfDagThreadVars *ewtn = (ErfDagThreadVars *)data;
(void)SC_ATOMIC_SET(ewtn->livedev->pkts,
StatsGetLocalCounterValue(tv, ewtn->packets));
(void)SC_ATOMIC_SET(ewtn->livedev->drop,
StatsGetLocalCounterValue(tv, ewtn->drops));
(void)SC_ATOMIC_SET(ewtn->livedev->pkts, StatsCounterGetLocalValue(&tv->stats, ewtn->packets));
(void)SC_ATOMIC_SET(ewtn->livedev->drop, StatsCounterGetLocalValue(&tv->stats, ewtn->drops));
SCLogInfo("Stream: %d; Bytes: %"PRIu64"; Packets: %"PRIu64
"; Drops: %"PRIu64,
ewtn->dagstream,
ewtn->bytes,
StatsGetLocalCounterValue(tv, ewtn->packets),
StatsGetLocalCounterValue(tv, ewtn->drops));
SCLogInfo("Stream: %d; Bytes: %" PRIu64 "; Packets: %" PRIi64 "; Drops: %" PRIi64,
ewtn->dagstream, ewtn->bytes, StatsCounterGetLocalValue(&tv->stats, ewtn->packets),
StatsCounterGetLocalValue(&tv->stats, ewtn->drops));
}
/**

View file

@ -867,10 +867,10 @@ static void ReceiveNetmapThreadExitStats(ThreadVars *tv, void *data)
NetmapThreadVars *ntv = (NetmapThreadVars *)data;
NetmapDumpCounters(ntv);
SCLogPerf("%s: (%s) packets %" PRIu64 ", dropped %" PRIu64 ", bytes %" PRIu64 "",
SCLogPerf("%s: (%s) packets %" PRIi64 ", dropped %" PRIi64 ", bytes %" PRIu64 "",
ntv->ifsrc->ifname, tv->name,
StatsGetLocalCounterValue(tv, ntv->capture_kernel_packets),
StatsGetLocalCounterValue(tv, ntv->capture_kernel_drops), ntv->bytes);
StatsCounterGetLocalValue(&tv->stats, ntv->capture_kernel_packets),
StatsCounterGetLocalValue(&tv->stats, ntv->capture_kernel_drops), ntv->bytes);
}
/**

View file

@ -4060,20 +4060,20 @@ static int SigTestDetectAlertCounter(void)
p = UTHBuildPacket((uint8_t *)"boo", strlen("boo"), IPPROTO_TCP);
Detect(&tv, p, det_ctx);
FAIL_IF_NOT(StatsGetLocalCounterValue(&tv, det_ctx->counter_alerts) == 1);
FAIL_IF_NOT(StatsCounterGetLocalValue(&tv.stats, det_ctx->counter_alerts) == 1);
Detect(&tv, p, det_ctx);
FAIL_IF_NOT(StatsGetLocalCounterValue(&tv, det_ctx->counter_alerts) == 2);
FAIL_IF_NOT(StatsCounterGetLocalValue(&tv.stats, det_ctx->counter_alerts) == 2);
UTHFreePackets(&p, 1);
p = UTHBuildPacket((uint8_t *)"roo", strlen("roo"), IPPROTO_TCP);
Detect(&tv, p, det_ctx);
FAIL_IF_NOT(StatsGetLocalCounterValue(&tv, det_ctx->counter_alerts) == 2);
FAIL_IF_NOT(StatsCounterGetLocalValue(&tv.stats, det_ctx->counter_alerts) == 2);
UTHFreePackets(&p, 1);
p = UTHBuildPacket((uint8_t *)"laboosa", strlen("laboosa"), IPPROTO_TCP);
Detect(&tv, p, det_ctx);
FAIL_IF_NOT(StatsGetLocalCounterValue(&tv, det_ctx->counter_alerts) == 3);
FAIL_IF_NOT(StatsCounterGetLocalValue(&tv.stats, det_ctx->counter_alerts) == 3);
UTHFreePackets(&p, 1);
DetectEngineThreadCtxDeinit(&tv, (void *)det_ctx);