rtc: Improve performance of rtc_time64_to_tm. Add tests.
PATCH 1/1 rtc: Improve performance of rtc_time64_to_tm. Add tests.
From: Cassio Neri <[email protected]>
The current implementation of rtc_time64_to_tm contains unnecessary loops, branches and look-up tables. The new one uses an arithmetic-based algorithm appeared in [1] and is ~3.3 times faster.
The drawback is that the new code isn't intuitive and contains many 'magic numbers' (not unusual for this type of algorithm). However, [1] justifies all those numbers and, given this function's history, I reckon the code is unlikely to need much maintenance, if any at all.
Added file drivers/rtc/lib_test.c containing a KUnit test case that checks every day in a 160,000 years interval starting on 1970-01-01 against the expected result. A new config RTC_LIB_KUNIT_TEST symbol was introduced to give the option to run this test suite.
[1] Neri, Schneider, "Euclidean Affine Functions and Applications to Calendar Algorithms". https://arxiv.org/abs/2102.06959
---
* Disclaimer: I'm an author of [1] and, surely, I have an interest in seeing my algorithm made into the kernel. If not by this patch, I'm willing to work closely with maintainers, if they wish, in order to write an appropriate implementation.
* Benchmarks: It measures the time taken by each implementation to process 65,536 numbers. These numbers are pseudo-random under the uniform distribution on the interval corresponding to dates spanning 800 years starting at 1970-01-01.
https://quick-bench.com/q/ikF-97xNTtGvJlUbG2jLUuCl4e8
(Apologies that the benchmark is in C++ but results in C should be close.)
Disasembly: Shows, in particular, reduction in code size.
https://godbolt.org/z/dvj9G5vef
--- drivers/rtc/Kconfig | 10 ++++++ drivers/rtc/Makefile | 1 + drivers/rtc/lib.c | 79 ++++++++++++++++++++++++++++-------------- drivers/rtc/lib_test.c | 75 +++++++++++++++++++++++++++++++++++++++ include/linux/rtc.h | 6 +++- 5 files changed, 144 insertions(+), 27 deletions(-) create mode 100644 drivers/rtc/lib_test.c
drivers/rtc/Kconfig +10 -0
@@ -10,6 +10,16 @@ config RTC_MC146818_LIB bool select RTC_LIB +config RTC_LIB_KUNIT_TEST + tristate "KUnit test for RTC lib functions" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + select RTC_LIB + help + Enable this option to test RTC library functions. + + If unsure, say N. + menuconfig RTC_CLASS bool "Real Time Clock" default n
drivers/rtc/Makefile +1 -0
@@ -178,3 +178,4 @@ obj-$(CONFIG_RTC_DRV_WM8350) += rtc-wm8350.o obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o obj-$(CONFIG_RTC_DRV_XGENE) += rtc-xgene.o obj-$(CONFIG_RTC_DRV_ZYNQMP) += rtc-zynqmp.o +obj-$(CONFIG_RTC_LIB_KUNIT_TEST) += lib_test.o
drivers/rtc/lib.c +53 -26
108 lines not on this page · raw message
drivers/rtc/lib_test.c new file +75 -0
75 lines not on this page · raw message
include/linux/rtc.h +5 -1
@@ -220,7 +220,11 @@ void rtc_timer_do_work(struct work_struct *work);static inline bool is_leap_year (unsigned int year) { -return (!(year %4 ) && (year %100 )) || !(year %400 ); +/* This implementation is more branch-predictor friendly than the +* traditional: +* return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); +*/ +return year %100 !=0 ? year %4 ==0 : year %400 ==0 ; }#define devm_rtc_register_device (device) \
Signature · 1 line
2.31.0
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Cassio,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on abelloni/rtc-next]
[also build test ERROR on v5.13-rc3 next-20210528]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Cassio-Neri/rtc-Improve-performance-of-rtc_time64_to_tm-Add-tests/20210530-003728 base: https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git rtc-next config: i386-randconfig-r012-20210530 (attached as .config) compiler: gcc-9 (Debian 9.3.0-22) 9.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/8747741de658b538ddea6a357747ad5fb9211b45 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Cassio-Neri/rtc-Improve-performance-of-rtc_time64_to_tm-Add-tests/20210530-003728 git checkout 8747741de658b538ddea6a357747ad5fb9211b45 # save the attached .config to linux build tree make W=1 ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
ld: drivers/rtc/lib_test.o: in function `rtc_time64_to_tm_test_date_range':
>> drivers/rtc/lib_test.c:56: undefined reference to `__divdi3'
vim +56 drivers/rtc/lib_test.c
28
29 /*
30 * Checks every day in a 160000 years interval starting on 1970-01-01
31 * against the expected result.
32 */
33 static void rtc_time64_to_tm_test_date_range(struct kunit *test)
34 {
35 /*
36 * 160000 years = (160000 / 400) * 400 years
37 * = (160000 / 400) * 146097 days
38 * = (160000 / 400) * 146097 * 86400 seconds
39 */
40 time64_t total_secs = ((time64_t) 160000) / 400 * 146097 * 86400;
41 int year = 1970;
42 int month = 1;
43 int mday = 1;
44 int yday = 1;
45
46 struct rtc_time result;
47 time64_t secs;
48
49 for (secs = 0; secs <= total_secs; secs += 86400) {
50
51 rtc_time64_to_tm(secs, &result);
52
53 #define FAIL_MSG "%d/%02d/%02d (%2d) : %ld", \
54 year, month, mday, yday, secs/86400
55
> 56 KUNIT_ASSERT_EQ_MSG(test, year - 1900, result.tm_year, FAIL_MSG);
57 KUNIT_ASSERT_EQ_MSG(test, month - 1, result.tm_mon, FAIL_MSG);
58 KUNIT_ASSERT_EQ_MSG(test, mday, result.tm_mday, FAIL_MSG);
59 KUNIT_ASSERT_EQ_MSG(test, yday, result.tm_yday, FAIL_MSG);
60
61 advance_date(&year, &month, &mday, &yday);
62 }
63 }
64 ---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Cassio,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on abelloni/rtc-next]
[also build test ERROR on v5.13-rc3 next-20210528]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Cassio-Neri/rtc-Improve-performance-of-rtc_time64_to_tm-Add-tests/20210530-003728 base: https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git rtc-next config: h8300-allyesconfig (attached as .config) compiler: h8300-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/8747741de658b538ddea6a357747ad5fb9211b45
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Cassio-Neri/rtc-Improve-performance-of-rtc_time64_to_tm-Add-tests/20210530-003728 git checkout 8747741de658b538ddea6a357747ad5fb9211b45
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=h8300
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
h8300-linux-ld: drivers/rtc/lib_test.o: in function `.L11':
>> lib_test.c:(.text+0x14d): undefined reference to `__divdi3'
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org