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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
|
/*
* Copyright (c) 2008-2009, Mark Wong
*/
#ifdef __linux__
#define _GNU_SOURCE
#endif /* __linux__ */
#include <stdlib.h>
#if defined(__FreeBSD__) || defined(__OpenBSD__)
#include <sys/tree.h>
#endif /* __FreeBSD__*/
#ifdef __linux__
#include "tree.h"
#endif /* __linux__ */
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <libpq-fe.h>
#include "pg.h"
#include "remote.h"
#include "utils.h"
#define QUERY_CPUTIME \
"SELECT user, nice, system, idle, iowait\n" \
"FROM pg_cputime()"
#define QUERY_LOADAVG \
"SELECT load1, load5, load15, last_pid\n" \
"FROM pg_loadavg()"
#define QUERY_MEMUSAGE \
"SELECT memused, memfree, memshared, membuffers, memcached,\n" \
" swapused, swapfree, swapcached\n" \
"FROM pg_memusage()"
#define QUERY_PROCTAB \
"WITH lock_activity AS\n" \
"(\n" \
" SELECT pid, count(*) AS lock_count\n" \
" FROM pg_locks\n" \
" WHERE relation IS NOT NULL\n" \
" GROUP BY pid\n" \
")\n" \
"SELECT a.pid, comm, fullcomm, a.state, utime, stime,\n" \
" starttime, vsize, rss, usename, rchar, wchar,\n" \
" syscr, syscw, reads, writes, cwrites, b.state,\n" \
" extract(EPOCH FROM age(clock_timestamp(),\n" \
" xact_start))::BIGINT,\n" \
" extract(EPOCH FROM age(clock_timestamp(),\n" \
" query_start))::BIGINT,\n" \
" coalesce(lock_count, 0) AS lock_count\n" \
"FROM pg_proctab() a LEFT OUTER JOIN pg_stat_activity b\n" \
" ON a.pid = b.pid\n" \
" LEFT OUTER JOIN lock_activity c\n" \
" ON a.pid = c.pid;"
#define QUERY_PROCTAB_QUERY \
"WITH lock_activity AS\n" \
"(\n" \
" SELECT pid, count(*) AS lock_count\n" \
" FROM pg_locks\n" \
" GROUP BY pid\n" \
")\n" \
"SELECT a.pid, comm, query, a.state, utime, stime,\n" \
" starttime, vsize, rss, usename, rchar, wchar,\n" \
" syscr, syscw, reads, writes, cwrites, b.state,\n" \
" extract(EPOCH FROM age(clock_timestamp(),\n" \
" xact_start))::BIGINT,\n" \
" extract(EPOCH FROM age(clock_timestamp(),\n" \
" query_start))::BIGINT,\n" \
" coalesce(lock_count, 0) AS lock_count\n" \
"FROM pg_proctab() a LEFT OUTER JOIN pg_stat_activity b\n" \
" ON a.pid = b.pid\n" \
" LEFT OUTER JOIN lock_activity c\n" \
" ON a.pid = c.pid;"
#define QUERY_PG_PROC \
"SELECT COUNT(*)\n" \
"FROM pg_catalog.pg_proc\n" \
"WHERE proname = '%s'"
enum column_cputime
{
c_cpu_user, c_cpu_nice, c_cpu_system, c_cpu_idle,
c_cpu_iowait
};
enum column_loadavg
{
c_load1, c_load5, c_load15, c_last_pid
};
enum column_memusage
{
c_memused, c_memfree, c_memshared, c_membuffers,
c_memcached, c_swapused, c_swapfree, c_swapcached
};
enum column_proctab
{
c_pid, c_comm, c_fullcomm, c_state, c_utime, c_stime,
c_starttime, c_vsize, c_rss, c_username,
c_rchar, c_wchar, c_syscr, c_syscw, c_reads, c_writes, c_cwrites,
c_pgstate, c_xtime, c_qtime, c_locks
};
#define bytetok(x) (((x) + 512) >> 10)
#define INITIAL_ACTIVE_SIZE (256)
#define PROCBLOCK_SIZE (32)
#define NCPUSTATES 5
#define NMEMSTATS 5
#define NSWAPSTATS 3
#define MEMUSED 0
#define MEMFREE 1
#define MEMSHARED 2
#define MEMBUFFERS 3
#define MEMCACHED 4
#define NMEMSTATS 5
#define SWAPUSED 0
#define SWAPFREE 1
#define SWAPCACHED 2
struct top_proc_r
{
RB_ENTRY(top_proc_r) entry;
pid_t pid;
char *name;
char *usename;
unsigned long size;
unsigned long rss; /* in k */
int state;
int pgstate;
unsigned long time;
unsigned long start_time;
unsigned long xtime;
unsigned long qtime;
unsigned int locks;
double pcpu;
/* The change in the previous values and current values. */
long long rchar_diff;
long long wchar_diff;
long long syscr_diff;
long long syscw_diff;
long long read_bytes_diff;
long long write_bytes_diff;
long long cancelled_write_bytes_diff;
/* The absolute values. */
long long rchar;
long long wchar;
long long syscr;
long long syscw;
long long read_bytes;
long long write_bytes;
long long cancelled_write_bytes;
/* Replication data */
char *application_name;
char *client_addr;
char *repstate;
char *primary;
char *sent;
char *write;
char *flush;
char *replay;
long long sent_lag;
long long write_lag;
long long flush_lag;
long long replay_lag;
};
static time_t boottime = -1;
static struct top_proc_r *pgrtable;
static int proc_r_index;
int topprocrcmp(struct top_proc_r *, struct top_proc_r *);
RB_HEAD(pgprocr, top_proc_r) head_proc_r = RB_INITIALIZER(&head_proc_r);
RB_PROTOTYPE(pgprocr, top_proc_r, entry, topprocrcmp)
RB_GENERATE(pgprocr, top_proc_r, entry, topprocrcmp)
static char *cpustatenames[NCPUSTATES + 1] =
{
"user", "nice", "system", "idle", "iowait", NULL
};
static char *memorynames[NMEMSTATS + 1] =
{
"K used, ", "K free, ", "K shared, ", "K buffers, ", "K cached", NULL
};
/* these are names given to allowed sorting orders -- first is default */
static char *ordernames[] =
{
"cpu", "size", "res", "xtime", "qtime", "rchar", "wchar", "syscr",
"syscw", "reads", "writes", "cwrites", "locks", "command", "flag",
"rlag", "slag", "wlag", NULL
};
static char *swapnames[NSWAPSTATS + 1] =
{
"K used, ", "K free, ", "K cached", NULL
};
static char fmt_header[] =
" PID X SIZE RES STATE XTIME QTIME %CPU LOCKS COMMAND";
char fmt_header_io_r[] =
" PID RCHAR WCHAR SYSCR SYSCW READS WRITES CWRITES COMMAND";
char fmt_header_replication_r[] =
" PID USERNAME APPLICATION CLIENT STATE PRIMARY SENT WRITE FLUSH REPLAY SLAG WLAG FLAG RLAG";
/* Now the array that maps process state to a weight. */
unsigned char sort_state_r[] =
{
0, /* empty */
6, /* run */
3, /* sleep */
5, /* disk wait */
1, /* zombie */
2, /* stop */
4 /* swap */
};
static int64_t cpu_states[NCPUSTATES];
static long memory_stats[NMEMSTATS];
static int process_states[NPROCSTATES];
static long swap_stats[NSWAPSTATS];
static struct timeval lasttime;
static int64_t cp_time[NCPUSTATES];
static int64_t cp_old[NCPUSTATES];
static int64_t cp_diff[NCPUSTATES];
#define ORDERKEY_PCTCPU if ((result = (int)(p2->pcpu - p1->pcpu)) == 0)
#define ORDERKEY_STATE if ((result = p1->pgstate < p2->pgstate))
#define ORDERKEY_RSSIZE if ((result = p2->rss - p1->rss) == 0)
#define ORDERKEY_LAG_FLUSH if ((result = p2->flush_lag - p1->flush_lag) == 0)
#define ORDERKEY_LAG_REPLAY if ((result = p2->replay_lag - \
p1->replay_lag) == 0)
#define ORDERKEY_LAG_SENT if ((result = p2->sent_lag - p1->sent_lag) == 0)
#define ORDERKEY_LAG_WRITE if ((result = p2->write_lag - p1->write_lag) == 0)
#define ORDERKEY_MEM if ((result = p2->size - p1->size) == 0)
#define ORDERKEY_NAME if ((result = strcmp(p1->name, p2->name)) == 0)
#define ORDERKEY_RCHAR if ((result = p1->rchar - p2->rchar) == 0)
#define ORDERKEY_WCHAR if ((result = p1->wchar - p2->wchar) == 0)
#define ORDERKEY_SYSCR if ((result = p1->syscr - p2->syscr) == 0)
#define ORDERKEY_SYSCW if ((result = p1->syscw - p2->syscw) == 0)
#define ORDERKEY_READS if ((result = p1->read_bytes - p2->read_bytes) == 0)
#define ORDERKEY_WRITES if ((result = p1->write_bytes - p2->write_bytes) == 0)
#define ORDERKEY_CWRITES if ((result = p1->cancelled_write_bytes - p2->cancelled_write_bytes) == 0)
#define ORDERKEY_XTIME if ((result = p2->xtime - p1->xtime) == 0)
#define ORDERKEY_QTIME if ((result = p2->qtime - p1->qtime) == 0)
#define ORDERKEY_LOCKS if ((result = p2->locks - p1->locks) == 0)
int check_for_function(PGconn *, char *);
static int compare_cmd_r(const void *, const void *);
static int compare_cpu_r(const void *, const void *);
static int compare_cwrites_r(const void *, const void *);
static int compare_lag_flush(const void *, const void *);
static int compare_lag_replay(const void *, const void *);
static int compare_lag_sent(const void *, const void *);
static int compare_lag_write(const void *, const void *);
static int compare_locks_r(const void *, const void *);
static int compare_qtime_r(const void *, const void *);
static int compare_rchar_r(const void *, const void *);
static int compare_reads_r(const void *, const void *);
static int compare_res_r(const void *, const void *);
static int compare_size_r(const void *, const void *);
static int compare_syscr_r(const void *, const void *);
static int compare_syscw_r(const void *, const void *);
static int compare_wchar_r(const void *, const void *);
static int compare_writes_r(const void *, const void *);
static int compare_xtime_r(const void *, const void *);
int
check_for_function(PGconn *pgconn, char *procname)
{
PGresult *pgresult = NULL;
int rows = 0;
int count;
char sql[128];
sprintf(sql, QUERY_PG_PROC, procname);
pgresult = PQexec(pgconn, sql);
rows = PQntuples(pgresult);
/* Don't need to clean up on error, the program will exit shortly after. */
if (rows == 0)
{
fprintf(stderr, "Error executing '%s'.\n", sql);
return -1;
}
count = atoi(PQgetvalue(pgresult, 0, 0));
if (count == 0)
{
fprintf(stderr, "Stored function '%s' is missing.\n", procname);
return -1;
}
if (pgresult != NULL)
PQclear(pgresult);
return 0;
}
int (*proc_compares_r[]) () =
{
compare_cpu_r,
compare_size_r,
compare_res_r,
compare_xtime_r,
compare_qtime_r,
compare_rchar_r,
compare_wchar_r,
compare_syscr_r,
compare_syscw_r,
compare_reads_r,
compare_writes_r,
compare_cwrites_r,
compare_locks_r,
compare_cmd_r,
compare_lag_flush,
compare_lag_replay,
compare_lag_sent,
compare_lag_write,
NULL
};
/* The comparison function for sorting by command name. */
static int
compare_cmd_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_NAME
ORDERKEY_PCTCPU
ORDERKEY_STATE
ORDERKEY_RSSIZE
ORDERKEY_MEM
;
return (result);
}
/* compare_cpu_r - the comparison function for sorting by cpu percentage */
static int
compare_cpu_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_PCTCPU
ORDERKEY_STATE
ORDERKEY_RSSIZE
ORDERKEY_MEM
;
return (result);
}
static int
compare_cwrites_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_CWRITES
ORDERKEY_RCHAR
ORDERKEY_WCHAR
ORDERKEY_SYSCR
ORDERKEY_SYSCW
ORDERKEY_READS
ORDERKEY_WRITES
ORDERKEY_NAME
;
return (result);
}
static int
compare_lag_flush(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_LAG_FLUSH
ORDERKEY_PCTCPU
ORDERKEY_STATE
ORDERKEY_MEM
ORDERKEY_RSSIZE
;
return (result);
}
static int
compare_lag_replay(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_LAG_REPLAY
ORDERKEY_PCTCPU
ORDERKEY_STATE
ORDERKEY_MEM
ORDERKEY_RSSIZE
;
return (result);
}
static int
compare_lag_sent(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_LAG_SENT
ORDERKEY_PCTCPU
ORDERKEY_STATE
ORDERKEY_MEM
ORDERKEY_RSSIZE
;
return (result);
}
static int
compare_lag_write(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_LAG_WRITE
ORDERKEY_PCTCPU
ORDERKEY_STATE
ORDERKEY_MEM
ORDERKEY_RSSIZE
;
return (result);
}
/*
* compare_locks_r - the comparison function for sorting by total locks
* acquired
*/
int
compare_locks_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_LOCKS
ORDERKEY_QTIME
ORDERKEY_PCTCPU
ORDERKEY_STATE
ORDERKEY_MEM
ORDERKEY_RSSIZE
;
return (result);
}
/* compare_qtime_r - the comparison function for sorting by total cpu qtime */
static int
compare_qtime_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_QTIME
ORDERKEY_PCTCPU
ORDERKEY_STATE
ORDERKEY_MEM
ORDERKEY_RSSIZE
;
return (result);
}
/* The comparison function for sorting by resident set size. */
static int
compare_res_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_RSSIZE
ORDERKEY_MEM
ORDERKEY_PCTCPU
ORDERKEY_STATE
;
return (result);
}
/* The comparison function for sorting by total memory usage. */
static int
compare_rchar_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_RCHAR
ORDERKEY_WCHAR
ORDERKEY_SYSCR
ORDERKEY_SYSCW
ORDERKEY_READS
ORDERKEY_WRITES
ORDERKEY_CWRITES
ORDERKEY_NAME
;
return (result);
}
static int
compare_reads_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_READS
ORDERKEY_RCHAR
ORDERKEY_WCHAR
ORDERKEY_SYSCR
ORDERKEY_SYSCW
ORDERKEY_WRITES
ORDERKEY_CWRITES
ORDERKEY_NAME
;
return (result);
}
static int
compare_size_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_MEM
ORDERKEY_RSSIZE
ORDERKEY_PCTCPU
ORDERKEY_STATE
;
return (result);
}
static int
compare_syscr_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_SYSCR
ORDERKEY_RCHAR
ORDERKEY_WCHAR
ORDERKEY_SYSCW
ORDERKEY_READS
ORDERKEY_WRITES
ORDERKEY_CWRITES
ORDERKEY_NAME
;
return (result);
}
static int
compare_syscw_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_SYSCW
ORDERKEY_RCHAR
ORDERKEY_WCHAR
ORDERKEY_SYSCR
ORDERKEY_READS
ORDERKEY_WRITES
ORDERKEY_CWRITES
ORDERKEY_NAME
;
return (result);
}
/* compare_xtime_r - the comparison function for sorting by total cpu xtime */
static int
compare_xtime_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_XTIME
ORDERKEY_PCTCPU
ORDERKEY_STATE
ORDERKEY_MEM
ORDERKEY_RSSIZE
;
return (result);
}
static int
compare_wchar_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_WCHAR
ORDERKEY_RCHAR
ORDERKEY_SYSCR
ORDERKEY_SYSCW
ORDERKEY_READS
ORDERKEY_WRITES
ORDERKEY_CWRITES
ORDERKEY_NAME
;
return (result);
}
static int
compare_writes_r(const void *v1, const void *v2)
{
struct top_proc_r *p1 = (struct top_proc_r *) v1;
struct top_proc_r *p2 = (struct top_proc_r *) v2;
int result;
ORDERKEY_WRITES
ORDERKEY_RCHAR
ORDERKEY_WCHAR
ORDERKEY_SYSCR
ORDERKEY_SYSCW
ORDERKEY_READS
ORDERKEY_CWRITES
ORDERKEY_NAME
;
return (result);
}
char *
format_header_r(char *uname_field)
{
int uname_len = strlen(uname_field);
if (uname_len > 8)
uname_len = 8;
memcpy(strchr(fmt_header, 'X'), uname_field, uname_len);
return fmt_header;
}
char *
format_next_io_r(caddr_t handler)
{
static char fmt[MAX_COLS]; /* static area where result is built */
struct top_proc_r *p = &pgrtable[proc_r_index++];
snprintf(fmt, sizeof(fmt),
"%7d %5s %5s %7lld %7lld %5s %6s %7s %s",
(int) p->pid,
format_b(p->rchar_diff),
format_b(p->wchar_diff),
p->syscr_diff,
p->syscw_diff,
format_b(p->read_bytes_diff),
format_b(p->write_bytes_diff),
format_b(p->cancelled_write_bytes_diff),
p->name);
return (fmt);
}
char *
format_next_process_r(caddr_t handler)
{
static char fmt[MAX_COLS]; /* static area where result is built */
struct top_proc_r *p = &pgrtable[proc_r_index++];
snprintf(fmt, sizeof(fmt),
"%7d %-10.8s %5s %5s %-6s %5s %5s %5.1f %5d %s",
(int) p->pid, /* Some OS's need to cast pid_t to int. */
p->usename,
format_k(p->size),
format_k(p->rss),
backendstatenames[p->pgstate],
format_time(p->xtime),
format_time(p->qtime),
p->pcpu * 100.0,
p->locks,
p->name);
return (fmt);
}
char *
format_next_replication_r(caddr_t handle)
{
static char fmt[MAX_COLS]; /* static area where result is built */
struct top_proc_r *p = &pgrtable[proc_r_index++];
snprintf(fmt, sizeof(fmt),
"%7d %-8.8s %-11.11s %15s %-9.9s %9s %9s %9s %9s %9s %5s %5s %5s %5s",
p->pid,
p->usename,
p->application_name,
p->client_addr,
p->repstate,
p->primary,
p->sent,
p->write,
p->flush,
p->replay,
format_b(p->sent_lag),
format_b(p->write_lag),
format_b(p->flush_lag),
format_b(p->replay_lag));
/* return the result */
return (fmt);
}
void
get_system_info_r(struct system_info *info, struct pg_conninfo_ctx *conninfo)
{
PGresult *pgresult = NULL;
int rows = 0;
connect_to_db(conninfo);
if (conninfo->connection != NULL)
{
pgresult = PQexec(conninfo->connection, QUERY_LOADAVG);
rows = PQntuples(pgresult);
}
/* Get load averages. */
if (rows > 0)
{
info->load_avg[0] = atof(PQgetvalue(pgresult, 0, c_load1));
info->load_avg[1] = atof(PQgetvalue(pgresult, 0, c_load5));
info->load_avg[2] = atof(PQgetvalue(pgresult, 0, c_load15));
info->last_pid = atoi(PQgetvalue(pgresult, 0, c_last_pid));
}
else
{
info->load_avg[0] = 0;
info->load_avg[1] = 0;
info->load_avg[2] = 0;
info->last_pid = 0;
}
/* Get processor time info. */
if (conninfo->connection != NULL)
{
pgresult = PQexec(conninfo->connection, QUERY_CPUTIME);
rows = PQntuples(pgresult);
}
if (rows > 0)
{
cp_time[0] = atol(PQgetvalue(pgresult, 0, c_cpu_user));
cp_time[1] = atol(PQgetvalue(pgresult, 0, c_cpu_nice));
cp_time[2] = atol(PQgetvalue(pgresult, 0, c_cpu_system));
cp_time[3] = atol(PQgetvalue(pgresult, 0, c_cpu_idle));
cp_time[4] = atol(PQgetvalue(pgresult, 0, c_cpu_iowait));
/* convert cp_time counts to percentages */
percentages(NCPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
}
else
{
cpu_states[0] = 0;
cpu_states[1] = 0;
cpu_states[2] = 0;
cpu_states[3] = 0;
cpu_states[4] = 0;
}
/* Get system wide memory usage. */
if (conninfo->connection != NULL)
{
pgresult = PQexec(conninfo->connection, QUERY_MEMUSAGE);
rows = PQntuples(pgresult);
}
if (rows > 0)
{
memory_stats[MEMUSED] = atol(PQgetvalue(pgresult, 0, c_memused));
memory_stats[MEMFREE] = atol(PQgetvalue(pgresult, 0, c_memfree));
memory_stats[MEMSHARED] = atol(PQgetvalue(pgresult, 0, c_memshared));
memory_stats[MEMBUFFERS] = atol(PQgetvalue(pgresult, 0, c_membuffers));
memory_stats[MEMCACHED] = atol(PQgetvalue(pgresult, 0, c_memcached));
swap_stats[SWAPUSED] = atol(PQgetvalue(pgresult, 0, c_swapused));
swap_stats[SWAPFREE] = atol(PQgetvalue(pgresult, 0, c_swapfree));
swap_stats[SWAPCACHED] = atol(PQgetvalue(pgresult, 0, c_swapcached));
}
else
{
memory_stats[MEMUSED] = 0;
memory_stats[MEMFREE] = 0;
memory_stats[MEMSHARED] = 0;
memory_stats[MEMBUFFERS] = 0;
memory_stats[MEMCACHED] = 0;
swap_stats[SWAPUSED] = 0;
swap_stats[SWAPFREE] = 0;
swap_stats[SWAPCACHED] = 0;
}
info->cpustates = cpu_states;
info->memory = memory_stats;
info->swap = swap_stats;
if (pgresult != NULL)
PQclear(pgresult);
disconnect_from_db(conninfo);
}
caddr_t
get_process_info_r(struct system_info *si, struct process_select *sel,
int compare_index, struct pg_conninfo_ctx *conninfo, int mode)
{
int i;
PGresult *pgresult = NULL;
int rows;
struct timeval thistime;
double timediff;
int active_procs = 0;
int total_procs = 0;
int show_idle = sel->idle;
struct top_proc_r *n,
*p;
memset(process_states, 0, sizeof(process_states));
/* Calculate the time difference since our last check. */
gettimeofday(&thistime, 0);
if (lasttime.tv_sec)
{
timediff = ((thistime.tv_sec - lasttime.tv_sec) +
(thistime.tv_usec - lasttime.tv_usec) * 1e-6);
}
else
{
timediff = 0;
}
lasttime = thistime;
timediff *= HZ; /* Convert to ticks. */
connect_to_db(conninfo);
if (conninfo->connection != NULL)
{
switch (mode)
{
case MODE_REPLICATION:
pgresult = pg_replication(conninfo->connection);
break;
default:
if (sel->fullcmd == 2)
{
pgresult = PQexec(conninfo->connection, QUERY_PROCTAB_QUERY);
}
else
{
pgresult = PQexec(conninfo->connection, QUERY_PROCTAB);
}
}
rows = PQntuples(pgresult);
}
else
{
rows = 0;
}
if (rows > 0)
{
p = realloc(pgrtable, sizeof(struct top_proc_r) * rows);
if (p == NULL)
{
fprintf(stderr, "realloc error\n");
if (pgresult != NULL)
PQclear(pgresult);
disconnect_from_db(conninfo);
exit(1);
}
pgrtable = p;
}
for (i = 0; i < rows; i++)
{
unsigned long otime;
long long value;
n = malloc(sizeof(struct top_proc_r));
if (n == NULL)
{
fprintf(stderr, "malloc error\n");
if (pgresult != NULL)
PQclear(pgresult);
disconnect_from_db(conninfo);
exit(1);
}
memset(n, 0, sizeof(struct top_proc_r));
n->pid = atoi(PQgetvalue(pgresult, i, c_pid));
p = RB_INSERT(pgprocr, &head_proc_r, n);
if (p != NULL)
{
free(n);
n = p;
}
else
{
n->time = 0;
}
otime = n->time;
switch (mode)
{
case MODE_REPLICATION:
update_str(&n->usename, PQgetvalue(pgresult, i, 1));
update_str(&n->application_name, PQgetvalue(pgresult, i, 2));
update_str(&n->client_addr, PQgetvalue(pgresult, i, 3));
update_str(&n->repstate, PQgetvalue(pgresult, i, 4));
update_str(&n->primary, PQgetvalue(pgresult, i, 5));
update_str(&n->sent, PQgetvalue(pgresult, i, 6));
update_str(&n->write, PQgetvalue(pgresult, i, 7));
update_str(&n->flush, PQgetvalue(pgresult, i, 8));
update_str(&n->replay, PQgetvalue(pgresult, i, 9));
n->sent_lag = atol(PQgetvalue(pgresult, i, 10));
n->write_lag = atol(PQgetvalue(pgresult, i, 11));
n->flush_lag = atol(PQgetvalue(pgresult, i, 12));
n->replay_lag = atol(PQgetvalue(pgresult, i, 13));
memcpy(&pgrtable[active_procs++], n, sizeof(struct top_proc_r));
break;
default:
if (sel->fullcmd && PQgetvalue(pgresult, i, c_fullcomm))
update_str(&n->name, printable(PQgetvalue(pgresult, i, c_fullcomm)));
else
update_str(&n->name, printable(PQgetvalue(pgresult, i, c_comm)));
switch (PQgetvalue(pgresult, i, c_state)[0])
{
case 'R':
n->state = 1;
break;
case 'S':
n->state = 2;
break;
case 'D':
n->state = 3;
break;
case 'Z':
n->state = 4;
break;
case 'T':
n->state = 5;
break;
case 'W':
n->state = 6;
break;
case '\0':
continue;
}
update_state(&n->pgstate, PQgetvalue(pgresult, i, c_pgstate));
n->time = (unsigned long) atol(PQgetvalue(pgresult, i, c_utime));
n->time += (unsigned long) atol(PQgetvalue(pgresult, i, c_stime));
n->start_time = (unsigned long)
atol(PQgetvalue(pgresult, i, c_starttime));
n->size = bytetok((unsigned long)
atol(PQgetvalue(pgresult, i, c_vsize)));
n->rss = bytetok((unsigned long)
atol(PQgetvalue(pgresult, i, c_rss)));
update_str(&n->usename, PQgetvalue(pgresult, i, c_username));
n->xtime = atol(PQgetvalue(pgresult, i, c_xtime));
n->qtime = atol(PQgetvalue(pgresult, i, c_qtime));
n->locks = atol(PQgetvalue(pgresult, i, c_locks));
value = atoll(PQgetvalue(pgresult, i, c_rchar));
n->rchar_diff = value - n->rchar;
n->rchar = value;
value = atoll(PQgetvalue(pgresult, i, c_wchar));
n->wchar_diff = value - n->wchar;
n->wchar = value;
value = atoll(PQgetvalue(pgresult, i, c_syscr));
n->syscr_diff = value - n->syscr;
n->syscr = value;
value = atoll(PQgetvalue(pgresult, i, c_syscw));
n->syscw_diff = value - n->syscw;
n->syscw = value;
value = atoll(PQgetvalue(pgresult, i, c_reads));
n->read_bytes_diff = value - n->read_bytes;
n->read_bytes = value;
value = atoll(PQgetvalue(pgresult, i, c_writes));
n->write_bytes_diff = value - n->write_bytes;
n->write_bytes = value;
value = atoll(PQgetvalue(pgresult, i, c_cwrites));
n->cancelled_write_bytes_diff = value - n->cancelled_write_bytes;
n->cancelled_write_bytes = value;
++total_procs;
++process_states[n->pgstate];
if (timediff > 0.0)
{
if ((n->pcpu = (n->time - otime) / timediff) < 0.0001)
n->pcpu = 0;
}
if ((show_idle || n->pgstate != STATE_IDLE) &&
(sel->usename[0] == '\0' ||
strcmp(n->usename, sel->usename) == 0))
memcpy(&pgrtable[active_procs++], n, sizeof(struct top_proc_r));
}
}
if (pgresult != NULL)
PQclear(pgresult);
disconnect_from_db(conninfo);
si->p_active = active_procs;
si->p_total = total_procs;
si->procstates = process_states;
/* Sort the "active" procs if specified. */
if (compare_index >= 0 && si->p_active)
qsort(pgrtable, si->p_active, sizeof(struct top_proc_r),
proc_compares_r[compare_index]);
/* don't even pretend that the return value thing here isn't bogus */
proc_r_index = 0;
return 0;
}
int
machine_init_r(struct statics *statics, struct pg_conninfo_ctx *conninfo)
{
/* Make sure the remote system has the stored function installed. */
connect_to_db(conninfo);
if (conninfo->connection == NULL)
{
fprintf(stderr, "Cannot connect to database.\n");
return -1;
}
if (check_for_function(conninfo->connection, "pg_cputime") != 0)
return -1;
if (check_for_function(conninfo->connection, "pg_loadavg") != 0)
return -1;
if (check_for_function(conninfo->connection, "pg_memusage") != 0)
return -1;
if (check_for_function(conninfo->connection, "pg_proctab") != 0)
return -1;
disconnect_from_db(conninfo);
/* fill in the statics information */
statics->procstate_names = procstatenames;
statics->cpustate_names = cpustatenames;
statics->memory_names = memorynames;
statics->swap_names = swapnames;
statics->order_names = ordernames;
statics->boottime = boottime;
statics->flags.fullcmds = 1;
statics->flags.warmup = 1;
return 0;
}
int
topprocrcmp(struct top_proc_r *e1, struct top_proc_r *e2)
{
return (e1->pid < e2->pid ? -1 : e1->pid > e2->pid);
}
|