-
Notifications
You must be signed in to change notification settings - Fork 11
/
postgres_stats.c
1386 lines (1211 loc) · 39.6 KB
/
postgres_stats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <unistd.h>
#include <dirent.h>
#include "postgres.h"
#include "miscadmin.h"
#include "access/htup_details.h"
#include "access/heapam.h"
#include "access/xact.h"
#include "access/xlog.h"
#if PG_VERSION_NUM >= 150000
#include "access/xlogrecovery.h"
#endif
#include "catalog/pg_authid.h"
#include "catalog/pg_database.h"
#include "catalog/pg_type.h"
#include "lib/stringinfo.h"
#include "pgstat.h"
#include "replication/walreceiver.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/timestamp.h"
#if PG_VERSION_NUM < 90400
#include "utils/tqual.h"
#endif
#include "storage/bufmgr.h"
#include "storage/ipc.h"
#include "storage/predicate_internals.h"
#include "storage/procarray.h"
#include "storage/proc.h"
#include "system_stats.h"
#include "postgres_stats.h"
extern system_stat system_stats_old;
extern pid_t PostmasterPid;
extern int MaxBackends;
static size_t cmdline_prefix_len;
static char *cmdline_prefix;
static size_t postmaster_pid_len;
static char postmaster_pid[32];
static unsigned long long mem_page_size;
typedef struct {
proc_stat *values;
size_t size;
size_t pos;
} proc_stat_list;
static proc_stat_list proc_stats;
static pg_stat pg_stats_current;
static pg_stat pg_stats_new;
typedef struct {
pg_stat_activity *p; /* pointer to a corresponding pg_stat_activity entry */
uint32 pid;
#if PG_VERSION_NUM < 90600
uint32 field1; /* a 32-bit ID field */
uint32 field2; /* a 32-bit ID field */
uint32 field3; /* a 32-bit ID field */
uint16 field4; /* a 16-bit ID field */
uint8 type; /* see enum LockTagType */
bool granted;
#endif
} _lock;
#if PG_VERSION_NUM >= 90600
#define UINT32_ACCESS_ONCE(var) ((uint32)(*((volatile uint32 *)&(var))))
#define IS_GRANTED(l) false
#else
#define IS_GRANTED(l) l.granted
static int cmp_lock(const void *arg1, const void *arg2)
{
const _lock *l1 = (const _lock *) arg1;
const _lock *l2 = (const _lock *) arg2;
if (l1->type > l2->type) return 1;
if (l1->type < l2->type) return -1;
if (l1->field1 > l2->field1) return 1;
if (l1->field1 < l2->field1) return -1;
if (l1->field2 > l2->field2) return 1;
if (l1->field2 < l2->field2) return -1;
if (l1->field3 > l2->field3) return 1;
if (l1->field3 < l2->field3) return -1;
if (l1->field4 > l2->field4) return 1;
if (l1->field4 < l2->field4) return -1;
if (l1->granted > l2->granted) return 1;
if (l1->granted < l2->granted) return -1;
if (l1->pid > l2->pid) return 1;
if (l1->pid < l2->pid) return -1;
return 0;
}
#endif
static _lock *get_pg_locks(int *num_locks)
{
_lock *locks = NULL;
LockData *lockData = GetLockStatusData();
#if PG_VERSION_NUM < 90600
PredicateLockData *predLockData = GetPredicateLockStatusData();
*num_locks = 2 * lockData->nelements + predLockData->nelements;
#else
*num_locks = lockData->nelements;
#endif
if (*num_locks > 0)
{
int i, j = 0;
locks = palloc0(*num_locks * sizeof(_lock));
for (i = 0; i < lockData->nelements;)
{
_lock *l;
LockInstanceData *instance = &(lockData->locks[i]);
bool granted = false;
if (instance->holdMask)
{
instance->holdMask = 0;
granted = true;
}
if (!granted)
{
i++;
if (instance->waitLockMode == NoLock)
continue;
}
#if PG_VERSION_NUM >= 90600
else continue;
#endif
l = locks + j++;
l->pid = instance->pid;
#if PG_VERSION_NUM < 90600
l->granted = granted;
l->type = instance->locktag.locktag_type;
switch ((LockTagType) l->type)
{
default:
case LOCKTAG_OBJECT:
case LOCKTAG_USERLOCK:
case LOCKTAG_TUPLE:
l->field4 = instance->locktag.locktag_field4;
case LOCKTAG_PAGE:
l->field3 = instance->locktag.locktag_field3;
case LOCKTAG_RELATION:
case LOCKTAG_RELATION_EXTEND:
case LOCKTAG_VIRTUALTRANSACTION:
l->field2 = instance->locktag.locktag_field2;
case LOCKTAG_TRANSACTION:
l->field1 = instance->locktag.locktag_field1;
}
#endif
}
#if PG_VERSION_NUM < 90600
for (i = 0; i < predLockData->nelements; ++i)
{
PREDICATELOCKTARGETTAG *predTag = &(predLockData->locktags[i]);
PredicateLockTargetType lockType = GET_PREDICATELOCKTARGETTAG_TYPE(*predTag);
_lock *l = locks + j++;
l->granted = true;
l->pid = predLockData->xacts[i].pid;
l->type = lockType == PREDLOCKTAG_RELATION ? lockType : lockType + 1;
switch (lockType)
{
case PREDLOCKTAG_TUPLE:
l->field4 = GET_PREDICATELOCKTARGETTAG_OFFSET(*predTag);
case PREDLOCKTAG_PAGE:
l->field3 = GET_PREDICATELOCKTARGETTAG_PAGE(*predTag);
default:
l->field2 = GET_PREDICATELOCKTARGETTAG_RELATION(*predTag);
l->field1 = GET_PREDICATELOCKTARGETTAG_DB(*predTag);
}
}
if ((*num_locks = j) > 1)
qsort(locks, *num_locks, sizeof(_lock), cmp_lock);
#endif
}
return locks;
}
static pg_stat_activity *find_pg_proc_by_pid(pg_stat_activity_list pg_stats, uint32 pid)
{
pg_stat_activity *low = pg_stats.values;
pg_stat_activity *high = low + pg_stats.pos - 1;
while (low <= high)
{
pg_stat_activity *middle = low + (high - low) / 2;
if (pid < middle->pid)
high = middle - 1;
else if (pid > middle->pid)
low = middle + 1;
else
return middle;
}
return NULL;
}
static void update_blockers(pg_stat_activity *blocked, pg_stat_activity *blocker)
{
blocker->is_blocker = true;
if (blocked->blockers)
((uint32 *)blocked->blockers)[blocked->num_blockers] = blocker->pid;
else
blocked->blockers = blocker->pid;
blocked->num_blockers++;
}
static void fix_blockers_array(pg_stat_activity *blocked)
{
if (blocked->num_blockers < 2)
{
uint32 tmp = blocked->num_blockers == 1 ? *((uint32 *)blocked->blockers) : 0;
pfree((uint32 *)blocked->blockers);
blocked->blockers = tmp;
}
}
static void enrich_pg_stats(MemoryContext othercxt, pg_stat_activity_list pg_stats, _lock *locks, int num_locks)
{
int i, j, k, num_blockers;
for (i = 0; i < num_locks; i++)
{
_lock *l = &locks[i];
#if PG_VERSION_NUM < 90600
int granted = -1;
#endif
if (IS_GRANTED((*l)) || !(l->p = find_pg_proc_by_pid(pg_stats, l->pid)))
continue;
#if PG_VERSION_NUM < 90600
/* find block of processes waiting for the same resource */
for (j = i + 1, num_blockers = 0; j < num_locks; j++)
{
_lock *m = &locks[j];
if (l->type != m->type || l->field1 != m->field1 || l->field2 != m->field2
|| l->field3 != m->field3 || l->field4 != m->field4) break;
m->p = find_pg_proc_by_pid(pg_stats, m->pid);
if (m->p && IS_GRANTED((*m)))
{
if (granted == -1) granted = j;
num_blockers++;
}
}
if (granted == -1)
goto adjust_i;
for (k = i; k < granted; k++)
{
l = &locks[k];
if (l->p && !l->p->blockers) /* we assume that process can't wait for more than one lock */
{
int m;
if (num_blockers > 1)
l->p->blockers = (uintptr_t) palloc(num_blockers * sizeof(uint32));
for (m = granted; m < j; m++)
if (locks[m].p && l->pid != locks[m].pid)
update_blockers(l->p, locks[m].p);
if (num_blockers > 1)
fix_blockers_array(l->p);
}
}
adjust_i:
i = j - 1;
#else
{
Datum *blockers;
ArrayType *elems;
MemoryContext oldcxt = MemoryContextSwitchTo(othercxt);
elems = DatumGetArrayTypeP(DirectFunctionCall1(pg_blocking_pids, Int32GetDatum(l->pid)));
deconstruct_array(elems, INT4OID, sizeof(int32), true, 'i', &blockers, NULL, &num_blockers);
MemoryContextSwitchTo(oldcxt);
if (num_blockers > 1)
l->p->blockers = (uintptr_t) palloc(num_blockers * sizeof(uint32));
for (j = 0; j < num_blockers; ++j)
{
bool e = false;
int m;
pg_stat_activity *blocker;
if ((k = DatumGetInt32(blockers[j])) == 0) continue;
for (m = 0; m < l->p->num_blockers; m++)
if ((e = (((uint32 *)l->p->blockers)[m] == k)))
break;
if (!e && (blocker = find_pg_proc_by_pid(pg_stats, k)))
update_blockers(l->p, blocker);
}
if (num_blockers > 1)
fix_blockers_array(l->p);
}
#endif
}
}
static void pg_stat_activity_list_init(pg_stat_activity_list *list)
{
list->pos = 0;
list->size = MaxBackends + 17;
list->values = palloc(sizeof(pg_stat_activity) * list->size);
}
static void pg_stat_activity_list_free_resources(pg_stat_activity_list *list)
{
size_t i;
for (i = 0; i < list->pos; ++i) {
pg_stat_activity ps = list->values[i];
FREE(ps.query);
FREE(ps.usename);
FREE(ps.datname);
if (ps.num_blockers > 1)
pfree((uint32 *)ps.blockers);
if (ps.ps.free_cmdline)
FREE(ps.ps.cmdline);
}
list->pos = 0;
}
static void pg_stat_activity_list_add(pg_stat_activity_list *list, pg_stat_activity ps)
{
if (list->values == NULL)
list->pos = list->size = 0;
if (list->pos >= list->size) {
int new_size = list->size > 0 ? list->size * 2 : 7;
list->size = new_size;
list->values = repalloc(list->values, sizeof(pg_stat_activity)*new_size);
}
list->values[list->pos++] = ps;
}
static void db_stat_list_init(db_stat_list *list)
{
list->pos = 0;
list->size = 3;
list->values = palloc(sizeof(db_stat) * list->size);
}
static void db_stat_list_free_resources(db_stat_list *list)
{
size_t i;
for (i = 0; i < list->pos; ++i)
FREE(list->values[i].datname);
list->pos = 0;
}
static void db_stat_list_add(db_stat_list *list, db_stat ds)
{
if (list->values == NULL)
list->pos = list->size = 0;
if (list->pos >= list->size) {
int new_size = list->size > 0 ? list->size * 2 : 7;
list->size = new_size;
list->values = repalloc(list->values, sizeof(db_stat)*new_size);
}
list->values[list->pos++] = ds;
}
static size_t json_escaped_size(const char *s, size_t len)
{
size_t ret = 0;
while (len-- > 0) {
switch (*s) {
case 0x00:
return ret;
case '"':
case '\\':
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
ret += 1; /* from 1 byte to \x 2 bytes */
break;
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x05:
case 0x06:
case 0x07:
case 0x0b:
case 0x0e:
case 0x0f:
case 0x10:
case 0x11:
case 0x12:
case 0x13:
case 0x14:
case 0x15:
case 0x16:
case 0x17:
case 0x18:
case 0x19:
case 0x1a:
case 0x1b:
case 0x1c:
case 0x1d:
case 0x1e:
case 0x1f:
ret += 5; /* from 1 byte to \uxxxx 6 bytes */
break;
}
++s;
++ret;
}
return ret;
}
static char *json_escape_string_len(const char *s, size_t len)
{
char *ret = palloc(json_escaped_size(s, len) + 3);
char *r = ret;
*(r++) = '"';
while (len-- > 0) {
switch (*s) {
case 0x00:
*(r++) = '"';
*r = '\0';
return ret;
case '"':
*(r++) = '\\';
*r = '"';
break;
case '\\':
*(r++) = '\\';
*r = '\\';
break;
case '\b':
*(r++) = '\\';
*r = 'b';
break;
case '\f':
*(r++) = '\\';
*r = 'f';
break;
case '\n':
*(r++) = '\\';
*r = 'n';
break;
case '\r':
*(r++) = '\\';
*r = 'r';
break;
case '\t':
*(r++) = '\\';
*r = 't';
break;
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x05:
case 0x06:
case 0x07:
case 0x0b:
case 0x0e:
case 0x0f:
case 0x10:
case 0x11:
case 0x12:
case 0x13:
case 0x14:
case 0x15:
case 0x16:
case 0x17:
case 0x18:
case 0x19:
case 0x1a:
case 0x1b:
case 0x1c:
case 0x1d:
case 0x1e:
case 0x1f:
{
// convert a number 0..15 to its hex representation (0..f)
static const char hexify[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
// print character c as \uxxxx
*(r++) = '\\';
*(r++) = 'u';
*(r++) = '0';
*(r++) = '0';
*(r++) = hexify[*s >> 4];
*r = hexify[*s & 0x0f];
break;
}
default:
*r = *s;
break;
}
++r;
++s;
}
*(r++) = '"';
*r = '\0';
return ret;
}
static char *json_escape_string(const char *s)
{
return json_escape_string_len(s, -1);
}
static unsigned long long get_memory_usage(const char *proc_file)
{
ssize_t n;
char *p, *endptr, buf[255];
unsigned long resident = 0, share = 0;
int fd = open(proc_file, O_RDONLY);
if (fd < 0) return 0;
if ((n = read(fd, buf, sizeof(buf) - 1)) > 0) {
buf[n] = '\0';
for (p = buf; *p != ' ' && *p != '\0'; ++p);
if (*p == ' ') {
resident = strtoul(p + 1, &endptr, 10);
if (endptr > p + 1 && *endptr == ' ')
share = strtoul(endptr + 1, NULL, 10);
}
}
close(fd);
return (unsigned long long)(resident - share) * mem_page_size;
}
#define TAB_ENTRY(STRING, TARGET) {STRING, sizeof(STRING) - 1, TARGET}
#define IO_TAB(NAME) TAB_ENTRY(#NAME ": ", &pi.NAME)
static proc_io read_proc_io(const char *proc_file)
{
int i = 0, j = 0;
proc_io pi = {0,};
char *dpos, *endptr, buf[255];
struct _io_tab {
const char *name;
size_t name_len;
unsigned long long *value;
} io_tab[] = {
IO_TAB(read_bytes),
IO_TAB(write_bytes),
IO_TAB(cancelled_write_bytes),
{NULL, 0, NULL}
};
FILE *iofd = fopen(proc_file, "r");
if (iofd == NULL)
return pi;
while (fgets(buf, sizeof(buf), iofd) && i < lengthof(io_tab) - 1) {
for (j = 0; io_tab[j].name != NULL; ++j)
if (strncmp(io_tab[j].name, buf, io_tab[j].name_len) == 0) {
++i;
dpos = buf + io_tab[j].name_len;
*io_tab[j].value = strtoull(dpos, &endptr, 10);
if (endptr > dpos)
pi.available = true;
break;
}
}
fclose(iofd);
return pi;
}
static proc_stat read_proc_stat(const char *proc_file)
{
ssize_t n;
char *t, buf[512];
proc_stat ps = {0,};
int statfd = open(proc_file, O_RDONLY);
if (statfd < 0)
return ps;
if ((n = read(statfd, buf, sizeof(buf) - 1)) > 0) {
buf[n] = '\0';
for (t = buf; *t != ')' && *t != '\0'; ++t);
if (*t == ')' && strncmp(t + 3, postmaster_pid, postmaster_pid_len) == 0) {
ps.fields = sscanf(t + 2, "%c %d %*d %*d %*d %*d %*u %*u %*u \
%*u %*u %lu %lu %*d %*d %ld %*d %*d %*d %llu %lu %ld %*u %*u %*u %*u %*u %*u \
%*u %*u %*u %*u %*u %*u %*u %*d %*d %*u %*u %llu %lu", &ps.state,
&ps.ppid, &ps.utime, &ps.stime, &ps.priority, &ps.start_time, &ps.vsize,
&ps.rss, &ps.delayacct_blkio_ticks, &ps.gtime);
if (ps.fields < 8) {
elog(DEBUG1, "Can't parse content of %s", proc_file);
ps.fields = ps.ppid = 0;
}
}
}
close(statfd);
return ps;
}
static bool proc_stats_add(proc_stat_list *list, proc_stat ps)
{
if (list->values == NULL)
list->pos = list->size = 0;
if (list->pos >= list->size) {
int new_size = list->size > 0 ? list->size * 2 : 7;
list->size = new_size;
list->values = repalloc(list->values, sizeof(proc_stat)*new_size);
}
list->values[list->pos++] = ps;
return true;
}
static int proc_stat_cmp(const void *el1, const void *el2)
{
return ((const proc_stat *) el1)->pid - ((const proc_stat *) el2)->pid;
}
static int pg_stat_activity_cmp(const void *el1, const void *el2)
{
return ((const pg_stat_activity *) el1)->pid - ((const pg_stat_activity *) el2)->pid;
}
static int db_stat_cmp(const void *el1, const void *el2)
{
return ((const db_stat *) el1)->databaseid - ((const db_stat *) el2)->databaseid;
}
static void read_procfs(proc_stat_list *list)
{
DIR *proc;
struct dirent *dp = NULL;
pid_t pid;
char *endptr;
char proc_file[32] = "/proc";
proc_stat ps;
list->pos = 0;
if ((proc = opendir(proc_file)) == NULL) {
elog(WARNING, "couldn't open '/proc'");
return;
}
while (errno = 0, NULL != (dp = readdir(proc))) {
if (dp->d_name[0] >= '1' && dp->d_name[0] <= '9'
&& (pid = strtoul(dp->d_name, &endptr, 10)) > 0
&& endptr > dp->d_name && *endptr == '\0'
&& pid != MyProcPid) { // skip themself
size_t pid_len = endptr - dp->d_name;
proc_file[5] = '/';
memcpy(proc_file + 6, dp->d_name, pid_len);
proc_file[pid_len + 6] = '/';
strcpy(proc_file + pid_len + 7, "stat");
ps = read_proc_stat(proc_file);
if (ps.fields && ps.ppid == PostmasterPid) {
ps.pid = pid;
proc_file[pid_len + 11] = 'm'; // statm
proc_file[pid_len + 12] = '\0';
ps.uss = get_memory_usage(proc_file);
strcpy(proc_file + pid_len + 7, "io");
ps.io = read_proc_io(proc_file);
proc_stats_add(list, ps);
}
}
}
if (dp == NULL && errno != 0)
elog(WARNING, "error reading /proc");
closedir(proc);
if (list->pos > 0)
qsort(list->values, list->pos, sizeof(proc_stat), proc_stat_cmp);
}
static void merge_stats(pg_stat_activity_list *pg_stats, proc_stat_list proc_stats)
{
size_t pg_stats_pos = 0, proc_stats_pos = 0;
size_t pg_stats_size = pg_stats->pos;
/* Both list are sorted on pid so we need to traverse it only once */
while (pg_stats_pos < pg_stats_size || proc_stats_pos < proc_stats.pos) {
if (proc_stats_pos >= proc_stats.pos) { /* new backends? */
break;
} else if (pg_stats_pos >= pg_stats_size || // No more entries from pg_stats_activity (special process?)
pg_stats->values[pg_stats_pos].pid > proc_stats.values[proc_stats_pos].pid) {
pg_stat_activity pgs = {0,};
pgs.ps = proc_stats.values[proc_stats_pos];
pgs.pid = pgs.ps.pid;
pgs.type = PG_UNDEFINED; /* unknown process */
pg_stat_activity_list_add(pg_stats, pgs);
++proc_stats_pos;
} else if (pg_stats->values[pg_stats_pos].pid == proc_stats.values[proc_stats_pos].pid) {
pg_stats->values[pg_stats_pos++].ps = proc_stats.values[proc_stats_pos++];
} else { /* pg_stats->values[pg_pos].pid < proc_stats.values[ps_pos].pid, new backend? */
++pg_stats_pos;
}
}
if (pg_stats->pos > 0)
qsort(pg_stats->values, pg_stats->pos, sizeof(pg_stat_activity), pg_stat_activity_cmp);
}
#define PROCESS_PATTERN " process"
#if PG_VERSION_NUM >= 130000
#define SUFFIX_PATTERN ""
#define UNKNOWN_PROC_NAME UNKNOWN_NAME
#else
#define SUFFIX_PATTERN " "
#define UNKNOWN_PROC_NAME "???" PROCESS_PATTERN SUFFIX_PATTERN
#endif
#define PARALLEL_WORKER_PROC_NAME PARALLEL_WORKER_NAME " for PID"
#define LOGICAL_LAUNCHER_PROC_NAME LOGICAL_LAUNCHER_NAME SUFFIX_PATTERN
#if PG_VERSION_NUM >= 170000
#define LOGICAL_TABLESYNC_WORKER_PROC_NAME "logical replication tablesync worker for"
#else
#define LOGICAL_TABLESYNC_WORKER_PROC_NAME "logical replication worker for"
#endif
#if PG_VERSION_NUM < 160000
#define LOGICAL_APPLY_WORKER_PROC_NAME "logical replication worker for"
#else
#define LOGICAL_APPLY_WORKER_PROC_NAME "logical replication apply worker for"
#endif
#define LOGICAL_PARALLEL_WORKER_PROC_NAME "logical replication parallel apply worker for"
#define BACKEND_ENTRY(CMDLINE_PATTERN, TYPE) TAB_ENTRY(CMDLINE_PATTERN " ", PG_##TYPE)
#define CMDLINE(TYPE) TYPE##_PROC_NAME
#define OTH_BACKEND(TYPE) BACKEND_ENTRY(CMDLINE(TYPE), TYPE)
#if PG_VERSION_NUM < 110000
#define WAL_RECEIVER_PROC_NAME "wal receiver"
#define WAL_SENDER_PROC_NAME "wal sender"
#define WAL_WRITER_PROC_NAME "wal writer"
#define BG_WRITER_PROC_NAME "writer"
#define BG_WORKER_PROC_NAME "bgworker:"
#define CMDLINE_PATTERN(TYPE) CMDLINE(TYPE) PROCESS_PATTERN
#define BGWORKER(TYPE) BACKEND_ENTRY(CMDLINE(BG_WORKER) " " CMDLINE(TYPE), TYPE)
#else
#define WAL_RECEIVER_PROC_NAME WAL_RECEIVER_NAME
#define WAL_SENDER_PROC_NAME WAL_SENDER_NAME
#define WAL_WRITER_PROC_NAME WAL_WRITER_NAME
#define BG_WRITER_PROC_NAME "background writer"
#define CMDLINE_PATTERN(TYPE) CMDLINE(TYPE)
#define BGWORKER(TYPE) OTH_BACKEND(TYPE)
#endif
#define AUX_BACKEND(TYPE) BACKEND_ENTRY(CMDLINE_PATTERN(TYPE) SUFFIX_PATTERN, TYPE)
#if PG_VERSION_NUM >= 160000
#define DBENTRY_FIELD(DBENTRY, FIELD) DBENTRY->FIELD
#else
#define DBENTRY_FIELD(DBENTRY, FIELD) DBENTRY->n_##FIELD
#endif
static PgBackendType parse_cmdline(const char * const buf, const char **rest)
{
*rest = buf;
if (strncmp(buf, cmdline_prefix, cmdline_prefix_len) == 0) {
int j;
const char * const cmd = buf + cmdline_prefix_len;
struct _backend_tab {
const char *name;
size_t name_len;
PgBackendType type;
} backend_tab[] = {
AUX_BACKEND(ARCHIVER),
AUX_BACKEND(STARTUP),
AUX_BACKEND(WAL_RECEIVER),
BACKEND_ENTRY(CMDLINE_PATTERN(WAL_SENDER), WAL_SENDER),
AUX_BACKEND(AUTOVAC_LAUNCHER),
AUX_BACKEND(AUTOVAC_WORKER),
#if PG_VERSION_NUM >= 90600
BGWORKER(PARALLEL_WORKER),
#endif
#if PG_VERSION_NUM >= 100000
BGWORKER(LOGICAL_LAUNCHER),
BGWORKER(LOGICAL_APPLY_WORKER),
#endif
#if PG_VERSION_NUM >= 160000
BGWORKER(LOGICAL_PARALLEL_WORKER),
BGWORKER(LOGICAL_TABLESYNC_WORKER),
#endif
#if PG_VERSION_NUM < 110000
OTH_BACKEND(BG_WORKER),
#endif
AUX_BACKEND(CHECKPOINTER),
AUX_BACKEND(LOGGER),
AUX_BACKEND(STATS_COLLECTOR),
AUX_BACKEND(WAL_WRITER),
AUX_BACKEND(BG_WRITER),
#if PG_VERSION_NUM >= 170000
AUX_BACKEND(SLOTSYNC_WORKER),
AUX_BACKEND(WAL_SUMMARIZER),
#endif
OTH_BACKEND(UNKNOWN),
{NULL, 0, PG_UNDEFINED}
};
for (j = 0; backend_tab[j].name != NULL; ++j)
if (strncmp(cmd, backend_tab[j].name, backend_tab[j].name_len) == 0) {
*rest = cmd + backend_tab[j].name_len;
#if PG_VERSION_NUM < 160000
if (backend_tab[j].type == PG_LOGICAL_APPLY_WORKER && strstr(*rest, " sync "))
return PG_LOGICAL_TABLESYNC_WORKER;
#endif
return backend_tab[j].type;
}
#if PG_VERSION_NUM >= 110000
{
size_t len = strlen(cmd) - sizeof(SUFFIX_PATTERN);
if (len > 0 && strcmp(cmd + len, " " SUFFIX_PATTERN) == 0) {
*rest = cmd;
return PG_BG_WORKER;
}
}
#endif
}
return PG_UNDEFINED;
}
static void read_proc_cmdline(pg_stat_activity *stat)
{
FILE *f;
char buf[MAXPGPATH];
char proc_file[32];
if (stat->type != PG_UNDEFINED && stat->type != PG_ARCHIVER
&& stat->type != PG_STARTUP && stat->type != PG_WAL_RECEIVER
&& (stat->type != PG_WAL_SENDER || stat->query))
return;
sprintf(proc_file, "/proc/%d/cmdline", stat->pid);
if ((f = fopen(proc_file, "r")) == NULL) return;
if (fgets(buf, sizeof(buf), f)) {
const char *rest;
PgBackendType type = parse_cmdline(buf, &rest);
stat->type = type;
if ((type == PG_WAL_RECEIVER || type == PG_WAL_SENDER
|| type == PG_ARCHIVER || type == PG_STARTUP) && *rest) {
if (type == PG_WAL_SENDER && stat->usename) {
size_t len = strlen(stat->usename) - 2;
if (strncmp(rest, stat->usename + 1, len) == 0 && rest[len] == ' ')
rest += len + 1;
}
stat->query = json_escape_string(rest);
} else if ((type >= PG_LOGICAL_TABLESYNC_WORKER || type == PG_BG_WORKER) && *rest)
stat->ps.cmdline = json_escape_string_len(rest, strlen(rest) - sizeof(SUFFIX_PATTERN));
else if (type == PG_PARALLEL_WORKER && *rest)
stat->parent_pid = strtoul(rest, NULL, 10);
}
fclose(f);
}
static void calculate_stats_diff(pg_stat_activity *old_stat, pg_stat_activity *new_stat, unsigned long long itv)
{
proc_stat *o = &old_stat->ps;
proc_stat *n = &new_stat->ps;
if (n->fields < 9) return;
n->delayacct_blkio_ticks_diff = S_VALUE(o->delayacct_blkio_ticks, n->delayacct_blkio_ticks, itv);
n->io.read_diff = S_VALUE(o->io.read_bytes, n->io.read_bytes, itv) / 1024;
n->io.write_diff = S_VALUE(o->io.write_bytes, n->io.write_bytes, itv) / 1024;
n->io.cancelled_write_diff = S_VALUE(o->io.cancelled_write_bytes, n->io.cancelled_write_bytes, itv) / 1024;
n->utime_diff = SP_VALUE_100(o->utime, n->utime, itv);
n->stime_diff = SP_VALUE_100(o->stime, n->stime, itv);
n->gtime_diff = SP_VALUE_100(o->gtime, n->gtime, itv);
}
static void diff_pg_stat_activity(pg_stat_activity_list old_activity, pg_stat_activity_list new_activity, unsigned long long itv)
{
size_t old_pos = 0, new_pos = 0;
while (old_pos < old_activity.pos || new_pos < new_activity.pos) {
if (new_pos >= new_activity.pos)
old_activity.values[old_pos++].ps.free_cmdline = true;
else if (old_pos >= old_activity.pos)
read_proc_cmdline(new_activity.values + new_pos++);
else if (old_activity.values[old_pos].pid == new_activity.values[new_pos].pid) {
if (old_activity.values[old_pos].ps.start_time != new_activity.values[new_pos].ps.start_time)
old_activity.values[old_pos].ps.free_cmdline = true;
else if (old_activity.values[old_pos].type != PG_UNDEFINED && new_activity.values[new_pos].type == PG_UNDEFINED) {
new_activity.values[new_pos].type = old_activity.values[old_pos].type;
if (new_activity.values[new_pos].type >= PG_LOGICAL_TABLESYNC_WORKER || new_activity.values[new_pos].type == PG_BG_WORKER)
new_activity.values[new_pos].ps.cmdline = old_activity.values[old_pos].ps.cmdline;
}
read_proc_cmdline(new_activity.values + new_pos);
calculate_stats_diff(old_activity.values + old_pos++, new_activity.values + new_pos++, itv);
} else if (old_activity.values[old_pos].pid > new_activity.values[new_pos].pid)
read_proc_cmdline(new_activity.values + new_pos++);
else // old.pid < new.pid
old_activity.values[old_pos++].ps.free_cmdline = true;
}
}
static void diff_db_stats(db_stat_list old_db, db_stat_list new_db, unsigned long long itv)
{
size_t old_pos = 0, new_pos = 0;
while (old_pos < old_db.pos && new_pos < new_db.pos) {
if (old_db.values[old_pos].databaseid == new_db.values[new_pos].databaseid) {
new_db.values[new_pos].n_xact_commit_diff = S_VALUE(old_db.values[old_pos].n_xact_commit, new_db.values[new_pos].n_xact_commit, itv);
new_db.values[new_pos].n_xact_rollback_diff = S_VALUE(old_db.values[old_pos].n_xact_rollback, new_db.values[new_pos].n_xact_rollback, itv);
new_db.values[new_pos].n_blocks_fetched_diff = S_VALUE(old_db.values[old_pos].n_blocks_fetched, new_db.values[new_pos].n_blocks_fetched, itv);
new_db.values[new_pos].n_blocks_hit_diff = S_VALUE(old_db.values[old_pos].n_blocks_hit, new_db.values[new_pos].n_blocks_hit, itv);
new_db.values[new_pos].n_tuples_returned_diff = S_VALUE(old_db.values[old_pos].n_tuples_returned, new_db.values[new_pos].n_tuples_returned, itv);
new_db.values[new_pos].n_tuples_fetched_diff = S_VALUE(old_db.values[old_pos].n_tuples_fetched, new_db.values[new_pos].n_tuples_fetched, itv);
new_db.values[new_pos].n_tuples_updated_diff = S_VALUE(old_db.values[old_pos].n_tuples_updated, new_db.values[new_pos].n_tuples_updated, itv);
new_db.values[new_pos].n_tuples_inserted_diff = S_VALUE(old_db.values[old_pos].n_tuples_inserted, new_db.values[new_pos].n_tuples_inserted, itv);
new_db.values[new_pos].n_tuples_deleted_diff = S_VALUE(old_db.values[old_pos].n_tuples_deleted, new_db.values[new_pos].n_tuples_deleted, itv);
new_db.values[new_pos].n_temp_files_diff = S_VALUE(old_db.values[old_pos].n_temp_files, new_db.values[new_pos].n_temp_files, itv);
new_db.values[new_pos].n_temp_bytes_diff = S_VALUE(old_db.values[old_pos].n_temp_bytes, new_db.values[new_pos].n_temp_bytes, itv);
if (new_db.track_io_timing) {
new_db.values[new_pos].n_block_read_time_diff = MINIMUM(S_VALUE(old_db.values[old_pos].n_block_read_time, new_db.values[new_pos].n_block_read_time, itv)/10000.0, 100.0);
new_db.values[new_pos].n_block_write_time_diff = MINIMUM(S_VALUE(old_db.values[old_pos].n_block_write_time, new_db.values[new_pos].n_block_write_time, itv)/10000.0, 100.0);
}
old_pos++;
new_pos++;
} else if (old_db.values[old_pos].databaseid > new_db.values[new_pos].databaseid)
new_pos++;
else /* old_db.values[old_pos].databaseid < new_db.values[new_pos].databaseid */
old_pos++;
}
}
static void diff_pg_stats(pg_stat old_stats, pg_stat *new_stats)
{
unsigned long long itv;
if (old_stats.uptime == 0) return;
itv = new_stats->uptime - old_stats.uptime;
diff_pg_stat_activity(old_stats.activity, new_stats->activity, itv);
diff_db_stats(old_stats.db, new_stats->db, itv);
new_stats->wal_metrics.current_diff = S_VALUE(old_stats.wal_metrics.current_wal_lsn, new_stats->wal_metrics.current_wal_lsn, itv) / 1024;
new_stats->wal_metrics.receive_diff = S_VALUE(old_stats.wal_metrics.last_wal_receive_lsn, new_stats->wal_metrics.last_wal_receive_lsn, itv) / 1024;
new_stats->wal_metrics.replay_diff = S_VALUE(old_stats.wal_metrics.last_wal_replay_lsn, new_stats->wal_metrics.last_wal_replay_lsn, itv) / 1024;
}
static double calculate_age(TimestampTz ts)
{
double ret = (GetCurrentTimestamp() - ts) / 1000000.0;
return ret > 0 ? ret : 0;
}
#if PG_VERSION_NUM >= 100000
static PgBackendType map_backend_type(BackendType type)
{
switch (type)
{
case B_AUTOVAC_LAUNCHER:
return PG_AUTOVAC_LAUNCHER;
case B_AUTOVAC_WORKER: