40
40
PGWS_DIMENSIONS_APPNAME))
41
41
static volatile sig_atomic_t shutdown_requested = false;
42
42
43
- int saved_profile_dimensions ; //TODO should be initialized with the same value as GUC?
43
+ int saved_profile_dimensions ;
44
44
int saved_history_dimensions ;
45
45
46
46
static void handle_sigterm (SIGNAL_ARGS );
@@ -72,12 +72,15 @@ pgws_register_wait_collector(void)
72
72
static void
73
73
alloc_history (History * observations , int count )
74
74
{
75
- observations -> items = (HistoryItem * ) palloc0 (sizeof (HistoryItem ) * count );
75
+ int serialized_size ;
76
+
77
+ saved_history_dimensions = pgws_history_dimensions ;
78
+ serialized_size = get_serialized_size (saved_history_dimensions , true);
79
+
80
+ observations -> serialized_items = (char * ) palloc0 (serialized_size * count );
76
81
observations -> index = 0 ;
77
82
observations -> count = count ;
78
83
observations -> wraparound = false;
79
-
80
- saved_history_dimensions = pgws_history_dimensions ;
81
84
}
82
85
83
86
/*
@@ -86,13 +89,17 @@ alloc_history(History *observations, int count)
86
89
static void
87
90
realloc_history (History * observations , int count )
88
91
{
89
- HistoryItem * newitems ;
92
+ char * newitems ;
90
93
int copyCount ,
91
94
i ,
92
95
j ;
96
+ int serialized_size ;
97
+
98
+ //saved_history_dimensions = pgws_history_dimensions; // TODO вроде как
99
+ serialized_size = get_serialized_size (saved_history_dimensions , true);
93
100
94
101
/* Allocate new array for history */
95
- newitems = (HistoryItem * ) palloc0 (sizeof ( HistoryItem ) * count );
102
+ newitems = (char * ) palloc0 (serialized_size * count );
96
103
97
104
/* Copy entries from old array to the new */
98
105
if (observations -> wraparound )
@@ -111,19 +118,19 @@ realloc_history(History *observations, int count)
111
118
{
112
119
if (j >= observations -> count )
113
120
j = 0 ;
114
- memcpy (& newitems [i ], & observations -> items [j ], sizeof (HistoryItem ));
121
+ memcpy ((newitems + i * serialized_size ),
122
+ (observations -> serialized_items + j * serialized_size ),
123
+ serialized_size );
115
124
i ++ ;
116
125
j ++ ;
117
126
}
118
127
119
128
/* Switch to new history array */
120
- pfree (observations -> items );
121
- observations -> items = newitems ;
129
+ pfree (observations -> serialized_items );
130
+ observations -> serialized_items = newitems ;
122
131
observations -> index = copyCount ;
123
132
observations -> count = count ;
124
133
observations -> wraparound = false;
125
-
126
- saved_history_dimensions = pgws_history_dimensions ;
127
134
}
128
135
129
136
static void
@@ -140,18 +147,19 @@ handle_sigterm(SIGNAL_ARGS)
140
147
/*
141
148
* Get next item of history with rotation.
142
149
*/
143
- static HistoryItem *
150
+ static char *
144
151
get_next_observation (History * observations )
145
152
{
146
- HistoryItem * result ;
153
+ char * result ;
154
+ int serialized_size = get_serialized_size (saved_history_dimensions , true);
147
155
148
156
/* Check for wraparound */
149
157
if (observations -> index >= observations -> count )
150
158
{
151
159
observations -> index = 0 ;
152
160
observations -> wraparound = true;
153
161
}
154
- result = & observations -> items [observations -> index ];
162
+ result = & observations -> serialized_items [observations -> index * serialized_size ];
155
163
observations -> index ++ ;
156
164
return result ;
157
165
}
@@ -413,8 +421,8 @@ serialize_item(SamplingDimensions dimensions, int dimensions_mask,
413
421
}
414
422
415
423
/* copy all the fields without ts/count */
416
- * serialized_key = palloc0 (* serialized_size + 1 );
417
- strcpy (* serialized_key , dummy_array );
424
+ * serialized_key = palloc0 (* serialized_size );
425
+ memcpy (* serialized_key , dummy_array , * serialized_size );
418
426
419
427
if (is_history )
420
428
{
@@ -430,8 +438,8 @@ serialize_item(SamplingDimensions dimensions, int dimensions_mask,
430
438
}
431
439
432
440
/* copy everything */
433
- * serialized_item = palloc0 (* serialized_size + 1 );
434
- strcpy (* serialized_item , dummy_array );
441
+ * serialized_item = palloc0 (* serialized_size );
442
+ memcpy (* serialized_item , dummy_array , * serialized_size );
435
443
}
436
444
437
445
void
@@ -570,17 +578,17 @@ probe_waits(History *observations, HTAB *profile_hash,
570
578
LWLockAcquire (ProcArrayLock , LW_SHARED );
571
579
for (i = 0 ; i < ProcGlobal -> allProcCount ; i ++ )
572
580
{
573
- HistoryItem item_history ,
574
- * observation ;
575
- ProfileItem item_profile ;
581
+ // HistoryItem item_history,
582
+ // *observation;
583
+ // ProfileItem item_profile;
576
584
PGPROC * proc = & ProcGlobal -> allProcs [i ];
577
585
int pid ;
578
586
uint32 wait_event_info ;
579
587
SamplingDimensions common_dimensions ,
580
588
history_dimensions ,
581
589
profile_dimensions ;
582
- int dimensions_mask_common = pgws_history_dimensions |
583
- pgws_profile_dimensions ;
590
+ int dimensions_mask_common = saved_history_dimensions |
591
+ saved_profile_dimensions ;
584
592
585
593
/* Check if we need to sample this process */
586
594
if (!pgws_should_sample_proc (proc , & pid , & wait_event_info ))
@@ -598,21 +606,27 @@ probe_waits(History *observations, HTAB *profile_hash,
598
606
599
607
copy_dimensions (& history_dimensions ,
600
608
& common_dimensions ,
601
- pgws_history_dimensions );
609
+ saved_history_dimensions );
602
610
copy_dimensions (& profile_dimensions ,
603
611
& common_dimensions ,
604
- pgws_profile_dimensions );
612
+ saved_profile_dimensions );
605
613
606
- item_history .ts = ts ;
607
- item_history .dimensions = history_dimensions ;
614
+ // item_history.ts = ts;
615
+ // item_history.dimensions = history_dimensions;
608
616
609
617
/* Write to the history if needed */
610
618
if (write_history )
611
619
{
612
- //TODO вот тут что-то сделать нужно??? потому что мы не запаковываем
613
- //историю
620
+ char * serialized_key ,
621
+ * serialized_item ,
622
+ * observation ;
623
+ int serialized_size = 0 ;
624
+
614
625
observation = get_next_observation (observations );
615
- * observation = item_history ;
626
+ serialize_item (history_dimensions , saved_history_dimensions ,
627
+ & serialized_item , & serialized_key , & serialized_size ,
628
+ ts , (uint64 ) 0 , true);
629
+ memcpy (observation , serialized_item , serialized_size );
616
630
}
617
631
618
632
/* Write to the profile if needed */
@@ -626,9 +640,9 @@ probe_waits(History *observations, HTAB *profile_hash,
626
640
* stored_item ;
627
641
628
642
if (!profile_pid )
629
- item_profile . dimensions .pid = 0 ;
643
+ profile_dimensions .pid = 0 ;
630
644
631
- serialize_item (item_profile . dimensions , saved_profile_dimensions ,
645
+ serialize_item (profile_dimensions , saved_profile_dimensions ,
632
646
& serialized_item , & serialized_key , & serialized_size ,
633
647
(TimestampTz ) 0 , count , false);
634
648
@@ -659,8 +673,9 @@ probe_waits(History *observations, HTAB *profile_hash,
659
673
* Send waits history to shared memory queue.
660
674
*/
661
675
static void
662
- send_history (History * observations , shm_mq_handle * mqh )
676
+ send_history (History * observations , shm_mq_handle * mqh ) //TODO TODO TODO
663
677
{
678
+ int serialized_size = get_serialized_size (saved_history_dimensions , true);
664
679
Size count ,
665
680
i ;
666
681
shm_mq_result mq_result ;
@@ -679,11 +694,20 @@ send_history(History *observations, shm_mq_handle *mqh)
679
694
"receiver of message queue has been detached" )));
680
695
return ;
681
696
}
697
+ /* Send saved_dimensions next */
698
+ mq_result = shm_mq_send_compat (mqh , sizeof (saved_history_dimensions ), & saved_history_dimensions , false, true);
699
+ if (mq_result == SHM_MQ_DETACHED )
700
+ {
701
+ ereport (WARNING ,
702
+ (errmsg ("pg_wait_sampling collector: "
703
+ "receiver of message queue has been detached" )));
704
+ return ;
705
+ }
682
706
for (i = 0 ; i < count ; i ++ )
683
707
{
684
708
mq_result = shm_mq_send_compat (mqh ,
685
- sizeof ( HistoryItem ) ,
686
- & observations -> items [ i ] ,
709
+ serialized_size ,
710
+ ( observations -> serialized_items + i * serialized_size ) ,
687
711
false,
688
712
true);
689
713
if (mq_result == SHM_MQ_DETACHED )
@@ -703,7 +727,8 @@ static void
703
727
send_profile (HTAB * profile_hash , shm_mq_handle * mqh )
704
728
{
705
729
HASH_SEQ_STATUS scan_status ;
706
- ProfileItem * item ;
730
+ char * serialized_item ;
731
+ int serialized_size = get_serialized_size (saved_profile_dimensions , true);
707
732
Size count = hash_get_num_entries (profile_hash );
708
733
shm_mq_result mq_result ;
709
734
@@ -716,10 +741,19 @@ send_profile(HTAB *profile_hash, shm_mq_handle *mqh)
716
741
"receiver of message queue has been detached" )));
717
742
return ;
718
743
}
744
+ /* Send saved_dimensions next */
745
+ mq_result = shm_mq_send_compat (mqh , sizeof (saved_profile_dimensions ), & saved_profile_dimensions , false, true);
746
+ if (mq_result == SHM_MQ_DETACHED )
747
+ {
748
+ ereport (WARNING ,
749
+ (errmsg ("pg_wait_sampling collector: "
750
+ "receiver of message queue has been detached" )));
751
+ return ;
752
+ }
719
753
hash_seq_init (& scan_status , profile_hash );
720
- while ((item = (ProfileItem * ) hash_seq_search (& scan_status )) != NULL )
754
+ while ((serialized_item = (char * ) hash_seq_search (& scan_status )) != NULL )
721
755
{
722
- mq_result = shm_mq_send_compat (mqh , sizeof ( ProfileItem ), item , false,
756
+ mq_result = shm_mq_send_compat (mqh , serialized_size , serialized_item , false,
723
757
true);
724
758
if (mq_result == SHM_MQ_DETACHED )
725
759
{
0 commit comments