Skip to contentSkip to conversation

Xenomai and futexes - Native API optimized

Xenomai-help M. Koehrer 3 participants 5 messages May 11, 2007

lore
Share & tools
lore

M. Koehrer

author
#0 lore raw

Hi,

here are my latest results:
I included xeno_nucleus and xeno_native into the kernel. (see first oprofile result). (I also added debug information to the kernel which was of no additional help).
I detected that most of the time was spent in __ipipe_hard_cpuid.
I looked at that routine and broke that using 2 additional helper functions that are use to "monitor" the apic_read() resp. the GET_APIC_ID calls (see patch below).
The results of this experiment can be found at "second oprofile result" below. When I interpret the results correctly, then it looks as if the apic_read() is actually eating up the performance. As this call "leaves" the CPU internal "area" and accesses the external APIC this sounds sensible.
I think this is done here to detect the current CPU.
Is it possible to detect differently or to store that information somehow with a thread (TLS) to avoid requesting it frequently?

I hope that helps a little bit to identify this issue and (perhaps) to find a faster solution. Thanks for all feedback on this!

Regards

Mathias

------------ Begin of patch -----------------

ipipe.c 2007-05-11 08:47:41.000000000 +0200 renamed from ipipe.c.orig 2007-05-09 16:16:32.000000000 +0200 +18 -1
@@ -72,13 +72,30 @@
 
 int (*__ipipe_logical_cpuid)(void) = &__ipipe_boot_cpuid;
 
+
+unsigned long __ipipe_hard_cpuid_apic_read(void)
+{
+    return apic_read(APIC_ID);
+}
+
+unsigned __ipipe_hard_cpuid_get_apic_id(unsigned long apic)
+{
+        return GET_APIC_ID(apic);
+}
+
+
 static notrace int __ipipe_hard_cpuid(void)
 {
        unsigned long flags;
        int cpu;
+        unsigned long apic;
+        unsigned apic_id;
 
        local_irq_save_hw_notrace(flags);
-       cpu = __ipipe_apicid_2_cpu[GET_APIC_ID(apic_read(APIC_ID))];
+       // cpu = __ipipe_apicid_2_cpu[GET_APIC_ID(apic_read(APIC_ID))];
+        apic = __ipipe_hard_cpuid_apic_read();
+        apic_id = __ipipe_hard_cpuid_get_apic_id(apic);
+        cpu = __ipipe_apicid_2_cpu[apic_id];
        local_irq_restore_hw_notrace(flags);
        return cpu;
 }

--------- End of patch

------------- First oprofile result: -----------------------------
Using default event: GLOBAL_POWER_EVENTS:100000:1:1:1
Daemon started.
Profiler running.
delta is 50404054895 per step: 5040
Stopping profiling.
CPU: P4 / Xeon, speed 3192.16 MHz (estimated)
Counted GLOBAL_POWER_EVENTS events (time during which processor is not stopped) with a unit mask of 0x01 (ma ndatory) count 100000

samples  %        image name               app name                 symbol name
6273     56.8928  vmlinux                  vmlinux                  __ipipe_hard_cpuid
900       8.1625  vmlinux                  vmlinux                  rt_sem_v
694       6.2942  vmlinux                  vmlinux                  xnregistry_fetch
321       2.9113  vmlinux                  vmlinux                  __ipipe_dispatch_event
293       2.6574  bash                     bash                     (no symbols)
250       2.2674  vmlinux                  vmlinux                  hrtimer_run_queues
227       2.0588  libc-2.3.6.so            libc-2.3.6.so            (no symbols)
140       1.2697  vmlinux                  vmlinux                  delay_tsc
123       1.1155  libnative.so.0.0.0       libnative.so.0.0.0       rt_sem_p
103       0.9342  vmlinux                  vmlinux                  __ipipe_stall_root
78        0.7074  vmlinux                  vmlinux                  __ipipe_test_and_stall_root
67        0.6077  vmlinux                  vmlinux                  apic_timer_interrupt
66        0.5986  vmlinux                  vmlinux                  sysenter_past_esp
60        0.5442  vmlinux                  vmlinux                  __ipipe_restore_pipeline_head
56        0.5079  vmlinux                  vmlinux                  do_wp_page
56        0.5079  vmlinux                  vmlinux                  search_by_key
54        0.4898  oprofiled                oprofiled                (no symbols)
41        0.3718  vmlinux                  vmlinux                  __ipipe_sync_stage
37        0.3356  ld-2.3.6.so              ld-2.3.6.so              do_lookup_x
37        0.3356  vmlinux                  vmlinux                  __handle_mm_fault
25        0.2267  vmlinux                  vmlinux                  __ipipe_handle_exception
25        0.2267  vmlinux                  vmlinux                  find_get_page
25        0.2267  vmlinux                  vmlinux                  get_page_from_freelist
25        0.2267  vmlinux                  vmlinux                  run_timer_softirq
25        0.2267  vmlinux                  vmlinux                  scheduler_tick
24        0.2177  vmlinux                  vmlinux                  sysenter_exit
23        0.2086  vmlinux                  vmlinux                  __ipipe_syscall_root
23        0.2086  vmlinux                  vmlinux                  __ipipe_unstall_root
22        0.1995  libnative.so.0.0.0       libnative.so.0.0.0       rt_sem_v
21        0.1905  vmlinux                  vmlinux                  __ipipe_test_root
21        0.1905  vmlinux                  vmlinux                  ata_bmdma_start
19        0.1723  ld-2.3.6.so              ld-2.3.6.so              strcmp
19        0.1723  vmlinux                  vmlinux                  ata_altstatus
19        0.1723  vmlinux                  vmlinux                  ata_bmdma_irq_clear
18        0.1633  oprofile                 oprofile                 (no symbols)
18        0.1633  vmlinux                  vmlinux                  find_vma
18        0.1633  vmlinux                  vmlinux                  flush_tlb_page
18        0.1633  vmlinux                  vmlinux                  release_pages
18        0.1633  vmlinux                  vmlinux                  unmap_vmas
17        0.1542  ld-2.3.6.so              ld-2.3.6.so              _dl_relocate_object
17        0.1542  vmlinux                  vmlinux                  __ipipe_unstall_iret_root

---------------------------------------------

------------- Second oprofile result: -----------------------------
Using default event: GLOBAL_POWER_EVENTS:100000:1:1:1
Daemon started.
Profiler running.
delta is 51846556098 per step: 5184
Stopping profiling.
CPU: P4 / Xeon, speed 3192.33 MHz (estimated)
Counted GLOBAL_POWER_EVENTS events (time during which processor is not stopped) with a unit mask of 0x01 (ma ndatory) count 100000

samples  %        image name               app name                 symbol name
4350     39.4307  vmlinux                  vmlinux                  __ipipe_hard_cpuid_apic_read
2336     21.1748  vmlinux                  vmlinux                  __ipipe_hard_cpuid
306       2.7737  bash                     bash                     (no symbols)
287       2.6015  vmlinux                  vmlinux                  __ipipe_dispatch_event
276       2.5018  vmlinux                  vmlinux                  sysenter_past_esp
269       2.4384  vmlinux                  vmlinux                  hrtimer_run_queues
264       2.3930  vmlinux                  vmlinux                  __ipipe_syscall_root
245       2.2208  vmlinux                  vmlinux                  xnregistry_fetch
209       1.8945  libc-2.3.6.so            libc-2.3.6.so            (no symbols)
173       1.5682  vmlinux                  vmlinux                  rt_sem_v
154       1.3959  vmlinux                  vmlinux                  __ipipe_restore_pipeline_head
128       1.1603  vmlinux                  vmlinux                  __copy_from_user_ll_nozero
102       0.9246  vmlinux                  vmlinux                  delay_tsc
100       0.9065  vmlinux                  vmlinux                  __ipipe_stall_root
100       0.9065  vmlinux                  vmlinux                  hisyscall_event
90        0.8158  vmlinux                  vmlinux                  apic_timer_interrupt
89        0.8067  vmlinux                  vmlinux                  __ipipe_test_and_stall_root
80        0.7252  vmlinux                  vmlinux                  rt_sem_p
74        0.6708  vmlinux                  vmlinux                  sysenter_exit
58        0.5257  vmlinux                  vmlinux                  do_wp_page
53        0.4804  oprofiled                oprofiled                (no symbols)
53        0.4804  vmlinux                  vmlinux                  search_by_key
50        0.4532  vmlinux                  vmlinux                  __ipipe_sync_stage
40        0.3626  vmlinux                  vmlinux                  __rt_sem_v
39        0.3535  vmlinux                  vmlinux                  __rt_sem_p
31        0.2810  ld-2.3.6.so              ld-2.3.6.so              do_lookup_x
30        0.2719  vmlinux                  vmlinux                  find_get_page
27        0.2447  vmlinux                  vmlinux                  ata_bmdma_start
24        0.2175  vmlinux                  vmlinux                  __ipipe_unstall_root
24        0.2175  vmlinux                  vmlinux                  run_timer_softirq
23        0.2085  vmlinux                  vmlinux                  unmap_vmas
22        0.1994  vmlinux                  vmlinux                  flush_tlb_page
21        0.1904  ld-2.3.6.so              ld-2.3.6.so              strcmp
21        0.1904  vmlinux                  vmlinux                  __handle_mm_fault
19        0.1722  vmlinux                  vmlinux                  __ipipe_test_root
18        0.1632  vmlinux                  vmlinux                  do_page_fault
17        0.1541  oprofile                 oprofile                 (no symbols)
17        0.1541  vmlinux                  vmlinux                  __ipipe_handle_exception
16        0.1450  vmlinux                  vmlinux                  __d_lookup
15        0.1360  vmlinux                  vmlinux                  filemap_nopage
15        0.1360  vmlinux                  vmlinux                  get_page_from_freelist
15        0.1360  vmlinux                  vmlinux                  page_remove_rmap
15        0.1360  vmlinux                  vmlinux                  restore_nocheck_notrace
14        0.1269  vmlinux                  vmlinux                  _atomic_dec_and_lock
14        0.1269  vmlinux                  vmlinux                  copy_page_range
14        0.1269  vmlinux                  vmlinux                  scheduler_tick
12        0.1088  ld-2.3.6.so              ld-2.3.6.so              _dl_relocate_object
12        0.1088  vmlinux                  vmlinux                  __find_get_block
12        0.1088  vmlinux                  vmlinux                  __ipipe_unstall_iret_root

--------------------------------------------------------------

Quoted 11 lines

define CONFIG_XENO_OPT_DEBUG and CONFIG_DEBUG_KERNEL/CONFIG_DEBUG_INFO to have symbols and all. OProfile is only able to look up virtual address
when debug symbols are present in file. You may have to pass the --vmlinux option to opcontrol. Compiling Xenomai like suggested by Jan will make your life easier, and will bring you an extra mini speedup if you're in that kind of business.

Signature · 1 line
Stephane
Signature · 9 lines
Mathias Koehrer
[email protected]


Viel oder wenig? Schnell oder langsam? Unbegrenzt surfen + telefonieren
ohne Zeit- und Volumenbegrenzung? DAS TOP ANGEBOT JETZT bei Arcor: günstig
und schnell mit DSL - das All-Inclusive-Paket für clevere Doppel-Sparer,
nur  39,85 €  inkl. DSL- und ISDN-Grundgebühr!
http://www.arcor.de/rd/emf-dsl-2
author
#1 lore raw

Hi!

As written in my mail before, I measured that the time is lost in __ipipe_hard_cpuid. This function will only be called with a SMP setup.
I have now disabled SMP in my kernel (but enabled APIC and IO APIC) to do the same experiment. And the result with a UP system is dramatically better!!!!
It is now less than 1microsecond per loop (compared against about 5 microseconds in SMP). See below the oprofile output for UP.

Regards

Mathias
--------------------------------------------- oprofile output for UP ---------------- Using default event: GLOBAL_POWER_EVENTS:100000:1:1:1
Daemon started.
Profiler running.
delta is 9190565309 per step: 919
Stopping profiling.
CPU: P4 / Xeon, speed 3192.2 MHz (estimated)
Counted GLOBAL_POWER_EVENTS events (time during which processor is not stopped) with a unit mask of 0x01 (ma ndatory) count 100000

samples  %        image name               app name                 symbol name
1669     18.5115  vmlinux                  vmlinux                  __ipipe_dispatch_event
985      10.9250  vmlinux                  vmlinux                  sysenter_past_esp
711       7.8860  vmlinux                  vmlinux                  xnregistry_fetch
550       6.1003  libc-2.3.6.so            libc-2.3.6.so            (no symbols)
479       5.3128  vmlinux                  vmlinux                  __copy_from_user_ll_nozero
474       5.2573  vmlinux                  vmlinux                  __ipipe_restore_pipeline_head
471       5.2240  vmlinux                  vmlinux                  rt_sem_p
451       5.0022  vmlinux                  vmlinux                  search_by_key
294       3.2609  bash                     bash                     (no symbols)
240       2.6619  vmlinux                  vmlinux                  hisyscall_event
237       2.6287  vmlinux                  vmlinux                  rt_sem_v
236       2.6176  libnative.so.0.0.0       libnative.so.0.0.0       rt_sem_p
156       1.7303  libnative.so.0.0.0       libnative.so.0.0.0       rt_sem_v
145       1.6083  performance              performance              mytaska
61        0.6766  vmlinux                  vmlinux                  do_wp_page
60        0.6655  vmlinux                  vmlinux                  acpi_pm_read
56        0.6211  vmlinux                  vmlinux                  __find_get_block
53        0.5878  vmlinux                  vmlinux                  memset
52        0.5768  oprofiled                oprofiled                (no symbols)
43        0.4769  ld-2.3.6.so              ld-2.3.6.so              do_lookup_x
34        0.3771  syslogd                  syslogd                  (no symbols)
34        0.3771  vmlinux                  vmlinux                  release_console_sem
33        0.3660  vmlinux                  vmlinux                  __ipipe_unstall_root
33        0.3660  vmlinux                  vmlinux                  memcpy
31        0.3438  vmlinux                  vmlinux                  do_journal_end
30        0.3327  klogd                    klogd                    (no symbols)
30        0.3327  vmlinux                  vmlinux                  unmap_vmas
29        0.3217  oprofile                 oprofile                 (no symbols)
27        0.2995  vmlinux                  vmlinux                  __copy_from_user_ll
27        0.2995  vmlinux                  vmlinux                  do_con_write
27        0.2995  vmlinux                  vmlinux                  ll_rw_block
25        0.2773  vmlinux                  vmlinux                  do_syslog
21        0.2329  ld-2.3.6.so              ld-2.3.6.so              strcmp
21        0.2329  vmlinux                  vmlinux                  get_page_from_freelist
21        0.2329  vmlinux                  vmlinux                  sysenter_exit
21        0.2329  vmlinux                  vmlinux                  write_chan
19        0.2107  vmlinux                  vmlinux                  __handle_mm_fault
19        0.2107  vmlinux                  vmlinux                  __ipipe_unstall_iret_root
19        0.2107  vmlinux                  vmlinux                  generic_file_buffered_write
18        0.1996  vmlinux                  vmlinux                  bit_waitqueue
18        0.1996  vmlinux                  vmlinux                  reiserfs_update_sd_size
17        0.1886  vmlinux                  vmlinux                  journal_mark_dirty
17        0.1886  vmlinux                  vmlinux                  reiserfs_prepare_for_journal
17        0.1886  vmlinux                  vmlinux                  vsnprintf
16        0.1775  vmlinux                  vmlinux                  unlock_buffer
15        0.1664  vmlinux                  vmlinux                  memmove
15        0.1664  vmlinux                  vmlinux                  number
14        0.1553  vmlinux                  vmlinux                  __link_path_walk
14        0.1553  vmlinux                  vmlinux                  conv_uni_to_pc
14        0.1553  vmlinux                  vmlinux                  kmem_cache_alloc
14        0.1553  vmlinux                  vmlinux                  radix_tree_lookup
13        0.1442  ld-2.3.6.so              ld-2.3.6.so              _dl_relocate_object
13        0.1442  vmlinux                  vmlinux                  get_num_ver
12        0.1331  vmlinux                  vmlinux                  __d_lookup
12        0.1331  vmlinux                  vmlinux                  __ipipe_handle_exception
11        0.1220  vmlinux                  vmlinux                  __block_prepare_write

l--------------------------------------

Signature · 9 lines
Mathias Koehrer
[email protected]


Viel oder wenig? Schnell oder langsam? Unbegrenzt surfen + telefonieren
ohne Zeit- und Volumenbegrenzung? DAS TOP ANGEBOT JETZT bei Arcor: günstig
und schnell mit DSL - das All-Inclusive-Paket für clevere Doppel-Sparer,
nur  39,85 €  inkl. DSL- und ISDN-Grundgebühr!
http://www.arcor.de/rd/emf-dsl-2
#2 lore raw
In reply to M. Koehrer
Quoted 15 lines · M. Koehrer

Hi,

here are my latest results:
I included xeno_nucleus and xeno_native into the kernel. (see first oprofile result). (I also added debug information to the kernel which was of no additional help).
I detected that most of the time was spent in __ipipe_hard_cpuid.
I looked at that routine and broke that using 2 additional helper functions that are use to "monitor" the apic_read() resp. the GET_APIC_ID calls (see patch below).
The results of this experiment can be found at "second oprofile result" below. When I interpret the results correctly, then it looks as if the apic_read() is actually eating up the performance. As this call "leaves" the CPU internal "area" and accesses the external APIC this sounds sensible.
I think this is done here to detect the current CPU.
Is it possible to detect differently or to store that information somehow with a thread (TLS) to avoid requesting it frequently?

Actually, this is how Linux works. On x86, it used to derive the CPU ID from current_thread_info, which required a valid "current" what Xenomai kernel threads do not guarantee (their stack size is variable, but Linux needs a fixed, predefined one to resolve current). Nowadays (>= 2.6.20) we have PDA (Per-CPU Data Array) on x86, a register based approach, and I think we could safely use it for I-pipe/Xenomai as well. That would make A LOT of thinks easier, because replacing smp_processor_id all over the place to make some code I-pipe aware is a boring job (I recently did so for LTTng...) - and it results in slower code on SMP.

As an experiment, you could try implementing the cpuid lookup via raw_smp_processor_id(), given you use 2.6.20. Hmm, maybe/likely some Xenomai tuning is required as well to make sure that the PDA is set correctly when a Xenomai task is created/migrated. Results welcome!

Jan

author
#3 lore raw

Hi Jan,

Quoted 4 lines

As an experiment, you could try implementing the cpuid lookup via raw_smp_processor_id(), given you use 2.6.20. Hmm, maybe/likely some Xenomai tuning is required as well to make sure that the PDA is set correctly when a Xenomai task is created/migrated. Results welcome!

I have applied a patch (below) to my SMP kernel 2.6.20.4.
And actually the results are now much better.
I have been back on my "original" kernel version without the oprofile support. The "original" ipipe.c delivered times of 4.5 us per step with my test project. The patched ipipe.c (using raw_smp_processor_id()) delivers 1.25 us per step! This is really an excellent improvement!
I do not know if there are any side effects - if not: why not using this method instead of the previous one...

Regards

Mathias

------------------------- patch start ------------------

ipipe.c 2007-05-11 10:00:44.000000000 +0200 renamed from ipipe.c.orig 2007-05-09 16:16:32.000000000 +0200 +4 -0
@@ -74,6 +74,7 @@
 
 static notrace int __ipipe_hard_cpuid(void)
 {
+#if 0
        unsigned long flags;
        int cpu;
 
@@ -81,6 +82,9 @@
        cpu = __ipipe_apicid_2_cpu[GET_APIC_ID(apic_read(APIC_ID))];
        local_irq_restore_hw_notrace(flags);
        return cpu;
+#endif
+        return raw_smp_processor_id();
+
 }
 
 #endif /* CONFIG_SMP */

------------------------- patch end ------------------

Signature · 9 lines
Mathias Koehrer
[email protected]


Viel oder wenig? Schnell oder langsam? Unbegrenzt surfen + telefonieren
ohne Zeit- und Volumenbegrenzung? DAS TOP ANGEBOT JETZT bei Arcor: günstig
und schnell mit DSL - das All-Inclusive-Paket für clevere Doppel-Sparer,
nur  39,85 €  inkl. DSL- und ISDN-Grundgebühr!
http://www.arcor.de/rd/emf-dsl-2
#4 lore raw
Quoted 7 lines · M. Koehrer

Hi Jan,

Quoted 4 lines

As an experiment, you could try implementing the cpuid lookup via raw_smp_processor_id(), given you use 2.6.20. Hmm, maybe/likely some Xenomai tuning is required as well to make sure that the PDA is set correctly when a Xenomai task is created/migrated. Results welcome!

Quoted 8 lines

I have applied a patch (below) to my SMP kernel 2.6.20.4.
And actually the results are now much better.
I have been back on my "original" kernel version without the oprofile support. The "original" ipipe.c delivered times of 4.5 us per step with my test project. The patched ipipe.c (using raw_smp_processor_id()) delivers 1.25 us per step! This is really an excellent improvement!
I do not know if there are any side effects - if not: why not using this method instead of the previous one...

We are going to use it. PDAs have been recently introduced and the I-pipe patch for x86_64 already uses them, but I simply overlooked the fact that they have been made available for x86 too. Will fix, thanks.

Quoted 29 lines

Regards

Mathias

------------------------- patch start ------------------

ipipe.c 2007-05-11 10:00:44.000000000 +0200 renamed from ipipe.c.orig 2007-05-09 16:16:32.000000000 +0200 +4 -1
@@ -74,6 +74,7 @@
 
 static notrace int __ipipe_hard_cpuid(void)
 {
+#if 0
        unsigned long flags;
        int cpu;
 
@@ -81,6 +82,9 @@
        cpu = __ipipe_apicid_2_cpu[GET_APIC_ID(apic_read(APIC_ID))];
        local_irq_restore_hw_notrace(flags);
        return cpu;
+#endif
+        return raw_smp_processor_id();
+
 }
 
 #endif /* CONFIG_SMP */
------------------------- patch end ------------------
Signature · 1 line
Philippe.