Skip to contentSkip to conversation

Mutex enhancement for Auto relaxed threads

Xenomai Makarand Pradhan 4 participants 9 messages Nov 22, 2012 – Nov 27, 2012

lore
Share & tools
lore
#0 lore raw

Greetings,

I have a proposal for enhancing the process of locking a real-time mutex in auto-relaxed Xenomai threads. It will save a lot of CPU cycles in some scenarios, and I would like to get your expert opinion about it (I have coded the enhancement for native skin and changes are attached to this email).

Xenomai mutex is designed in such a way that trapping to the kernel is not needed.This is a very efficient design which saves a great deal of CPU cycles.
However, there is an exception to this efficient behavior: if the Xenomai thread is an auto-relaxed thread (i.e. running with priority 0), locking a mutex always traps to the kernel because the current implementation handles auto-relaxing upon exiting the system call.

Look at this scenario:

A Xenomai thread
1) locks a mutex (which is an OUTER mutex in this scenario)
2) then loops multiple times through some operation that requires locking an unlocking some other mutex (INNER mutex)
3) unlocks the OUTER mutex

If the thread is a real-time thread, locking mutexes is done efficiently. If the thread is auto-relaxed though, both OUTER and INNER mutexes cause trapping to the kernel which is not really needed. Please note that locking the OUTER mutex already moves the thread to the primary domain, so handling the INNER mutex could be done as if the thread was a real-time (non-auto-relaxed) thread.

Enhancements:
The mutex code was modified in such a way that priority 0 (auto-relaxed) thread will do a system call only once when grabbing and releasing the outtermost mutex. The inner mutexes will not jump into the kernel. This change would significantly save CPU cycles by using the user space fast mutexes when the thread is not relaxed after grabbing the outtermost mutex.

I would highly appreciate your comments.

Best Regards,
Makarand
Software Engineer
RuggedCom Inc.
www.ruggedcom.com
300 Applewood Crescent
Concord, Ontario, L4K 5C7
Tel: (905)482-4523
Fax: (905)856-1995

Signature · 14 lines
___________________________________________________________________________
NOTICE OF CONFIDENTIALITY:
This e-mail and any attachments may contain confidential and privileged information.  If you are
not the intended recipient, please notify the sender immediately by return e-mail and delete this
e-mail and any copies.  Any dissemination or use of this information by a person other than the
intended recipient is unauthorized and may be illegal.
_____________________________________________________________________

 

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: xenomai_changes
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20121122/fc104aa6/attachment.ksh>
#1 lore raw
Quoted 7 lines · Makarand Pradhan

Greetings,

I have a proposal for enhancing the process of locking a real-time mutex in auto-relaxed Xenomai threads. It will save a lot of CPU cycles in some scenarios, and I would like to get your expert opinion about it (I have coded the enhancement for native skin and changes are attached to this email).

Xenomai mutex is designed in such a way that trapping to the kernel is not needed.This is a very efficient design which saves a great deal of CPU cycles.
However, there is an exception to this efficient behavior: if the Xenomai thread is an auto-relaxed thread (i.e. running with priority 0), locking a mutex always traps to the kernel because the current implementation handles auto-relaxing upon exiting the system call.

Quoted 6 lines

Look at this scenario:

A Xenomai thread
1) locks a mutex (which is an OUTER mutex in this scenario)
2) then loops multiple times through some operation that requires locking an unlocking some other mutex (INNER mutex)
3) unlocks the OUTER mutex

I did not look in details at your code, but what if I do

lock mutex1
lock mutex2
unlock mutex1
unlock mutex2

?

Signature · 1 line
					    Gilles.
#2 lore raw

Hi Gilles,

lock mutex1
lock mutex2
unlock mutex1
unlock mutex2

As per my current implementation, I expect the thread to relax after unlocking mutex1. If another RT thread starts waiting on mutex2, I expect the thread holding the mutex will harden and start running in primary again. Will quickly test this and get back.

I did consider this scenario but since we never use such a behaviour in our system, did not pursue it further.

This brings up another question that I was contemplating. Do we want to harden unless there is another RT thread wanting this mutex? If not then the implementation becomes simpler and the scenario mentioned would also get addressed automatically.

Rgds,
Mak.

Quoted 9 lines

I did not look in details at your code, but what if I do

lock mutex1
lock mutex2
unlock mutex1
unlock mutex2

?

Signature · 7 lines
___________________________________________________________________________
NOTICE OF CONFIDENTIALITY:
This e-mail and any attachments may contain confidential and privileged information.  If you are
not the intended recipient, please notify the sender immediately by return e-mail and delete this
e-mail and any copies.  Any dissemination or use of this information by a person other than the
intended recipient is unauthorized and may be illegal.
_____________________________________________________________________
#3 lore raw
Quoted 1 line

Will quickly test this and get back.

Yes. That's pretty much how the system behaves.

Rgds,
Makarand.

Test program attached:
Results:
task1: Prio 0 Auto relaxed
task2: Prio 10. RT.

root@ruggedcom:~# ./tc5 0 10 1
Format: tc5 prio1 prio2 mux_iterations.
Spawning: task1
task2: Mutexes created
task2: Mutexes in memory
Running task1: status: b00380
task1: Global test: 25
task1: Acquiring mux1
rt_mutex_acquire_inner: Did system call. lockcnt: 1, dsof: 1
task1: Acquired mux1. rc: 0
task1: status: b00180
task1: Acquiring mux2
task1: Acquired mux2. rc: 0
task1: status: b00180
task1: Releasing mux1
rt_mutex_release: Doing system call. lockcnt: 0, dsof: 1
task1: Released mux1. rc: 0
task1: status: b00380 <- Relaxed task1 while holding mutex 2.
task1: bprio: 0 cprio: 0 status: b00380 info.status: b00380
task2: Acquiring mux2.
task1: bprio: 0 cprio: 10 status: b01180 info.status: b01180 <- task1 jumps to primary again..
task1: Releasing mux2
rt_mutex_release: Doing system call. lockcnt: 0, dsof: 1
rt_mutex_acquire_inner: Did system call. lockcnt: 1, dsof: 1
task2: Acquired mux2.
task2: Releasing mux2
rt_mutex_release: Doing system call. lockcnt: 0, dsof: 1
task2: Released mux2. rc: 0
task2: status: 300180
task1: Released mux2. rc: 0
task1: status: b00380
Quoted 34 lines · Makarand Pradhan

Hi Gilles,

lock mutex1
lock mutex2
unlock mutex1
unlock mutex2

As per my current implementation, I expect the thread to relax after unlocking mutex1. If another RT thread starts waiting on mutex2, I expect the thread holding the mutex will harden and start running in primary again. Will quickly test this and get back.

I did consider this scenario but since we never use such a behaviour in our system, did not pursue it further.

This brings up another question that I was contemplating. Do we want to harden unless there is another RT thread wanting this mutex? If not then the implementation becomes simpler and the scenario mentioned would also get addressed automatically.

Rgds,
Mak.

Quoted 9 lines

I did not look in details at your code, but what if I do

lock mutex1
lock mutex2
unlock mutex1
unlock mutex2

?

Signature · 17 lines
___________________________________________________________________________
NOTICE OF CONFIDENTIALITY:
This e-mail and any attachments may contain confidential and privileged information.  If you are
not the intended recipient, please notify the sender immediately by return e-mail and delete this
e-mail and any copies.  Any dissemination or use of this information by a person other than the
intended recipient is unauthorized and may be illegal.
_____________________________________________________________________

 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: tc5.c
Type: text/x-csrc
Size: 4314 bytes
Desc: not available
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20121122/9631fb18/attachment.c>
#4 lore raw

Hi,

Made a small change.

So re sending changes.

Rgds,
Makarand.

Quoted 79 lines · Makarand Pradhan
Quoted 1 line

Will quickly test this and get back.

Yes. That's pretty much how the system behaves.

Rgds,
Makarand.

Test program attached:
Results:
task1: Prio 0 Auto relaxed
task2: Prio 10. RT.

root@ruggedcom:~# ./tc5 0 10 1
Format: tc5 prio1 prio2 mux_iterations.
Spawning: task1
task2: Mutexes created
task2: Mutexes in memory
Running task1: status: b00380
task1: Global test: 25
task1: Acquiring mux1
rt_mutex_acquire_inner: Did system call. lockcnt: 1, dsof: 1
task1: Acquired mux1. rc: 0
task1: status: b00180
task1: Acquiring mux2
task1: Acquired mux2. rc: 0
task1: status: b00180
task1: Releasing mux1
rt_mutex_release: Doing system call. lockcnt: 0, dsof: 1
task1: Released mux1. rc: 0
task1: status: b00380<- Relaxed task1 while holding mutex 2.
task1: bprio: 0 cprio: 0 status: b00380 info.status: b00380
task2: Acquiring mux2.
task1: bprio: 0 cprio: 10 status: b01180 info.status: b01180<- task1
jumps to primary again..
task1: Releasing mux2
rt_mutex_release: Doing system call. lockcnt: 0, dsof: 1
rt_mutex_acquire_inner: Did system call. lockcnt: 1, dsof: 1
task2: Acquired mux2.
task2: Releasing mux2
rt_mutex_release: Doing system call. lockcnt: 0, dsof: 1
task2: Released mux2. rc: 0
task2: status: 300180
task1: Released mux2. rc: 0
task1: status: b00380
Quoted 33 lines · Makarand Pradhan

Hi Gilles,

lock mutex1
lock mutex2
unlock mutex1
unlock mutex2

As per my current implementation, I expect the thread to relax after unlocking mutex1. If another RT thread starts waiting on mutex2, I expect the thread holding the mutex will harden and start running in primary again. Will quickly test this and get back.

I did consider this scenario but since we never use such a behaviour in our system, did not pursue it further.

This brings up another question that I was contemplating. Do we want to harden unless there is another RT thread wanting this mutex? If not then the implementation becomes simpler and the scenario mentioned would also get addressed automatically.

Rgds,
Mak.

Quoted 9 lines

I did not look in details at your code, but what if I do

lock mutex1
lock mutex2
unlock mutex1
unlock mutex2

?

Signature · 14 lines
___________________________________________________________________________
NOTICE OF CONFIDENTIALITY:
This e-mail and any attachments may contain confidential and privileged information.  If you are
not the intended recipient, please notify the sender immediately by return e-mail and delete this
e-mail and any copies.  Any dissemination or use of this information by a person other than the
intended recipient is unauthorized and may be illegal.
_____________________________________________________________________

 

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: xenomai_changes
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20121122/bd0c4072/attachment.ksh>
#5 lore raw
In reply to Gilles Chanteperdrix

Although you can use mutexes in this order, why would you want to do that? Logically, unlocking mutex1 would mean finishing some operation started when mutex1 was locked. If as a part of the operation you locked mutex2 and haven't unlocked it yet, that would mean you haven't completed your operation yet, and unlocking mutex1 at this point would probably be a bug. Am I wrong?

Michael Pustylnik, P.Eng.
Senior Software Engineer
RuggedCom Inc.
www.ruggedcom.com
300 Applewood Crescent
Concord, Ontario, L4K 5C7
Tel: (905)482-4523
Fax: (905)856-1995

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Gilles Chanteperdrix
Sent: Thursday, November 22, 2012 11:15 AM
To: Makarand Pradhan
Cc: [email protected]
Subject: Re: [Xenomai] Mutex enhancement for Auto relaxed threads

Quoted 7 lines · Makarand Pradhan

Greetings,

I have a proposal for enhancing the process of locking a real-time mutex in auto-relaxed Xenomai threads. It will save a lot of CPU cycles in some scenarios, and I would like to get your expert opinion about it (I have coded the enhancement for native skin and changes are attached to this email).

Xenomai mutex is designed in such a way that trapping to the kernel is not needed.This is a very efficient design which saves a great deal of CPU cycles.
However, there is an exception to this efficient behavior: if the Xenomai thread is an auto-relaxed thread (i.e. running with priority 0), locking a mutex always traps to the kernel because the current implementation handles auto-relaxing upon exiting the system call.

Quoted 6 lines

Look at this scenario:

A Xenomai thread
1) locks a mutex (which is an OUTER mutex in this scenario)
2) then loops multiple times through some operation that requires locking an unlocking some other mutex (INNER mutex)
3) unlocks the OUTER mutex

I did not look in details at your code, but what if I do

lock mutex1
lock mutex2
unlock mutex1
unlock mutex2

?

Signature · 10 lines
					    Gilles.

_______________________________________________
Xenomai mailing list
[email protected]
http://www.xenomai.org/mailman/listinfo/xenomai

No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 8.5.455 / Virus Database: 271.1.1/5393 - Release Date: 11/21/12 20:05:00
#6 lore raw
Quoted 1 line · Michael Pustylnik

Although you can use mutexes in this order, why would you want to do that? Logically, unlocking mutex1 would mean finishing some operation started when mutex1 was locked. If as a part of the operation you locked mutex2 and haven't unlocked it yet, that would mean you haven't completed your operation yet, and unlocking mutex1 at this point would probably be a bug. Am I wrong?

I'm not aware that POSIX or some other APIs Xenomai implements forbid such an ordering. And you never know if there is weird convoluted legacy code out there that depends on exactly this.

However, I think you can avoid the limitation by attaching the property "trap to kernel on mutex lock" to the thread instead of some mutex, analogously to xeno_current_mode.

Jan

PS: Please no top postings!

Quoted 31 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Gilles Chanteperdrix
Sent: Thursday, November 22, 2012 11:15 AM
To: Makarand Pradhan
Cc: [email protected]
Subject: Re: [Xenomai] Mutex enhancement for Auto relaxed threads

Quoted 7 lines · Makarand Pradhan

Greetings,

I have a proposal for enhancing the process of locking a real-time mutex in auto-relaxed Xenomai threads. It will save a lot of CPU cycles in some scenarios, and I would like to get your expert opinion about it (I have coded the enhancement for native skin and changes are attached to this email).

Xenomai mutex is designed in such a way that trapping to the kernel is not needed.This is a very efficient design which saves a great deal of CPU cycles.
However, there is an exception to this efficient behavior: if the Xenomai thread is an auto-relaxed thread (i.e. running with priority 0), locking a mutex always traps to the kernel because the current implementation handles auto-relaxing upon exiting the system call.

Quoted 6 lines

Look at this scenario:

A Xenomai thread
1) locks a mutex (which is an OUTER mutex in this scenario)
2) then loops multiple times through some operation that requires locking an unlocking some other mutex (INNER mutex)
3) unlocks the OUTER mutex

I did not look in details at your code, but what if I do

lock mutex1
lock mutex2
unlock mutex1
unlock mutex2

?

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#7 lore raw
Quoted 10 lines · Jan Kiszka
Quoted 1 line · Michael Pustylnik

Although you can use mutexes in this order, why would you want to do that? Logically, unlocking mutex1 would mean finishing some operation started when mutex1 was locked. If as a part of the operation you locked mutex2 and haven't unlocked it yet, that would mean you haven't completed your operation yet, and unlocking mutex1 at this point would probably be a bug. Am I wrong?

I'm not aware that POSIX or some other APIs Xenomai implements forbid such an ordering. And you never know if there is weird convoluted legacy code out there that depends on exactly this.

However, I think you can avoid the limitation by attaching the property "trap to kernel on mutex lock" to the thread instead of some mutex, analogously to xeno_current_mode.

I think the simplest solution to implement this is to share the resources count between user and kernel, using a pthread_key in user-space. However, I really do not see how to do this without breaking the ABI, so, this really is material for xenomai-forge, not for 2.6.

Signature · 1 line
					    Gilles.
#8 lore raw
Quoted 8 lines · Gilles Chanteperdrix
Quoted 7 lines · Jan Kiszka
Quoted 1 line · Michael Pustylnik

Although you can use mutexes in this order, why would you want to do that? Logically, unlocking mutex1 would mean finishing some operation started when mutex1 was locked. If as a part of the operation you locked mutex2 and haven't unlocked it yet, that would mean you haven't completed your operation yet, and unlocking mutex1 at this point would probably be a bug. Am I wrong?

I'm not aware that POSIX or some other APIs Xenomai implements forbid such an ordering. And you never know if there is weird convoluted legacy code out there that depends on exactly this.

Quoted 8 lines
Quoted 3 lines

However, I think you can avoid the limitation by attaching the property "trap to kernel on mutex lock" to the thread instead of some mutex, analogously to xeno_current_mode.

I think the simplest solution to implement this is to share the resources count between user and kernel, using a pthread_key in user-space. However, I really do not see how to do this without breaking the ABI, so, this really is material for xenomai-forge, not for 2.6.

For sure.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux