* mutex between processes?
@ 1998-08-04 4:00 Aurel Balmosan
1998-08-05 4:00 ` Jeff Denham
0 siblings, 1 reply; 14+ messages in thread
From: Aurel Balmosan @ 1998-08-04 4:00 UTC (permalink / raw)
Currently we use ipc semaphores to sync access of different
processes to a shared memory region. This appears to be quite
slow. About 25-30% system time are required due to the system
calles made.
Now we want to use mutex. (posix mutex) It does appear that
this can not be used easily for sync different processes on
shared memory (or mmap memory) For SUN-Solaris-2.5.1 it is
written in the manual to the pthread_mutex that it can be used
also for different processes via mmap or shared memory.
(not only for tasks within a process)
Is there something equal on OSF1 (DecUnix-4.0[abcd]) Important
is that no system call need to be made.
Regards,
Aurel Balmosan
--
Aurel Balmosan | Department SE-SW
ORGA Kartensysteme GmbH | phone: +49 5254 991 824
An der Kapelle 2 | fax : +49 5254 991 749
33104 Paderborn, Germany | mailto:ab@orga.com
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-04 4:00 mutex between processes? Aurel Balmosan
@ 1998-08-05 4:00 ` Jeff Denham
1998-08-05 4:00 ` Dirk Grunwald
0 siblings, 1 reply; 14+ messages in thread
From: Jeff Denham @ 1998-08-05 4:00 UTC (permalink / raw)
Aurel Balmosan wrote:
> Currently we use ipc semaphores to sync access of different
> processes to a shared memory region. This appears to be quite
> slow. About 25-30% system time are required due to the system
> calles made.
>
> Now we want to use mutex. (posix mutex) It does appear that
> this can not be used easily for sync different processes on
> shared memory (or mmap memory) For SUN-Solaris-2.5.1 it is
> written in the manual to the pthread_mutex that it can be used
> also for different processes via mmap or shared memory.
> (not only for tasks within a process)
>
> Is there something equal on OSF1 (DecUnix-4.0[abcd]) Important
> is that no system call need to be made.
The interprocess mutexes are called PSHARED (process-shared)
by POSIX. They're supposed to be available in the next major
release of, ahem, Compaq's Digital UNIX. I've lost track
of when that release is due to ship.
Sorry to disappoint on another front: a PSHARED implementation
is probably going to make a system call when there's contention
on the mutex. With no contention, the thread can lock and
go, but with contention, it's probably going to block in the
kernel, and that takes a system call.
__________________________________________________
Jeff Denham (jdenham@brighttiger.com)
Bright Tiger Technologies
SmartCluster� Software for Distributed Web Servers
http://www.brighttiger.com
125 Nagog Park
Acton, MA 01720
Phone: (978) 263-5455 x177
Fax: (978) 263-5547
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-05 4:00 ` Jeff Denham
@ 1998-08-05 4:00 ` Dirk Grunwald
1998-08-07 4:00 ` Aurel Balmosan
0 siblings, 1 reply; 14+ messages in thread
From: Dirk Grunwald @ 1998-08-05 4:00 UTC (permalink / raw)
Jeff Denham <jdenham@brighttiger.com> writes:
> Sorry to disappoint on another front: a PSHARED implementation
> is probably going to make a system call when there's contention
> on the mutex. With no contention, the thread can lock and
> go, but with contention, it's probably going to block in the
> kernel, and that takes a system call.
--
But, in a 'mmap' region, you can also use the native spinlock/critical
section mechanisms. These will differ from machine to machine.
For the alpha, the easiest way to construct an atomic spinlock would
be to use the API provided in /usr/include/alpha/bulitins.h.
E.g., a lock acquire would be:
acquire(volatile int *addr)
{
/* Spin, waiting for location to be available */
for(;;) {
while (*addr != 0);
if ( __ATOMIC_EXCH_LONG(addr, 1) == 0 ) {
break;
}
/* might not be needed, ya never know and
you're not doing much else right now anyway.. */
__MB();
}
}
release(volatile int *addr)
{
*addr = 0;
__MB();
}
--
Dirk Grunwald Assoc. Prof, Univ. of Colorado at Boulder
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-07 4:00 ` Jeff Denham
@ 1998-08-07 4:00 ` Aurel Balmosan
1998-08-07 4:00 ` Monty Brandenberg
1998-08-10 4:00 ` Dirk Grunwald
1 sibling, 1 reply; 14+ messages in thread
From: Aurel Balmosan @ 1998-08-07 4:00 UTC (permalink / raw)
Jeff Denham <jdenham@brighttiger.com> wrote:
> Whether a spinlock is appropriate for your app depends on
> the size/duration of your critical sections. If you'll have
> lots of contention of the lock or the lock is held for
> "long" periods, then the system call is a win. There's
> a crossover point where spinning no longer pays
> off.
Actually I want to use not a spinlock but a 4 byte integer
(unsigned int) to hold an index (actually about 40 million).
It only must be guaranteed that the write can write a value
without a reader getting only a part of the written index.
It does not matter if the reader get the old value. The writers
could be managed via a spinlock or a semaphore that does
not really matter. But the readers must never be blocked.
> The __MB() (memory barrier) is *absolutely* necessary for the spinlock to work
Does it flush the second and third level cache of a processor to the main memory?
I saw also the WMB() command which I see as withdraw memory barrier. Shouldn't
a combination of MB and WMB be used?
Such as: MB(); ATOMIC....(); WMB();
--
Aurel Balmosan | Department SE-SW
ORGA Kartensysteme GmbH | phone: +49 5254 991 824
An der Kapelle 2 | fax : +49 5254 991 749
33104 Paderborn, Germany | mailto:ab@orga.com
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-07 4:00 ` Aurel Balmosan
@ 1998-08-07 4:00 ` Monty Brandenberg
1998-08-09 4:00 ` Aurel Balmosan
0 siblings, 1 reply; 14+ messages in thread
From: Monty Brandenberg @ 1998-08-07 4:00 UTC (permalink / raw)
To: Aurel Balmosan
Aurel Balmosan wrote:
>
> > The __MB() (memory barrier) is *absolutely* necessary for the spinlock to work
>
> Does it flush the second and third level cache of a processor to the main memory?
> I saw also the WMB() command which I see as withdraw memory barrier. Shouldn't
> a combination of MB and WMB be used?
No, cache is coherent in all Alpha systems. MB (memory barrier) and WMB
(write memory barrier) operate on small, cache-like structures called
write buffers. These are misnamed as they are also involved with reading.
Also note that using MB (or WMB) alone does *not* guarantee visibility by
another processor or other system device. This requires slightly more
sophisticated algorithms or the use of interlocked instructions. See section
5.6 of the Alpha SRM for a very important discussion of this. Much of what is
in here applies on other systems as well and is understood by few programmers.
For mutex and semaphore algorithms that avoid expensive interlocked operations,
see papers by Leslie Lamport (of Dec SRC {or WRL?}). For seminal work,
there's C.A.R. Hoare.
Problems due to incorrect SMP algorithms are very difficult to debug. It is
much easier to do the research and get it right the first time....
m
--
Monty Brandenberg, Software Consultant MCB, Inc.
mcbinc@ne.mediaone.net.MAPS.SPAM P.O. Box 426188
mcbinc@world.std.com.MAPS.SPAM Cambridge, MA 02142
617.864.6907
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-05 4:00 ` Dirk Grunwald
@ 1998-08-07 4:00 ` Aurel Balmosan
1998-08-07 4:00 ` Jeff Denham
0 siblings, 1 reply; 14+ messages in thread
From: Aurel Balmosan @ 1998-08-07 4:00 UTC (permalink / raw)
Dirk Grunwald wrote:
> But, in a 'mmap' region, you can also use the native spinlock/critical
> section mechanisms. These will differ from machine to machine.
>
Does it work also on shared memory (I would guess so)And what about DEC-Alpha
with lets say 12 processors?
> /* might not be needed, ya never know and
> you're not doing much else right now anyway.. */
> __MB();
>
What is that? What does a Memory Barrier do and why is it needed?
Bye,
Aurel.
--
Aurel Balmosan | Department SE-SW
ORGA Kartensysteme GmbH | phone: +49 5254 991 824
An der Kapelle 2 | fax : +49 5254 991 749
33104 Paderborn, Germany | mailto:ab@orga.com
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-07 4:00 ` Aurel Balmosan
@ 1998-08-07 4:00 ` Jeff Denham
1998-08-07 4:00 ` Aurel Balmosan
1998-08-10 4:00 ` Dirk Grunwald
0 siblings, 2 replies; 14+ messages in thread
From: Jeff Denham @ 1998-08-07 4:00 UTC (permalink / raw)
Aurel Balmosan wrote:
> Dirk Grunwald wrote:
>
> > But, in a 'mmap' region, you can also use the native spinlock/critical
> > section mechanisms. These will differ from machine to machine.
> >
>
> Does it work also on shared memory (I would guess so)And what about DEC-Alpha
> with lets say 12 processors?
>
> > /* might not be needed, ya never know and
> > you're not doing much else right now anyway.. */
> > __MB();
> >
>
> What is that? What does a Memory Barrier do and why is it needed?
Yes, the spinlock approach will work in any support form
of shared memory.
Whether a spinlock is appropriate for your app depends on
the size/duration of your critical sections. If you'll have
lots of contention of the lock or the lock is held for
"long" periods, then the system call is a win. There's
a crossover point where spinning no longer pays
off.
The __MB() (memory barrier) is *absolutely* necessary for the spinlock to work
on an MP system. In Dirk's example, though,
I'm pretty sure that the __MB() in the release() routine
is in the wrong place -- it should be before the clearing
of the lock, not after. This guarantees that the
updates to the object being locked are seen by other
processors before they see that the lock is clear, grab it,
and see stale state in the object.
The more aggressive your memory subsystem is
at optimizing (i.e., reodering) its traffic to main
memory (can you say 21264?), the more quickly
your missing or misplaced memory barriers
will screw you.
Jeff
__________________________________________________
Jeff Denham (jdenham@brighttiger.com)
Bright Tiger Technologies
SmartCluster� Software for Distributed Web Servers
http://www.brighttiger.com
125 Nagog Park
Acton, MA 01720
Phone: (978) 263-5455 x177
Fax: (978) 263-5547
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-07 4:00 ` Monty Brandenberg
@ 1998-08-09 4:00 ` Aurel Balmosan
0 siblings, 0 replies; 14+ messages in thread
From: Aurel Balmosan @ 1998-08-09 4:00 UTC (permalink / raw)
Monty Brandenberg <mcbinc.MAPS.SPAM@ne.mediaone.net> wrote:
> Problems due to incorrect SMP algorithms are very difficult to debug. It is
> much easier to do the research and get it right the first time....
From this discusion I made following concept. Following structures
are in the shared memory.
Proc 1: spinlock, some data, current_accessed_item
Proc 2:
ItemTable:
Item 1: index_to_the_real_item
Item 2: ..
RealItem:
Item 1: (Some data depending on the item)
Item 2: ...
So now the proc 1 tries to access Item 1:
- lock its spinlock
- read real address of item1 from ItemTable
- set the current_accessed_item to the found address
- unlock its spinlock
- read the data
If a writer (there is only one at a time) want to
change item 1 it do following:
- lock global writer spinlock
- alloc and set a new item in RealItem,
- queue the old address of item 1 in a cleanup-queue.
- write new address into item 1 in ItemTable
- unlock global writer spinlock.
Some server do following:
- lock gloabal writer spinlock
- get an item address out of cleanup-queue
- unlock gloabal writer spinlock.
- check if someone accesses this item currently.
if yes put it again in the queue
if no
lock gloabal writer spinlock
mark the item as reusable
unlock gloabal writer spinlock.
- continue
The critical part is writing the current_accessed_item
and the index_to_the_real_item. It must be guaranteed that
after the unlock of the proc spinlock current_accessed_item
has the correct value for any process on any processor.
(How do I handle that?)
For the index_to_the_real_item it is needed that the reader
will allways get a valid (which may be old) address. I think
that ATOMIC_EXCHANGE... can be used by the writer.
(with or without _MB?)
Bye,
Aurel.
--
Aurel Balmosan | Department SE-SW
ORGA Kartensysteme GmbH | phone: +49 5254 991 824
An der Kapelle 2 | fax : +49 5254 991 749
33104 Paderborn, Germany | mailto:ab@orga.com
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-07 4:00 ` Jeff Denham
1998-08-07 4:00 ` Aurel Balmosan
@ 1998-08-10 4:00 ` Dirk Grunwald
1998-08-10 4:00 ` Monty Brandenberg
1 sibling, 1 reply; 14+ messages in thread
From: Dirk Grunwald @ 1998-08-10 4:00 UTC (permalink / raw)
Jeff Denham <jdenham@brighttiger.com> writes:
> The __MB() (memory barrier) is *absolutely* necessary for the
> spinlock to work on an MP system. In Dirk's example, though, I'm
> pretty sure that the __MB() in the release() routine is in the wrong
> place -- it should be before the clearing of the lock, not
> after. This guarantees that the updates to the object being locked
> are seen by other processors before they see that the lock is clear,
> grab it, and see stale state in the object.
--
Yeah, actually the "may not be needed" comment is more because I'm not
certain about the semantics of the builtin locking routines. If they
already do WMB/MB, then it's not needed. You'd really need to take a
look at the assembly code produced in a little more detail.
This, frankly, is why Digital should have provided & established
spinlocks, rather than insisting everyone use pthread mutex locks,
which have a lot of extra overhead. Sometimes portability is much less
important than performance.
--
Dirk Grunwald Assoc. Prof, Univ. of Colorado at Boulder
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-10 4:00 ` Dirk Grunwald
@ 1998-08-10 4:00 ` Monty Brandenberg
1998-08-10 4:00 ` Dirk Grunwald
0 siblings, 1 reply; 14+ messages in thread
From: Monty Brandenberg @ 1998-08-10 4:00 UTC (permalink / raw)
To: Dirk Grunwald
Dirk Grunwald wrote:
>
> This, frankly, is why Digital should have provided & established
> spinlocks, rather than insisting everyone use pthread mutex locks,
> which have a lot of extra overhead. Sometimes portability is much less
> important than performance.
For user-mode code? I'll have to mildly-to-strongly disagree with that.
Having one or more threads eating their entire scheduling quantum in
a spin while starving the lock holder isn't generally a path to performance
or responsiveness. One place where you can see this in action is on
the BeOS and documented in the last two newsletters which should be
available publicly. Here, the OS supplies an improved version of
semaphores called benaphores which avoids syscalls in the absence of
contention. The two recent newsletters document what happens with
naive coding which results in high ping-pong rates between threads and
the case of a third thread that can consume a scheduling quantum. The
situation with a user-mode spinlock is actually worse. A correct-but-
naive multithreaded application that used spinlocks would have higher
latencies and lower throughput than a correct-but-naive version that
used syscall-based mutex and semaphore services. One result would be a
continuous stream of newsgroup threads claiming Digital Unix SMP sucks.
I'll disagree with myself with some conditions: supplying spinlock code
to usermode, with suitable warnings and tutorials, could provide a better
path for some applications. It would also allow programmers to avoid
having to invent this themselves should they insist on going down this
route. This would encapsulate the platform requirements for correct SMP
communication which most programmers aren't going to code correctly.
Question to research: do the TIS services support user-mode spinlocks?
m
--
Monty Brandenberg, Software Consultant MCB, Inc.
mcbinc@ne.mediaone.net.MAPS.SPAM P.O. Box 426188
mcbinc@world.std.com.MAPS.SPAM Cambridge, MA 02142
617.864.6907
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-10 4:00 ` Monty Brandenberg
@ 1998-08-10 4:00 ` Dirk Grunwald
1998-08-10 4:00 ` Monty Brandenberg
0 siblings, 1 reply; 14+ messages in thread
From: Dirk Grunwald @ 1998-08-10 4:00 UTC (permalink / raw)
Monty Brandenberg <mcbinc.MAPS.SPAM@ne.mediaone.net> writes:
>
> Dirk Grunwald wrote:
> >
> > This, frankly, is why Digital should have provided & established
> > spinlocks, rather than insisting everyone use pthread mutex locks,
> > which have a lot of extra overhead. Sometimes portability is much less
> > important than performance.
>
> For user-mode code? I'll have to mildly-to-strongly disagree with that.
> Having one or more threads eating their entire scheduling quantum in
> a spin while starving the lock holder isn't generally a path to performance
> or responsiveness. One place where you can see this in action is on
---
Look, if I'm just incrementing a variable (the problem being described
by the person in question) there's no reason to even *hint* at the
possibility of a context switch forced by the thread library during
that incremenet.
E.g., if I'm going
pthread_mutex_lock(foo);
counter++;
pthread_mutex_unlock(foo);
it will take more time to get into/out of the O/S than to simply way.
Many performance oriented programmers would know this and exercise
proper judgement about when to use a spinlock.
However, without representative & provided "spinlock" code, even
expert programmers will screw up the WB's, MB's and the like needed
for correctness.
We shouldn't assume all programmers don't know how to write concurrent code.
--
Dirk Grunwald Assoc. Prof, Univ. of Colorado at Boulder
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-10 4:00 ` Dirk Grunwald
@ 1998-08-10 4:00 ` Monty Brandenberg
1998-08-11 4:00 ` Aurel Balmosan
0 siblings, 1 reply; 14+ messages in thread
From: Monty Brandenberg @ 1998-08-10 4:00 UTC (permalink / raw)
To: Dirk Grunwald
Dirk Grunwald wrote:
>
> Look, if I'm just incrementing a variable (the problem being described
> by the person in question) there's no reason to even *hint* at the
> possibility of a context switch forced by the thread library during
> that incremenet.
>
> E.g., if I'm going
>
> pthread_mutex_lock(foo);
> counter++;
> pthread_mutex_unlock(foo);
>
> it will take more time to get into/out of the O/S than to simply way.
>
> Many performance oriented programmers would know this and exercise
> proper judgement about when to use a spinlock.
Let's break things down into two cases: Shared-read/exclusive-write data
who correctness is (case 1) not critical or (case 2) critical.
Case 1: An application can elect not to put the data under a mutex.
All algorithms that use that data as input must be prepared to deal with
incorrect or inconsistent values in the structure. Candidates for this
class are: performance counters, profiling code, debug flags. Other
application data can fall into this class keeping the incorrectness/
inconsistency aspect in mind and communicating it to those poor bastards
who have to do maintenance on the thing once you've gone on to greener
pastures.
Case 2: Since I write device drivers and similar kernel code, almost
everything I do falls into this area. I'll discuss this in an application
space, which is slightly different from kernel, where there are roughly
four approaches:
Case 2.a: Do nothing. This will get you into trouble. It's hard to
debug and your co-workers will hate you. You will be forced to write
Visual Basic faux-programs for the rest of your life.
Case 2.b: System mutex. In the case of Digital Unix, use pthreads,
mach thread control, or TIS services to implement a classic mutex
around the data. In the case of pthreads, the system services even
guarantee data ordering in an SMP system between system calls. That
is, all the requirements of section 5.6 of the Alpha SRM are satisified
with respect to cpu/cpu operations. Device/cpu ordering is a separate
issue.
Case 2.c: System mutex with syscall avoidance. The idea is that syscalls
are expensive (a relative measure as I'll detail in a moment) so avoiding
them is A Good Thing. It is achieved by using an interlocked counter or
similar mechanism to detect contention for a mutex and to only make
mutex calls when contention arises. I've offered the BeOS' Benaphores
up as an example of this and they are generally a win for infrequent
locking. However, there can be major performance hits due to scheduler
activity. See the Be Newsletters for details.
Case 2.d: User-mode spinlocks. In a way, this is a return to DOS programming.
The idea is that the cost of a thread simply spinning on an smp-safe flag
will be less than the cost of entering and exiting a pair of system services.
There a number of reasons why this may not be so which I'll go into in a
moment. First, I'll discuss implementation. An smp-safe spinlock cannot
be constructed from STx, LDx, and MB. It must be built from LDx_L and
STx_C instructions. These are the load locked/store conditionals that
use the architecture- and implementation-defined interlocked access protocols
to guarantee that multiple cpus see identical versions of longwords or
quadwords. From an old version of the SRM, here's a spin-lock algorithm
(may contain errors):
stq_c_loop:
spin_loop:
LDQ_L r1, lock_variable
BLBS r1, already_set
OR r1, #1, r2
STQ_C r2, lock_variable
BEQ r2, stq_c_fail
MB
; Begin critical section
<Your code deleted>
; End critical section
MB
STQ r31, lock_variable ; clear lock
; Return to application code
; Forward branching failure targets
already_set:
; Sleep, check for too many iterations, or do nothing
BR spin_loop
stq_c_fail:
; Check for too many interations.
BR stq_c_loop
Now let's consider the merits:
. Cost of syscall versus cost of spinlock contention. The time to execute
a trivial syscall (trap, prepare return value, dismiss trap) on an EV5-class
alpha can be as low as 500 nS (nano). Obviously, a real syscall is going to
take more time. It's been some time since I've benchmarked it but a heavy-
weight syscall using the ioctl interface, a sleep, an interrupt in an
ISR routine to do a wakeup was under 10 uS and 6 uS comes to mind. A mutex
acquire/release pair could be as fast as 1 uS for no contention, and on
the order of 10 uS (plus sleep time) with contention. The user-mode spinlock
widens the spread. First, with no contention, you still need a correct
implementation of the spinlock and that will eat a few cycles (see algorithm)
but is still faster than a syscall. On a single processor, with, say,
a 10,000 uS scheduling quantum, encountering contention, the losing thread
is in a cpu-spin and will have to exhaust its quantum before the lock-owning
thread can proceed and release the lock. In the best case, the lock-owning
thread is scheduled, clears the lock, and *immediately* sleeps allowing
the first thread to execute and do useful work. In this case, the statistical
expected value for lock acquisition is 5,000 uS. I.e. lower bound is half
the scheduling quantum. There are worse cases: The lock-owning thread
is cpu-bound and continues to consume a full quantum every time it is
scheduled. Expected value for lock acquisition: 15,000 uS or 1.5 times
the scheduling quantum. Worse still: lock-owning thread consumes a quantum
and re-acquires lock just before losing quantum. Expected value for lock
acquisition: >1.5 times the scheduling quantum. Now we have:
T.exp.mut Expected time to execute real mutex code
T.exp.spin Expected time to execute user-mode spinlock code
T.exp.unlock Expected time to unlock mutex under contention
T.spin.nocont Expected time to acquire spinlock with no contention
P.mut.cont Probability of encountering contention with real mutex code
P.spin.cont Probability of encountering contention with user-mode spinlock
P.best Probability of best-case spinlock contention resolution
P.worse Probability of worse-case spinlock contention resolution
P.worst Probability of worst-case spinlock contention resolution
T.exp.mut = (P.mut.cont * (10uS + T.exp.unlock))
+ ((1 - P.mut.cont) * 1uS)
T.exp.spin = (P.spin.cont * ((P.best * 5,000 uS)
+(P.worse * 15,000 uS)
+(P.worst * 20,000 uS)))
+ ((1 - P.spin.cont) * T.spin.nocont)
1 ~ P.best + P.worse + P.worst
We can make some assumptions:
P.best should be near 1.
T.spin.nocont is on the order of 10's of nS unless some of the
situations below arise.
T.exp.unlock is dependent upon the application's algorithms but it
also represents useful work as opposed to the time
consumed under the P.spin.cont term which is dead CPU.
How you weight it is a designer's decision.
The goal is to keep T.exp.spin well under (i.e. the benefit outweighs
the effort of supporting and debugging home-grown spinlock code on
every supported system) T.exp.mut. Obviously, there are several
independent variables but a few will likely dominate. If P.spin.cont is
much greater than 1/5000, its term dominates T.exp.spin and bounds
that latter to a minimum of 1uS which is the lower bound for T.exp.mut
regardless of P.mut.cont. What do you need to do? Profile your application
so that you can estimate P.spin.cont. Certain application decisions
will affect this value. If the spinlock is coarse-grained, i.e. a few
locks cover all data structures, the probability goes up. As the time
required to execute the code in the critical section goes up, this
probability goes up. If the spinlocks are fine-grained, the probability
goes down *but* T.spin.nocont starts to increase exponentially. That
is because the system bus protocol for interlocked operations typically
has very large granularity.
. On an SMP system, the times under the P.spin.cont term should be
smaller. A thread has the potential to get running on one processor and
clear the lock allowing a blocked thread on the other system to run (which
is why spinlocks work well for kernel mode code which first assumes priority
ordering under spl level). However, with more than two threads, or more
specifically, with too many more threads than cpus, P.spin.cont goes
back up. And with too many cpus, T.spin.nocont goes up (this represents
the asymptotic limit of smp systems).
. The user-mode spinlocks are subject to some of the problems of 2.c.
See lit. for details.
. The particular times in the above are not definitive. I haven't
benchmarked the pthread, mach thread, or tis services. Only the kernel-mode
sleep mechanisms, interrupt dispatching, and trap handling. The numbers
I present are certainly possible but what is actually achieved in the
libraries needs competent benchmarking.
Finally, if you go down the SMP route, make *certain* you develop and
test on an SMP system. You can violate all the data ordering requirements
on a single processor system and everything will work correctly as a
single cpu sees only one consistent view of the system. Put that code
on an smp machine and it will very quickly fall apart in obvious and
not-so-obvious ways.
> However, without representative & provided "spinlock" code, even
> expert programmers will screw up the WB's, MB's and the like needed
> for correctness.
Representative and provided spinlock code is in the Alpha SRM. If you are
attempting to implement SMP algorithms on Alpha without this book, or the
corresponding book on other architectures, you're not doing your
employers good service.
> We shouldn't assume all programmers don't know how to write concurrent code.
That's a nice sentiment but it doesn't stand up to scutiny. It's a
better than one sigma programmer who can use explicit concurrent programming
services correctly. (I present the Modula-3 project's motivation as proof.)
Those who can actually implement the services correctly are out at
three sigma, I'd say.
m
--
Monty Brandenberg, Software Consultant MCB, Inc.
mcbinc@ne.mediaone.net.MAPS.SPAM P.O. Box 426188
mcbinc@world.std.com.MAPS.SPAM Cambridge, MA 02142
617.864.6907
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-10 4:00 ` Monty Brandenberg
@ 1998-08-11 4:00 ` Aurel Balmosan
1998-08-11 4:00 ` Monty Brandenberg
0 siblings, 1 reply; 14+ messages in thread
From: Aurel Balmosan @ 1998-08-11 4:00 UTC (permalink / raw)
Monty Brandenberg <mcbinc.MAPS.SPAM@ne.mediaone.net> wrote:
> . Cost of syscall versus cost of spinlock contention. The time to execute
> a trivial syscall (trap, prepare return value, dismiss trap) on an EV5-class
> alpha can be as low as 500 nS (nano). Obviously, a real syscall is going to
> take more time. It's been some time since I've benchmarked it but a heavy-
> weight syscall using the ioctl interface, a sleep, an interrupt in an
> ISR routine to do a wakeup was under 10 uS and 6 uS comes to mind. A mutex
> acquire/release pair could be as fast as 1 uS for no contention, and on
> the order of 10 uS (plus sleep time) with contention. The user-mode spinlock
> widens the spread. First, with no contention, you still need a correct
> implementation of the spinlock and that will eat a few cycles (see algorithm)
> but is still faster than a syscall. On a single processor, with, say,
> a 10,000 uS scheduling quantum, encountering contention, the losing thread
> is in a cpu-spin and will have to exhaust its quantum before the lock-owning
> thread can proceed and release the lock. In the best case, the lock-owning
> thread is scheduled, clears the lock, and *immediately* sleeps allowing
> the first thread to execute and do useful work. In this case, the statistical
> expected value for lock acquisition is 5,000 uS. I.e. lower bound is half
> the scheduling quantum. There are worse cases: The lock-owning thread
> is cpu-bound and continues to consume a full quantum every time it is
> scheduled. Expected value for lock acquisition: 15,000 uS or 1.5 times
> the scheduling quantum. Worse still: lock-owning thread consumes a quantum
> and re-acquires lock just before losing quantum. Expected value for lock
> acquisition: >1.5 times the scheduling quantum. Now we have:
> ...
In case there are many spinlocks and the probability for an already
locked is very low (1/n where n>10000000) spinlocks would be the
right choice. That would be the case if I spend a spinlock for each
item. In case of structures used by every process (lets say admin
data area where every one needs to write from time to time) an
IPC semaphore is still the best solution.
> Representative and provided spinlock code is in the Alpha SRM. If you are
> attempting to implement SMP algorithms on Alpha without this book, or the
> corresponding book on other architectures, you're not doing your
> employers good service.
Hmm, Alpha SRM? I wander where one get that book. I didn't saw it with our
machine.
--
Aurel Balmosan | Department SE-SW
ORGA Kartensysteme GmbH | phone: +49 5254 991 824
An der Kapelle 2 | fax : +49 5254 991 749
33104 Paderborn, Germany | mailto:ab@orga.com
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: mutex between processes?
1998-08-11 4:00 ` Aurel Balmosan
@ 1998-08-11 4:00 ` Monty Brandenberg
0 siblings, 0 replies; 14+ messages in thread
From: Monty Brandenberg @ 1998-08-11 4:00 UTC (permalink / raw)
To: Aurel Balmosan
Aurel Balmosan wrote:
> Hmm, Alpha SRM? I wander where one get that book. I didn't saw it with our
> machine.
Okay, I lied. It's the Alpha ARM (from Amazon.com, the digitalpress site
isn't working):
Alpha Architecture Reference Manual
by Richard L. Sites, Alpha Architecture Reference Manual, Alpha
Architecture Committee, Richard T. Witek
Our Price: $54.95
Paperback 3rd edition (April 1998)
Digital Press; ISBN: 1555582028
--
Monty Brandenberg, Software Consultant MCB, Inc.
mcbinc@ne.mediaone.net.MAPS.SPAM P.O. Box 426188
mcbinc@world.std.com.MAPS.SPAM Cambridge, MA 02142
617.864.6907
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~1998-08-11 4:00 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-08-04 4:00 mutex between processes? Aurel Balmosan
1998-08-05 4:00 ` Jeff Denham
1998-08-05 4:00 ` Dirk Grunwald
1998-08-07 4:00 ` Aurel Balmosan
1998-08-07 4:00 ` Jeff Denham
1998-08-07 4:00 ` Aurel Balmosan
1998-08-07 4:00 ` Monty Brandenberg
1998-08-09 4:00 ` Aurel Balmosan
1998-08-10 4:00 ` Dirk Grunwald
1998-08-10 4:00 ` Monty Brandenberg
1998-08-10 4:00 ` Dirk Grunwald
1998-08-10 4:00 ` Monty Brandenberg
1998-08-11 4:00 ` Aurel Balmosan
1998-08-11 4:00 ` Monty Brandenberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox