runtime: make timers faster

This is a follow up to:
https://golang.org/cl/12876047/
time: lower level interface to Timer: embedding, compact interface callback with fast
callback

Timers can be heavily used in networking applications.
Current implementation at least has problems with scalability:

$ ./time.test -test.run=none -test.bench=StartStop -test.benchtime=1s
-test.cpu=1,2,4,8,16,32
PASS
BenchmarkStartStop  10000000           214 ns/op
BenchmarkStartStop-2     5000000           515 ns/op
BenchmarkStartStop-4     5000000           735 ns/op
BenchmarkStartStop-8     2000000           804 ns/op
BenchmarkStartStop-16    5000000           708 ns/op
BenchmarkStartStop-32    5000000           679 ns/op

Some spot optimizations can be applied as well. Probably more efficient data structure
can be used, but it's not clear to me how to do better than current 4-ary heap.

FTR here is BenchmarkStartStop profile with 8 procs:

+  13.75%%  time.test  [kernel.kallsyms]  [k] _raw_spin_lock_irqsave                     
                                                            ▒
+  11.25%%  time.test  time.test          [.] runtime.lock                               
                                                            ◆
+  11.15%%  time.test  time.test          [.] runtime.xchg                               
                                                            ▒
+   6.89%%  time.test  time.test          [.] runtime.procyield                          
                                                            ▒
+   6.32%%  time.test  [kernel.kallsyms]  [k] _raw_spin_lock                             
                                                            ▒
+   4.06%%  time.test  time.test          [.] runtime.cas                                
                                                            ▒
+   3.49%%  time.test  [kernel.kallsyms]  [k] gup_pte_range                              
                                                            ▒
+   1.87%%  time.test  time.test          [.] runtime.deltimer                           
                                                            ▒
+   1.80%%  time.test  [kernel.kallsyms]  [k] get_futex_key                              
                                                            ▒
+   1.71%%  time.test  [kernel.kallsyms]  [k] put_page                                   
                                                            ▒
+   1.58%%  time.test  [kernel.kallsyms]  [k] try_to_wake_up                             
                                                            ▒
+   1.55%%  time.test  [kernel.kallsyms]  [k] __wait_on_bit_lock                         
                                                            ▒
+   1.42%%  time.test  time.test          [.] flushptrbuf                                
                                                            ▒
+   1.38%%  time.test  [kernel.kallsyms]  [k] get_user_pages_fast                        
                                                            ▒
+   1.38%%  time.test  time.test          [.] siftup                                     
                                                            ▒
+   1.22%%  time.test  [kernel.kallsyms]  [k] copy_user_generic_string                   
                                                            ▒
+   1.19%%  time.test  time.test          [.] runtime.casp                               
                                                            ▒
+   1.10%%  time.test  [kernel.kallsyms]  [k] unlock_page                                
                                                            ▒
+   1.04%%  time.test  [kernel.kallsyms]  [k] get_futex_key_refs                         
                                                            ▒
+   1.01%%  time.test  time.test          [.] addtimer                                   
                                                            ▒
+   1.00%%  time.test  [kernel.kallsyms]  [k] drop_futex_key_refs                        
                                                            ▒
+   0.98%%  time.test  [kernel.kallsyms]  [k] prepare_to_wait_exclusive                  
                                                            ▒
+   0.97%%  time.test  [kernel.kallsyms]  [k] __wake_up_bit                              
                                                            ▒
+   0.94%%  time.test  [kernel.kallsyms]  [k] __wake_up_common                           
                                                            ▒
+   0.81%%  time.test  [kernel.kallsyms]  [k] audit_filter_syscall                       
                                                            ▒
+   0.75%%  time.test  [kernel.kallsyms]  [k] __schedule                                 
                                                            ▒
+   0.72%%  time.test  time.test          [.] runtime.mallocgc                           
                                                            ▒
+   0.72%%  time.test  time.test          [.] siftdown

2 thoughts on “runtime: make timers faster

  1. The ideas suggested here have been implemented for 1.14. Any further timer speedups can be done in new issues.

  2. Pingback: Azure DevOps

Comments are closed.