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

Commite5863bc

Browse files
author
Alexander Korotkov
committed
Some rework.
1 parenta379e36 commite5863bc

File tree

10 files changed

+574
-470
lines changed

10 files changed

+574
-470
lines changed

‎contrib/pg_stat_wait/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# contrib/pg_stat_wait/Makefile
22

33
MODULE_big = pg_stat_wait
4-
OBJS = pg_stat_wait.o collector.o
4+
OBJS = pg_stat_wait.o collector.o descr.o
55

66
EXTENSION = pg_stat_wait
77
DATA = pg_stat_wait--1.0.sql

‎contrib/pg_stat_wait/collector.c

Lines changed: 50 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -20,70 +20,14 @@
2020

2121
#include"pg_stat_wait.h"
2222

23-
CollectorShmqHeader*hdr=NULL;
24-
25-
staticvoid*pgsw;
26-
shm_toc*toc;
27-
shm_mq*mq;
2823
staticvolatilesig_atomic_tshutdown_requested= false;
2924

30-
inthistorySize;
31-
inthistoryPeriod;
32-
boolhistorySkipLatch;
33-
3425
staticvoidhandle_sigterm(SIGNAL_ARGS);
3526
staticvoidcollector_main(Datummain_arg);
3627

3728
/*
38-
*Estimate shared memory space needed.
29+
*Register background worker for collecting waits history.
3930
*/
40-
Size
41-
CollectorShmemSize(void)
42-
{
43-
shm_toc_estimatore;
44-
Sizesize;
45-
46-
shm_toc_initialize_estimator(&e);
47-
shm_toc_estimate_chunk(&e,sizeof(CollectorShmqHeader));
48-
shm_toc_estimate_chunk(&e, (Size)COLLECTOR_QUEUE_SIZE);
49-
shm_toc_estimate_keys(&e,2);
50-
size=shm_toc_estimate(&e);
51-
52-
returnsize;
53-
}
54-
55-
CollectorShmqHeader*
56-
GetCollectorMem(boolinit)
57-
{
58-
boolfound;
59-
Sizesegsize=CollectorShmemSize();
60-
61-
pgsw=ShmemInitStruct("pg_stat_wait",segsize,&found);
62-
if (!init&& !found)
63-
{
64-
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
65-
errmsg("A collector memory wasn't initialized yet")));
66-
}
67-
68-
if (!found)
69-
{
70-
void*mq_mem;
71-
72-
toc=shm_toc_create(PG_STAT_WAIT_MAGIC,pgsw,segsize);
73-
hdr=shm_toc_allocate(toc,sizeof(CollectorShmqHeader));
74-
shm_toc_insert(toc,0,hdr);
75-
76-
mq_mem=shm_toc_allocate(toc,COLLECTOR_QUEUE_SIZE);
77-
shm_toc_insert(toc,1,mq_mem);
78-
}
79-
else
80-
{
81-
toc=shm_toc_attach(PG_STAT_WAIT_MAGIC,pgsw);
82-
hdr=shm_toc_lookup(toc,0);
83-
}
84-
returnhdr;
85-
}
86-
8731
void
8832
RegisterWaitsCollector(void)
8933
{
@@ -110,33 +54,26 @@ AllocHistory(History *observations, int count)
11054
observations->wraparound= false;
11155
}
11256

113-
/* Read current wait information from proc, if readCurrent is true,
57+
/*
58+
* Read current wait information from proc, if readCurrent is true,
11459
* then it reads from currently going wait, and can be inconsistent
11560
*/
116-
int
117-
GetCurrentWaitsState(PGPROC*proc,HistoryItem*item,intidx)
61+
void
62+
ReadCurrentWait(PGPROC*proc,HistoryItem*item)
11863
{
119-
instr_timecurrentTime;
120-
#ifdefNOT_USED
121-
ProcWait*wait;
64+
CurrentWaitEvent*wait;
65+
instr_timecurrentTime;
12266

123-
if (idx==-1)
124-
return0;
67+
wait=&cur_wait_events[proc->pgprocno];
12568

126-
INSTR_TIME_SET_CURRENT(currentTime);
127-
wait=&proc->waits.waitsBuf[idx];
12869
item->backendPid=proc->pid;
129-
item->classId= (int)wait->classId;
130-
if (item->classId==0)
131-
return0;
132-
133-
item->eventId= (int)wait->eventId;
70+
item->classid=wait->classid;
71+
item->eventid=wait->eventid;
72+
memcpy(item->params,wait->params,sizeof(item->params));
13473

135-
INSTR_TIME_SUBTRACT(currentTime,wait->startTime);
74+
INSTR_TIME_SET_CURRENT(currentTime);
75+
INSTR_TIME_SUBTRACT(currentTime,wait->start_time);
13676
item->waitTime=INSTR_TIME_GET_MICROSEC(currentTime);
137-
memcpy(item->params,wait->params,sizeof(item->params));
138-
#endif
139-
return1;
14077
}
14178

14279
staticvoid
@@ -149,7 +86,9 @@ handle_sigterm(SIGNAL_ARGS)
14986
errno=save_errno;
15087
}
15188

152-
/* Circulation in history */
89+
/*
90+
* Get next item of history with rotation.
91+
*/
15392
staticHistoryItem*
15493
get_next_observation(History*observations)
15594
{
@@ -165,41 +104,41 @@ get_next_observation(History *observations)
165104
returnresult;
166105
}
167106

168-
/* Gets current waits from backends */
107+
/*
108+
* Read current waits from backends and write them to history array.
109+
*/
169110
staticvoid
170111
write_waits_history(History*observations,TimestampTzcurrent_ts)
171112
{
172113
inti;
173114

174-
#ifdefNOT_USED
175115
LWLockAcquire(ProcArrayLock,LW_SHARED);
176-
for (i=0;i<ProcGlobal->allProcCount;++i)
116+
for (i=0;i<ProcGlobal->allProcCount;i++)
177117
{
178-
HistoryItemitem,*observation;
179-
PGPROC*proc=&ProcGlobal->allProcs[i];
180-
intstateOk=GetCurrentWaitsState(proc,&item,proc->waits.readIdx);
118+
HistoryItemitem,
119+
*observation;
120+
PGPROC*proc=&ProcGlobal->allProcs[i];
181121

182-
/* mark waits as read */
183-
proc->waits.readIdx=-1;
122+
ReadCurrentWait(proc,&item);
184123

185-
if (stateOk)
186-
{
187-
if (historySkipLatch&&item.classId==WAIT_LATCH)
188-
continue;
124+
if (historySkipLatch&&item.classid==WAIT_LATCH)
125+
continue;
189126

190-
item.ts=current_ts;
191-
observation=get_next_observation(observations);
192-
*observation=item;
193-
}
127+
item.ts=current_ts;
128+
observation=get_next_observation(observations);
129+
*observation=item;
194130
}
195131
LWLockRelease(ProcArrayLock);
196-
#endif
197132
}
198133

134+
/*
135+
* Send waits history to shared memory queue.
136+
*/
199137
staticvoid
200138
send_history(History*observations,shm_mq_handle*mqh)
201139
{
202-
intcount,i;
140+
intcount,
141+
i;
203142

204143
if (observations->wraparound)
205144
count=observations->count;
@@ -211,13 +150,15 @@ send_history(History *observations, shm_mq_handle *mqh)
211150
shm_mq_send(mqh,sizeof(HistoryItem),&observations->items[i], false);
212151
}
213152

153+
/*
154+
* Main routine of wait history collector.
155+
*/
214156
staticvoid
215157
collector_main(Datummain_arg)
216158
{
217-
shm_mq*mq;
218-
shm_mq_handle*mqh;
219-
Historyobservations;
220-
MemoryContextold_context,collector_context;
159+
Historyobservations;
160+
MemoryContextold_context,
161+
collector_context;
221162

222163
/*
223164
* Establish signal handlers.
@@ -232,7 +173,7 @@ collector_main(Datum main_arg)
232173
pqsignal(SIGTERM,handle_sigterm);
233174
BackgroundWorkerUnblockSignals();
234175

235-
hdr->latch=&MyProc->procLatch;
176+
collector_hdr->latch=&MyProc->procLatch;
236177

237178
CurrentResourceOwner=ResourceOwnerCreate(NULL,"pg_stat_wait collector");
238179
collector_context=AllocSetContextCreate(TopMemoryContext,
@@ -246,8 +187,9 @@ collector_main(Datum main_arg)
246187

247188
while (1)
248189
{
249-
intrc;
250-
TimestampTzcurrent_ts;
190+
intrc;
191+
TimestampTzcurrent_ts;
192+
shm_mq_handle*mqh;
251193

252194
ResetLatch(&MyProc->procLatch);
253195
current_ts=GetCurrentTimestamp();
@@ -263,19 +205,18 @@ collector_main(Datum main_arg)
263205
if (rc&WL_POSTMASTER_DEATH)
264206
exit(1);
265207

266-
if (hdr->request==HISTORY_REQUEST)
208+
if (collector_hdr->request==HISTORY_REQUEST)
267209
{
268-
hdr->request=NO_REQUEST;
210+
collector_hdr->request=NO_REQUEST;
269211

270-
mq= (shm_mq*)shm_toc_lookup(toc,1);
271-
shm_mq_set_sender(mq,MyProc);
272-
mqh=shm_mq_attach(mq,NULL,NULL);
212+
shm_mq_set_sender(collector_mq,MyProc);
213+
mqh=shm_mq_attach(collector_mq,NULL,NULL);
273214
shm_mq_wait_for_attach(mqh);
274215

275-
if (shm_mq_get_receiver(mq)!=NULL)
216+
if (shm_mq_get_receiver(collector_mq)!=NULL)
276217
send_history(&observations,mqh);
277218

278-
shm_mq_detach(mq);
219+
shm_mq_detach(collector_mq);
279220
}
280221
}
281222

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp