Skip to content

Commit a8110d3

Browse files
author
Alexander Korotkov
committed
Some refactoring.
1 parent 46de4ea commit a8110d3

File tree

4 files changed

+165
-211
lines changed

4 files changed

+165
-211
lines changed

collector.c

+23-33
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@
99
*/
1010
#include "postgres.h"
1111

12-
#include "access/htup_details.h"
1312
#include "catalog/pg_type.h"
1413
#include "funcapi.h"
1514
#include "miscadmin.h"
1615
#include "postmaster/bgworker.h"
1716
#include "storage/ipc.h"
1817
#include "storage/procarray.h"
1918
#include "storage/procsignal.h"
20-
#include "storage/s_lock.h"
2119
#include "storage/shm_mq.h"
2220
#include "storage/shm_toc.h"
2321
#include "storage/spin.h"
2422
#include "utils/guc.h"
2523
#include "utils/memutils.h"
2624
#include "utils/resowner.h"
27-
#include "portability/instr_time.h"
2825

2926
#include "pg_wait_sampling.h"
3027

@@ -37,13 +34,12 @@ static void collector_main(Datum main_arg);
3734
* Register background worker for collecting waits history.
3835
*/
3936
void
40-
RegisterWaitsCollector(void)
37+
register_wait_collector(void)
4138
{
4239
BackgroundWorker worker;
4340

4441
/* set up common data for all our workers */
45-
worker.bgw_flags = BGWORKER_SHMEM_ACCESS |
46-
BGWORKER_BACKEND_DATABASE_CONNECTION;
42+
worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
4743
worker.bgw_start_time = BgWorkerStart_ConsistentState;
4844
worker.bgw_restart_time = BGW_NEVER_RESTART;
4945
worker.bgw_main = collector_main;
@@ -57,7 +53,7 @@ RegisterWaitsCollector(void)
5753
* Allocate memory for waits history.
5854
*/
5955
void
60-
AllocHistory(History *observations, int count)
56+
alloc_history(History *observations, int count)
6157
{
6258
observations->items = (HistoryItem *) palloc0(sizeof(HistoryItem) * count);
6359
observations->index = 0;
@@ -69,7 +65,7 @@ AllocHistory(History *observations, int count)
6965
* Reallocate memory for changed number of history items.
7066
*/
7167
static void
72-
ReallocHistory(History *observations, int count)
68+
realloc_history(History *observations, int count)
7369
{
7470
HistoryItem *newitems;
7571
int copyCount;
@@ -85,14 +81,17 @@ ReallocHistory(History *observations, int count)
8581
copyCount = Min(copyCount, count);
8682

8783
i = 0;
88-
j = observations->index;
84+
if (observations->wraparound)
85+
j = observations->index + 1;
86+
else
87+
j = 0;
8988
while (i < copyCount)
9089
{
91-
j--;
92-
if (j < 0)
93-
j = observations->count - 1;
90+
if (j >= observations->count)
91+
j = 0;
9492
memcpy(&newitems[i], &observations->items[j], sizeof(HistoryItem));
9593
i++;
94+
j++;
9695
}
9796

9897
pfree(observations->items);
@@ -103,17 +102,6 @@ ReallocHistory(History *observations, int count)
103102
observations->wraparound = false;
104103
}
105104

106-
/*
107-
* Read current wait information for given proc.
108-
*/
109-
void
110-
read_current_wait(PGPROC *proc, HistoryItem *item)
111-
{
112-
item->pid = proc->pid;
113-
item->wait_event_info = proc->wait_event_info;
114-
item->ts = GetCurrentTimestamp();
115-
}
116-
117105
static void
118106
handle_sigterm(SIGNAL_ARGS)
119107
{
@@ -149,13 +137,14 @@ static void
149137
probe_waits(History *observations, HTAB *profile_hash,
150138
bool write_history, bool write_profile)
151139
{
152-
int i,
153-
newSize;
140+
int i,
141+
newSize;
142+
TimestampTz ts = GetCurrentTimestamp();
154143

155144
/* Realloc waits history if needed */
156145
newSize = collector_hdr->historySize;
157146
if (observations->count != newSize)
158-
ReallocHistory(observations, newSize);
147+
realloc_history(observations, newSize);
159148

160149
LWLockAcquire(ProcArrayLock, LW_SHARED);
161150
for (i = 0; i < ProcGlobal->allProcCount; i++)
@@ -167,7 +156,9 @@ probe_waits(History *observations, HTAB *profile_hash,
167156
if (proc->pid == 0)
168157
continue;
169158

170-
read_current_wait(proc, &item);
159+
item.pid = proc->pid;
160+
item.wait_event_info = proc->wait_event_info;
161+
item.ts = ts;
171162

172163
if (write_history)
173164
{
@@ -196,7 +187,7 @@ probe_waits(History *observations, HTAB *profile_hash,
196187
static void
197188
send_history(History *observations, shm_mq_handle *mqh)
198189
{
199-
int count,
190+
Size count,
200191
i;
201192

202193
if (observations->wraparound)
@@ -212,12 +203,11 @@ send_history(History *observations, shm_mq_handle *mqh)
212203
static void
213204
send_profile(HTAB *profile_hash, shm_mq_handle *mqh)
214205
{
215-
HASH_SEQ_STATUS scan_status;
216-
ProfileItem *item;
217-
long count = hash_get_num_entries(profile_hash);
206+
HASH_SEQ_STATUS scan_status;
207+
ProfileItem *item;
208+
Size count = hash_get_num_entries(profile_hash);
218209

219210
shm_mq_send(mqh, sizeof(count), &count, false);
220-
221211
hash_seq_init(&scan_status, profile_hash);
222212
while ((item = (ProfileItem *) hash_seq_search(&scan_status)) != NULL)
223213
{
@@ -288,7 +278,7 @@ collector_main(Datum main_arg)
288278
ALLOCSET_DEFAULT_INITSIZE,
289279
ALLOCSET_DEFAULT_MAXSIZE);
290280
old_context = MemoryContextSwitchTo(collector_context);
291-
AllocHistory(&observations, collector_hdr->historySize);
281+
alloc_history(&observations, collector_hdr->historySize);
292282
MemoryContextSwitchTo(old_context);
293283

294284
profile_ts = history_ts = GetCurrentTimestamp();

pg_wait_sampling--1.0.sql

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
CREATE FUNCTION pg_wait_sampling_get_current (
77
pid int4,
88
OUT pid int4,
9-
OUT ts timestamptz,
109
OUT event_type text,
1110
OUT event text
1211
)

0 commit comments

Comments
 (0)