Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf68cd84

Browse files
committed
injection_points: Add some fixed-numbered statistics
Like7553443, this acts mainly as a template to show what can beachieved with fixed-numbered stats (like WAL, bgwriter, etc.) with thepluggable cumulative statistics APIs introduced in7949d95.Fixed-numbered stats are defined in their own file, namedinjection_stats_fixed.c, separated entirely from the variable-numberedcase in injection_stats.c. This is mainly for clarity as having bothexamples in the same file would be confusing.Note that this commit uses the helper routines added in2eff9e6.The stats stored track globally the number of times injection pointshave been attached, detached or run. Two more fields should be addedlater for the number of times a point has been cached or loaded, butwhat's here is enough as a template.More TAP tests are added, providing coverage for fixed-numbered customstats.Author: Michael PaquierReviewed-by: Dmitry Dolgov, Bertrand DrouvotDiscussion:https://postgr.es/m/Zmqm9j5EO0I4W8dx@paquier.xyz
1 parent7553443 commitf68cd84

File tree

8 files changed

+229
-2
lines changed

8 files changed

+229
-2
lines changed

‎src/test/modules/injection_points/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ MODULE_big = injection_points
44
OBJS =\
55
$(WIN32RES)\
66
injection_points.o\
7-
injection_stats.o
7+
injection_stats.o\
8+
injection_stats_fixed.o
89
EXTENSION = injection_points
910
DATA = injection_points--1.0.sql
1011
PGFILEDESC = "injection_points - facility for injection points"

‎src/test/modules/injection_points/injection_points--1.0.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ CREATE FUNCTION injection_points_stats_numcalls(IN point_name TEXT)
8484
RETURNSbigint
8585
AS'MODULE_PATHNAME','injection_points_stats_numcalls'
8686
LANGUAGE C STRICT;
87+
88+
--
89+
-- injection_points_stats_fixed()
90+
--
91+
-- Reports fixed-numbered statistics for injection points.
92+
CREATEFUNCTIONinjection_points_stats_fixed(OUT numattach int8,
93+
OUT numdetach int8,
94+
OUT numrun int8)
95+
RETURNS record
96+
AS'MODULE_PATHNAME','injection_points_stats_fixed'
97+
LANGUAGE C STRICT;

‎src/test/modules/injection_points/injection_points.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ injection_points_attach(PG_FUNCTION_ARGS)
297297
condition.pid=MyProcPid;
298298
}
299299

300+
pgstat_report_inj_fixed(1,0,0);
300301
InjectionPointAttach(name,"injection_points",function,&condition,
301302
sizeof(InjectionPointCondition));
302303

@@ -342,6 +343,7 @@ injection_points_run(PG_FUNCTION_ARGS)
342343
{
343344
char*name=text_to_cstring(PG_GETARG_TEXT_PP(0));
344345

346+
pgstat_report_inj_fixed(0,0,1);
345347
INJECTION_POINT(name);
346348

347349
PG_RETURN_VOID();
@@ -432,6 +434,7 @@ injection_points_detach(PG_FUNCTION_ARGS)
432434
{
433435
char*name=text_to_cstring(PG_GETARG_TEXT_PP(0));
434436

437+
pgstat_report_inj_fixed(0,1,0);
435438
if (!InjectionPointDetach(name))
436439
elog(ERROR,"could not detach injection point \"%s\"",name);
437440

@@ -459,4 +462,5 @@ _PG_init(void)
459462
return;
460463

461464
pgstat_register_inj();
465+
pgstat_register_inj_fixed();
462466
}

‎src/test/modules/injection_points/injection_stats.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
#ifndefINJECTION_STATS
1616
#defineINJECTION_STATS
1717

18+
/* injection_stats.c */
1819
externvoidpgstat_register_inj(void);
1920
externvoidpgstat_create_inj(constchar*name);
2021
externvoidpgstat_drop_inj(constchar*name);
2122
externvoidpgstat_report_inj(constchar*name);
2223

24+
/* injection_stats_fixed.c */
25+
externvoidpgstat_register_inj_fixed(void);
26+
externvoidpgstat_report_inj_fixed(uint32numattach,
27+
uint32numdetach,
28+
uint32numrun);
29+
2330
#endif
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
typedefstructPgStat_StatInjFixedEntry
28+
{
29+
PgStat_Counternumattach;/* number of points attached */
30+
PgStat_Counternumdetach;/* number of points detached */
31+
PgStat_Counternumrun;/* number of points run */
32+
TimestampTzstat_reset_timestamp;
33+
}PgStat_StatInjFixedEntry;
34+
35+
typedefstructPgStatShared_InjectionPointFixed
36+
{
37+
LWLocklock;/* protects all the counters */
38+
uint32changecount;
39+
PgStat_StatInjFixedEntrystats;
40+
PgStat_StatInjFixedEntryreset_offset;
41+
}PgStatShared_InjectionPointFixed;
42+
43+
/* Callbacks for fixed-numbered stats */
44+
staticvoidinjection_stats_fixed_init_shmem_cb(void*stats);
45+
staticvoidinjection_stats_fixed_reset_all_cb(TimestampTzts);
46+
staticvoidinjection_stats_fixed_snapshot_cb(void);
47+
48+
staticconstPgStat_KindInfoinjection_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+
#definePGSTAT_KIND_INJECTION_FIXED130
65+
66+
/* Track if fixed-numbered stats are loaded */
67+
staticboolinj_fixed_loaded= false;
68+
69+
staticvoid
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+
staticvoid
79+
injection_stats_fixed_reset_all_cb(TimestampTzts)
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+
staticvoid
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_StatInjFixedEntryreset;
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+
#defineFIXED_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(uint32numattach,
137+
uint32numdetach,
138+
uint32numrun)
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+
TupleDesctupdesc;
163+
Datumvalues[3]= {0};
164+
boolnulls[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+
}

‎src/test/modules/injection_points/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ endif
77
injection_points_sources=files(
88
'injection_points.c',
99
'injection_stats.c',
10+
'injection_stats_fixed.c',
1011
)
1112

1213
if host_system=='windows'

‎src/test/modules/injection_points/t/001_stats.pl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,27 @@
3333
my$numcalls =$node->safe_psql('postgres',
3434
"SELECT injection_points_stats_numcalls('stats-notice');");
3535
is($numcalls,'2','number of stats calls');
36+
my$fixedstats =$node->safe_psql('postgres',
37+
"SELECT * FROM injection_points_stats_fixed();");
38+
is($fixedstats,'1|0|2','number of fixed stats');
3639

3740
# Restart the node cleanly, stats should still be around.
3841
$node->restart;
3942
$numcalls =$node->safe_psql('postgres',
4043
"SELECT injection_points_stats_numcalls('stats-notice');");
4144
is($numcalls,'2','number of stats after clean restart');
45+
$fixedstats =$node->safe_psql('postgres',
46+
"SELECT * FROM injection_points_stats_fixed();");
47+
is($fixedstats,'1|0|2','number of fixed stats after clean restart');
4248

4349
# On crash the stats are gone.
4450
$node->stop('immediate');
4551
$node->start;
4652
$numcalls =$node->safe_psql('postgres',
4753
"SELECT injection_points_stats_numcalls('stats-notice');");
48-
is($numcalls,'','number of stats after clean restart');
54+
is($numcalls,'','number of stats after crash');
55+
$fixedstats =$node->safe_psql('postgres',
56+
"SELECT * FROM injection_points_stats_fixed();");
57+
is($fixedstats,'0|0|0','number of fixed stats after crash');
4958

5059
done_testing();

‎src/tools/pgindent/typedefs.list

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,7 @@ PgStatShared_Database
21202120
PgStatShared_Function
21212121
PgStatShared_HashEntry
21222122
PgStatShared_InjectionPoint
2123+
PgStatShared_InjectionPointFixed
21232124
PgStatShared_IO
21242125
PgStatShared_Relation
21252126
PgStatShared_ReplSlot
@@ -2152,6 +2153,7 @@ PgStat_SnapshotEntry
21522153
PgStat_StatDBEntry
21532154
PgStat_StatFuncEntry
21542155
PgStat_StatInjEntry
2156+
PgStat_StatInjFixedEntry
21552157
PgStat_StatReplSlotEntry
21562158
PgStat_StatSubEntry
21572159
PgStat_StatTabEntry

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp