spin locks

Started by Bruce Momjianalmost 28 years ago7 messages
#1Bruce Momjian
maillist@candle.pha.pa.us

spin-lock.patch

I'm not sure if this is really useful, but it seems stupid to have
a backend wasting cpu cycles in a busy loop while the process which
should release the lock is waiting for the cpu. So I added a call
to process_yield() if the spin lock can't obtained.
This has been implemented and tested only on Linux. I don't know if
other OS have process_yield(). If someone can check please do it.

Massimo brings up a good point. Most of our s_lock.h locking does asm
mutex loops looking for a lock. Unless we are using a multi-cpu
machine, there is no way this is going to change while we are spinning.

Linux has process_yield(), but most OS's don't. Is there a
platform-independent way to relinquish the cpu if the first attempt at
the spinlock fails? Would a select() of 1 microsecond work?

--
Bruce Momjian
maillist@candle.pha.pa.us

#2The Hermit Hacker
scrappy@hub.org
In reply to: Bruce Momjian (#1)
Re: [HACKERS] spin locks

On Sun, 15 Feb 1998, Bruce Momjian wrote:

spin-lock.patch

I'm not sure if this is really useful, but it seems stupid to have
a backend wasting cpu cycles in a busy loop while the process which
should release the lock is waiting for the cpu. So I added a call
to process_yield() if the spin lock can't obtained.
This has been implemented and tested only on Linux. I don't know if
other OS have process_yield(). If someone can check please do it.

Massimo brings up a good point. Most of our s_lock.h locking does asm
mutex loops looking for a lock. Unless we are using a multi-cpu
machine, there is no way this is going to change while we are spinning.

I'm not quite sure I follow this...in a multi-cpu environment,
would process_yield() introduce a problem? *raised eyebrow*

Linux has process_yield(), but most OS's don't. Is there a
platform-independent way to relinquish the cpu if the first attempt at
the spinlock fails? Would a select() of 1 microsecond work?

There is nothing wrong with introducing an OS specific
optimization to the code...we can add a HAVE_PROCESS_YIELD to config.h and
if a system has it, use it...

Marc G. Fournier
Systems Administrator @ hub.org
primary: scrappy@hub.org secondary: scrappy@{freebsd|postgresql}.org

#3Jordan Henderson
jordanh@ccia.com
In reply to: The Hermit Hacker (#2)
Re: [HACKERS] spin locks

Folks,

On the spin lock and multiple CPU's, this should not be
a compile time issue, but a runtime issue. What do you
think?

Jordan Henderson

#4The Hermit Hacker
scrappy@hub.org
In reply to: Jordan Henderson (#3)
Re: [HACKERS] spin locks

On Sun, 15 Feb 1998, Jordan Henderson wrote:

Folks,

On the spin lock and multiple CPU's, this should not be
a compile time issue, but a runtime issue. What do you
think?

And you are proposing...?

Marc G. Fournier
Systems Administrator @ hub.org
primary: scrappy@hub.org secondary: scrappy@{freebsd|postgresql}.org

#5Jordan Henderson
jordanh@ccia.com
In reply to: The Hermit Hacker (#4)
Re: [HACKERS] spin locks

Folks,

On the thread in regards to spinlocks and multiple CPU's.
The line of thought appeared to be select a compile
time option to determine behavior, and whether to yield or not.

I am thinking that if it comes to having alternate code, the
system should be able to make the determination at runtime,
not compile time. I don't know if all of the platforms supported
have a version of the sysinfo utility, but here is how, at runtime
it gets the number of CPUS available:

**** EXCERPTED FROM SYSINFO SOURCE 3.3.1 ****
/*
* Use sysconf() to find number of CPU's.
*/
extern char *GetNumCpuSysconf()
{
int Num = -1;
static char *NumStr = NULL;

if (NumStr)
return(NumStr);

#if defined(_SC_NPROCESSORS_CONF)
Num = (int) sysconf(_SC_NPROCESSORS_CONF);
if (Num >= 0) {
NumStr = itoa(Num);
if (NumStr)
NumStr = strdup(NumStr);
}
#endif /* _SC_NPROCESSORS_CONF */

return(NumStr);
}

What I would propose, if the decision is made to yield,
that at initialization time, the number of CPU's available
are determined, and a flag set, or, an indirect jump
changed. This would allow the software to have both
personalities, depending on which system it found it
self running on.

Thoughts?
Jordan Henderson

#6Bruce Momjian
maillist@candle.pha.pa.us
In reply to: Jordan Henderson (#3)
Re: [HACKERS] spin locks

Folks,

On the spin lock and multiple CPU's, this should not be
a compile time issue, but a runtime issue. What do you
think?

Jordan Henderson

Yes.

--
Bruce Momjian
maillist@candle.pha.pa.us

#7Massimo Dal Zotto
dz@cs.unitn.it
In reply to: Bruce Momjian (#6)
Re: [HACKERS] spin locks

Folks,

On the spin lock and multiple CPU's, this should not be
a compile time issue, but a runtime issue. What do you
think?

Jordan Henderson

Yes.

Yes, if we have the tprintf patch we can also add an option flag and change
it un the fly while the backends are running.

--
Massimo Dal Zotto

+----------------------------------------------------------------------+
|  Massimo Dal Zotto                e-mail:  dz@cs.unitn.it            |
|  Via Marconi, 141                 phone:  ++39-461-534251            |
|  38057 Pergine Valsugana (TN)     www:  http://www.cs.unitn.it/~dz/  |
|  Italy                            pgp:  finger dz@tango.cs.unitn.it  |
+----------------------------------------------------------------------+