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

Commitb7c34c2

Browse files
author
Alexander Korotkov
committed
Some comments.
1 parenta8110d3 commitb7c34c2

File tree

3 files changed

+63
-41
lines changed

3 files changed

+63
-41
lines changed

‎collector.c

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ register_wait_collector(void)
3838
{
3939
BackgroundWorkerworker;
4040

41-
/*set upcommon data for all our workers */
41+
/*Set upbackground worker parameters */
4242
worker.bgw_flags=BGWORKER_SHMEM_ACCESS;
4343
worker.bgw_start_time=BgWorkerStart_ConsistentState;
4444
worker.bgw_restart_time=BGW_NEVER_RESTART;
@@ -68,11 +68,14 @@ static void
6868
realloc_history(History*observations,intcount)
6969
{
7070
HistoryItem*newitems;
71-
intcopyCount;
72-
inti,j;
71+
intcopyCount,
72+
i,
73+
j;
7374

75+
/* Allocate new array for history */
7476
newitems= (HistoryItem*)palloc0(sizeof(HistoryItem)*count);
7577

78+
/* Copy entries from old array to the new */
7679
if (observations->wraparound)
7780
copyCount=observations->count;
7881
else
@@ -94,9 +97,9 @@ realloc_history(History *observations, int count)
9497
j++;
9598
}
9699

100+
/* Switch to new history array */
97101
pfree(observations->items);
98102
observations->items=newitems;
99-
100103
observations->index=copyCount;
101104
observations->count=count;
102105
observations->wraparound= false;
@@ -131,7 +134,8 @@ get_next_observation(History *observations)
131134
}
132135

133136
/*
134-
* Read current waits from backends and write them to history array.
137+
* Read current waits from backends and write them to history array
138+
* and/or profile hash.
135139
*/
136140
staticvoid
137141
probe_waits(History*observations,HTAB*profile_hash,
@@ -146,6 +150,7 @@ probe_waits(History *observations, HTAB *profile_hash,
146150
if (observations->count!=newSize)
147151
realloc_history(observations,newSize);
148152

153+
/* Iterate PGPROCs under shared lock */
149154
LWLockAcquire(ProcArrayLock,LW_SHARED);
150155
for (i=0;i<ProcGlobal->allProcCount;i++)
151156
{
@@ -156,16 +161,19 @@ probe_waits(History *observations, HTAB *profile_hash,
156161
if (proc->pid==0)
157162
continue;
158163

164+
/* Collect next wait event sample */
159165
item.pid=proc->pid;
160166
item.wait_event_info=proc->wait_event_info;
161167
item.ts=ts;
162168

169+
/* Write to the history if needed */
163170
if (write_history)
164171
{
165172
observation=get_next_observation(observations);
166173
*observation=item;
167174
}
168175

176+
/* Write to the profile if needed */
169177
if (write_profile)
170178
{
171179
ProfileItem*profileItem;
@@ -200,6 +208,9 @@ send_history(History *observations, shm_mq_handle *mqh)
200208
shm_mq_send(mqh,sizeof(HistoryItem),&observations->items[i], false);
201209
}
202210

211+
/*
212+
* Send profile to shared memory queue.
213+
*/
203214
staticvoid
204215
send_profile(HTAB*profile_hash,shm_mq_handle*mqh)
205216
{
@@ -215,6 +226,9 @@ send_profile(HTAB *profile_hash, shm_mq_handle *mqh)
215226
}
216227
}
217228

229+
/*
230+
* Make hash table for wait profile.
231+
*/
218232
staticHTAB*
219233
make_profile_hash()
220234
{
@@ -229,6 +243,9 @@ make_profile_hash()
229243
HASH_FUNCTION |HASH_ELEM);
230244
}
231245

246+
/*
247+
* Delta between two timestamps in milliseconds.
248+
*/
232249
staticint64
233250
millisecs_diff(TimestampTztz1,TimestampTztz2)
234251
{
@@ -322,46 +339,51 @@ collector_main(Datum main_arg)
322339
}
323340
}
324341

342+
/* Shutdown if requested */
343+
if (shutdown_requested)
344+
break;
345+
325346
rc=WaitLatch(&MyProc->procLatch,WL_LATCH_SET |WL_TIMEOUT |WL_POSTMASTER_DEATH,
326347
Min(history_period- (int)history_diff,
327348
profile_period- (int)profile_diff));
328349

329350
if (rc&WL_POSTMASTER_DEATH)
330351
exit(1);
331352

332-
if (shutdown_requested)
333-
break;
334-
335353
ResetLatch(&MyProc->procLatch);
336354

355+
/* Handle request if any */
337356
if (collector_hdr->request!=NO_REQUEST)
338357
{
339358
SHMRequestrequest=collector_hdr->request;
340359

341360
collector_hdr->request=NO_REQUEST;
342361

343-
shm_mq_set_sender(collector_mq,MyProc);
344-
mqh=shm_mq_attach(collector_mq,NULL,NULL);
345-
shm_mq_wait_for_attach(mqh);
346-
347-
if (shm_mq_get_receiver(collector_mq)!=NULL)
362+
if (request==HISTORY_REQUEST||request==PROFILE_REQUEST)
348363
{
349-
if (request==HISTORY_REQUEST)
350-
{
351-
send_history(&observations,mqh);
352-
}
353-
elseif (request==PROFILE_REQUEST)
364+
/* Send history or profile */
365+
shm_mq_set_sender(collector_mq,MyProc);
366+
mqh=shm_mq_attach(collector_mq,NULL,NULL);
367+
shm_mq_wait_for_attach(mqh);
368+
if (shm_mq_get_receiver(collector_mq)!=NULL)
354369
{
355-
send_profile(profile_hash,mqh);
356-
}
357-
elseif (request==PROFILE_RESET)
358-
{
359-
hash_destroy(profile_hash);
360-
profile_hash=make_profile_hash();
370+
if (request==HISTORY_REQUEST)
371+
{
372+
send_history(&observations,mqh);
373+
}
374+
elseif (request==PROFILE_REQUEST)
375+
{
376+
send_profile(profile_hash,mqh);
377+
}
361378
}
379+
shm_mq_detach(collector_mq);
380+
}
381+
elseif (request==PROFILE_RESET)
382+
{
383+
/* Reset profile hash */
384+
hash_destroy(profile_hash);
385+
profile_hash=make_profile_hash();
362386
}
363-
364-
shm_mq_detach(collector_mq);
365387
}
366388
}
367389

‎pg_wait_sampling.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,15 @@ pgws_shmem_startup(void)
103103
collector_mq=shm_toc_lookup(toc,1);
104104
}
105105

106+
/* Initialize GUC variables in shared memory */
106107
DefineCustomIntVariable("pg_wait_sampling.history_size",
107108
"Sets size of waits history.",NULL,
108109
&collector_hdr->historySize,5000,100,INT_MAX,
109110
PGC_SUSET,0,shmem_int_guc_check_hook,NULL,NULL);
110-
111111
DefineCustomIntVariable("pg_wait_sampling.history_period",
112112
"Sets period of waits history sampling.",NULL,
113113
&collector_hdr->historyPeriod,10,1,INT_MAX,
114114
PGC_SUSET,0,shmem_int_guc_check_hook,NULL,NULL);
115-
116115
DefineCustomIntVariable("pg_wait_sampling.profile_period",
117116
"Sets period of waits profile sampling.",NULL,
118117
&collector_hdr->profilePeriod,10,1,INT_MAX,
@@ -173,7 +172,8 @@ _PG_fini(void)
173172
}
174173

175174
/*
176-
* Find PGPROC entry responsible for given pid.
175+
* Find PGPROC entry responsible for given pid assuming ProcArrayLock was
176+
* already taken.
177177
*/
178178
staticPGPROC*
179179
search_proc(intpid)
@@ -199,7 +199,7 @@ search_proc(int pid)
199199

200200
typedefstruct
201201
{
202-
HistoryItem*state;
202+
HistoryItem*items;
203203
TimestampTzts;
204204
}WaitCurrentContext;
205205

@@ -238,31 +238,31 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS)
238238

239239
if (!PG_ARGISNULL(0))
240240
{
241-
HistoryItemitem;
241+
HistoryItem*item;
242242
PGPROC*proc;
243243

244244
proc=search_proc(PG_GETARG_UINT32(0));
245-
item.pid=proc->pid;
246-
item.wait_event_info=proc->wait_event_info;
247-
params->state= (HistoryItem*)palloc0(sizeof(HistoryItem));
245+
params->items= (HistoryItem*)palloc0(sizeof(HistoryItem));
246+
item=&params->items[0];
247+
item->pid=proc->pid;
248+
item->wait_event_info=proc->wait_event_info;
248249
funcctx->max_calls=1;
249-
*params->state=item;
250250
}
251251
else
252252
{
253253
intprocCount=ProcGlobal->allProcCount,
254254
i,
255255
j=0;
256256

257-
params->state= (HistoryItem*)palloc0(sizeof(HistoryItem)*procCount);
257+
params->items= (HistoryItem*)palloc0(sizeof(HistoryItem)*procCount);
258258
for (i=0;i<procCount;i++)
259259
{
260260
PGPROC*proc=&ProcGlobal->allProcs[i];
261261

262262
if (proc!=NULL&&proc->pid!=0)
263263
{
264-
params->state[j].pid=proc->pid;
265-
params->state[j].wait_event_info=proc->wait_event_info;
264+
params->items[j].pid=proc->pid;
265+
params->items[j].wait_event_info=proc->wait_event_info;
266266
j++;
267267
}
268268
}
@@ -288,7 +288,7 @@ pg_wait_sampling_get_current(PG_FUNCTION_ARGS)
288288
*event;
289289
HistoryItem*item;
290290

291-
item=&params->state[funcctx->call_cntr];
291+
item=&params->items[funcctx->call_cntr];
292292

293293
MemSet(values,0,sizeof(values));
294294
MemSet(nulls,0,sizeof(nulls));

‎pg_wait_sampling.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
typedefstruct
2424
{
25-
intpid;
25+
uint32pid;
2626
uint32wait_event_info;
2727
uint64count;
2828
}ProfileItem;
2929

3030
typedefstruct
3131
{
32-
intpid;
32+
uint32pid;
3333
uint32wait_event_info;
3434
TimestampTzts;
3535
}HistoryItem;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp