Skip to content

Commit b777b5e

Browse files
edumazetThomas Gleixner
authored andcommitted
time/jiffies: Inline jiffies_to_msecs() and jiffies_to_usecs()
For common cases (HZ=100, 250 or 1000), these helpers are at most one multiply, so there is no point calling a tiny function. Keep them out of line for HZ=300 and others. This saves cycles in TCP fast path, among other things. $ scripts/bloat-o-meter -t vmlinux.old vmlinux.new add/remove: 0/8 grow/shrink: 25/89 up/down: 530/-3474 (-2944) ... nla_put_msecs 193 - -193 message_stats_print 2131 920 -1211 Total: Before=25365208, After=25362264, chg -0.01% Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Thomas Gleixner <tglx@kernel.org> Link: https://patch.msgid.link/20260210170226.57209-1-edumazet@google.com
1 parent 192c015 commit b777b5e

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

include/linux/jiffies.h

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,44 @@ extern unsigned long preset_lpj;
434434
/*
435435
* Convert various time units to each other:
436436
*/
437-
extern unsigned int jiffies_to_msecs(const unsigned long j);
438-
extern unsigned int jiffies_to_usecs(const unsigned long j);
437+
438+
#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
439+
/**
440+
* jiffies_to_msecs - Convert jiffies to milliseconds
441+
* @j: jiffies value
442+
*
443+
* This inline version takes care of HZ in {100,250,1000}.
444+
*
445+
* Return: milliseconds value
446+
*/
447+
static inline unsigned int jiffies_to_msecs(const unsigned long j)
448+
{
449+
return (MSEC_PER_SEC / HZ) * j;
450+
}
451+
#else
452+
unsigned int jiffies_to_msecs(const unsigned long j);
453+
#endif
454+
455+
#if !(USEC_PER_SEC % HZ)
456+
/**
457+
* jiffies_to_usecs - Convert jiffies to microseconds
458+
* @j: jiffies value
459+
*
460+
* Return: microseconds value
461+
*/
462+
static inline unsigned int jiffies_to_usecs(const unsigned long j)
463+
{
464+
/*
465+
* Hz usually doesn't go much further MSEC_PER_SEC.
466+
* jiffies_to_usecs() and usecs_to_jiffies() depend on that.
467+
*/
468+
BUILD_BUG_ON(HZ > USEC_PER_SEC);
469+
470+
return (USEC_PER_SEC / HZ) * j;
471+
}
472+
#else
473+
unsigned int jiffies_to_usecs(const unsigned long j);
474+
#endif
439475

440476
/**
441477
* jiffies_to_nsecs - Convert jiffies to nanoseconds

kernel/time/time.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,20 +365,16 @@ SYSCALL_DEFINE1(adjtimex_time32, struct old_timex32 __user *, utp)
365365
}
366366
#endif
367367

368+
#if HZ > MSEC_PER_SEC || (MSEC_PER_SEC % HZ)
368369
/**
369370
* jiffies_to_msecs - Convert jiffies to milliseconds
370371
* @j: jiffies value
371372
*
372-
* Avoid unnecessary multiplications/divisions in the
373-
* two most common HZ cases.
374-
*
375373
* Return: milliseconds value
376374
*/
377375
unsigned int jiffies_to_msecs(const unsigned long j)
378376
{
379-
#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
380-
return (MSEC_PER_SEC / HZ) * j;
381-
#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
377+
#if HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
382378
return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
383379
#else
384380
# if BITS_PER_LONG == 32
@@ -390,7 +386,9 @@ unsigned int jiffies_to_msecs(const unsigned long j)
390386
#endif
391387
}
392388
EXPORT_SYMBOL(jiffies_to_msecs);
389+
#endif
393390

391+
#if (USEC_PER_SEC % HZ)
394392
/**
395393
* jiffies_to_usecs - Convert jiffies to microseconds
396394
* @j: jiffies value
@@ -405,17 +403,14 @@ unsigned int jiffies_to_usecs(const unsigned long j)
405403
*/
406404
BUILD_BUG_ON(HZ > USEC_PER_SEC);
407405

408-
#if !(USEC_PER_SEC % HZ)
409-
return (USEC_PER_SEC / HZ) * j;
410-
#else
411-
# if BITS_PER_LONG == 32
406+
#if BITS_PER_LONG == 32
412407
return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
413-
# else
408+
#else
414409
return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
415-
# endif
416410
#endif
417411
}
418412
EXPORT_SYMBOL(jiffies_to_usecs);
413+
#endif
419414

420415
/**
421416
* mktime64 - Converts date to seconds.

0 commit comments

Comments
 (0)