Skip to content

Commit 4725344

Browse files
edumazetKAGA-KOKO
authored andcommitted
time/timecounter: Inline timecounter_cyc2time()
New network transport protocols want NIC drivers to get hardware timestamps of all incoming packets, and possibly all outgoing packets. One example is the upcoming 'Swift congestion control' which is used by TCP transport and is the primary need for timecounter_cyc2time(). This means timecounter_cyc2time() can be called more than 100 million times per second on a busy server. Inlining timecounter_cyc2time() brings a 12% improvement on a UDP receive stress test on a 100Gbit NIC. Note that FDO, LTO, PGO are unable to magically help for this case, presumably because NIC drivers are almost exclusively shipped as modules. Add an unlikely() around the cc_cyc2ns_backwards() case, even if FDO (when used) is able to take care of this optimization. Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Link: https://research.google/pubs/swift-delay-is-simple-and-effective-for-congestion-control-in-the-datacenter/ Link: https://patch.msgid.link/20251129095740.3338476-1-edumazet@google.com
1 parent 8f0b4cc commit 4725344

2 files changed

Lines changed: 29 additions & 37 deletions

File tree

include/linux/timecounter.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ extern void timecounter_init(struct timecounter *tc,
115115
*/
116116
extern u64 timecounter_read(struct timecounter *tc);
117117

118+
/*
119+
* This is like cyclecounter_cyc2ns(), but it is used for computing a
120+
* time previous to the time stored in the cycle counter.
121+
*/
122+
static inline u64 cc_cyc2ns_backwards(const struct cyclecounter *cc, u64 cycles, u64 frac)
123+
{
124+
return ((cycles * cc->mult) - frac) >> cc->shift;
125+
}
126+
118127
/**
119128
* timecounter_cyc2time - convert a cycle counter to same
120129
* time base as values returned by
@@ -131,7 +140,25 @@ extern u64 timecounter_read(struct timecounter *tc);
131140
*
132141
* Returns: cycle counter converted to nanoseconds since the initial time stamp
133142
*/
134-
extern u64 timecounter_cyc2time(const struct timecounter *tc,
135-
u64 cycle_tstamp);
143+
static inline u64 timecounter_cyc2time(const struct timecounter *tc, u64 cycle_tstamp)
144+
{
145+
const struct cyclecounter *cc = tc->cc;
146+
u64 delta = (cycle_tstamp - tc->cycle_last) & cc->mask;
147+
u64 nsec = tc->nsec, frac = tc->frac;
148+
149+
/*
150+
* Instead of always treating cycle_tstamp as more recent than
151+
* tc->cycle_last, detect when it is too far in the future and
152+
* treat it as old time stamp instead.
153+
*/
154+
if (unlikely(delta > cc->mask / 2)) {
155+
delta = (tc->cycle_last - cycle_tstamp) & cc->mask;
156+
nsec -= cc_cyc2ns_backwards(cc, delta, frac);
157+
} else {
158+
nsec += cyclecounter_cyc2ns(cc, delta, tc->mask, &frac);
159+
}
160+
161+
return nsec;
162+
}
136163

137164
#endif

kernel/time/timecounter.c

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,3 @@ u64 timecounter_read(struct timecounter *tc)
6262
}
6363
EXPORT_SYMBOL_GPL(timecounter_read);
6464

65-
/*
66-
* This is like cyclecounter_cyc2ns(), but it is used for computing a
67-
* time previous to the time stored in the cycle counter.
68-
*/
69-
static u64 cc_cyc2ns_backwards(const struct cyclecounter *cc,
70-
u64 cycles, u64 mask, u64 frac)
71-
{
72-
u64 ns = (u64) cycles;
73-
74-
ns = ((ns * cc->mult) - frac) >> cc->shift;
75-
76-
return ns;
77-
}
78-
79-
u64 timecounter_cyc2time(const struct timecounter *tc,
80-
u64 cycle_tstamp)
81-
{
82-
u64 delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
83-
u64 nsec = tc->nsec, frac = tc->frac;
84-
85-
/*
86-
* Instead of always treating cycle_tstamp as more recent
87-
* than tc->cycle_last, detect when it is too far in the
88-
* future and treat it as old time stamp instead.
89-
*/
90-
if (delta > tc->cc->mask / 2) {
91-
delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
92-
nsec -= cc_cyc2ns_backwards(tc->cc, delta, tc->mask, frac);
93-
} else {
94-
nsec += cyclecounter_cyc2ns(tc->cc, delta, tc->mask, &frac);
95-
}
96-
97-
return nsec;
98-
}
99-
EXPORT_SYMBOL_GPL(timecounter_cyc2time);

0 commit comments

Comments
 (0)