Skip to content

Commit 5bc04b5

Browse files
committed
More fixes after review
- Remove unneded palloc - Move initialization of possibly-null fields behind mask-checking (in fill_values_and_nulls function) - Switch api_version and parameter amount in *_internal functions, this makes safety check look better - Remove pgstat_clear_*** from pg_wait_sampling_get_current, it could mess with other functions unexpectedly - Use correct bitmask in get_profile/history_internal - Add dimensions_mask to Profile and History to save it through multiple calls to get_profile/history - Use statically allocated varialbes ts and count in deserialize_array - Take care of padding in deserialize_array - Leave only common_dimensions in probe_waits - Remove unneded +1 in serialize_item - Remove serialized_key in probe_waits, it's not needed - Add pfree to stop leaking serialized_item - Always copy only count to hash table in probe_waits
1 parent 885496f commit 5bc04b5

File tree

3 files changed

+90
-148
lines changed

3 files changed

+90
-148
lines changed

collector.c

Lines changed: 20 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -235,51 +235,6 @@ fill_dimensions(SamplingDimensions *dimensions, PGPROC *proc,
235235
}
236236
}
237237

238-
static void
239-
copy_dimensions (SamplingDimensions *dst, SamplingDimensions *src,
240-
int dst_dimensions_mask)
241-
{
242-
if (dst_dimensions_mask & PGWS_DIMENSIONS_PID)
243-
dst->pid = src->pid;
244-
245-
if (dst_dimensions_mask & PGWS_DIMENSIONS_WAIT_EVENT_TYPE ||
246-
dst_dimensions_mask & PGWS_DIMENSIONS_WAIT_EVENT)
247-
dst->wait_event_info = src->wait_event_info;
248-
249-
if (dst_dimensions_mask & PGWD_DIMENSIONS_QUERY_ID)
250-
dst->queryId = src->queryId;
251-
252-
if (dst_dimensions_mask & PGWS_DIMENSIONS_ROLE_ID)
253-
dst->role_id = src->role_id;
254-
255-
if (dst_dimensions_mask & PGWS_DIMENSIONS_DB_ID)
256-
dst->database_id = src->database_id;
257-
258-
if (dst_dimensions_mask & PGWS_DIMENSIONS_PARALLEL_LEADER_PID)
259-
dst->parallel_leader_pid = src->parallel_leader_pid;
260-
261-
if (dst_dimensions_mask & PGWS_DIMENSIONS_IS_REGULAR_BE)
262-
dst->is_regular_backend = src->is_regular_backend;
263-
264-
if (dst_dimensions_mask & PGWS_DIMENSIONS_BE_TYPE)
265-
dst->backend_type = src->backend_type;
266-
267-
if (dst_dimensions_mask & PGWS_DIMENSIONS_BE_STATE)
268-
dst->backend_state = src->backend_state;
269-
270-
if (dst_dimensions_mask & PGWS_DIMENSIONS_BE_START_TIME)
271-
dst->backend_start = src->backend_start;
272-
273-
if (dst_dimensions_mask & PGWS_DIMENSIONS_CLIENT_ADDR)
274-
dst->client_addr = src->client_addr;
275-
276-
if (dst_dimensions_mask & PGWS_DIMENSIONS_CLIENT_HOSTNAME)
277-
strcpy(dst->client_hostname, src->client_hostname);
278-
279-
if (dst_dimensions_mask & PGWS_DIMENSIONS_APPNAME)
280-
strcpy(dst->appname, src->appname);
281-
}
282-
283238
int
284239
get_serialized_size(int dimensions_mask, bool need_last_field)
285240
{
@@ -321,10 +276,10 @@ get_serialized_size(int dimensions_mask, bool need_last_field)
321276

322277
static void
323278
serialize_item(SamplingDimensions dimensions, int dimensions_mask,
324-
char **serialized_item, char **serialized_key, int *serialized_size,
279+
char **serialized_item, int *serialized_size,
325280
TimestampTz ts, uint64 count, bool is_history)
326281
{
327-
char dummy_array[sizeof(SamplingDimensions) + sizeof(uint64) + 1];
282+
char dummy_array[sizeof(SamplingDimensions) + sizeof(uint64)];
328283

329284
memset(dummy_array, 0, sizeof(dummy_array));
330285

@@ -420,10 +375,6 @@ serialize_item(SamplingDimensions dimensions, int dimensions_mask,
420375
*serialized_size += sizeof(dimensions.appname);
421376
}
422377

423-
/* copy all the fields without ts/count */
424-
*serialized_key = palloc0(*serialized_size);
425-
memcpy(*serialized_key, dummy_array, *serialized_size);
426-
427378
if (is_history)
428379
{
429380
memcpy(dummy_array + *serialized_size, &ts,
@@ -444,7 +395,7 @@ serialize_item(SamplingDimensions dimensions, int dimensions_mask,
444395

445396
void
446397
deserialize_item(SamplingDimensions *dimensions, char *serialized_item,
447-
int dimensions_mask, TimestampTz *ts, uint64 *count)
398+
int dimensions_mask, TimestampTz *ts, uint64 *count, bool is_history)
448399
{
449400
int idx = 0;
450401

@@ -542,14 +493,13 @@ deserialize_item(SamplingDimensions *dimensions, char *serialized_item,
542493
idx += sizeof(dimensions->appname);
543494
}
544495

545-
if (ts)
496+
if (is_history)
546497
{
547498
memcpy(ts, serialized_item + idx,
548499
sizeof(TimestampTz));
549500
idx += sizeof(TimestampTz);
550501
}
551-
552-
if (count)
502+
else
553503
{
554504
memcpy(count, serialized_item + idx,
555505
sizeof(uint64));
@@ -581,9 +531,7 @@ probe_waits(History *observations, HTAB *profile_hash,
581531
PGPROC *proc = &ProcGlobal->allProcs[i];
582532
int pid;
583533
uint32 wait_event_info;
584-
SamplingDimensions common_dimensions,
585-
history_dimensions,
586-
profile_dimensions;
534+
SamplingDimensions common_dimensions;
587535
int dimensions_mask_common = saved_history_dimensions |
588536
saved_profile_dimensions;
589537

@@ -594,33 +542,25 @@ probe_waits(History *observations, HTAB *profile_hash,
594542
/*
595543
* We zero dimensions with memset to avoid doing it field-by-field
596544
*/
597-
memset(&history_dimensions, 0, sizeof(SamplingDimensions));
598-
memset(&profile_dimensions, 0, sizeof(SamplingDimensions));
599545
memset(&common_dimensions, 0, sizeof(SamplingDimensions));
600546

601547
fill_dimensions(&common_dimensions, proc, pid, wait_event_info,
602548
pgws_proc_queryids[i], dimensions_mask_common);
603549

604-
copy_dimensions(&history_dimensions,
605-
&common_dimensions,
606-
saved_history_dimensions);
607-
copy_dimensions(&profile_dimensions,
608-
&common_dimensions,
609-
saved_profile_dimensions);
610-
611550
/* Write to the history if needed */
612551
if (write_history)
613552
{
614-
char *serialized_key,
615-
*serialized_item,
553+
char *serialized_item,
616554
*observation;
617555
int serialized_size = 0;
618556

619557
observation = get_next_observation(observations);
620-
serialize_item(history_dimensions, saved_history_dimensions,
621-
&serialized_item, &serialized_key, &serialized_size,
558+
serialize_item(common_dimensions, saved_history_dimensions,
559+
&serialized_item, &serialized_size,
622560
ts, (uint64) 0, true);
623561
memcpy(observation, serialized_item, serialized_size);
562+
563+
pfree(serialized_item);
624564
}
625565

626566
/* Write to the profile if needed */
@@ -629,30 +569,29 @@ probe_waits(History *observations, HTAB *profile_hash,
629569
bool found;
630570
int serialized_size = 0;
631571
uint64 count = 1;
632-
char *serialized_key,
633-
*serialized_item,
572+
char *serialized_item,
634573
*stored_item;
635574

636575
if (!profile_pid)
637-
profile_dimensions.pid = 0;
576+
common_dimensions.pid = 0;
638577

639-
serialize_item(profile_dimensions, saved_profile_dimensions,
640-
&serialized_item, &serialized_key, &serialized_size,
578+
serialize_item(common_dimensions, saved_profile_dimensions,
579+
&serialized_item, &serialized_size,
641580
(TimestampTz) 0, count, false);
642581

643-
stored_item = (char *) hash_search(profile_hash, serialized_key,
582+
stored_item = (char *) hash_search(profile_hash, serialized_item,
644583
HASH_ENTER, &found);
645584

646585
if (found)
647586
{
648587
memcpy(&count, (stored_item + serialized_size - sizeof(uint64)),
649588
sizeof(uint64));
650589
count++;
651-
memcpy((stored_item + serialized_size - sizeof(uint64)), &count,
652-
sizeof(uint64));
653590
}
654-
else
655-
memcpy(stored_item, serialized_item, serialized_size);
591+
/* Copy new or incremented count to hash table */
592+
memcpy((stored_item + serialized_size - sizeof(uint64)), &count, sizeof(uint64));
593+
594+
pfree(serialized_item);
656595
}
657596
}
658597
LWLockRelease(ProcArrayLock);

0 commit comments

Comments
 (0)