|
| 1 | +/* ------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pgstat_backend.c |
| 4 | + * Implementation of backend statistics. |
| 5 | + * |
| 6 | + * This file contains the implementation of backend 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 | + * This statistics kind uses a proc number as object ID for the hash table |
| 12 | + * of pgstats. Entries are created each time a process is spawned, and are |
| 13 | + * dropped when the process exits. These are not written to the pgstats file |
| 14 | + * on disk. |
| 15 | + * |
| 16 | + * Copyright (c) 2001-2024, PostgreSQL Global Development Group |
| 17 | + * |
| 18 | + * IDENTIFICATION |
| 19 | + * src/backend/utils/activity/pgstat_backend.c |
| 20 | + * ------------------------------------------------------------------------- |
| 21 | + */ |
| 22 | + |
| 23 | +#include"postgres.h" |
| 24 | + |
| 25 | +#include"utils/pgstat_internal.h" |
| 26 | + |
| 27 | +/* |
| 28 | + * Returns statistics of a backend by proc number. |
| 29 | + */ |
| 30 | +PgStat_Backend* |
| 31 | +pgstat_fetch_stat_backend(ProcNumberprocNumber) |
| 32 | +{ |
| 33 | +PgStat_Backend*backend_entry; |
| 34 | + |
| 35 | +backend_entry= (PgStat_Backend*)pgstat_fetch_entry(PGSTAT_KIND_BACKEND, |
| 36 | +InvalidOid,procNumber); |
| 37 | + |
| 38 | +returnbackend_entry; |
| 39 | +} |
| 40 | + |
| 41 | +/* |
| 42 | + * Flush out locally pending backend statistics |
| 43 | + * |
| 44 | + * If no stats have been recorded, this function returns false. |
| 45 | + */ |
| 46 | +bool |
| 47 | +pgstat_backend_flush_cb(PgStat_EntryRef*entry_ref,boolnowait) |
| 48 | +{ |
| 49 | +PgStatShared_Backend*shbackendioent; |
| 50 | +PgStat_BackendPendingIO*pendingent; |
| 51 | +PgStat_BktypeIO*bktype_shstats; |
| 52 | + |
| 53 | +if (!pgstat_lock_entry(entry_ref,nowait)) |
| 54 | +return false; |
| 55 | + |
| 56 | +shbackendioent= (PgStatShared_Backend*)entry_ref->shared_stats; |
| 57 | +bktype_shstats=&shbackendioent->stats.stats; |
| 58 | +pendingent= (PgStat_BackendPendingIO*)entry_ref->pending; |
| 59 | + |
| 60 | +for (intio_object=0;io_object<IOOBJECT_NUM_TYPES;io_object++) |
| 61 | +{ |
| 62 | +for (intio_context=0;io_context<IOCONTEXT_NUM_TYPES;io_context++) |
| 63 | +{ |
| 64 | +for (intio_op=0;io_op<IOOP_NUM_TYPES;io_op++) |
| 65 | +{ |
| 66 | +instr_timetime; |
| 67 | + |
| 68 | +bktype_shstats->counts[io_object][io_context][io_op]+= |
| 69 | +pendingent->counts[io_object][io_context][io_op]; |
| 70 | + |
| 71 | +time=pendingent->pending_times[io_object][io_context][io_op]; |
| 72 | + |
| 73 | +bktype_shstats->times[io_object][io_context][io_op]+= |
| 74 | +INSTR_TIME_GET_MICROSEC(time); |
| 75 | +} |
| 76 | +} |
| 77 | +} |
| 78 | + |
| 79 | +pgstat_unlock_entry(entry_ref); |
| 80 | + |
| 81 | +return true; |
| 82 | +} |
| 83 | + |
| 84 | +/* |
| 85 | + * Simpler wrapper of pgstat_backend_flush_cb() |
| 86 | + */ |
| 87 | +void |
| 88 | +pgstat_flush_backend(boolnowait) |
| 89 | +{ |
| 90 | +PgStat_EntryRef*entry_ref; |
| 91 | + |
| 92 | +if (!pgstat_tracks_backend_bktype(MyBackendType)) |
| 93 | +return; |
| 94 | + |
| 95 | +entry_ref=pgstat_get_entry_ref(PGSTAT_KIND_BACKEND,InvalidOid, |
| 96 | +MyProcNumber, false,NULL); |
| 97 | +(void)pgstat_backend_flush_cb(entry_ref,nowait); |
| 98 | +} |
| 99 | + |
| 100 | +/* |
| 101 | + * Create backend statistics entry for proc number. |
| 102 | + */ |
| 103 | +void |
| 104 | +pgstat_create_backend(ProcNumberprocnum) |
| 105 | +{ |
| 106 | +PgStat_EntryRef*entry_ref; |
| 107 | +PgStatShared_Backend*shstatent; |
| 108 | + |
| 109 | +entry_ref=pgstat_prep_pending_entry(PGSTAT_KIND_BACKEND,InvalidOid, |
| 110 | +procnum,NULL); |
| 111 | + |
| 112 | +shstatent= (PgStatShared_Backend*)entry_ref->shared_stats; |
| 113 | + |
| 114 | +/* |
| 115 | + * NB: need to accept that there might be stats from an older backend, |
| 116 | + * e.g. if we previously used this proc number. |
| 117 | + */ |
| 118 | +memset(&shstatent->stats,0,sizeof(shstatent->stats)); |
| 119 | +} |
| 120 | + |
| 121 | +/* |
| 122 | + * Find or create a local PgStat_BackendPendingIO entry for proc number. |
| 123 | + */ |
| 124 | +PgStat_BackendPendingIO* |
| 125 | +pgstat_prep_backend_pending(ProcNumberprocnum) |
| 126 | +{ |
| 127 | +PgStat_EntryRef*entry_ref; |
| 128 | + |
| 129 | +entry_ref=pgstat_prep_pending_entry(PGSTAT_KIND_BACKEND,InvalidOid, |
| 130 | +procnum,NULL); |
| 131 | + |
| 132 | +returnentry_ref->pending; |
| 133 | +} |
| 134 | + |
| 135 | +/* |
| 136 | + * Backend statistics are not collected for all BackendTypes. |
| 137 | + * |
| 138 | + * The following BackendTypes do not participate in the backend stats |
| 139 | + * subsystem: |
| 140 | + * - The same and for the same reasons as in pgstat_tracks_io_bktype(). |
| 141 | + * - B_BG_WRITER, B_CHECKPOINTER, B_STARTUP and B_AUTOVAC_LAUNCHER because their |
| 142 | + * I/O stats are already visible in pg_stat_io and there is only one of those. |
| 143 | + * |
| 144 | + * Function returns true if BackendType participates in the backend stats |
| 145 | + * subsystem and false if it does not. |
| 146 | + * |
| 147 | + * When adding a new BackendType, also consider adding relevant restrictions to |
| 148 | + * pgstat_tracks_io_object() and pgstat_tracks_io_op(). |
| 149 | + */ |
| 150 | +bool |
| 151 | +pgstat_tracks_backend_bktype(BackendTypebktype) |
| 152 | +{ |
| 153 | +/* |
| 154 | + * List every type so that new backend types trigger a warning about |
| 155 | + * needing to adjust this switch. |
| 156 | + */ |
| 157 | +switch (bktype) |
| 158 | +{ |
| 159 | +caseB_INVALID: |
| 160 | +caseB_AUTOVAC_LAUNCHER: |
| 161 | +caseB_DEAD_END_BACKEND: |
| 162 | +caseB_ARCHIVER: |
| 163 | +caseB_LOGGER: |
| 164 | +caseB_WAL_RECEIVER: |
| 165 | +caseB_WAL_WRITER: |
| 166 | +caseB_WAL_SUMMARIZER: |
| 167 | +caseB_BG_WRITER: |
| 168 | +caseB_CHECKPOINTER: |
| 169 | +caseB_STARTUP: |
| 170 | +return false; |
| 171 | + |
| 172 | +caseB_AUTOVAC_WORKER: |
| 173 | +caseB_BACKEND: |
| 174 | +caseB_BG_WORKER: |
| 175 | +caseB_STANDALONE_BACKEND: |
| 176 | +caseB_SLOTSYNC_WORKER: |
| 177 | +caseB_WAL_SENDER: |
| 178 | +return true; |
| 179 | +} |
| 180 | + |
| 181 | +return false; |
| 182 | +} |
| 183 | + |
| 184 | +void |
| 185 | +pgstat_backend_reset_timestamp_cb(PgStatShared_Common*header,TimestampTzts) |
| 186 | +{ |
| 187 | +((PgStatShared_Backend*)header)->stats.stat_reset_timestamp=ts; |
| 188 | +} |