LCOV - code coverage report
Current view: top level - src/test/modules/test_lwlock_tranches - test_lwlock_tranches.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 32 37 86.5 %
Date: 2025-09-20 09:17:50 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*--------------------------------------------------------------------------
       2             :  *
       3             :  * test_lwlock_tranches.c
       4             :  *      Test code for LWLock tranches allocated by extensions.
       5             :  *
       6             :  * Copyright (c) 2025, PostgreSQL Global Development Group
       7             :  *
       8             :  * IDENTIFICATION
       9             :  *      src/test/modules/test_lwlock_tranches/test_lwlock_tranches.c
      10             :  *
      11             :  * -------------------------------------------------------------------------
      12             :  */
      13             : 
      14             : #include "postgres.h"
      15             : 
      16             : #include "fmgr.h"
      17             : #include "miscadmin.h"
      18             : #include "storage/lwlock.h"
      19             : #include "utils/builtins.h"
      20             : #include "utils/wait_classes.h"
      21             : 
      22           2 : PG_MODULE_MAGIC;
      23             : 
      24             : #define STARTUP_TRANCHE_NAME "test_lwlock_tranches_startup"
      25             : #define DYNAMIC_TRANCHE_NAME "test_lwlock_tranches_dynamic"
      26             : 
      27             : #define NUM_STARTUP_TRANCHES (32)
      28             : #define NUM_DYNAMIC_TRANCHES (256 - NUM_STARTUP_TRANCHES)
      29             : 
      30             : #define GET_TRANCHE_NAME(a) GetLWLockIdentifier(PG_WAIT_LWLOCK, (a))
      31             : 
      32             : static shmem_request_hook_type prev_shmem_request_hook;
      33             : static void test_lwlock_tranches_shmem_request(void);
      34             : 
      35             : void
      36           2 : _PG_init(void)
      37             : {
      38           2 :     prev_shmem_request_hook = shmem_request_hook;
      39           2 :     shmem_request_hook = test_lwlock_tranches_shmem_request;
      40           2 : }
      41             : 
      42             : static void
      43           2 : test_lwlock_tranches_shmem_request(void)
      44             : {
      45           2 :     if (prev_shmem_request_hook)
      46           0 :         prev_shmem_request_hook();
      47             : 
      48          66 :     for (int i = 0; i < NUM_STARTUP_TRANCHES; i++)
      49          64 :         RequestNamedLWLockTranche(STARTUP_TRANCHE_NAME, 1);
      50           2 : }
      51             : 
      52             : /*
      53             :  * Checks that GetLWLockIdentifier() returns the expected value for tranches
      54             :  * registered via RequestNamedLWLockTranche() and LWLockNewTrancheId().
      55             :  */
      56           4 : PG_FUNCTION_INFO_V1(test_lwlock_tranches);
      57             : Datum
      58           2 : test_lwlock_tranches(PG_FUNCTION_ARGS)
      59             : {
      60             :     int         dynamic_tranches[NUM_DYNAMIC_TRANCHES];
      61             : 
      62         450 :     for (int i = 0; i < NUM_DYNAMIC_TRANCHES; i++)
      63         448 :         dynamic_tranches[i] = LWLockNewTrancheId(DYNAMIC_TRANCHE_NAME);
      64             : 
      65          66 :     for (int i = 0; i < NUM_STARTUP_TRANCHES; i++)
      66             :     {
      67          64 :         if (strcmp(GET_TRANCHE_NAME(LWTRANCHE_FIRST_USER_DEFINED + i),
      68             :                    STARTUP_TRANCHE_NAME) != 0)
      69           0 :             elog(ERROR, "incorrect startup lock tranche name");
      70             :     }
      71             : 
      72         450 :     for (int i = 0; i < NUM_DYNAMIC_TRANCHES; i++)
      73             :     {
      74         448 :         if (strcmp(GET_TRANCHE_NAME(dynamic_tranches[i]),
      75             :                    DYNAMIC_TRANCHE_NAME) != 0)
      76           0 :             elog(ERROR, "incorrect dynamic lock tranche name");
      77             :     }
      78             : 
      79           2 :     PG_RETURN_VOID();
      80             : }
      81             : 
      82             : /*
      83             :  * Wrapper for LWLockNewTrancheId().
      84             :  */
      85           4 : PG_FUNCTION_INFO_V1(test_lwlock_tranche_creation);
      86             : Datum
      87           6 : test_lwlock_tranche_creation(PG_FUNCTION_ARGS)
      88             : {
      89           6 :     char       *tranche_name = PG_ARGISNULL(0) ? NULL : TextDatumGetCString(PG_GETARG_DATUM(0));
      90             : 
      91           6 :     (void) LWLockNewTrancheId(tranche_name);
      92             : 
      93           0 :     PG_RETURN_VOID();
      94             : }
      95             : 
      96             : /*
      97             :  * Wrapper for GetNamedLWLockTranche().
      98             :  */
      99           4 : PG_FUNCTION_INFO_V1(test_lwlock_tranche_lookup);
     100             : Datum
     101           4 : test_lwlock_tranche_lookup(PG_FUNCTION_ARGS)
     102             : {
     103           4 :     char       *tranche_name = TextDatumGetCString(PG_GETARG_DATUM(0));
     104             : 
     105           4 :     (void) GetNamedLWLockTranche(tranche_name);
     106             : 
     107           2 :     PG_RETURN_VOID();
     108             : }
     109             : 
     110             : /*
     111             :  * Wrapper for LWLockInitialize().
     112             :  */
     113           4 : PG_FUNCTION_INFO_V1(test_lwlock_initialize);
     114             : Datum
     115           2 : test_lwlock_initialize(PG_FUNCTION_ARGS)
     116             : {
     117           2 :     int         tranche_id = PG_GETARG_INT32(0);
     118             :     LWLock      lock;
     119             : 
     120           2 :     LWLockInitialize(&lock, tranche_id);
     121             : 
     122           0 :     PG_RETURN_VOID();
     123             : }

Generated by: LCOV version 1.16