From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7DB4E606A322 for ; Thu, 3 Dec 2026 18:01:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=networkplumber-org.20260707.gappssmtp.com header.i=@networkplumber-org.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=QFL3h3Kl Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E8A93406B6; Thu, 3 Dec 2026 19:00:58 +0100 (CET) Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by mails.dpdk.org (Postfix) with ESMTPS id BE22D40653 for ; Thu, 3 Dec 2026 19:00:56 +0100 (CET) Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-cc751eddf25so91487a12.3 for ; Thu, 03 Dec 2026 10:00:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20260707.gappssmtp.com; s=20260707; t=1796320855; x=1796925655; 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:content-type; bh=gO4/VIaa3P2Uy67+iiRdoJuee+gozqIzvOQ3MxfbzIM=; b=QFL3h3KlcBl0MFDIhlFit1JELyx4l4C8jrAnbvJFjjuyul/ICKv0ueJayDb3ZQVenR TevGXcSQkf0mDjXRX/KZN5+KVckrojhvzqwHEzD6cmIEc3sEjFzDKk/w5mSs4W67Sg57 Tc/+WqYZBeWODLte0T2C5Sgdrb1VzHIX9BCr/KG9+TPrWPXvKi4oRaho+ezwiFdkyWWP n9h2DPBfvjE/NCubM4oSP44TaHqfBtf742DF+C5nlq3NVEjlRBlPisMqlofdM8kP+sW4 Mhuqf07dzzqaDs7MGLlSOV2B4Mwhu39RNKk+ggrj6AiujZV54frTJRk8bKau68EGoMoq hGbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796320855; x=1796925655; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=gO4/VIaa3P2Uy67+iiRdoJuee+gozqIzvOQ3MxfbzIM=; b=o3goWo77ZSVGzuYjKeRAhVRTsjMeWHxlo3zAMqyYYAr5i+zn/6yXE6TY4YDqoeJB04 bo4T4h/cwXnorZNkKyXYA6kA3oehxgCknpxfTRoZfR0RcER8q1VMut/0W6I/0U/ArOMx luIQEyn3Q0nBk/T/7c+zU+k48xb1wKpyisKDO3NpF9lLkD9qMj6y8SYeyqXPPORV1Cig 8HudTLTyR+0TzwrXqPaVfXXytXZpu0VtOuvkFj1NIH9R6X36re+AlEteUQGqvkDWXVw8 quqrkpHQK3xIze7u+7rlZQ7fQFYvlzy82GQZCrFMSBnWq2R6gCpch7y7fX+nJGoA5KtG uRtw== X-Gm-Message-State: APuU4Z8q8QQiQnVVvTxzXtN9Cv6knJJCdk6uKKaP8FymnS4yleu4TiwK A2bRu2/QmUH5otAFGKfLoE0f5fvKHCYseKJ2nEtcF9PlGBf0WC9qtWKWzBUvL3xu3SxEBLMQPVD Z1mnAqbY= X-Gm-Gg: AZFdTZdYd1rTKKMwgHcwHuaYTSw7G21wUDgxHkgAwZqAIU4OSr2ATvjiQalKneHIylX RqqVPdsD3gapI0I9mXUfP49zXWRDUhiS8FdT4xsu2j3sT4VCv0OpRZi1KMmZ4300wUyQc0LNCHy zy9q2EYwWju4acZrLlwhuExiomUc65gj15/1Sz0VkxQ1XuOn3xepUTyY3MidBftONcfioYO6WGS WPUniYYXqsjdTSwyb1IswZptRa67yIm7iELtis28L7ZMF7wlKwJHLMSZKqVNKLY5kT4WHF5rA2D VLTP1TUx9Vao4N2PZEKMhjw+uRJL4/pu/R03IED3mg3/dumcVW2Qb2xYs2x8ysoTjowKQxPBhG3 +h5s1ldwjGokZ7YCQH+Z2GZ2cYr2gYM//QP/xnK8QCJee5xdcVFOtN7sFo29u/XYSevKjdK0sLd iP7/Gm65ha0hRWFHcuuZiLb5BjhXnTg2uQ5oZid14k1mkNzeUZqkbCJsFT8jpvDrrPc3uijEMrg qROV/jZtrWEfpPl0xXVqgL+Swxb8OazIqp924IGwuBmqjEn0o7m5mDVhUygHjfL0WUuwn9cXq2X dXxqAvgA3TiFrm5OsWB0NQaO360TXmH8bpgbyMUa/3B+NGfHF/ixwC6abB0GhXIJGHrw X-Received: by 2002:a17:90b:2e49:b0:3a4:b0f3:6597 with SMTP id 98e67ed59e1d1-3b41c58ae4dmr44237607a91.0.1796320854882; Thu, 03 Dec 2026 10:00:54 -0800 (PST) Received: from phoenix.lan (204-195-112-43.wavecable.com. [204.195.112.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c7649efbsm5329952a91.3.2026.12.03.10.00.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 10:00:54 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v3 3/6] eal: add function to convert TSC to epoch time Date: Thu, 3 Dec 2026 09:59:45 -0800 Message-ID: <20261203180050.73444-4-stephen@networkplumber.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261203180050.73444-1-stephen@networkplumber.org> References: <20261201224258.1962108-1-stephen@networkplumber.org> <20261203180050.73444-1-stephen@networkplumber.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Archive: List-Post: List-Help: List-Subscribe: , X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The pcapng library converts the TSC into nanoseconds since 1/1/1970, because that is what the pcapng file format records. Nothing about that conversion is specific to packet capture: it needs the TSC frequency and a reference point relating the TSC to the wall clock, both of which EAL already has or can take at init. Record the TSC alongside a CLOCK_REALTIME reading while the TSC frequency is being determined, and add rte_tsc_to_ns() to convert any TSC value using that reference point. The seconds are computed before the remainder is scaled, so the multiply by NS_PER_S cannot overflow. A test converts values up to ten days out, where scaling the whole cycle count would wrap. The reference point is taken per process rather than shared through the memory config, so a secondary process may differ from the primary by the sampling error. Signed-off-by: Stephen Hemminger --- app/test/test_cycles.c | 34 +++++++++++++++ doc/api/doxy-api-index.md | 1 + doc/guides/rel_notes/release_27_03.rst | 6 +++ lib/eal/common/eal_common_timer.c | 59 ++++++++++++++++++++++++++ lib/eal/include/rte_time.h | 34 +++++++++++++++ 5 files changed, 134 insertions(+) diff --git a/app/test/test_cycles.c b/app/test/test_cycles.c index 68a31ac450..84ecd09fdb 100644 --- a/app/test/test_cycles.c +++ b/app/test/test_cycles.c @@ -2,11 +2,13 @@ * Copyright(c) 2010-2014 Intel Corporation */ +#include #include #include #include #include +#include #include "test.h" @@ -54,3 +56,35 @@ test_user_delay_us(void) } REGISTER_FAST_TEST(user_delay_us, NOHUGE_OK, ASAN_OK, test_user_delay_us); + +/* + * Scaling a cycle count by NS_PER_S overflows past a few seconds worth + * of cycles, so check that converting a TSC value well into the future + * still advances by the expected amount. + */ +static int +test_tsc_to_ns(void) +{ + static const unsigned int offsets[] = { 1, 8, 3600, 10 * 86400 }; + uint64_t hz = rte_get_tsc_hz(); + uint64_t tsc = rte_get_tsc_cycles(); + uint64_t base = rte_tsc_to_ns(tsc); + unsigned int i; + + TEST_ASSERT(hz != 0, "TSC frequency is zero"); + + for (i = 0; i < RTE_DIM(offsets); i++) { + uint64_t want = (uint64_t)offsets[i] * NS_PER_S; + uint64_t got = rte_tsc_to_ns(tsc + + (uint64_t)offsets[i] * hz) - base; + + /* Allow a nanosecond either way for the division. */ + TEST_ASSERT(got + 1 >= want && got <= want + 1, + "+%u s: got %"PRIu64" want %"PRIu64, + offsets[i], got, want); + } + + return 0; +} + +REGISTER_FAST_TEST(tsc_to_ns_autotest, NOHUGE_OK, ASAN_OK, test_tsc_to_ns); diff --git a/doc/api/doxy-api-index.md b/doc/api/doxy-api-index.md index 974f2d48ab..62d5db1b3b 100644 --- a/doc/api/doxy-api-index.md +++ b/doc/api/doxy-api-index.md @@ -81,6 +81,7 @@ The public API headers are grouped by topics: - **timers**: [cycles](@ref rte_cycles.h), + [time](@ref rte_time.h), [timer](@ref rte_timer.h), [alarm](@ref rte_alarm.h) diff --git a/doc/guides/rel_notes/release_27_03.rst b/doc/guides/rel_notes/release_27_03.rst index a91813faca..63bb5ad42d 100644 --- a/doc/guides/rel_notes/release_27_03.rst +++ b/doc/guides/rel_notes/release_27_03.rst @@ -55,6 +55,12 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Added API to convert TSC to epoch time.** + + Added the experimental ``rte_tsc_to_ns()`` function in ``rte_time.h`` + to convert a TSC value into nanoseconds since 1/1/1970, + using a reference point recorded during EAL initialization. + Removed Items ------------- diff --git a/lib/eal/common/eal_common_timer.c b/lib/eal/common/eal_common_timer.c index 67ede70c62..e1871189ea 100644 --- a/lib/eal/common/eal_common_timer.c +++ b/lib/eal/common/eal_common_timer.c @@ -8,8 +8,10 @@ #include #include #include +#include #include #include +#include #include #include "eal_private.h" @@ -18,6 +20,10 @@ /* The frequency of the RDTSC timer resolution */ static uint64_t eal_tsc_resolution_hz; +/* Reference point relating the TSC to the wall clock, taken at init */ +static uint64_t eal_tsc_base; +static uint64_t eal_epoch_ns; + /* Pointer to user delay function */ RTE_EXPORT_SYMBOL(rte_delay_us) void (*rte_delay_us)(unsigned int) = NULL; @@ -39,6 +45,36 @@ rte_get_tsc_hz(void) return eal_tsc_resolution_hz; } +/* + * Convert a count of cycles to nanoseconds. + * + * Compute the whole seconds first, so that the remainder is always + * less than the frequency and scaling it by NS_PER_S cannot overflow. + */ +static uint64_t +cycles_to_ns(uint64_t cycles) +{ + uint64_t secs = cycles / eal_tsc_resolution_hz; + uint64_t rem = cycles % eal_tsc_resolution_hz; + + return secs * NS_PER_S + (rem * NS_PER_S) / eal_tsc_resolution_hz; +} + +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_tsc_to_ns, 27.03) +uint64_t +rte_tsc_to_ns(uint64_t tsc) +{ + /* + * A caller may convert a TSC value sampled before the reference + * point was taken, so handle both directions on an unsigned + * magnitude. + */ + if (unlikely(tsc < eal_tsc_base)) + return eal_epoch_ns - cycles_to_ns(eal_tsc_base - tsc); + + return eal_epoch_ns + cycles_to_ns(tsc - eal_tsc_base); +} + static uint64_t estimate_tsc_freq(void) { @@ -52,6 +88,27 @@ estimate_tsc_freq(void) return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ); } +/* + * Record the TSC value matching a wall clock reading, so that any TSC + * value can later be expressed as nanoseconds since the Unix epoch. + * Read the TSC either side of the clock to halve the sampling error. + */ +static void +init_tsc_base(void) +{ + struct timespec ts; + uint64_t cycles; + + cycles = rte_get_tsc_cycles(); + if (clock_gettime(CLOCK_REALTIME, &ts) < 0) { + EAL_LOG(WARNING, "Could not read wall clock," + " TSC to epoch conversion unavailable"); + return; + } + eal_tsc_base = (cycles + rte_get_tsc_cycles()) / 2; + eal_epoch_ns = rte_timespec_to_ns(&ts); +} + int init_tsc_freq(void) { @@ -71,6 +128,7 @@ init_tsc_freq(void) } eal_tsc_resolution_hz = mcfg->tsc_hz; + init_tsc_base(); return 0; } @@ -87,6 +145,7 @@ init_tsc_freq(void) EAL_LOG(DEBUG, "TSC frequency is ~%" PRIu64 " KHz", freq / 1000); eal_tsc_resolution_hz = freq; mcfg->tsc_hz = freq; + init_tsc_base(); return 0; } diff --git a/lib/eal/include/rte_time.h b/lib/eal/include/rte_time.h index c5c3a233e4..3122e062ab 100644 --- a/lib/eal/include/rte_time.h +++ b/lib/eal/include/rte_time.h @@ -5,9 +5,17 @@ #ifndef _RTE_TIME_H_ #define _RTE_TIME_H_ +/** + * @file + * + * Time and cycle counter conversion. + */ + #include #include +#include + #ifdef __cplusplus extern "C" { #endif @@ -102,6 +110,32 @@ rte_ns_to_timespec(uint64_t nsec) return ts; } +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Convert a TSC value into nanoseconds since the Unix epoch. + * + * EAL records the TSC alongside a CLOCK_REALTIME reading during + * initialization, and this converts using that reference point and the + * TSC frequency. Values sampled before initialization are also handled, + * and the result tracks the wall clock as it was at init: later changes + * to the system time are not reflected. + * + * Each process takes its own reference point, so values converted in a + * secondary process may differ from the primary by the sampling error. + * + * Must be called after rte_eal_init(), which fails if the TSC frequency + * cannot be determined. + * + * @param tsc + * A value read from rte_get_tsc_cycles() or rte_rdtsc(). + * @return + * Nanoseconds since 1/1/1970. + */ +__rte_experimental +uint64_t rte_tsc_to_ns(uint64_t tsc); + #ifdef __cplusplus } #endif -- 2.53.0