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

Commit29c3dd7

Browse files
author
Alexander Korotkov
committed
Enable trace functions.
1 parent712b6b1 commit29c3dd7

File tree

4 files changed

+123
-46
lines changed

4 files changed

+123
-46
lines changed

‎contrib/pg_stat_wait/collector.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
* collector.c
3+
*Collector of wait event history.
4+
*
5+
* Copyright (c) 2015-2016, Postgres Professional
6+
*
7+
* IDENTIFICATION
8+
* contrib/pg_stat_wait/collector.c
9+
*/
110
#include"postgres.h"
211

312
#include"access/htup_details.h"
@@ -45,6 +54,9 @@ RegisterWaitsCollector(void)
4554
RegisterBackgroundWorker(&worker);
4655
}
4756

57+
/*
58+
* Allocate memory for waits history.
59+
*/
4860
void
4961
AllocHistory(History*observations,intcount)
5062
{
@@ -55,8 +67,7 @@ AllocHistory(History *observations, int count)
5567
}
5668

5769
/*
58-
* Read current wait information from proc, if readCurrent is true,
59-
* then it reads from currently going wait, and can be inconsistent
70+
* Read current wait information for given proc.
6071
*/
6172
void
6273
ReadCurrentWait(PGPROC*proc,HistoryItem*item)
@@ -71,10 +82,10 @@ ReadCurrentWait(PGPROC *proc, HistoryItem *item)
7182

7283
wrap=&cur_wait_events[proc->pgprocno];
7384
previdx=wrap->curidx;
74-
event=&wrap->data[(previdx+1) %2];
7585

7686
pg_read_barrier();
7787

88+
event=&wrap->data[(previdx+1) %2];
7889
item->backendPid=proc->pid;
7990
item->classid=event->classeventid >>16;
8091
item->eventid=event->classeventid&0xFFFF;

‎contrib/pg_stat_wait/descr.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
* descr.c
3+
*Meta information about wait events.
4+
*
5+
* Copyright (c) 2015-2016, Postgres Professional
6+
*
7+
* IDENTIFICATION
8+
* contrib/pg_stat_wait/descr.c
9+
*/
110
#include"postgres.h"
211
#include"fmgr.h"
312
#include"funcapi.h"
@@ -85,7 +94,9 @@ const int numberOfEvents[] = {
8594
WAIT_NETWORK_EVENTS_COUNT
8695
};
8796

88-
97+
/*
98+
* Get name of particular wait event.
99+
*/
89100
staticconstchar*
90101
getWaitEventName(uint32classid,uint32eventid)
91102
{

‎contrib/pg_stat_wait/pg_stat_wait.c

Lines changed: 81 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
* pg_stat_wait.c
3+
*Track information about wait events.
4+
*
5+
* Copyright (c) 2015-2016, Postgres Professional
6+
*
7+
* IDENTIFICATION
8+
* contrib/pg_stat_wait/pg_stat_waits.c
9+
*/
110
#include"postgres.h"
211
#include"fmgr.h"
312
#include"funcapi.h"
@@ -26,11 +35,14 @@ boolwaitsHistoryOn;
2635
inthistorySize;
2736
inthistoryPeriod;
2837
boolhistorySkipLatch;
38+
39+
/* Shared memory variables */
2940
shm_toc*toc=NULL;
3041
CollectorShmqHeader*collector_hdr=NULL;
3142
shm_mq*collector_mq=NULL;
3243
CurrentWaitEventWrap*cur_wait_events=NULL;
3344
ProfileItem*profile=NULL;
45+
TraceInfo*trace_info=NULL;
3446

3547
staticintmaxProcs;
3648

@@ -57,7 +69,8 @@ pgsw_shmem_size(void)
5769

5870
shm_toc_estimate_chunk(&e,sizeof(CurrentWaitEventWrap)*maxProcs);
5971
shm_toc_estimate_chunk(&e,sizeof(ProfileItem)*maxProcs*WAIT_EVENTS_COUNT);
60-
nkeys=2;
72+
shm_toc_estimate_chunk(&e,sizeof(TraceInfo)*maxProcs);
73+
nkeys=3;
6174

6275
if (waitsHistoryOn)
6376
{
@@ -72,6 +85,9 @@ pgsw_shmem_size(void)
7285
returnsize;
7386
}
7487

88+
/*
89+
* Make initial state of current wait events.
90+
*/
7591
staticvoid
7692
init_current_wait_event()
7793
{
@@ -114,12 +130,16 @@ pgsw_shmem_startup(void)
114130
shm_toc_insert(toc,1,profile);
115131
memset(profile,0,sizeof(ProfileItem)*maxProcs*WAIT_EVENTS_COUNT);
116132

133+
trace_info=shm_toc_allocate(toc,sizeof(TraceInfo)*maxProcs);
134+
shm_toc_insert(toc,2,trace_info);
135+
memset(trace_info,0,sizeof(TraceInfo)*maxProcs);
136+
117137
if (waitsHistoryOn)
118138
{
119139
collector_hdr=shm_toc_allocate(toc,sizeof(CollectorShmqHeader));
120-
shm_toc_insert(toc,2,collector_hdr);
140+
shm_toc_insert(toc,3,collector_hdr);
121141
collector_mq=shm_toc_allocate(toc,COLLECTOR_QUEUE_SIZE);
122-
shm_toc_insert(toc,3,collector_mq);
142+
shm_toc_insert(toc,4,collector_mq);
123143
}
124144
}
125145
else
@@ -128,11 +148,12 @@ pgsw_shmem_startup(void)
128148

129149
cur_wait_events=shm_toc_lookup(toc,0);
130150
profile=shm_toc_lookup(toc,1);
151+
trace_info=shm_toc_lookup(toc,2);
131152

132153
if (waitsHistoryOn)
133154
{
134-
collector_hdr=shm_toc_lookup(toc,2);
135-
collector_mq=shm_toc_lookup(toc,3);
155+
collector_hdr=shm_toc_lookup(toc,3);
156+
collector_mq=shm_toc_lookup(toc,4);
136157
}
137158
}
138159

@@ -308,6 +329,9 @@ _PG_fini(void)
308329
shmem_startup_hook=prev_shmem_startup_hook;
309330
}
310331

332+
/*
333+
* Make a TupleDesc describing single item of waits history.
334+
*/
311335
staticTupleDesc
312336
get_history_item_tupledesc()
313337
{
@@ -459,7 +483,7 @@ get_proc_pid_by_idx(int i)
459483
}
460484

461485
staticint
462-
find_proc_offset(intpid)
486+
find_procno(intpid)
463487
{
464488
inti,result=-1;
465489

@@ -499,7 +523,7 @@ pg_stat_wait_get_profile(PG_FUNCTION_ARGS)
499523
if (!PG_ARGISNULL(0))
500524
{
501525
params->procPid=PG_GETARG_UINT32(0);
502-
params->procIdx=find_proc_offset(params->procPid);
526+
params->procIdx=find_procno(params->procPid);
503527
}
504528
else
505529
{
@@ -749,34 +773,40 @@ PG_FUNCTION_INFO_V1(pg_start_trace);
749773
Datum
750774
pg_start_trace(PG_FUNCTION_ARGS)
751775
{
752-
#ifdefNOT_USED
753-
PGPROC*proc;
776+
intprocno;
777+
TraceInfo*traceItem;
754778
char*filename=PG_GETARG_CSTRING(1);
755779

756-
if (strlen(filename) >=WAIT_TRACE_FN_LEN)
780+
if (strlen(filename)>WAIT_TRACE_FN_LEN)
757781
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
758-
errmsg("length of filename limited to %d",(WAIT_TRACE_FN_LEN-1))));
782+
errmsg("length of filename limited to %d",WAIT_TRACE_FN_LEN)));
759783

760784
if (!is_absolute_path(filename))
761785
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
762786
errmsg("path must be absolute")));
763787

764-
proc=NULL;
765788
if (PG_ARGISNULL(0))
766-
proc=MyProc;
789+
{
790+
procno=MyProc->pgprocno;
791+
}
767792
else
768-
proc=search_proc(PG_GETARG_INT32(0));
769-
770-
if (proc!=NULL)
771793
{
772-
if (proc->waits.traceOn)
794+
procno=find_procno(PG_GETARG_INT32(0));
795+
if (procno<0)
773796
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
774-
errmsg("trace is already working in backend")));
775-
776-
strcpy(proc->waits.traceFn,filename);
777-
proc->waits.traceOn= true;
797+
errmsg("pid is not found")));
778798
}
779-
#endif
799+
800+
traceItem=&trace_info[procno];
801+
if (traceItem->traceOn)
802+
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
803+
errmsg("trace is already working in backend")));
804+
805+
strcpy(traceItem->filename,filename);
806+
807+
pg_write_barrier();
808+
809+
traceItem->traceOn= true;
780810

781811
PG_RETURN_VOID();
782812
}
@@ -785,41 +815,51 @@ PG_FUNCTION_INFO_V1(pg_is_in_trace);
785815
Datum
786816
pg_is_in_trace(PG_FUNCTION_ARGS)
787817
{
788-
#ifdefNOT_USED
789-
PGPROC*proc=NULL;
818+
intprocno;
819+
TraceInfo*traceItem;
790820

791821
if (PG_ARGISNULL(0))
792-
proc=MyProc;
822+
{
823+
procno=MyProc->pgprocno;
824+
}
793825
else
794-
proc=search_proc(PG_GETARG_INT32(0));
826+
{
827+
procno=find_procno(PG_GETARG_INT32(0));
828+
if (procno<0)
829+
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
830+
errmsg("pid is not found")));
831+
}
795832

796-
if (proc)
797-
PG_RETURN_BOOL(proc->waits.traceOn);
798-
#endif
833+
traceItem=&trace_info[procno];
799834

800-
PG_RETURN_BOOL(false);
835+
PG_RETURN_BOOL(traceItem->traceOn);
801836
}
802837

803838
PG_FUNCTION_INFO_V1(pg_stop_trace);
804839
Datum
805840
pg_stop_trace(PG_FUNCTION_ARGS)
806841
{
807-
PGPROC*proc=NULL;
842+
intprocno;
843+
TraceInfo*traceItem;
844+
808845
if (PG_ARGISNULL(0))
809-
proc=MyProc;
846+
{
847+
procno=MyProc->pgprocno;
848+
}
810849
else
811-
proc=search_proc(PG_GETARG_INT32(0));
812-
813-
#ifdefNOT_USED
814-
if (proc!=NULL)
815850
{
816-
if (!proc->waits.traceOn)
851+
procno=find_procno(PG_GETARG_INT32(0));
852+
if (procno<0)
817853
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
818-
errmsg("trace is not started")));
819-
820-
proc->waits.traceOn= false;
854+
errmsg("pid is not found")));
821855
}
822-
#endif
856+
857+
traceItem=&trace_info[procno];
858+
if (!traceItem->traceOn)
859+
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
860+
errmsg("trace is not started")));
861+
862+
traceItem->traceOn= false;
823863

824864
PG_RETURN_VOID();
825865
}

‎contrib/pg_stat_wait/pg_stat_wait.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
* pg_stat_wait.h
3+
*Headers for pg_stat_wait extension.
4+
*
5+
* Copyright (c) 2015-2016, Postgres Professional
6+
*
7+
* IDENTIFICATION
8+
* contrib/pg_stat_wait/pg_stat_waits.h
9+
*/
110
#ifndef__PG_STAT_WAIT_H__
211
#define__PG_STAT_WAIT_H__
312

@@ -89,7 +98,13 @@ typedef struct
8998
WAIT_LOCKS_COUNT + WAIT_IO_EVENTS_COUNT + \
9099
WAIT_NETWORK_EVENTS_COUNT)
91100

92-
#defineWAIT_TRACE_FN_LEN(4096 + 1)
101+
#defineWAIT_TRACE_FN_LEN(4096)
102+
103+
typedefstruct
104+
{
105+
booltraceOn;
106+
charfilename[WAIT_TRACE_FN_LEN+1];
107+
}TraceInfo;
93108

94109
/* pg_stat_wait.c */
95110
externvoidcheck_shmem(void);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp