Skip to content

Commit e027219

Browse files
committed
Add basic spinlock tests to regression tests.
As s_lock_test, the already existing test for spinlocks, isn't run in an automated fashion (and doesn't test a normal backend environment), adding tests that are run as part of a normal regression run is a good idea. Particularly in light of several recent and upcoming spinlock related fixes. Currently the new tests are run as part of the pre-existing test_atomic_ops() test. That perhaps can be quibbled about, but for now seems ok. The only operations that s_lock_test tests but the new tests don't are the detection of a stuck spinlock and S_LOCK_FREE (which is otherwise unused, not implemented on all platforms, and will be removed). This currently contains a test for more than INT_MAX spinlocks (only run with --disable-spinlocks), to ensure the recent commit fixing a bug with more than INT_MAX spinlock initializations is correct. That test is somewhat slow, so we might want to disable it after a few days. It might be worth retiring s_lock_test after this. The added coverage of a stuck spinlock probably isn't worth the added complexity? Author: Andres Freund Discussion: https://postgr.es/m/20200606023103.avzrctgv7476xj7i@alap3.anarazel.de
1 parent 070f490 commit e027219

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

src/test/regress/regress.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "executor/spi.h"
3232
#include "miscadmin.h"
3333
#include "port/atomics.h"
34+
#include "storage/spin.h"
3435
#include "utils/builtins.h"
3536
#include "utils/geo_decls.h"
3637
#include "utils/rel.h"
@@ -787,6 +788,108 @@ test_atomic_uint64(void)
787788
EXPECT_EQ_U64(pg_atomic_fetch_and_u64(&var, ~0), 0);
788789
}
789790

791+
/*
792+
* Perform, fairly minimal, testing of the spinlock implementation.
793+
*
794+
* It's likely worth expanding these to actually test concurrency etc, but
795+
* having some regularly run tests is better than none.
796+
*/
797+
static void
798+
test_spinlock(void)
799+
{
800+
/*
801+
* Basic tests for spinlocks, as well as the underlying operations.
802+
*
803+
* We embed the spinlock in a struct with other members to test that the
804+
* spinlock operations don't perform too wide writes.
805+
*/
806+
{
807+
struct test_lock_struct
808+
{
809+
char data_before[4];
810+
slock_t lock;
811+
char data_after[4];
812+
} struct_w_lock;
813+
814+
memcpy(struct_w_lock.data_before, "abcd", 4);
815+
memcpy(struct_w_lock.data_after, "ef12", 4);
816+
817+
/* test basic operations via the SpinLock* API */
818+
SpinLockInit(&struct_w_lock.lock);
819+
SpinLockAcquire(&struct_w_lock.lock);
820+
SpinLockRelease(&struct_w_lock.lock);
821+
822+
/* test basic operations via underlying S_* API */
823+
S_INIT_LOCK(&struct_w_lock.lock);
824+
S_LOCK(&struct_w_lock.lock);
825+
S_UNLOCK(&struct_w_lock.lock);
826+
827+
/* and that "contended" acquisition works */
828+
s_lock(&struct_w_lock.lock, "testfile", 17, "testfunc");
829+
S_UNLOCK(&struct_w_lock.lock);
830+
831+
/*
832+
* Check, using TAS directly, that a single spin cycle doesn't block
833+
* when acquiring an already acquired lock.
834+
*/
835+
#ifdef TAS
836+
S_LOCK(&struct_w_lock.lock);
837+
838+
if (!TAS(&struct_w_lock.lock))
839+
elog(ERROR, "acquired already held spinlock");
840+
841+
#ifdef TAS_SPIN
842+
if (!TAS_SPIN(&struct_w_lock.lock))
843+
elog(ERROR, "acquired already held spinlock");
844+
#endif /* defined(TAS_SPIN) */
845+
846+
S_UNLOCK(&struct_w_lock.lock);
847+
#endif /* defined(TAS) */
848+
849+
/*
850+
* Verify that after all of this the non-lock contents are still
851+
* correct.
852+
*/
853+
if (memcmp(struct_w_lock.data_before, "abcd", 4) != 0)
854+
elog(ERROR, "padding before spinlock modified");
855+
if (memcmp(struct_w_lock.data_after, "ef12", 4) != 0)
856+
elog(ERROR, "padding after spinlock modified");
857+
}
858+
859+
/*
860+
* Ensure that allocating more than INT32_MAX emulated spinlocks
861+
* works. That's interesting because the spinlock emulation uses a 32bit
862+
* integer to map spinlocks onto semaphores. There've been bugs...
863+
*/
864+
#ifndef HAVE_SPINLOCKS
865+
{
866+
/*
867+
* Initialize enough spinlocks to advance counter close to
868+
* wraparound. It's too expensive to perform acquire/release for each,
869+
* as those may be syscalls when the spinlock emulation is used (and
870+
* even just atomic TAS would be expensive).
871+
*/
872+
for (uint32 i = 0; i < INT32_MAX - 100000; i++)
873+
{
874+
slock_t lock;
875+
876+
SpinLockInit(&lock);
877+
}
878+
879+
for (uint32 i = 0; i < 200000; i++)
880+
{
881+
slock_t lock;
882+
883+
SpinLockInit(&lock);
884+
885+
SpinLockAcquire(&lock);
886+
SpinLockRelease(&lock);
887+
SpinLockAcquire(&lock);
888+
SpinLockRelease(&lock);
889+
}
890+
}
891+
#endif
892+
}
790893

791894
PG_FUNCTION_INFO_V1(test_atomic_ops);
792895
Datum
@@ -798,6 +901,12 @@ test_atomic_ops(PG_FUNCTION_ARGS)
798901

799902
test_atomic_uint64();
800903

904+
/*
905+
* Arguably this shouldn't be tested as part of this function, but it's
906+
* closely enough related that that seems ok for now.
907+
*/
908+
test_spinlock();
909+
801910
PG_RETURN_BOOL(true);
802911
}
803912

0 commit comments

Comments
 (0)