LCOV - differential code coverage report
Current view: top level - src/backend/utils/activity - pgstat_slru.c (source / functions) Coverage Total Hit UBC GNC CBC ECB DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 98.6 % 74 73 1 7 66 1 20
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 17 17 7 10 7
Baseline: lcov-20250906-005545-baseline Branches: 73.7 % 38 28 10 28
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 7 7 7
(30,360] days: 100.0 % 1 1 1
(360..) days: 98.5 % 66 65 1 65 1
Function coverage date bins:
(1,7] days: 100.0 % 7 7 7
(360..) days: 100.0 % 10 10 10
Branch coverage date bins:
(360..) days: 73.7 % 38 28 10 28

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /* -------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pgstat_slru.c
                                  4                 :                :  *    Implementation of SLRU statistics.
                                  5                 :                :  *
                                  6                 :                :  * This file contains the implementation of SLRU statistics. It is kept
                                  7                 :                :  * separate from pgstat.c to enforce the line between the statistics access /
                                  8                 :                :  * storage implementation and the details about individual types of
                                  9                 :                :  * statistics.
                                 10                 :                :  *
                                 11                 :                :  * Copyright (c) 2001-2025, PostgreSQL Global Development Group
                                 12                 :                :  *
                                 13                 :                :  * IDENTIFICATION
                                 14                 :                :  *    src/backend/utils/activity/pgstat_slru.c
                                 15                 :                :  * -------------------------------------------------------------------------
                                 16                 :                :  */
                                 17                 :                : 
                                 18                 :                : #include "postgres.h"
                                 19                 :                : 
                                 20                 :                : #include "utils/pgstat_internal.h"
                                 21                 :                : #include "utils/timestamp.h"
                                 22                 :                : 
                                 23                 :                : 
                                 24                 :                : static inline PgStat_SLRUStats *get_slru_entry(int slru_idx);
                                 25                 :                : static void pgstat_reset_slru_counter_internal(int index, TimestampTz ts);
                                 26                 :                : 
                                 27                 :                : 
                                 28                 :                : /*
                                 29                 :                :  * SLRU statistics counts waiting to be flushed out.  We assume this variable
                                 30                 :                :  * inits to zeroes.  Entries are one-to-one with slru_names[].  Changes of
                                 31                 :                :  * SLRU counters are reported within critical sections so we use static memory
                                 32                 :                :  * in order to avoid memory allocation.
                                 33                 :                :  */
                                 34                 :                : static PgStat_SLRUStats pending_SLRUStats[SLRU_NUM_ELEMENTS];
                                 35                 :                : static bool have_slrustats = false;
                                 36                 :                : 
                                 37                 :                : 
                                 38                 :                : /*
                                 39                 :                :  * Reset counters for a single SLRU.
                                 40                 :                :  *
                                 41                 :                :  * Permission checking for this function is managed through the normal
                                 42                 :                :  * GRANT system.
                                 43                 :                :  */
                                 44                 :                : void
 1249 andres@anarazel.de         45                 :CBC           3 : pgstat_reset_slru(const char *name)
                                 46                 :                : {
                                 47                 :              3 :     TimestampTz ts = GetCurrentTimestamp();
                                 48                 :                : 
 1044 peter@eisentraut.org       49         [ -  + ]:              3 :     Assert(name != NULL);
                                 50                 :                : 
 1249 andres@anarazel.de         51                 :              3 :     pgstat_reset_slru_counter_internal(pgstat_get_slru_index(name), ts);
 1265                            52                 :              3 : }
                                 53                 :                : 
                                 54                 :                : /*
                                 55                 :                :  * SLRU statistics count accumulation functions --- called from slru.c
                                 56                 :                :  */
                                 57                 :                : 
                                 58                 :                : #define PGSTAT_COUNT_SLRU(stat)                     \
                                 59                 :                : void                                                \
                                 60                 :                : CppConcat(pgstat_count_slru_,stat)(int slru_idx)    \
                                 61                 :                : {                                                   \
                                 62                 :                :     get_slru_entry(slru_idx)->stat += 1;         \
 1265 andres@anarazel.de         63                 :ECB      (1397) : }
                                 64                 :                : 
                                 65                 :                : /* pgstat_count_slru_blocks_zeroed */
    4 michael@paquier.xyz        66                 :GNC        1406 : PGSTAT_COUNT_SLRU(blocks_zeroed)
                                 67                 :                : 
                                 68                 :                : /* pgstat_count_slru_blocks_hit */
                                 69                 :         869065 : PGSTAT_COUNT_SLRU(blocks_hit)
                                 70                 :                : 
                                 71                 :                : /* pgstat_count_slru_blocks_exists */
                                 72                 :            105 : PGSTAT_COUNT_SLRU(blocks_exists)
                                 73                 :                : 
                                 74                 :                : /* pgstat_count_slru_blocks_read */
                                 75                 :           1623 : PGSTAT_COUNT_SLRU(blocks_read)
                                 76                 :                : 
                                 77                 :                : /* pgstat_count_slru_blocks_written */
                                 78                 :           2707 : PGSTAT_COUNT_SLRU(blocks_written)
                                 79                 :                : 
                                 80                 :                : /* pgstat_count_slru_flush */
                                 81                 :           8415 : PGSTAT_COUNT_SLRU(flush)
                                 82                 :                : 
                                 83                 :                : /* pgstat_count_slru_truncate */
                                 84                 :           1651 : PGSTAT_COUNT_SLRU(truncate)
                                 85                 :                : 
                                 86                 :                : /*
                                 87                 :                :  * Support function for the SQL-callable pgstat* functions. Returns
                                 88                 :                :  * a pointer to the slru statistics struct.
                                 89                 :                :  */
                                 90                 :                : PgStat_SLRUStats *
 1249 andres@anarazel.de         91                 :CBC          62 : pgstat_fetch_slru(void)
                                 92                 :                : {
                                 93                 :             62 :     pgstat_snapshot_fixed(PGSTAT_KIND_SLRU);
                                 94                 :                : 
                                 95                 :             62 :     return pgStatLocal.snapshot.slru;
                                 96                 :                : }
                                 97                 :                : 
                                 98                 :                : /*
                                 99                 :                :  * Returns SLRU name for an index. The index may be above SLRU_NUM_ELEMENTS,
                                100                 :                :  * in which case this returns NULL. This allows writing code that does not
                                101                 :                :  * know the number of entries in advance.
                                102                 :                :  */
                                103                 :                : const char *
                                104                 :            558 : pgstat_get_slru_name(int slru_idx)
                                105                 :                : {
 1265                           106   [ +  -  +  + ]:            558 :     if (slru_idx < 0 || slru_idx >= SLRU_NUM_ELEMENTS)
                                107                 :             62 :         return NULL;
                                108                 :                : 
                                109                 :            496 :     return slru_names[slru_idx];
                                110                 :                : }
                                111                 :                : 
                                112                 :                : /*
                                113                 :                :  * Determine index of entry for a SLRU with a given name. If there's no exact
                                114                 :                :  * match, returns index of the last "other" entry used for SLRUs defined in
                                115                 :                :  * external projects.
                                116                 :                :  */
                                117                 :                : int
 1249                           118                 :           7207 : pgstat_get_slru_index(const char *name)
                                119                 :                : {
                                120                 :                :     int         i;
                                121                 :                : 
 1265                           122         [ +  + ]:          28824 :     for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
                                123                 :                :     {
                                124         [ +  + ]:          28823 :         if (strcmp(slru_names[i], name) == 0)
                                125                 :           7206 :             return i;
                                126                 :                :     }
                                127                 :                : 
                                128                 :                :     /* return index of the last entry (which is the "other" one) */
                                129                 :              1 :     return (SLRU_NUM_ELEMENTS - 1);
                                130                 :                : }
                                131                 :                : 
                                132                 :                : /*
                                133                 :                :  * Flush out locally pending SLRU stats entries
                                134                 :                :  *
                                135                 :                :  * If nowait is true, this function returns false on lock failure. Otherwise
                                136                 :                :  * this function always returns true.
                                137                 :                :  *
                                138                 :                :  * If nowait is true, this function returns true if the lock could not be
                                139                 :                :  * acquired. Otherwise return false.
                                140                 :                :  */
                                141                 :                : bool
  362 michael@paquier.xyz       142                 :          30998 : pgstat_slru_flush_cb(bool nowait)
                                143                 :                : {
 1249 andres@anarazel.de        144                 :          30998 :     PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
                                145                 :                :     int         i;
                                146                 :                : 
                                147         [ +  + ]:          30998 :     if (!have_slrustats)
                                148                 :          18592 :         return false;
                                149                 :                : 
                                150         [ +  + ]:          12406 :     if (!nowait)
                                151                 :           9809 :         LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
                                152         [ -  + ]:           2597 :     else if (!LWLockConditionalAcquire(&stats_shmem->lock, LW_EXCLUSIVE))
 1249 andres@anarazel.de        153                 :UBC           0 :         return true;
                                154                 :                : 
 1249 andres@anarazel.de        155         [ +  + ]:CBC      111654 :     for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
                                156                 :                :     {
                                157                 :          99248 :         PgStat_SLRUStats *sharedent = &stats_shmem->stats[i];
                                158                 :          99248 :         PgStat_SLRUStats *pendingent = &pending_SLRUStats[i];
                                159                 :                : 
                                160                 :                : #define SLRU_ACC(fld) sharedent->fld += pendingent->fld
                                161                 :          99248 :         SLRU_ACC(blocks_zeroed);
                                162                 :          99248 :         SLRU_ACC(blocks_hit);
                                163                 :          99248 :         SLRU_ACC(blocks_read);
                                164                 :          99248 :         SLRU_ACC(blocks_written);
                                165                 :          99248 :         SLRU_ACC(blocks_exists);
                                166                 :          99248 :         SLRU_ACC(flush);
                                167                 :          99248 :         SLRU_ACC(truncate);
                                168                 :                : #undef SLRU_ACC
                                169                 :                :     }
                                170                 :                : 
                                171                 :                :     /* done, clear the pending entry */
                                172   [ +  -  +  -  :         806390 :     MemSet(pending_SLRUStats, 0, sizeof(pending_SLRUStats));
                                     +  -  +  -  +  
                                                 + ]
                                173                 :                : 
                                174                 :          12406 :     LWLockRelease(&stats_shmem->lock);
                                175                 :                : 
                                176                 :          12406 :     have_slrustats = false;
                                177                 :                : 
                                178                 :          12406 :     return false;
                                179                 :                : }
                                180                 :                : 
                                181                 :                : void
  422 michael@paquier.xyz       182                 :           1029 : pgstat_slru_init_shmem_cb(void *stats)
                                183                 :                : {
                                184                 :           1029 :     PgStatShared_SLRU *stats_shmem = (PgStatShared_SLRU *) stats;
                                185                 :                : 
                                186                 :           1029 :     LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
                                187                 :           1029 : }
                                188                 :                : 
                                189                 :                : void
 1249 andres@anarazel.de        190                 :            228 : pgstat_slru_reset_all_cb(TimestampTz ts)
                                191                 :                : {
                                192         [ +  + ]:           2052 :     for (int i = 0; i < SLRU_NUM_ELEMENTS; i++)
                                193                 :           1824 :         pgstat_reset_slru_counter_internal(i, ts);
                                194                 :            228 : }
                                195                 :                : 
                                196                 :                : void
                                197                 :            679 : pgstat_slru_snapshot_cb(void)
                                198                 :                : {
                                199                 :            679 :     PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
                                200                 :                : 
                                201                 :            679 :     LWLockAcquire(&stats_shmem->lock, LW_SHARED);
                                202                 :                : 
                                203                 :            679 :     memcpy(pgStatLocal.snapshot.slru, &stats_shmem->stats,
                                204                 :                :            sizeof(stats_shmem->stats));
                                205                 :                : 
                                206                 :            679 :     LWLockRelease(&stats_shmem->lock);
 1265                           207                 :            679 : }
                                208                 :                : 
                                209                 :                : /*
                                210                 :                :  * Returns pointer to entry with counters for given SLRU (based on the name
                                211                 :                :  * stored in SlruCtl as lwlock tranche name).
                                212                 :                :  */
                                213                 :                : static inline PgStat_SLRUStats *
 1249                           214                 :         884972 : get_slru_entry(int slru_idx)
                                215                 :                : {
 1265                           216                 :         884972 :     pgstat_assert_is_up();
                                217                 :                : 
                                218                 :                :     /*
                                219                 :                :      * The postmaster should never register any SLRU statistics counts; if it
                                220                 :                :      * did, the counts would be duplicated into child processes via fork().
                                221                 :                :      */
                                222   [ +  +  -  + ]:         884972 :     Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
                                223                 :                : 
                                224   [ +  -  -  + ]:         884972 :     Assert((slru_idx >= 0) && (slru_idx < SLRU_NUM_ELEMENTS));
                                225                 :                : 
 1249                           226                 :         884972 :     have_slrustats = true;
   40 michael@paquier.xyz       227                 :         884972 :     pgstat_report_fixed = true;
                                228                 :                : 
 1249 andres@anarazel.de        229                 :         884972 :     return &pending_SLRUStats[slru_idx];
                                230                 :                : }
                                231                 :                : 
                                232                 :                : static void
                                233                 :           1827 : pgstat_reset_slru_counter_internal(int index, TimestampTz ts)
                                234                 :                : {
                                235                 :           1827 :     PgStatShared_SLRU *stats_shmem = &pgStatLocal.shmem->slru;
                                236                 :                : 
                                237                 :           1827 :     LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
                                238                 :                : 
                                239                 :           1827 :     memset(&stats_shmem->stats[index], 0, sizeof(PgStat_SLRUStats));
                                240                 :           1827 :     stats_shmem->stats[index].stat_reset_timestamp = ts;
                                241                 :                : 
                                242                 :           1827 :     LWLockRelease(&stats_shmem->lock);
 1265                           243                 :           1827 : }
        

Generated by: LCOV version 2.4-beta