|
| 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 */ |
| 27 | +typedef struct PgStat_StatInjFixedEntry |
| 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 | + TimestampTz stat_reset_timestamp; |
| 33 | +} PgStat_StatInjFixedEntry; |
| 34 | + |
| 35 | +typedef struct PgStatShared_InjectionPointFixed |
| 36 | +{ |
| 37 | + LWLock lock; /* protects all the counters */ |
| 38 | + uint32 changecount; |
| 39 | + PgStat_StatInjFixedEntry stats; |
| 40 | + PgStat_StatInjFixedEntry reset_offset; |
| 41 | +} PgStatShared_InjectionPointFixed; |
| 42 | + |
| 43 | +/* Callbacks for fixed-numbered stats */ |
| 44 | +static void injection_stats_fixed_init_shmem_cb(void *stats); |
| 45 | +static void injection_stats_fixed_reset_all_cb(TimestampTz ts); |
| 46 | +static void injection_stats_fixed_snapshot_cb(void); |
| 47 | + |
| 48 | +static const PgStat_KindInfo injection_stats_fixed = { |
| 49 | + .name = "injection_points_fixed", |
| 50 | + .fixed_amount = true, |
| 51 | + |
| 52 | + .shared_size = sizeof(PgStat_StatInjFixedEntry), |
| 53 | + .shared_data_off = offsetof(PgStatShared_InjectionPointFixed, stats), |
| 54 | + .shared_data_len = sizeof(((PgStatShared_InjectionPointFixed *) 0)->stats), |
| 55 | + |
| 56 | + .init_shmem_cb = injection_stats_fixed_init_shmem_cb, |
| 57 | + .reset_all_cb = injection_stats_fixed_reset_all_cb, |
| 58 | + .snapshot_cb = injection_stats_fixed_snapshot_cb, |
| 59 | +}; |
| 60 | + |
| 61 | +/* |
| 62 | + * Kind ID reserved for statistics of injection points. |
| 63 | + */ |
| 64 | +#define PGSTAT_KIND_INJECTION_FIXED 130 |
| 65 | + |
| 66 | +/* Track if fixed-numbered stats are loaded */ |
| 67 | +static bool inj_fixed_loaded = false; |
| 68 | + |
| 69 | +static void |
| 70 | +injection_stats_fixed_init_shmem_cb(void *stats) |
| 71 | +{ |
| 72 | + PgStatShared_InjectionPointFixed *stats_shmem = |
| 73 | + (PgStatShared_InjectionPointFixed *) stats; |
| 74 | + |
| 75 | + LWLockInitialize(&stats_shmem->lock, LWTRANCHE_PGSTATS_DATA); |
| 76 | +} |
| 77 | + |
| 78 | +static void |
| 79 | +injection_stats_fixed_reset_all_cb(TimestampTz ts) |
| 80 | +{ |
| 81 | + PgStatShared_InjectionPointFixed *stats_shmem = |
| 82 | + pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED); |
| 83 | + |
| 84 | + LWLockAcquire(&stats_shmem->lock, LW_EXCLUSIVE); |
| 85 | + pgstat_copy_changecounted_stats(&stats_shmem->reset_offset, |
| 86 | + &stats_shmem->stats, |
| 87 | + sizeof(stats_shmem->stats), |
| 88 | + &stats_shmem->changecount); |
| 89 | + stats_shmem->stats.stat_reset_timestamp = ts; |
| 90 | + LWLockRelease(&stats_shmem->lock); |
| 91 | +} |
| 92 | + |
| 93 | +static void |
| 94 | +injection_stats_fixed_snapshot_cb(void) |
| 95 | +{ |
| 96 | + PgStatShared_InjectionPointFixed *stats_shmem = |
| 97 | + pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED); |
| 98 | + PgStat_StatInjFixedEntry *stat_snap = |
| 99 | + pgstat_get_custom_snapshot_data(PGSTAT_KIND_INJECTION_FIXED); |
| 100 | + PgStat_StatInjFixedEntry *reset_offset = &stats_shmem->reset_offset; |
| 101 | + PgStat_StatInjFixedEntry reset; |
| 102 | + |
| 103 | + pgstat_copy_changecounted_stats(stat_snap, |
| 104 | + &stats_shmem->stats, |
| 105 | + sizeof(stats_shmem->stats), |
| 106 | + &stats_shmem->changecount); |
| 107 | + |
| 108 | + LWLockAcquire(&stats_shmem->lock, LW_SHARED); |
| 109 | + memcpy(&reset, reset_offset, sizeof(stats_shmem->stats)); |
| 110 | + LWLockRelease(&stats_shmem->lock); |
| 111 | + |
| 112 | + /* compensate by reset offsets */ |
| 113 | +#define FIXED_COMP(fld) stat_snap->fld -= reset.fld; |
| 114 | + FIXED_COMP(numattach); |
| 115 | + FIXED_COMP(numdetach); |
| 116 | + FIXED_COMP(numrun); |
| 117 | +#undef FIXED_COMP |
| 118 | +} |
| 119 | + |
| 120 | +/* |
| 121 | + * Workhorse to do the registration work, called in _PG_init(). |
| 122 | + */ |
| 123 | +void |
| 124 | +pgstat_register_inj_fixed(void) |
| 125 | +{ |
| 126 | + pgstat_register_kind(PGSTAT_KIND_INJECTION_FIXED, &injection_stats_fixed); |
| 127 | + |
| 128 | + /* mark stats as loaded */ |
| 129 | + inj_fixed_loaded = true; |
| 130 | +} |
| 131 | + |
| 132 | +/* |
| 133 | + * Report fixed number of statistics for an injection point. |
| 134 | + */ |
| 135 | +void |
| 136 | +pgstat_report_inj_fixed(uint32 numattach, |
| 137 | + uint32 numdetach, |
| 138 | + uint32 numrun) |
| 139 | +{ |
| 140 | + PgStatShared_InjectionPointFixed *stats_shmem; |
| 141 | + |
| 142 | + /* leave if disabled */ |
| 143 | + if (!inj_fixed_loaded) |
| 144 | + return; |
| 145 | + |
| 146 | + stats_shmem = pgstat_get_custom_shmem_data(PGSTAT_KIND_INJECTION_FIXED); |
| 147 | + |
| 148 | + pgstat_begin_changecount_write(&stats_shmem->changecount); |
| 149 | + stats_shmem->stats.numattach += numattach; |
| 150 | + stats_shmem->stats.numdetach += numdetach; |
| 151 | + stats_shmem->stats.numrun += numrun; |
| 152 | + pgstat_end_changecount_write(&stats_shmem->changecount); |
| 153 | +} |
| 154 | + |
| 155 | +/* |
| 156 | + * SQL function returning fixed-numbered statistics for injection points. |
| 157 | + */ |
| 158 | +PG_FUNCTION_INFO_V1(injection_points_stats_fixed); |
| 159 | +Datum |
| 160 | +injection_points_stats_fixed(PG_FUNCTION_ARGS) |
| 161 | +{ |
| 162 | + TupleDesc tupdesc; |
| 163 | + Datum values[3] = {0}; |
| 164 | + bool nulls[3] = {0}; |
| 165 | + PgStat_StatInjFixedEntry *stats; |
| 166 | + |
| 167 | + if (!inj_fixed_loaded) |
| 168 | + PG_RETURN_NULL(); |
| 169 | + |
| 170 | + pgstat_snapshot_fixed(PGSTAT_KIND_INJECTION_FIXED); |
| 171 | + stats = pgstat_get_custom_snapshot_data(PGSTAT_KIND_INJECTION_FIXED); |
| 172 | + |
| 173 | + /* Initialise attributes information in the tuple descriptor */ |
| 174 | + tupdesc = CreateTemplateTupleDesc(3); |
| 175 | + TupleDescInitEntry(tupdesc, (AttrNumber) 1, "numattach", |
| 176 | + INT8OID, -1, 0); |
| 177 | + TupleDescInitEntry(tupdesc, (AttrNumber) 2, "numdetach", |
| 178 | + INT8OID, -1, 0); |
| 179 | + TupleDescInitEntry(tupdesc, (AttrNumber) 3, "numrun", |
| 180 | + INT8OID, -1, 0); |
| 181 | + BlessTupleDesc(tupdesc); |
| 182 | + |
| 183 | + values[0] = Int64GetDatum(stats->numattach); |
| 184 | + values[1] = Int64GetDatum(stats->numdetach); |
| 185 | + values[2] = Int64GetDatum(stats->numrun); |
| 186 | + nulls[0] = false; |
| 187 | + nulls[1] = false; |
| 188 | + nulls[2] = false; |
| 189 | + |
| 190 | + /* Returns the record as Datum */ |
| 191 | + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); |
| 192 | +} |
0 commit comments