1 /*--------------------------------------------------------------------------
3 * injection_stats_fixed.c
4 * Code for fixed-numbered statistics of injection points.
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/test/modules/injection_points/injection_stats_fixed.c
12 * -------------------------------------------------------------------------
19 #include "common/hashfn.h"
21 #include "injection_stats.h"
23 #include "utils/builtins.h"
24 #include "utils/pgstat_internal.h"
26 /* Structures for statistics of injection points, fixed-size */
27 typedef struct PgStat_StatInjFixedEntry
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 */
34 TimestampTz stat_reset_timestamp
;
35 } PgStat_StatInjFixedEntry
;
37 typedef struct PgStatShared_InjectionPointFixed
39 LWLock lock
; /* protects all the counters */
41 PgStat_StatInjFixedEntry stats
;
42 PgStat_StatInjFixedEntry reset_offset
;
43 } PgStatShared_InjectionPointFixed
;
45 /* Callbacks for fixed-numbered stats */
46 static void injection_stats_fixed_init_shmem_cb(void *stats
);
47 static void injection_stats_fixed_reset_all_cb(TimestampTz ts
);
48 static void injection_stats_fixed_snapshot_cb(void);
50 static const PgStat_KindInfo injection_stats_fixed
= {
51 .name
= "injection_points_fixed",
53 .write_to_file
= true,
55 .shared_size
= sizeof(PgStat_StatInjFixedEntry
),
56 .shared_data_off
= offsetof(PgStatShared_InjectionPointFixed
, stats
),
57 .shared_data_len
= sizeof(((PgStatShared_InjectionPointFixed
*) 0)->stats
),
59 .init_shmem_cb
= injection_stats_fixed_init_shmem_cb
,
60 .reset_all_cb
= injection_stats_fixed_reset_all_cb
,
61 .snapshot_cb
= injection_stats_fixed_snapshot_cb
,
65 * Kind ID reserved for statistics of injection points.
67 #define PGSTAT_KIND_INJECTION_FIXED 130
69 /* Track if fixed-numbered stats are loaded */
70 static bool inj_fixed_loaded
= false;
73 injection_stats_fixed_init_shmem_cb(void *stats
)
75 PgStatShared_InjectionPointFixed
*stats_shmem
=
76 (PgStatShared_InjectionPointFixed
*) stats
;
78 LWLockInitialize(&stats_shmem
->lock
, LWTRANCHE_PGSTATS_DATA
);
82 injection_stats_fixed_reset_all_cb(TimestampTz ts
)
84 PgStatShared_InjectionPointFixed
*stats_shmem
=
85 pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED
);
87 LWLockAcquire(&stats_shmem
->lock
, LW_EXCLUSIVE
);
88 pgstat_copy_changecounted_stats(&stats_shmem
->reset_offset
,
90 sizeof(stats_shmem
->stats
),
91 &stats_shmem
->changecount
);
92 stats_shmem
->stats
.stat_reset_timestamp
= ts
;
93 LWLockRelease(&stats_shmem
->lock
);
97 injection_stats_fixed_snapshot_cb(void)
99 PgStatShared_InjectionPointFixed
*stats_shmem
=
100 pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED
);
101 PgStat_StatInjFixedEntry
*stat_snap
=
102 pgstat_get_custom_snapshot_data(PGSTAT_KIND_INJECTION_FIXED
);
103 PgStat_StatInjFixedEntry
*reset_offset
= &stats_shmem
->reset_offset
;
104 PgStat_StatInjFixedEntry reset
;
106 pgstat_copy_changecounted_stats(stat_snap
,
108 sizeof(stats_shmem
->stats
),
109 &stats_shmem
->changecount
);
111 LWLockAcquire(&stats_shmem
->lock
, LW_SHARED
);
112 memcpy(&reset
, reset_offset
, sizeof(stats_shmem
->stats
));
113 LWLockRelease(&stats_shmem
->lock
);
115 /* compensate by reset offsets */
116 #define FIXED_COMP(fld) stat_snap->fld -= reset.fld;
117 FIXED_COMP(numattach
);
118 FIXED_COMP(numdetach
);
120 FIXED_COMP(numcached
);
121 FIXED_COMP(numloaded
);
126 * Workhorse to do the registration work, called in _PG_init().
129 pgstat_register_inj_fixed(void)
131 pgstat_register_kind(PGSTAT_KIND_INJECTION_FIXED
, &injection_stats_fixed
);
133 /* mark stats as loaded */
134 inj_fixed_loaded
= true;
138 * Report fixed number of statistics for an injection point.
141 pgstat_report_inj_fixed(uint32 numattach
,
147 PgStatShared_InjectionPointFixed
*stats_shmem
;
149 /* leave if disabled */
150 if (!inj_fixed_loaded
|| !inj_stats_enabled
)
153 stats_shmem
= pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED
);
155 pgstat_begin_changecount_write(&stats_shmem
->changecount
);
156 stats_shmem
->stats
.numattach
+= numattach
;
157 stats_shmem
->stats
.numdetach
+= numdetach
;
158 stats_shmem
->stats
.numrun
+= numrun
;
159 stats_shmem
->stats
.numcached
+= numcached
;
160 stats_shmem
->stats
.numloaded
+= numloaded
;
161 pgstat_end_changecount_write(&stats_shmem
->changecount
);
165 * SQL function returning fixed-numbered statistics for injection points.
167 PG_FUNCTION_INFO_V1(injection_points_stats_fixed
);
169 injection_points_stats_fixed(PG_FUNCTION_ARGS
)
172 Datum values
[5] = {0};
174 PgStat_StatInjFixedEntry
*stats
;
176 if (!inj_fixed_loaded
|| !inj_stats_enabled
)
179 pgstat_snapshot_fixed(PGSTAT_KIND_INJECTION_FIXED
);
180 stats
= pgstat_get_custom_snapshot_data(PGSTAT_KIND_INJECTION_FIXED
);
182 /* Initialise attributes information in the tuple descriptor */
183 tupdesc
= CreateTemplateTupleDesc(5);
184 TupleDescInitEntry(tupdesc
, (AttrNumber
) 1, "numattach",
186 TupleDescInitEntry(tupdesc
, (AttrNumber
) 2, "numdetach",
188 TupleDescInitEntry(tupdesc
, (AttrNumber
) 3, "numrun",
190 TupleDescInitEntry(tupdesc
, (AttrNumber
) 4, "numcached",
192 TupleDescInitEntry(tupdesc
, (AttrNumber
) 5, "numloaded",
194 BlessTupleDesc(tupdesc
);
196 values
[0] = Int64GetDatum(stats
->numattach
);
197 values
[1] = Int64GetDatum(stats
->numdetach
);
198 values
[2] = Int64GetDatum(stats
->numrun
);
199 values
[3] = Int64GetDatum(stats
->numcached
);
200 values
[4] = Int64GetDatum(stats
->numloaded
);
207 /* Returns the record as Datum */
208 PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc
, values
, nulls
)));