From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 4E81A468E5 for ; Fri, 13 Jun 2025 02:35:56 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4469942E4C; Fri, 13 Jun 2025 02:35:56 +0200 (CEST) Received: from mail-qv1-f45.google.com (mail-qv1-f45.google.com [209.85.219.45]) by mails.dpdk.org (Postfix) with ESMTP id 88ACF42D78 for ; Fri, 13 Jun 2025 02:35:53 +0200 (CEST) Received: by mail-qv1-f45.google.com with SMTP id 6a1803df08f44-6facacf521eso16811146d6.3 for ; Thu, 12 Jun 2025 17:35:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1749774953; x=1750379753; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y4RI3Hi+CDfklctP5Be2ck9aVrPXNQIin7JU9YiliR8=; b=WFyNK1oPcXROrJyEc/E4zy7+/tf/OaXaPPkOE0/cLAiUFYeil6c2OLlj+UQC2NPV2P SaZJ+z5cGTWt5bZoYYw1Ifax90X/kC1MjDc1XXVDaYU+O4r87SqDQxiUdo8Li6arr8W/ hxXmrEYyaN8KsfIKNBdcNv0ju6Z6TEKAsAsYgqaqZaSD0LGZ8eRkrlXYdujbso7Uowc0 41mNj7YQ7TzLf7nznXVTAdV0/JsszCixVynKTQhaiz2lzytsE38NdX1rrAE1dv90p+B5 TzpnLZA1ul5ISd6YvLlhkRdWF2b6S6U/VBDQ2uIOE/kfSJW4H+J3in84X4nOi8rccyjT tHPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749774953; x=1750379753; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y4RI3Hi+CDfklctP5Be2ck9aVrPXNQIin7JU9YiliR8=; b=YnBGkeXQsGfFb0fVRLRLtffpSPB5exl2qUvAXI79oeXYc9POIe6MzhOU8IE2xTsCws RGVee06M5T7p2+QugVOGLXViwc1p133qHOWcOm6w6PEF/NSD1v410/581yJM8xosHhrI HJBRHMkNQIYz6wAFAa7x4/D7o1sRTKrLPmhzyZgIycq8O8DxnKlThsHAioxQMockbXrH ygcnMo3BF96xmP1fJOhYD84a775TPra4Xpn3Qka5JYGR9HwN4BykPrH/xP95/JsK36hB 1s24y414G5jRwKIH6L6yO9GOflRxlTupV2cEEWvW8ZADSiHdEGWakCWyBhDEJBn1nPbp jaUQ== X-Forwarded-Encrypted: i=1; AJvYcCUxsni3Pf76vD0kGMxQ5seTHC1aD0lIzleX9DmSLR0tKjyn3bmK/hE13TqTLtyT8MVTXvTbRr4=@dpdk.org X-Gm-Message-State: AOJu0Yy4x2WK+UlHJB8nWgck0N+qHONFUmxvzX68YfVVRexTYgcMq7fh W93Wkd2oKbV4itG+HGX1rWTlRe28hspU+tYuyBVg9B45kFN2+yHPf9ViaCuiI7sGvsc= X-Gm-Gg: ASbGncsRUfzi5/IeXLh6ELTP6YtZcH86vo022by8BTBXply6GO2gleJ+ngAgbTXr6hu iaFsoXV7Y2p2eHu/REXDqu49Rzac9Q12P3EDafu91YlOvJOn4yWzw67mwF89VpSw8SZTRCXTWZs Q1IeH+krllbuuZkXnntrZDF75M21f9u5tbntj8d0RRXf2tO+00KVyJdHwsYffy+butsv8hvNlEi ttCz9F42dqtdNM3o5NloHGDispmgPR9P2gZlCA3r6OaM4KfsLNSBh4jOGz4eTrAOFuAXsizUObf pwWZoY+5mQGUSTlPllKBEeskHMMpz0SWoNGM5lqt42kK4W8d7XrfLhmykyAs/6fa1LkrCU/FKAr BjxdKhQFedbzQ3iBtful8KpoPEm0DyitPKY1U X-Google-Smtp-Source: AGHT+IGDNIubUuBMb4MuH3h6R9XbEpJeT9tITxQiWdcZd6v6p6cTs9gmzO7nyglUQRWK9IhYSZpSxw== X-Received: by 2002:a05:6214:2588:b0:6f8:c2b9:b1f7 with SMTP id 6a1803df08f44-6fb3e59d00bmr13552776d6.13.1749774952789; Thu, 12 Jun 2025 17:35:52 -0700 (PDT) Received: from hermes.lan (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fb35c31566sm15691856d6.61.2025.06.12.17.35.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jun 2025 17:35:52 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , stable@dpdk.org, Reshma Pattan , Harry van Haaren , Remy Horton Subject: [PATCH 1/2] latencystats: fix receive sample MP issues Date: Thu, 12 Jun 2025 17:34:25 -0700 Message-ID: <20250613003547.39239-2-stephen@networkplumber.org> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250613003547.39239-1-stephen@networkplumber.org> References: <20250613003547.39239-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org The receive callback was not safe with multiple queues. If one receive queue callback decides to take a sample it needs to add that sample and do atomic update to the previous TSC sample value. Add a new lock for that. Also, add code to handle TSC wraparound in comparison. Perhaps this should move to rte_cycles.h? Signed-off-by: Stephen Hemminger Fixes: 5cd3cac9ed22 ("latency: added new library for latency stats") Cc: stable@dpdk.org --- lib/latencystats/rte_latencystats.c | 47 ++++++++++++++++++----------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/latencystats/rte_latencystats.c b/lib/latencystats/rte_latencystats.c index 6873a44a92..181c53dd0e 100644 --- a/lib/latencystats/rte_latencystats.c +++ b/lib/latencystats/rte_latencystats.c @@ -45,10 +45,19 @@ timestamp_dynfield(struct rte_mbuf *mbuf) timestamp_dynfield_offset, rte_mbuf_timestamp_t *); } +/* Compare two 64 bit timer counter but deal with wraparound correctly. */ +static inline bool tsc_after(uint64_t t0, uint64_t t1) +{ + return (int64_t)(t1 - t0) < 0; +} + +#define tsc_before(a, b) tsc_after(b, a) + static const char *MZ_RTE_LATENCY_STATS = "rte_latencystats"; static int latency_stats_index; + +static rte_spinlock_t sample_lock = RTE_SPINLOCK_INITIALIZER; static uint64_t samp_intvl; -static uint64_t timer_tsc; static uint64_t prev_tsc; #define LATENCY_AVG_SCALE 4 @@ -147,25 +156,27 @@ add_time_stamps(uint16_t pid __rte_unused, void *user_cb __rte_unused) { unsigned int i; - uint64_t diff_tsc, now; + uint64_t now = rte_rdtsc(); - /* - * For every sample interval, - * time stamp is marked on one received packet. - */ - now = rte_rdtsc(); - for (i = 0; i < nb_pkts; i++) { - diff_tsc = now - prev_tsc; - timer_tsc += diff_tsc; - - if ((pkts[i]->ol_flags & timestamp_dynflag) == 0 - && (timer_tsc >= samp_intvl)) { - *timestamp_dynfield(pkts[i]) = now; - pkts[i]->ol_flags |= timestamp_dynflag; - timer_tsc = 0; + /* Check without locking */ + if (likely(tsc_before(now, prev_tsc + samp_intvl))) + return nb_pkts; + + /* Try and get sample, skip if sample is being done by other core. */ + if (likely(rte_spinlock_trylock(&sample_lock))) { + for (i = 0; i < nb_pkts; i++) { + struct rte_mbuf *m = pkts[i]; + + /* skip if already timestamped */ + if (unlikely(m->ol_flags & timestamp_dynflag)) + continue; + + m->ol_flags |= timestamp_dynflag; + *timestamp_dynfield(m) = now; + prev_tsc = now; + break; } - prev_tsc = now; - now = rte_rdtsc(); + rte_spinlock_unlock(&sample_lock); } return nb_pkts; -- 2.47.2