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

Commit4302efe

Browse files
author
Alexander Korotkov
committed
Session tracing.
1 parent29c3dd7 commit4302efe

File tree

4 files changed

+162
-33
lines changed

4 files changed

+162
-33
lines changed

‎contrib/pg_stat_wait/descr.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,16 @@ const int numberOfEvents[] = {
9494
WAIT_NETWORK_EVENTS_COUNT
9595
};
9696

97+
constchar*
98+
getWaitClassName(uint32classid)
99+
{
100+
returnwaitClassNames[classid];
101+
}
102+
97103
/*
98104
* Get name of particular wait event.
99105
*/
100-
staticconstchar*
106+
constchar*
101107
getWaitEventName(uint32classid,uint32eventid)
102108
{
103109
switch (classid)

‎contrib/pg_stat_wait/pg_stat_wait--1.0.sql

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
\echo Use"CREATE EXTENSION pg_stat_wait" to load this file. \quit
55

66
CREATEFUNCTIONpg_wait_class_list(
7-
OUT class_id int4,
8-
OUT name cstring
7+
OUT class_id int4,
8+
OUT name cstring
99
)
1010
RETURNS SETOF record
1111
AS'MODULE_PATHNAME'
1212
LANGUAGE C VOLATILE;
1313

1414
CREATEFUNCTIONpg_wait_event_list(
15-
OUT class_id int4,
16-
OUT event_id int4,
17-
OUT name cstring
15+
OUT class_id int4,
16+
OUT event_id int4,
17+
OUT name cstring
1818
)
1919
RETURNS SETOF record
2020
AS'MODULE_PATHNAME'
@@ -34,11 +34,11 @@ CREATE VIEW pg_wait_events AS
3434

3535
/* Returns history, parameters count must be equal with WAIT_PARAMS_COUNT in proc.h*/
3636
CREATEFUNCTIONpg_stat_wait_get_history(
37-
OUT pid int4,
38-
OUT sample_tstimestamptz,
39-
OUT class_id int4,
40-
OUT event_id int4,
41-
OUT wait_time int8,
37+
OUT pid int4,
38+
OUT sample_tstimestamptz,
39+
OUT class_id int4,
40+
OUT event_id int4,
41+
OUT wait_time int8,
4242
OUT p1 int4,
4343
OUT p2 int4,
4444
OUT p3 int4,
@@ -56,12 +56,12 @@ CREATE VIEW pg_stat_wait_history AS
5656
ONe.class_id=h.class_idande.event_id=h.event_id;
5757

5858
CREATEFUNCTIONpg_stat_wait_get_profile(
59-
pid int4,
60-
resetboolean,
61-
OUT pid int4,
62-
OUT class_id int4,
63-
OUT event_id int4,
64-
OUT wait_time int8,
59+
pid int4,
60+
resetboolean,
61+
OUT pid int4,
62+
OUT class_id int4,
63+
OUT event_id int4,
64+
OUT wait_time int8,
6565
OUT wait_count int8
6666
)
6767
RETURNS SETOF record
@@ -80,12 +80,12 @@ AS 'MODULE_PATHNAME'
8080
LANGUAGE C VOLATILE STRICT;
8181

8282
CREATEFUNCTIONpg_stat_wait_get_current(
83-
pid int4,
84-
OUT pid int4,
85-
OUT sample_tstimestamptz,
86-
OUT class_id int4,
87-
OUT event_id int4,
88-
OUT wait_time int8,
83+
pid int4,
84+
OUT pid int4,
85+
OUT sample_tstimestamptz,
86+
OUT class_id int4,
87+
OUT event_id int4,
88+
OUT wait_time int8,
8989
OUT p1 int4,
9090
OUT p2 int4,
9191
OUT p3 int4,
@@ -97,29 +97,29 @@ AS 'MODULE_PATHNAME'
9797
LANGUAGE c VOLATILE CALLEDonNULL INPUT;
9898

9999
CREATEVIEWpg_stat_wait_currentAS
100-
SELECT pid, sample_ts,c.class_id,e.class_name,c.event_id,e.event_name,
101-
wait_time, p1, p2, p3, p4, p5
102-
FROM pg_stat_wait_get_current(null) c
103-
INNER JOIN pg_wait_events e
104-
ONe.class_id=c.class_idande.event_id=c.event_id;
100+
SELECT pid, sample_ts,c.class_id,e.class_name,c.event_id,e.event_name,
101+
wait_time, p1, p2, p3, p4, p5
102+
FROM pg_stat_wait_get_current(null) c
103+
INNER JOIN pg_wait_events e
104+
ONe.class_id=c.class_idande.event_id=c.event_id;
105105

106106
CREATEFUNCTIONpg_start_trace(
107-
backend_pid int4,
108-
filename cstring
107+
backend_pid int4,
108+
filename cstring
109109
)
110110
RETURNS void
111111
AS'MODULE_PATHNAME'
112112
LANGUAGE C VOLATILE CALLEDONNULL INPUT;
113113

114114
CREATEFUNCTIONpg_is_in_trace(
115-
backend_pid int4
115+
backend_pid int4
116116
)
117117
RETURNS bool
118118
AS'MODULE_PATHNAME'
119119
LANGUAGE C VOLATILE CALLEDONNULL INPUT;
120120

121121
CREATEFUNCTIONpg_stop_trace(
122-
backend_pid int4
122+
backend_pid int4
123123
)
124124
RETURNS void
125125
AS'MODULE_PATHNAME'

‎contrib/pg_stat_wait/pg_stat_wait.c

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include"storage/procarray.h"
2222
#include"storage/shm_mq.h"
2323
#include"storage/shm_toc.h"
24+
#include"utils/datetime.h"
2425
#include"utils/guc.h"
2526
#include"utils/wait.h"
2627

@@ -45,6 +46,7 @@ ProfileItem *profile = NULL;
4546
TraceInfo*trace_info=NULL;
4647

4748
staticintmaxProcs;
49+
staticboolin_wait_hook= false;
4850

4951
staticshmem_startup_hook_typeprev_shmem_startup_hook=NULL;
5052
staticwait_event_start_hook_typeprev_wait_event_start_hook=NULL;
@@ -185,20 +187,116 @@ update_profile(uint32 classid, uint32 eventid, uint64 interval)
185187
item->interval+=interval;
186188
}
187189

190+
staticvoid
191+
timestamptz_out_static(TimestampTzdt,char*buf)
192+
{
193+
inttz;
194+
structpg_tmtt,
195+
*tm=&tt;
196+
fsec_tfsec;
197+
constchar*tzn;
198+
199+
if (TIMESTAMP_NOT_FINITE(dt))
200+
EncodeSpecialTimestamp(dt,buf);
201+
elseif (timestamp2tm(dt,&tz,tm,&fsec,&tzn,NULL)==0)
202+
EncodeDateTime(tm,fsec, true,tz,tzn,DateStyle,buf);
203+
else
204+
ereport(ERROR,
205+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
206+
errmsg("timestamp out of range")));
207+
}
208+
209+
210+
staticvoid
211+
write_trace_start(FILE*fd,intclassid,inteventid,
212+
intp1,intp2,intp3,intp4,intp5)
213+
{
214+
TimestampTzcurrent_ts;
215+
intn;
216+
charbuf[256],tsbuf[MAXDATELEN+1];
217+
218+
Assert(fd!=NULL);
219+
current_ts=GetCurrentTimestamp();
220+
timestamptz_out_static(current_ts,tsbuf);
221+
n=snprintf(buf,sizeof(buf),"start %s %s %s %d %d %d %d %d\n",
222+
tsbuf,
223+
getWaitClassName(classid),
224+
getWaitEventName(classid,eventid),
225+
p1,p2,p3,p4,p5);
226+
227+
if (n!=-1)
228+
{
229+
fwrite(buf,sizeof(char),n,fd);
230+
fflush(fd);
231+
}
232+
else
233+
{
234+
elog(INFO,"Wait trace formatting error");
235+
}
236+
}
237+
238+
staticvoid
239+
write_trace_stop(FILE*fd,intclassId)
240+
{
241+
TimestampTzcurrent_ts;
242+
intn;
243+
charbuf[256],tsbuf[MAXDATELEN+1];
244+
245+
Assert(fd!=NULL);
246+
current_ts=GetCurrentTimestamp();
247+
timestamptz_out_static(current_ts,tsbuf);
248+
n=snprintf(buf,sizeof(buf),"stop %s %s\n",
249+
tsbuf,
250+
getWaitClassName(classId));
251+
252+
if (n!=-1)
253+
{
254+
fwrite(buf,sizeof(char),n,fd);
255+
fflush(fd);
256+
}
257+
}
258+
188259
staticvoid
189260
update_current_event(uint32classid,uint32eventid,
190261
uint32p1,uint32p2,uint32p3,uint32p4,uint32p5)
191262
{
192263
CurrentWaitEventWrap*wrap;
193264
CurrentWaitEvent*prevEvent,
194265
*event;
266+
TraceInfo*trace;
195267
instr_timecurTime;
196268

197269
if (!shmem_initialized|| !MyProc)
198270
return;
199271

200272
INSTR_TIME_SET_CURRENT(curTime);
201273

274+
trace=&trace_info[MyProc->pgprocno];
275+
/*
276+
* If tracing was started with `pg_start_trace`,
277+
* we initialize file descriptor here.
278+
*/
279+
if (trace->traceOn&&trace->fd==NULL)
280+
{
281+
trace->fd=fopen(trace->filename,"w");
282+
if (trace->fd==NULL)
283+
{
284+
trace->traceOn= false;
285+
elog(WARNING,"could not open trace file \"%s\": %m",
286+
trace->filename);
287+
}
288+
else
289+
{
290+
elog(INFO,"Trace was started to: %s",trace->filename);
291+
}
292+
}
293+
elseif (!trace->traceOn&&trace->fd!=NULL)
294+
{
295+
fclose(trace->fd);
296+
trace->fd=NULL;
297+
elog(INFO,"Trace was stopped");
298+
}
299+
202300
wrap=&cur_wait_events[MyProc->pgprocno];
203301
prevEvent=&wrap->data[wrap->curidx %2];
204302
event=&wrap->data[(wrap->curidx+1) %2];
@@ -211,6 +309,17 @@ update_current_event(uint32 classid, uint32 eventid,
211309
event->params[4]=p5;
212310
event->start_time=curTime;
213311

312+
if (trace->fd!=NULL&&prevEvent->classeventid!=event->classeventid)
313+
{
314+
uint32busyevent= (WAIT_CPU <<16) |WAIT_CPU_BUSY;
315+
if (prevEvent->classeventid!=busyevent)
316+
write_trace_stop(trace->fd, (prevEvent->classeventid >>16));
317+
if (event->classeventid!=busyevent)
318+
write_trace_start(trace->fd,classid,eventid,p1,p2,p3,p4,p5);
319+
}
320+
321+
322+
214323
INSTR_TIME_SUBTRACT(curTime,prevEvent->start_time);
215324

216325
update_profile(prevEvent->classeventid >>16,
@@ -221,18 +330,23 @@ update_current_event(uint32 classid, uint32 eventid,
221330
wrap->curidx++;
222331
}
223332

224-
225333
/*
226334
* Wait event start hook: put wait information to the shared memory.
227335
*/
228336
staticvoid
229337
pgsw_wait_event_start_hook(uint32classid,uint32eventid,
230338
uint32p1,uint32p2,uint32p3,uint32p4,uint32p5)
231339
{
340+
if (in_wait_hook)
341+
return;
342+
in_wait_hook= true;
343+
232344
update_current_event(classid,eventid,p1,p2,p3,p4,p5);
233345

234346
if (prev_wait_event_start_hook)
235347
prev_wait_event_start_hook(classid,eventid,p1,p2,p3,p4,p5);
348+
349+
in_wait_hook= false;
236350
}
237351

238352

@@ -243,10 +357,16 @@ pgsw_wait_event_start_hook(uint32 classid, uint32 eventid,
243357
staticvoid
244358
pgsw_wait_event_stop_hook(void)
245359
{
360+
if (in_wait_hook)
361+
return;
362+
in_wait_hook= true;
363+
246364
update_current_event(WAIT_CPU,WAIT_CPU_BUSY,0,0,0,0,0);
247365

248366
if (prev_wait_event_stop_hook)
249367
prev_wait_event_stop_hook();
368+
369+
in_wait_hook= false;
250370
}
251371

252372
/*

‎contrib/pg_stat_wait/pg_stat_wait.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ typedef struct
104104
{
105105
booltraceOn;
106106
charfilename[WAIT_TRACE_FN_LEN+1];
107+
FILE*fd;
107108
}TraceInfo;
108109

109110
/* pg_stat_wait.c */
@@ -122,5 +123,7 @@ extern void ReadCurrentWait(PGPROC *proc, HistoryItem *item);
122123

123124
/* descr.c */
124125
externconstintnumberOfEvents[];
126+
constchar*getWaitClassName(uint32classid);
127+
constchar*getWaitEventName(uint32classid,uint32eventid);
125128

126129
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp