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-2024, 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 "common/hashfn.h"
20 #include "funcapi.h"
21 #include "injection_stats.h"
22 #include "pgstat.h"
23 #include "utils/builtins.h"
24 #include "utils/pgstat_internal.h"
25 
26 /* Structures for statistics of injection points, fixed-size */
28 {
29  PgStat_Counter numattach; /* number of points attached */
30  PgStat_Counter numdetach; /* number of points detached */
31  PgStat_Counter numrun; /* number of points run */
32  PgStat_Counter numcached; /* number of points cached */
33  PgStat_Counter numloaded; /* number of points loaded */
36 
38 {
39  LWLock lock; /* protects all the counters */
44 
45 /* Callbacks for fixed-numbered stats */
46 static void injection_stats_fixed_init_shmem_cb(void *stats);
48 static void injection_stats_fixed_snapshot_cb(void);
49 
51  .name = "injection_points_fixed",
52  .fixed_amount = true,
53 
54  .shared_size = sizeof(PgStat_StatInjFixedEntry),
55  .shared_data_off = offsetof(PgStatShared_InjectionPointFixed, stats),
56  .shared_data_len = sizeof(((PgStatShared_InjectionPointFixed *) 0)->stats),
57 
58  .init_shmem_cb = injection_stats_fixed_init_shmem_cb,
59  .reset_all_cb = injection_stats_fixed_reset_all_cb,
60  .snapshot_cb = injection_stats_fixed_snapshot_cb,
61 };
62 
63 /*
64  * Kind ID reserved for statistics of injection points.
65  */
66 #define PGSTAT_KIND_INJECTION_FIXED 130
67 
68 /* Track if fixed-numbered stats are loaded */
69 static bool inj_fixed_loaded = false;
70 
71 static void
73 {
76 
78 }
79 
80 static void
82 {
85 
86  LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE);
88  &stats_shmem->stats,
89  sizeof(stats_shmem->stats),
90  &stats_shmem->changecount);
91  stats_shmem->stats.stat_reset_timestamp = ts;
92  LWLockRelease(&stats_shmem->lock);
93 }
94 
95 static void
97 {
100  PgStat_StatInjFixedEntry *stat_snap =
102  PgStat_StatInjFixedEntry *reset_offset = &stats_shmem->reset_offset;
104 
106  &stats_shmem->stats,
107  sizeof(stats_shmem->stats),
108  &stats_shmem->changecount);
109 
110  LWLockAcquire(&stats_shmem->lock, LW_SHARED);
111  memcpy(&reset, reset_offset, sizeof(stats_shmem->stats));
112  LWLockRelease(&stats_shmem->lock);
113 
114  /* compensate by reset offsets */
115 #define FIXED_COMP(fld) stat_snap->fld -= reset.fld;
116  FIXED_COMP(numattach);
117  FIXED_COMP(numdetach);
118  FIXED_COMP(numrun);
119  FIXED_COMP(numcached);
120  FIXED_COMP(numloaded);
121 #undef FIXED_COMP
122 }
123 
124 /*
125  * Workhorse to do the registration work, called in _PG_init().
126  */
127 void
129 {
131 
132  /* mark stats as loaded */
133  inj_fixed_loaded = true;
134 }
135 
136 /*
137  * Report fixed number of statistics for an injection point.
138  */
139 void
141  uint32 numdetach,
142  uint32 numrun,
143  uint32 numcached,
144  uint32 numloaded)
145 {
147 
148  /* leave if disabled */
150  return;
151 
153 
155  stats_shmem->stats.numattach += numattach;
156  stats_shmem->stats.numdetach += numdetach;
157  stats_shmem->stats.numrun += numrun;
158  stats_shmem->stats.numcached += numcached;
159  stats_shmem->stats.numloaded += numloaded;
161 }
162 
163 /*
164  * SQL function returning fixed-numbered statistics for injection points.
165  */
167 Datum
169 {
170  TupleDesc tupdesc;
171  Datum values[5] = {0};
172  bool nulls[5] = {0};
174 
176  PG_RETURN_NULL();
177 
180 
181  /* Initialise attributes information in the tuple descriptor */
182  tupdesc = CreateTemplateTupleDesc(5);
183  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "numattach",
184  INT8OID, -1, 0);
185  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "numdetach",
186  INT8OID, -1, 0);
187  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "numrun",
188  INT8OID, -1, 0);
189  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "numcached",
190  INT8OID, -1, 0);
191  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "numloaded",
192  INT8OID, -1, 0);
193  BlessTupleDesc(tupdesc);
194 
195  values[0] = Int64GetDatum(stats->numattach);
196  values[1] = Int64GetDatum(stats->numdetach);
197  values[2] = Int64GetDatum(stats->numrun);
198  values[3] = Int64GetDatum(stats->numcached);
199  values[4] = Int64GetDatum(stats->numloaded);
200  nulls[0] = false;
201  nulls[1] = false;
202  nulls[2] = false;
203  nulls[3] = false;
204  nulls[4] = false;
205 
206  /* Returns the record as Datum */
208 }
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:150
unsigned int uint32
Definition: c.h:506
int64 TimestampTz
Definition: timestamp.h:39
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2158
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#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:1116
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:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:707
@ LWTRANCHE_PGSTATS_DATA
Definition: lwlock.h:205
@ LW_SHARED
Definition: lwlock.h:115
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1054
void pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
Definition: pgstat.c:1458
int64 PgStat_Counter
Definition: pgstat.h:120
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)
uintptr_t Datum
Definition: postgres.h:64
void reset(void)
Definition: sql-declare.c:600
Definition: lwlock.h:42
PgStat_StatInjFixedEntry reset_offset
const char *const name
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:67
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:651