PostgreSQL Source Code: src/test/modules/injection_points/injection_stats_fixed.c Source File

PostgreSQL Source Code git master
injection_stats_fixed.c
Go to the documentation of this file.
1/*--------------------------------------------------------------------------
2 *
3 * injection_stats_fixed.c
4 * Code for fixed-numbered statistics of injection points.
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * IDENTIFICATION
10 * src/test/modules/injection_points/injection_stats_fixed.c
11 *
12 * -------------------------------------------------------------------------
13 */
14
15#include "postgres.h"
16
17#include "fmgr.h"
18
19#include "access/htup_details.h"
20#include "common/hashfn.h"
21#include "funcapi.h"
22#include "injection_stats.h"
23#include "pgstat.h"
24#include "utils/builtins.h"
25#include "utils/pgstat_internal.h"
26
27/* Structures for statistics of injection points, fixed-size */
28 typedef struct PgStat_StatInjFixedEntry
29{
30 PgStat_Counter numattach; /* number of points attached */
31 PgStat_Counter numdetach; /* number of points detached */
32 PgStat_Counter numrun; /* number of points run */
33 PgStat_Counter numcached; /* number of points cached */
34 PgStat_Counter numloaded; /* number of points loaded */
35 TimestampTz stat_reset_timestamp;
36 } PgStat_StatInjFixedEntry;
37
38 typedef struct PgStatShared_InjectionPointFixed
39{
40 LWLock lock; /* protects all the counters */
41 uint32 changecount;
42 PgStat_StatInjFixedEntry stats;
43 PgStat_StatInjFixedEntry reset_offset;
44 } PgStatShared_InjectionPointFixed;
45
46/* Callbacks for fixed-numbered stats */
47static void injection_stats_fixed_init_shmem_cb(void *stats);
48static void injection_stats_fixed_reset_all_cb(TimestampTz ts);
49static void injection_stats_fixed_snapshot_cb(void);
50
51 static const PgStat_KindInfo injection_stats_fixed = {
52 .name = "injection_points_fixed",
53 .fixed_amount = true,
54 .write_to_file = true,
55
56 .shared_size = sizeof(PgStat_StatInjFixedEntry),
57 .shared_data_off = offsetof(PgStatShared_InjectionPointFixed, stats),
58 .shared_data_len = sizeof(((PgStatShared_InjectionPointFixed *) 0)->stats),
59
60 .init_shmem_cb = injection_stats_fixed_init_shmem_cb,
61 .reset_all_cb = injection_stats_fixed_reset_all_cb,
62 .snapshot_cb = injection_stats_fixed_snapshot_cb,
63};
64
65/*
66 * Kind ID reserved for statistics of injection points.
67 */
68 #define PGSTAT_KIND_INJECTION_FIXED 26
69
70/* Track if fixed-numbered stats are loaded */
71 static bool inj_fixed_loaded = false;
72
73static void
74 injection_stats_fixed_init_shmem_cb(void *stats)
75{
76 PgStatShared_InjectionPointFixed *stats_shmem =
77 (PgStatShared_InjectionPointFixed *) stats;
78
79 LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA);
80}
81
82static void
83 injection_stats_fixed_reset_all_cb(TimestampTz ts)
84{
85 PgStatShared_InjectionPointFixed *stats_shmem =
86 pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED);
87
88 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
89 pgstat_copy_changecounted_stats(&stats_shmem->reset_offset,
90 &stats_shmem->stats,
91 sizeof(stats_shmem->stats),
92 &stats_shmem->changecount);
93 stats_shmem->stats.stat_reset_timestamp = ts;
94 LWLockRelease(&stats_shmem->lock);
95}
96
97static void
98 injection_stats_fixed_snapshot_cb(void)
99{
100 PgStatShared_InjectionPointFixed *stats_shmem =
101 pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED);
102 PgStat_StatInjFixedEntry *stat_snap =
103 pgstat_get_custom_snapshot_data(PGSTAT_KIND_INJECTION_FIXED);
104 PgStat_StatInjFixedEntry *reset_offset = &stats_shmem->reset_offset;
105 PgStat_StatInjFixedEntry reset;
106
107 pgstat_copy_changecounted_stats(stat_snap,
108 &stats_shmem->stats,
109 sizeof(stats_shmem->stats),
110 &stats_shmem->changecount);
111
112 LWLockAcquire(&stats_shmem->lock, LW_SHARED);
113 memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
114 LWLockRelease(&stats_shmem->lock);
115
116 /* compensate by reset offsets */
117#define FIXED_COMP(fld) stat_snap->fld -= reset.fld;
118 FIXED_COMP(numattach);
119 FIXED_COMP(numdetach);
120 FIXED_COMP(numrun);
121 FIXED_COMP(numcached);
122 FIXED_COMP(numloaded);
123#undef FIXED_COMP
124}
125
126/*
127 * Workhorse to do the registration work, called in _PG_init().
128 */
129void
130 pgstat_register_inj_fixed(void)
131{
132 pgstat_register_kind(PGSTAT_KIND_INJECTION_FIXED, &injection_stats_fixed);
133
134 /* mark stats as loaded */
135 inj_fixed_loaded = true;
136}
137
138/*
139 * Report fixed number of statistics for an injection point.
140 */
141void
142 pgstat_report_inj_fixed(uint32 numattach,
143 uint32 numdetach,
144 uint32 numrun,
145 uint32 numcached,
146 uint32 numloaded)
147{
148 PgStatShared_InjectionPointFixed *stats_shmem;
149
150 /* leave if disabled */
151 if (!inj_fixed_loaded || !inj_stats_enabled)
152 return;
153
154 stats_shmem = pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED);
155
156 LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
157
158 pgstat_begin_changecount_write(&stats_shmem->changecount);
159 stats_shmem->stats.numattach += numattach;
160 stats_shmem->stats.numdetach += numdetach;
161 stats_shmem->stats.numrun += numrun;
162 stats_shmem->stats.numcached += numcached;
163 stats_shmem->stats.numloaded += numloaded;
164 pgstat_end_changecount_write(&stats_shmem->changecount);
165
166 LWLockRelease(&stats_shmem->lock);
167}
168
169/*
170 * SQL function returning fixed-numbered statistics for injection points.
171 */
172 PG_FUNCTION_INFO_V1(injection_points_stats_fixed);
173Datum
174 injection_points_stats_fixed(PG_FUNCTION_ARGS)
175{
176 TupleDesc tupdesc;
177 Datum values[5] = {0};
178 bool nulls[5] = {0};
179 PgStat_StatInjFixedEntry *stats;
180
181 if (!inj_fixed_loaded || !inj_stats_enabled)
182 PG_RETURN_NULL();
183
184 pgstat_snapshot_fixed(PGSTAT_KIND_INJECTION_FIXED);
185 stats = pgstat_get_custom_snapshot_data(PGSTAT_KIND_INJECTION_FIXED);
186
187 /* Initialise attributes information in the tuple descriptor */
188 tupdesc = CreateTemplateTupleDesc(5);
189 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "numattach",
190 INT8OID, -1, 0);
191 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "numdetach",
192 INT8OID, -1, 0);
193 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "numrun",
194 INT8OID, -1, 0);
195 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "numcached",
196 INT8OID, -1, 0);
197 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "numloaded",
198 INT8OID, -1, 0);
199 BlessTupleDesc(tupdesc);
200
201 values[0] = Int64GetDatum(stats->numattach);
202 values[1] = Int64GetDatum(stats->numdetach);
203 values[2] = Int64GetDatum(stats->numrun);
204 values[3] = Int64GetDatum(stats->numcached);
205 values[4] = Int64GetDatum(stats->numloaded);
206 nulls[0] = false;
207 nulls[1] = false;
208 nulls[2] = false;
209 nulls[3] = false;
210 nulls[4] = false;
211
212 /* Returns the record as Datum */
213 PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
214}
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:153
uint32_t uint32
Definition: c.h:538
int64 TimestampTz
Definition: timestamp.h:39
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2260
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
bool inj_stats_enabled
struct PgStat_StatInjFixedEntry PgStat_StatInjFixedEntry
static void injection_stats_fixed_reset_all_cb(TimestampTz ts)
static bool inj_fixed_loaded
void pgstat_register_inj_fixed(void)
#define FIXED_COMP(fld)
Datum injection_points_stats_fixed(PG_FUNCTION_ARGS)
#define PGSTAT_KIND_INJECTION_FIXED
struct PgStatShared_InjectionPointFixed PgStatShared_InjectionPointFixed
void pgstat_report_inj_fixed(uint32 numattach, uint32 numdetach, uint32 numrun, uint32 numcached, uint32 numloaded)
static const PgStat_KindInfo injection_stats_fixed
static void injection_stats_fixed_snapshot_cb(void)
static void injection_stats_fixed_init_shmem_cb(void *stats)
PG_FUNCTION_INFO_V1(injection_points_stats_fixed)
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:698
@ LW_SHARED
Definition: lwlock.h:113
@ LW_EXCLUSIVE
Definition: lwlock.h:112
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1059
void pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
Definition: pgstat.c:1462
int64 PgStat_Counter
Definition: pgstat.h:66
static void * pgstat_get_custom_snapshot_data(PgStat_Kind kind)
static void * pgstat_get_custom_shmem_data(PgStat_Kind kind)
static void pgstat_end_changecount_write(uint32 *cc)
static void pgstat_begin_changecount_write(uint32 *cc)
static void pgstat_copy_changecounted_stats(void *dst, void *src, size_t len, uint32 *cc)
static Datum Int64GetDatum(int64 X)
Definition: postgres.h:403
uint64_t Datum
Definition: postgres.h:70
void reset(void)
Definition: sql-declare.c:600
Definition: lwlock.h:42
PgStat_StatInjFixedEntry stats
PgStat_StatInjFixedEntry reset_offset
const char *const name
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:182
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:842

AltStyle によって変換されたページ (->オリジナル) /