9
9
*/
10
10
#include "postgres.h"
11
11
12
- #include "access/htup_details.h"
13
12
#include "catalog/pg_type.h"
14
13
#include "funcapi.h"
15
14
#include "miscadmin.h"
16
15
#include "postmaster/bgworker.h"
17
16
#include "storage/ipc.h"
18
17
#include "storage/procarray.h"
19
18
#include "storage/procsignal.h"
20
- #include "storage/s_lock.h"
21
19
#include "storage/shm_mq.h"
22
20
#include "storage/shm_toc.h"
23
21
#include "storage/spin.h"
24
22
#include "utils/guc.h"
25
23
#include "utils/memutils.h"
26
24
#include "utils/resowner.h"
27
- #include "portability/instr_time.h"
28
25
29
26
#include "pg_wait_sampling.h"
30
27
@@ -37,13 +34,12 @@ static void collector_main(Datum main_arg);
37
34
* Register background worker for collecting waits history.
38
35
*/
39
36
void
40
- RegisterWaitsCollector (void )
37
+ register_wait_collector (void )
41
38
{
42
39
BackgroundWorker worker ;
43
40
44
41
/* 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 ;
47
43
worker .bgw_start_time = BgWorkerStart_ConsistentState ;
48
44
worker .bgw_restart_time = BGW_NEVER_RESTART ;
49
45
worker .bgw_main = collector_main ;
@@ -57,7 +53,7 @@ RegisterWaitsCollector(void)
57
53
* Allocate memory for waits history.
58
54
*/
59
55
void
60
- AllocHistory (History * observations , int count )
56
+ alloc_history (History * observations , int count )
61
57
{
62
58
observations -> items = (HistoryItem * ) palloc0 (sizeof (HistoryItem ) * count );
63
59
observations -> index = 0 ;
@@ -69,7 +65,7 @@ AllocHistory(History *observations, int count)
69
65
* Reallocate memory for changed number of history items.
70
66
*/
71
67
static void
72
- ReallocHistory (History * observations , int count )
68
+ realloc_history (History * observations , int count )
73
69
{
74
70
HistoryItem * newitems ;
75
71
int copyCount ;
@@ -85,14 +81,17 @@ ReallocHistory(History *observations, int count)
85
81
copyCount = Min (copyCount , count );
86
82
87
83
i = 0 ;
88
- j = observations -> index ;
84
+ if (observations -> wraparound )
85
+ j = observations -> index + 1 ;
86
+ else
87
+ j = 0 ;
89
88
while (i < copyCount )
90
89
{
91
- j -- ;
92
- if (j < 0 )
93
- j = observations -> count - 1 ;
90
+ if (j >= observations -> count )
91
+ j = 0 ;
94
92
memcpy (& newitems [i ], & observations -> items [j ], sizeof (HistoryItem ));
95
93
i ++ ;
94
+ j ++ ;
96
95
}
97
96
98
97
pfree (observations -> items );
@@ -103,17 +102,6 @@ ReallocHistory(History *observations, int count)
103
102
observations -> wraparound = false;
104
103
}
105
104
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
-
117
105
static void
118
106
handle_sigterm (SIGNAL_ARGS )
119
107
{
@@ -149,13 +137,14 @@ static void
149
137
probe_waits (History * observations , HTAB * profile_hash ,
150
138
bool write_history , bool write_profile )
151
139
{
152
- int i ,
153
- newSize ;
140
+ int i ,
141
+ newSize ;
142
+ TimestampTz ts = GetCurrentTimestamp ();
154
143
155
144
/* Realloc waits history if needed */
156
145
newSize = collector_hdr -> historySize ;
157
146
if (observations -> count != newSize )
158
- ReallocHistory (observations , newSize );
147
+ realloc_history (observations , newSize );
159
148
160
149
LWLockAcquire (ProcArrayLock , LW_SHARED );
161
150
for (i = 0 ; i < ProcGlobal -> allProcCount ; i ++ )
@@ -167,7 +156,9 @@ probe_waits(History *observations, HTAB *profile_hash,
167
156
if (proc -> pid == 0 )
168
157
continue ;
169
158
170
- read_current_wait (proc , & item );
159
+ item .pid = proc -> pid ;
160
+ item .wait_event_info = proc -> wait_event_info ;
161
+ item .ts = ts ;
171
162
172
163
if (write_history )
173
164
{
@@ -196,7 +187,7 @@ probe_waits(History *observations, HTAB *profile_hash,
196
187
static void
197
188
send_history (History * observations , shm_mq_handle * mqh )
198
189
{
199
- int count ,
190
+ Size count ,
200
191
i ;
201
192
202
193
if (observations -> wraparound )
@@ -212,12 +203,11 @@ send_history(History *observations, shm_mq_handle *mqh)
212
203
static void
213
204
send_profile (HTAB * profile_hash , shm_mq_handle * mqh )
214
205
{
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 );
218
209
219
210
shm_mq_send (mqh , sizeof (count ), & count , false);
220
-
221
211
hash_seq_init (& scan_status , profile_hash );
222
212
while ((item = (ProfileItem * ) hash_seq_search (& scan_status )) != NULL )
223
213
{
@@ -288,7 +278,7 @@ collector_main(Datum main_arg)
288
278
ALLOCSET_DEFAULT_INITSIZE ,
289
279
ALLOCSET_DEFAULT_MAXSIZE );
290
280
old_context = MemoryContextSwitchTo (collector_context );
291
- AllocHistory (& observations , collector_hdr -> historySize );
281
+ alloc_history (& observations , collector_hdr -> historySize );
292
282
MemoryContextSwitchTo (old_context );
293
283
294
284
profile_ts = history_ts = GetCurrentTimestamp ();
0 commit comments