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 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
|
// proc.C
//
// This program is free software. See the file COPYING for details.
// Author: Mattias Engdegrd, 1997-1999
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <sched.h>
#include "qps.h"
#include "proc.h"
#include "svec.C"
#include "uidstr.h"
#include "ttystr.h"
#include "wchan.h"
#include "details.h"
#ifdef SOLARIS
#include <sys/swap.h>
#include <sys/sysinfo.h>
#include <sys/mkdev.h>
#include <limits.h>
#endif
//#define FAKE_SMP 4 // for SMP debugging on UP machines
// socket states, from <linux/net.h> and touched to avoid name collisions
enum {
SSFREE = 0, /* not allocated */
SSUNCONNECTED, /* unconnected to any socket */
SSCONNECTING, /* in process of connecting */
SSCONNECTED, /* connected to socket */
SSDISCONNECTING /* in process of disconnecting */
};
const char *procdir = "/proc";
int Procinfo::page_k_shift;
Procinfo::Procinfo(int proc_pid)
: refcnt(1)
{
details = 0;
children = 0;
fd_files = 0;
maps = 0;
#ifdef LINUX
sock_inodes = 0;
socks_current = FALSE;
usocks_current = FALSE;
per_cpu_times = 0;
#endif
environ = 0;
envblock = 0;
if(readproc(proc_pid) < 0)
pid = -1; // invalidate object, will be deleted
selected = FALSE;
hidekids = FALSE;
}
Procinfo::~Procinfo()
{
if(details) {
details->process_gone();
details = 0;
}
delete environ;
if(envblock)
free(envblock);
#ifdef LINUX
delete sock_inodes;
delete[] per_cpu_times;
#endif
if(maps) {
maps->purge();
delete maps;
}
if(fd_files) {
fd_files->purge();
delete fd_files;
}
delete children;
}
// miscellaneous static initializations
void Procinfo::init_static()
{
#ifdef SOLARIS
if(!kc) {
kc = kstat_open();
if(!kc) {
perror("kstat_open");
exit(1);
}
}
#endif
#ifdef LINUX
socks.setAutoDelete(TRUE);
usocks.setAutoDelete(TRUE);
#endif
page_k_shift = 0;
for(int j = getpagesize(); j > 1024; j >>= 1)
page_k_shift++;
}
// return number of bytes read if ok, -1 if failed
int Procinfo::read_file(char *name, void *buf, int max)
{
int fd = open(name, O_RDONLY);
if(fd < 0) return -1;
int r = read(fd, buf, max);
close(fd);
return r;
}
static inline bool isprintable(unsigned char c)
{
// assume, somewhat navely, that all latin-1 characters are printable
return (c >= 0x20 && c < 0x7f) || c >= 0xa0;
}
// replace unprintables by spaces
static void make_printable(char *s)
{
while(*s) {
if(!isprintable(*s)) *s = ' ';
++s;
}
}
#ifdef LINUX
int Procinfo::readproc(int proc_pid)
{
char path[256];
char buf[256];
char sbuf[4096]; // should be enough to acommodate /proc/X/stat
char cmdbuf[MAX_CMD_LEN];
sprintf(path, "%s/%d", procdir, proc_pid);
pid = proc_pid;
// read /proc/XX/cmdline
strcpy(buf, path);
strcat(buf, "/cmdline");
int cmdlen;
if((cmdlen = read_file(buf, cmdbuf, MAX_CMD_LEN - 1)) < 0) return -1;
if(cmdlen == 0) {
cmdline = "";
} else {
for(int i = 0; i < cmdlen; i++)
if(!cmdbuf[i]) cmdbuf[i] = ' ';
int j = cmdlen - 1;
while(j >= 0 && cmdbuf[j] == ' ')
j--;
cmdbuf[j + 1] = '\0';
make_printable(cmdbuf);
cmdline = cmdbuf;
}
// read /proc/XX/stat
strcpy(buf, path);
strcat(buf, "/stat");
int statlen;
if((statlen = read_file(buf, sbuf, sizeof(sbuf) - 1)) <= 0) return -1;
sbuf[statlen] = '\0';
char *p = strrchr(sbuf, ')');
*p = '\0'; // split in two parts
comm = strchr(sbuf, '(') + 1;
//
// Not all values from /proc/#/stat are interesting; the ones left out
// have been retained in comments to see where they should go, in case
// they are needed again.
//
// In Linux 2.2.x, timeout has been removed, and the signal information
// here is obsolete (/proc/#/status has real-time signal info).
//
// There are undocumented values after wchan, unused so far:
// nswap pages swapped out since process started
// cnswap nswap of children
// exit_signal (2.2.x) signal sent to parent when process exits
// (The latter could provide a way to detect cloned processes since
// they usually have exit_signal != SIGCHLD, but I prefer waiting
// for real TIDs before implementing thread support.)
//
long stime, cstime;
sscanf(p + 2, "%c %d %d %d %d %d %lu %lu %lu %lu %lu "
"%ld %ld %ld %ld %d %d %*s %*s %lu %*s %*s %*s %*s %*s %*s %*s %*s "
"%*s %*s %*s %*s %lu",
&state, &ppid, &pgrp, &session, &tty, &tpgid,
&flags, &minflt, &cminflt, &majflt, &cmajflt,
&utime, &stime, &cutime, &cstime, &priority, &nice,
/* timeout, itrealvalue */
&starttime,
/* vsize */
/* rss */
/* rlim, startcode, endcode, startstack kstkesp kstkeip,
signal, blocked, sigignore, sigcatch */
&wchan);
utime += stime; // we make no user/system time distinction
cutime += cstime;
// read /proc/XX/statm
strcpy(buf, path);
strcat(buf, "/statm");
if((statlen = read_file(buf, sbuf, sizeof(sbuf) - 1)) <= 0) return -1;
sbuf[statlen] = '\0';
sscanf(sbuf, "%lu %lu %lu %lu %lu %lu %lu",
&size, &resident, &share, &trs, &lrs, &drs, &dt);
size <<= page_k_shift;
resident <<= page_k_shift;
share <<= page_k_shift;
trs <<= page_k_shift;
lrs <<= page_k_shift;
drs <<= page_k_shift;
pmem = 100.0 * resident / mem_total;
// read /proc/XX/status
strcpy(buf, path);
strcat(buf, "/status");
if((statlen = read_file(buf, sbuf, sizeof(sbuf) - 1)) <= 0) return -1;
sbuf[statlen] = '\0';
p = sbuf;
for(int i = 0; i < 4; i++)
p = strchr(p + 1, '\n'); // skip four lines
sscanf(p + 1, "Uid: %d %d %d %d Gid: %d %d %d %d",
&uid, &euid, &suid, &fsuid,
&gid, &egid, &sgid, &fsgid);
which_cpu = 0;
if(num_cpus > 1) {
strcpy(buf, path);
strcat(buf, "/cpu");
if((statlen = read_file(buf, sbuf, sizeof(sbuf) - 1)) <= 0) return -1;
sbuf[statlen] = '\0';
per_cpu_times = new unsigned long[num_cpus];
p = sbuf;
for(unsigned cpu = 0; cpu < num_cpus; cpu++) {
p = strchr(p, '\n') + 1;
unsigned long utime, stime;
sscanf(p, "%*s %lu %lu", &utime, &stime);
per_cpu_times[cpu] = utime + stime;
}
}
gettimeofday(&tv, 0);
policy = -1; // will get it when needed
rtprio = -1; // ditto
return pid;
}
#endif // LINUX
#ifdef SOLARIS
int Procinfo::readproc(int proc_pid)
{
char path[256];
pid = proc_pid;
sprintf(path, "%s/%d/psinfo", procdir, proc_pid);
psinfo_t psi;
if(read_file(path, (void *)&psi, sizeof(psi)) < (int)sizeof(psi))
return -1;
sprintf(path, "%s/%d/usage", procdir, proc_pid);
prusage_t pru;
if(read_file(path, (void *)&pru, sizeof(pru)) < (int)sizeof(pru))
return -1;
uid = psi.pr_uid;
euid = psi.pr_euid;
gid = psi.pr_gid;
egid = psi.pr_egid;
make_printable(psi.pr_psargs);
cmdline = psi.pr_psargs;
state = psi.pr_lwp.pr_sname;
comm = (state == 'Z') ? "<zombie>" : psi.pr_fname;
ppid = psi.pr_ppid;
pgrp = psi.pr_pgid;
session = psi.pr_sid;
tty = psi.pr_ttydev; // type?
flags = psi.pr_flag;
const int ns_ticks = 1000000000 / HZ;
utime = psi.pr_time.tv_sec * HZ + psi.pr_time.tv_nsec / ns_ticks;
cutime = psi.pr_ctime.tv_sec * HZ + psi.pr_ctime.tv_nsec / ns_ticks;
priority = psi.pr_lwp.pr_pri;
nice = psi.pr_lwp.pr_nice;
if(Qps::normalize_nice)
nice -= NZERO;
starttime = (psi.pr_start.tv_sec - boot_time) * HZ
+ psi.pr_start.tv_nsec / ns_ticks;
wchan = psi.pr_lwp.pr_wchan;
minflt = pru.pr_minf;
majflt = pru.pr_majf;
size = psi.pr_size;
resident = psi.pr_rssize;
nthreads = psi.pr_nlwp;
which_cpu = psi.pr_lwp.pr_onpro;
env_ofs = psi.pr_envp;
// pr_pctcpu and pr_pctmem are scaled so that 1.0 is stored as 0x8000
pcpu = psi.pr_pctcpu * (1 / 327.68);
pmem = psi.pr_pctmem * (1 / 327.68);
gettimeofday(&tv, 0);
rtprio = -1; // ditto
policy_name[0] = psi.pr_lwp.pr_clname[0];
policy_name[1] = psi.pr_lwp.pr_clname[1];
return pid;
}
#endif // SOLARIS
float Procinfo::loadavg[] = {0.0, 0.0, 0.0};
int Procinfo::mem_total = 0;
int Procinfo::mem_free = 0;
#ifdef LINUX
int Procinfo::mem_shared = 0;
int Procinfo::mem_buffers = 0;
int Procinfo::mem_cached = 0;
#endif
int Procinfo::swap_total = 0;
int Procinfo::swap_free = 0;
unsigned *Procinfo::cpu_times_vec = 0;
unsigned *Procinfo::old_cpu_times_vec = 0;
long Procinfo::boot_time = 0;
#ifdef LINUX
QIntDict<Sockinfo> Procinfo::socks(17);
bool Procinfo::socks_current = FALSE;
QIntDict<UnixSocket> Procinfo::usocks(17);
bool Procinfo::usocks_current = FALSE;
#endif
unsigned int Procinfo::num_cpus = 0;
unsigned int Procinfo::old_num_cpus = 0;
#ifdef SOLARIS
kstat_ctl_t *Procinfo::kc = 0;
#endif
#ifdef LINUX
// just grab the load averages
void Procinfo::read_loadavg()
{
char path[80];
char buf[512];
strcpy(path, procdir);
strcat(path, "/loadavg");
int n;
if((n = read_file(path, buf, sizeof(buf) - 1)) <= 0) {
fprintf(stderr, "qps: Cannot open /proc/loadavg"
" (make sure /proc is mounted)\n");
exit(1);
}
buf[n] = '\0';
sscanf(buf, "%f %f %f", &loadavg[0], &loadavg[1], &loadavg[2]);
}
#endif // LINUX
#ifdef SOLARIS
static float getscaled(kstat_t *ks, const char *name)
{
// load avgs are scaled by 256
kstat_named_t *kn = (kstat_named_t *)kstat_data_lookup(ks, (char *)name);
return kn ? kn->value.ui32 * (1 / 256.0) : 0.0;
}
void Procinfo::read_loadavg()
{
kstat_chain_update(kc);
kstat_t *ks = kstat_lookup(kc, "unix", 0, "system_misc");
if(!ks || kstat_read(kc, ks, 0) == -1) {
perror("kstat_lookup/read");
exit(1);
}
loadavg[0] = getscaled(ks, "avenrun_1min");
loadavg[1] = getscaled(ks, "avenrun_5min");
loadavg[2] = getscaled(ks, "avenrun_15min");
// we might as well get the boot time too since it's in the same kstat
// (not that it is going to change)
kstat_named_t *kn;
kn = (kstat_named_t *)kstat_data_lookup(ks, "boot_time");
if(kn)
boot_time = kn->value.ui32;
}
#endif // SOLARIS
#ifdef LINUX
// read information common to all processes
void Procinfo::read_common()
{
char path[80];
char buf[4096 + 1];
// read memory info
strcpy(path, procdir);
strcat(path, "/meminfo");
int n;
if((n = read_file(path, buf, sizeof(buf) - 1)) <= 0) return;
buf[n] = '\0';
// Skip the old /meminfo cruft, making this work in post-2.1.42 kernels
// as well. (values are now in kB)
char *p = strstr(buf, "MemTotal:");
sscanf(p, "MemTotal: %d kB\nMemFree: %d kB\nMemShared: %d kB\nBuffers: %d"
" kB\nCached: %d kB\nSwapTotal: %d kB\n SwapFree: %d",
&mem_total, &mem_free, &mem_shared, &mem_buffers,
&mem_cached, &swap_total, &swap_free);
// read system status
strcpy(path, procdir);
strcat(path, "/stat");
if((n = read_file(path, buf, sizeof(buf) - 1)) <= 0) return;
buf[n] = '\0';
old_num_cpus = num_cpus;
if(!num_cpus) {
// count cpus
#ifdef FAKE_SMP
num_cpus = 4;
#else
char *p;
p = strstr(buf, "cpu");
while(p < buf + sizeof(buf) - 4 && strncmp(p, "cpu", 3) == 0) {
num_cpus++;
if(strncmp(p, "cpu0", 4) == 0)
num_cpus--;
p = strchr(p, '\n');
if(p)
p++;
}
#endif
cpu_times_vec = new unsigned[CPUTIMES * num_cpus];
old_cpu_times_vec = new unsigned[CPUTIMES * num_cpus];
}
for(unsigned cpu = 0; cpu < num_cpus; cpu++)
for(int i = 0; i < CPUTIMES; i++)
old_cpu_times(cpu, i) = cpu_times(cpu, i);
if(num_cpus == 1) {
sscanf(buf, "cpu %u %u %u %u",
&cpu_times(0, CPUTIME_USER), &cpu_times(0, CPUTIME_NICE),
&cpu_times(0, CPUTIME_SYSTEM), &cpu_times(0, CPUTIME_IDLE));
} else {
#ifdef FAKE_SMP
sscanf(buf, "cpu %u %u %u %u",
&cpu_times(0, CPUTIME_USER), &cpu_times(0, CPUTIME_NICE),
&cpu_times(0, CPUTIME_SYSTEM), &cpu_times(0, CPUTIME_IDLE));
for(unsigned cpu = 1; cpu < num_cpus; cpu++) {
for(int i = 0; i < CPUTIMES; i++)
cpu_times(cpu, i) = cpu_times(0, i);
}
#else
// SMP
for(unsigned cpu = 0; cpu < num_cpus; cpu++) {
char cpu_buf[10];
sprintf(cpu_buf, "cpu%d", cpu);
if((p = strstr(buf, cpu_buf)) != 0) {
sscanf(p, "%*s %u %u %u %u",
&cpu_times(cpu, CPUTIME_USER),
&cpu_times(cpu, CPUTIME_NICE),
&cpu_times(cpu, CPUTIME_SYSTEM),
&cpu_times(cpu, CPUTIME_IDLE));
} else {
fprintf(stderr, "Error reading info for cpu %d\n", cpu);
abort();
}
}
#endif
}
// 2.0.x kernels (at least up to 2.0.33) have an SMP bug that reports
// cpu_time(CPUTIME_IDLE) incorrectly, since it doesn't take the number of
// cpus into account. This is fixed in 2.1.x kernels, but a workaround
// should really be here.
p = strstr(buf, "btime") + 6;
sscanf(p, "%lu", &boot_time);
}
#endif // LINUX
#ifdef SOLARIS
void Procinfo::read_common()
{
// memory info: this is easy - just use sysconf
mem_total = sysconf(_SC_PHYS_PAGES) << page_k_shift;
mem_free = sysconf(_SC_AVPHYS_PAGES) << page_k_shift;
// get swap info: somewhat trickier - we have to count all swap spaces
int nswaps = swapctl(SC_GETNSWP, 0);
swaptbl_t *st = (swaptbl_t *)malloc(sizeof(int)
+ nswaps * sizeof(swapent_t));
st->swt_n = nswaps;
// We are not interested in the paths, just the values, so we allocate
// one scratch buffer for all paths to keep swapctl happy.
char path_buf[PATH_MAX + 1];
for(int i = 0; i < nswaps; i++)
st->swt_ent[i].ste_path = path_buf;
swapctl(SC_LIST, st);
// count the swap spaces
swap_total = swap_free = 0;
for(int i = 0; i < nswaps; i++) {
swap_total += st->swt_ent[i].ste_pages;
swap_free += st->swt_ent[i].ste_free;
}
swap_total <<= page_k_shift;
swap_free <<= page_k_shift;
free(st);
if(cpu_times_vec) {
if(old_cpu_times_vec) free(old_cpu_times_vec);
old_cpu_times_vec = cpu_times_vec;
cpu_times_vec = (unsigned *)malloc(sizeof(unsigned)
* num_cpus * CPUTIMES);
}
old_num_cpus = num_cpus;
// cpu states: are stored as kstats named "cpu_statN", where N is the
// cpu number. Unfortunately, the cpu numbers are not guessable so we
// sweep the kstat chain for all of them, assuming (foolishly?)
// that they are in order.
kstat_chain_update(kc);
int cpu = 0;
for(kstat_t *ks = kc->kc_chain; ks; ks = ks->ks_next) {
if(strncmp(ks->ks_name, "cpu_stat", 8) == 0) {
if(kstat_read(kc, ks, NULL) == -1) {
perror("kstat_read");
exit(1);
}
cpu_stat_t *cs = (cpu_stat_t *)ks->ks_data;
if(cpu + 1 >= (int)num_cpus) {
num_cpus = cpu + 1;
cpu_times_vec = (unsigned *)realloc(cpu_times_vec,
num_cpus * CPUTIMES
* sizeof(unsigned));
}
cpu_times(cpu, CPUTIME_USER) = cs->cpu_sysinfo.cpu[CPU_USER];
cpu_times(cpu, CPUTIME_SYSTEM) = cs->cpu_sysinfo.cpu[CPU_KERNEL];
cpu_times(cpu, CPUTIME_WAIT) = cs->cpu_sysinfo.cpu[CPU_WAIT];
cpu_times(cpu, CPUTIME_IDLE) = cs->cpu_sysinfo.cpu[CPU_IDLE];
cpu++;
}
}
}
#endif // SOLARIS
int Procinfo::get_policy()
{
if(policy == -1)
policy = sched_getscheduler(pid);
return policy;
}
int Procinfo::get_rtprio()
{
if(rtprio == -1) {
struct sched_param p;
if(sched_getparam(pid, &p) == 0)
rtprio = p.sched_priority;
}
return rtprio;
}
#ifdef LINUX
void Procinfo::read_fd(int fdnum, char *path)
{
int len;
char buf[80];
struct stat sb;
// The fd mode is contained in the link permission bits
if(lstat(path, &sb) < 0)
return;
int mode = 0;
if(sb.st_mode & 0400) mode |= OPEN_READ;
if(sb.st_mode & 0200) mode |= OPEN_WRITE;
if( (len = readlink(path, buf, sizeof(buf) - 1)) > 0) {
buf[len] = '\0';
unsigned long dev, ino;
if((buf[0] == '[' // Linux 2.0 style /proc/fd
&& sscanf(buf, "[%lx]:%lu", &dev, &ino) == 2
&& dev == 0)
|| sscanf(buf, "socket:[%lu]", &ino) > 0) { // Linux 2.1
Sockinfo *si = Procinfo::socks[ino];
if(si) {
// a TCP or UDP socket
sock_inodes->add(SockInode(fdnum, ino));
QString s;
s.sprintf("%sp socket %d",
si->proto == Sockinfo::TCP ? "tc" : "ud", ino);
fd_files->add(new Fileinfo(fdnum, s, mode));
return;
} else {
// maybe a unix domain socket?
read_usockets();
UnixSocket *us = Procinfo::usocks[ino];
if(us) {
QString s;
char *tp = "?", *st = "?";
switch(us->type) {
case SOCK_STREAM: tp = "stream"; break;
case SOCK_DGRAM: tp = "dgram"; break;
}
switch(us->state) {
case SSFREE: st = "free"; break;
case SSUNCONNECTED: st = "unconn"; break;
case SSCONNECTING: st = "connecting"; break;
case SSCONNECTED: st = "connected"; break;
case SSDISCONNECTING: st = "disconn"; break;
}
s.sprintf("unix domain socket %d (%s, %s) ",
ino, tp, st);
s.append(us->name);
fd_files->add(new Fileinfo(fdnum, s, mode));
return;
}
}
}
// assume fds will be read in increasing order
fd_files->add(new Fileinfo(fdnum, buf, mode));
}
}
#endif // LINUX
#ifdef SOLARIS
void Procinfo::read_fd(int fdnum, char *path)
{
struct stat sb;
if(lstat(path, &sb) < 0) {
// The file has been closed, or we could really not stat it despite
// having it open (could be a fd passed from another process).
fd_files->add(new Fileinfo(fdnum, "(no info available)"));
return;
}
// We could in principle find out more about the fd, such as its mode
// (RDONLY, RDWR etc) and flags, but it's messy. pfiles uses an agent lwp
// for this, but I don't know how to do it.
QString s;
const char *n;
switch(sb.st_mode & S_IFMT) {
case S_IFCHR:
// if it is a tty, we might know its real name
if(sb.st_rdev != (dev_t)-1) {
QString t = Ttystr::name(sb.st_rdev);
if(t[0] != '?') {
s = "/dev/";
s.append(t);
break;
}
}
s.sprintf("char device %d:%d", major(sb.st_rdev), minor(sb.st_rdev));
break;
case S_IFBLK:
s.sprintf("block device %d:%d", major(sb.st_rdev), minor(sb.st_rdev));
break;
case S_IFLNK:
// Directories appear as symlinks in /proc/#/fd; we chdir() to it
// and see where we end up. Not efficient though.
// Besides, we change cwd a lot in unpredictable ways. This makes
// core dumps hard to find, if they are generated at all.
s = "directory ";
if(chdir(path) >= 0) {
char buf[512];
if(getcwd(buf, sizeof(buf)) >= 0) {
s.append(buf);
break;
}
}
s.append("(unknown)");
break;
default:
switch(sb.st_mode & S_IFMT) {
case S_IFIFO: // fifo or anonymous pipe
n = "pipe"; break;
case S_IFDIR: // this shouldn't happen
n = "directory"; break;
case S_IFREG:
n = "file"; break;
case S_IFSOCK:
n = "unix domain socket"; break;
case S_IFDOOR:
n = "door"; break;
default:
n = "unknown"; break;
}
s.sprintf("%s, dev %d:%d inode %d", n,
major(sb.st_dev), minor(sb.st_dev), sb.st_ino);
break;
}
fd_files->add(new Fileinfo(fdnum, s));
}
#endif // SOLARIS
// return TRUE if /proc/PID/fd could be read, FALSE otherwise
// store fileinfo, and also socket inodes separately
bool Procinfo::read_fds()
{
char path[80], *p;
sprintf(path, "%s/%d/fd", procdir, pid);
DIR *d = opendir(path);
if(!d) return FALSE;
if(!fd_files)
fd_files = new Svec<Fileinfo*>(8);
fd_files->clear();
#ifdef LINUX
if(!sock_inodes)
sock_inodes = new Svec<SockInode>(4);
sock_inodes->clear();
#endif
p = path + strlen(path) + 1;
p[-1] = '/';
struct dirent *e;
while((e = readdir(d)) != 0) {
if(e->d_name[0] == '.')
continue; // skip . and ..
strcpy(p, e->d_name);
int fdnum = atoi(p);
read_fd(fdnum, path);
}
closedir(d);
return TRUE;
}
#ifdef LINUX
bool Procinfo::read_socket_list(Sockinfo::proto_t proto, char *pseudofile)
{
char path[80];
strcpy(path, procdir);
strcat(path, "/net/");
strcat(path, pseudofile);
FILE *f = fopen(path, "r");
if(!f) return FALSE;
char buf[256];
fgets(buf, sizeof(buf), f); // skip header
while(fgets(buf, sizeof(buf), f) != 0) {
Sockinfo *si = new Sockinfo;
si->proto = proto;
unsigned local_port, rem_port, st, tr;
sscanf(buf + 6, "%x:%x %x:%x %x %x:%x %x:%x %x %d %d %d",
&si->local_addr, &local_port, &si->rem_addr, &rem_port,
&st, &si->tx_queue, &si->rx_queue,
&tr, &si->tm_when, &si->rexmits,
&si->uid, &si->timeout, &si->inode);
// fix fields that aren't sizeof(int)
si->local_port = local_port;
si->rem_port = rem_port;
si->st = st;
si->tr = tr;
socks.insert(si->inode, si);
if(socks.count() > socks.size() * 3)
socks.resize(socks.count());
}
fclose(f);
return TRUE;
}
bool Procinfo::read_usocket_list()
{
char path[80];
strcpy(path, procdir);
strcat(path, "/net/unix");
FILE *f = fopen(path, "r");
if(!f) return FALSE;
char buf[256];
fgets(buf, sizeof(buf), f); // skip header
while(fgets(buf, sizeof(buf), f)) {
if(buf[0])
buf[strlen(buf) - 1] = '\0'; // chomp newline
UnixSocket *us = new UnixSocket;
unsigned q;
unsigned type, state;
int n;
sscanf(buf, "%x: %x %x %x %x %x %ld %n",
&q, &q, &q, &us->flags, &type, &state, &us->inode, &n);
us->name = buf + n;
us->type = type;
us->state = state;
usocks.insert(us->inode, us);
if(usocks.count() > usocks.size() * 3)
usocks.resize(usocks.count());
}
fclose(f);
return TRUE;
}
void Procinfo::read_sockets()
{
if(socks_current)
return;
socks.clear();
if(!read_socket_list(Sockinfo::TCP, "tcp")
|| !read_socket_list(Sockinfo::UDP, "udp"))
return;
socks_current = TRUE;
}
void Procinfo::read_usockets()
{
if(usocks_current)
return;
usocks.clear();
if(!read_usocket_list())
return;
usocks_current = TRUE;
}
void Procinfo::invalidate_sockets()
{
socks_current = usocks_current = FALSE;
}
// return TRUE if /proc/XX/maps could be read, FALSE otherwise
bool Procinfo::read_maps()
{
// idea: here we could readlink /proc/XX/exe to identify the executable
// when running 2.0.x
char name[80];
sprintf(name, "%s/%d/maps", procdir, pid);
FILE *f = fopen(name, "r");
if(!f) return FALSE;
char line[1024]; // lines can be this long, or longer
if(!maps)
maps = new Svec<Mapsinfo *>;
else
maps->clear();
while(fgets(line, sizeof(line), f)) {
Mapsinfo *mi = new Mapsinfo;
int n;
unsigned int major, minor;
sscanf(line, "%lx-%lx %4c %lx %x:%x %lu%n",
&mi->from, &mi->to, mi->perm, &mi->offset,
&major, &minor, &mi->inode, &n);
mi->major = major; mi->minor = minor;
if(line[n] != '\n') {
int len = strlen(line);
if(line[len - 1] == '\n')
line[len - 1] = '\0';
while(line[n] == ' ' && line[n]) n++;
mi->filename = line + n;
} else if((mi->major | mi->minor | mi->inode) == 0)
mi->filename = "(anonymous)";
maps->add(mi);
}
fclose(f);
return TRUE;
}
// return TRUE if /proc/XX/environ could be read, FALSE otherwise
bool Procinfo::read_environ()
{
int bs = 4096; // good start
if(envblock) free(envblock);
envblock = (char *)malloc(bs + 1);
char path[128];
sprintf(path, "%s/%d/environ", procdir, pid);
int fd = open(path, O_RDONLY);
if(fd < 0) {
free(envblock);
envblock = 0;
return FALSE;
}
int n;
int ofs = 0;
while((n = read(fd, envblock + ofs, bs - ofs)) == bs - ofs) {
ofs = bs;
envblock = (char *)realloc(envblock, (bs += 4096) + 1);
}
close(fd);
if(n < 0) {
free(envblock);
envblock = 0;
return FALSE;
}
n += ofs;
envblock[n] = '\0';
if(!environ)
environ = new Svec<NameValue>(64);
else
environ->clear();
for(int i = 0; i < n;) {
char *p = strchr(envblock + i, '=');
if(p)
*p++ = '\0';
else // degenerate variable: treat as name with empty value
p = envblock + i + strlen(envblock + i);
make_printable(envblock + i);
make_printable(p);
environ->add(NameValue(envblock + i, p));
i = p - envblock + strlen(p) + 1;
}
return TRUE;
}
#endif // LINUX
#ifdef SOLARIS
// return TRUE if the process environment could be read, FALSE otherwise
bool Procinfo::read_environ()
{
int fd;
char file[128];
sprintf(file, "/proc/%d/as", pid);
if( (fd = open(file, O_RDONLY)) < 0)
return FALSE;
// Just read the first 8K from the environment. Adaptive code here is
// possible, but not really worth the effort.
int bs = 8192;
if(envblock) free(envblock);
envblock = (char *)malloc(bs);
if(pread(fd, envblock, bs, env_ofs) < 0) {
free(envblock);
envblock = 0;
return FALSE;
}
close(fd);
envblock[bs - 1] = '\0';
if(!environ)
environ = new Svec<NameValue>(64);
else
environ->clear();
for(int i = 0; i * (int)sizeof(char *) < bs && ((char **)envblock)[i];
i++) {
int b = ((char **)envblock)[i] - (char *)env_ofs;
if(b < 0 || b >= bs)
continue; // outside retrieved memory block
char *val = strchr(envblock + b, '=');
if(val)
*val++ = '\0';
else
val = (char *)""; // degenerate: treat as name with empty value
make_printable(envblock + b);
make_printable(val);
environ->add(NameValue(envblock + b, val));
}
return TRUE;
}
// return TRUE if /proc/XX/map could be read, FALSE otherwise
bool Procinfo::read_maps()
{
char name[128];
sprintf(name, "%s/%d/map", procdir, pid);
FILE *f = fopen(name, "r");
if(!f) return FALSE;
if(!maps)
maps = new Svec<Mapsinfo *>;
else
maps->clear();
prmap_t pm;
while(fread(&pm, sizeof(pm), 1, f) == 1) {
Mapsinfo *mi = new Mapsinfo;
mi->from = pm.pr_vaddr;
mi->to = pm.pr_vaddr + pm.pr_size;
mi->offset = pm.pr_offset;
mi->perm[0] = pm.pr_mflags & MA_READ ? 'r' : '-';
mi->perm[1] = pm.pr_mflags & MA_WRITE ? 'w' : '-';
mi->perm[2] = pm.pr_mflags & MA_EXEC ? 'x' : '-';
mi->perm[3] = pm.pr_mflags & MA_SHARED ? 's' : 'p';
if(pm.pr_mapname[0]) {
// To find device/inode, stat the file in /proc/#/object:
char obj[128];
sprintf(obj, "%s/%d/object/%s", procdir, pid, pm.pr_mapname);
struct stat sb;
if(lstat(obj, &sb) < 0) {
delete mi;
continue;
}
mi->major = major(sb.st_dev);
mi->minor = minor(sb.st_dev);
mi->inode = sb.st_ino;
if(strcmp(pm.pr_mapname, "a.out") == 0)
mi->filename = "(executable)";
} else {
mi->major = mi->minor = mi->inode = 0;
mi->filename = "(anonymous)";
}
maps->add(mi);
}
fclose(f);
return TRUE;
}
#endif // SOLARIS
Category::~Category()
{};
int Category::compare(Procinfo *a, Procinfo *b)
{
#if QT_VERSION < 200
return strcmp(string(a), string(b));
#else
return string(a).compare(string(b));
#endif
}
Cat_int::Cat_int(const char *heading, const char *explain,
int w, int Procinfo::*member)
: Category(heading, explain), int_member(member), field_width(w)
{}
QString Cat_int::string(Procinfo *p)
{
QString s;
s.setNum(p->*int_member);
return s;
}
int Cat_int::compare(Procinfo *a, Procinfo *b)
{
// qsort() only cares about the sign of the number returned by the
// comparison function; only a subtraction is necessary
return a->*int_member - b->*int_member;
}
Cat_uintl::Cat_uintl(const char *heading, const char *explain, int w,
unsigned long Procinfo::*member)
: Category(heading, explain), uintl_member(member), field_width(w)
{}
QString Cat_uintl::string(Procinfo *p)
{
QString s;
s.setNum(p->*uintl_member);
return s;
}
int Cat_uintl::compare(Procinfo *a, Procinfo *b)
{
int bu = b->*uintl_member, au = a->*uintl_member;
return bu >= au ? (bu == au ? 0 : 1) : -1;
}
Cat_hex::Cat_hex(const char *heading, const char *explain, int w,
unsigned long Procinfo::*member)
: Cat_uintl(heading, explain, w, member)
{}
QString Cat_hex::string(Procinfo *p)
{
QString s;
s.sprintf("%8x", p->*uintl_member);
return s;
}
Cat_swap::Cat_swap(const char *heading, const char *explain)
: Category(heading, explain)
{}
QString Cat_swap::string(Procinfo *p)
{
QString s;
// It can actually happen that size < resident (XSun under Solaris 2.6)
s.setNum(p->size > p->resident ? p->size - p->resident : 0);
return s;
}
int Cat_swap::compare(Procinfo *a, Procinfo *b)
{
return (b->size - b->resident) - (a->size - a->resident);
}
Cat_string::Cat_string(const char *heading, const char *explain,
QString Procinfo::*member)
: Category(heading, explain), str_member(member)
{}
QString Cat_string::string(Procinfo *p)
{
return p->*str_member;
}
Cat_user::Cat_user(const char *heading, const char *explain)
: Cat_string(heading, explain)
{}
QString Cat_user::string(Procinfo *p)
{
if(p->uid == p->euid)
return Uidstr::userName(p->uid);
else {
QString s = Uidstr::userName(p->uid);
#if QT_VERSION < 200
s.detach();
#endif
s.append(p->euid == 0 ? "*" : "+");
return s;
}
}
Cat_group::Cat_group(const char *heading, const char *explain)
: Cat_string(heading, explain)
{}
QString Cat_group::string(Procinfo *p)
{
if(p->gid == p->egid)
return Uidstr::groupName(p->gid);
else {
QString s = Uidstr::groupName(p->gid);
#if QT_VERSION < 200
s.detach();
#endif
s.append("*");
return s;
}
}
Cat_wchan::Cat_wchan(const char *heading, const char *explain)
: Cat_string(heading, explain)
{}
QString Cat_wchan::string(Procinfo *p)
{
return Wchan::name(p->wchan);
}
Cat_cmdline::Cat_cmdline(const char *heading, const char *explain)
: Cat_string(heading, explain)
{}
QString Cat_cmdline::string(Procinfo *p)
{
if(p->cmdline.isEmpty()) {
QString s("(");
s.append(p->comm);
s.append(")");
return s;
} else {
if(Qps::show_cmd_path)
return p->cmdline;
else {
QString s(p->cmdline);
#if QT_VERSION < 200
s.detach();
#endif
int i = s.find(' ');
if(i < 0)
i = s.length();
if(i > 0) {
i = s.findRev('/', i - 1);
if(i >= 0)
s.remove(0, i + 1);
}
return s;
}
}
}
Cat_dir::Cat_dir(const char *heading, const char *explain, const char *dirname,
QString Procinfo::*member)
: Cat_string(heading, explain),
dir(dirname),
cache(member)
{}
QString Cat_dir::string(Procinfo *p)
{
if((p->*cache).isNull()) {
char path[128], buf[512];
sprintf(path, "%s/%d/%s", procdir, p->pid, dir);
#ifdef LINUX
int n = readlink(path, buf, sizeof(buf) - 1);
if(n < 0) {
// Either a kernel process, or access denied.
// A hyphen is visually least disturbing here.
p->*cache = "-";
return p->*cache;
} else if(buf[0] != '[') {
// linux >= 2.1.x: path name directly in link
buf[n] = '\0';
p->*cache = buf;
return p->*cache;
}
#endif
// Either a Linux 2.0 link in [device]:inode form, or a Solaris link.
// To resolve it, we just chdir() to it and see where we end up.
// Perhaps we should change back later?
if(chdir(path) < 0) {
p->*cache = "-"; // Most likely access denied
} else {
// getcwd() is fairly expensive, but this is cached anyway
if(!getcwd(buf, sizeof(buf))) {
p->*cache = "(deleted)";
} else
p->*cache = buf;
}
}
return p->*cache;
}
Cat_state::Cat_state(const char *heading, const char *explain)
: Category(heading, explain)
{}
QString Cat_state::string(Procinfo *p)
{
QString s(" ");
s[0] = p->state;
#ifdef SOLARIS
if(p->state == 'Z')
return s;
#endif
s[1] = (p->resident == 0 && p->state != 'Z') ? 'W' : ' ';
int ni = p->nice;
#ifdef SOLARIS
if(!Qps::normalize_nice)
ni -= NZERO;
#endif
s[2] = (ni > 0) ? 'N' : ((ni < 0) ? '<' : ' ');
return s;
}
Cat_policy::Cat_policy(const char *heading, const char *explain)
: Category(heading, explain)
{}
QString Cat_policy::string(Procinfo *p)
{
QString s;
#ifdef LINUX
switch(p->get_policy()) {
case SCHED_FIFO:
s = "FI"; break; // first in, first out
case SCHED_RR:
s = "RR"; break; // round-robin
case SCHED_OTHER:
s = "TS"; break; // time-sharing
default:
s = "??"; break;
}
#endif
#ifdef SOLARIS
s = " ";
s[0] = p->policy_name[0];
s[1] = p->policy_name[1];
#endif
return s;
}
int Cat_policy::compare(Procinfo *a, Procinfo *b)
{
#ifdef LINUX
return b->get_policy() - a->get_policy();
#endif
#ifdef SOLARIS
int r = b->policy_name[0] - a->policy_name[0];
return r ? r : b->policy_name[1] - a->policy_name[1];
#endif
}
Cat_rtprio::Cat_rtprio(const char *heading, const char *explain)
: Category(heading, explain)
{}
QString Cat_rtprio::string(Procinfo *p)
{
QString s;
s.setNum(p->get_rtprio());
return s;
}
int Cat_rtprio::compare(Procinfo *a, Procinfo *b)
{
return b->get_rtprio() - a->get_rtprio();
}
Cat_time::Cat_time(const char *heading, const char *explain)
: Category(heading, explain)
{}
QString Cat_time::string(Procinfo *p)
{
QString s;
int ticks = p->utime;
if(Qps::cumulative)
ticks += p->cutime;
int t = ticks / HZ; // seconds
if(t < 10) {
int hundreds = ticks / (HZ / 100) % 100;
s.sprintf("%1d.%02ds", t, hundreds);
} else if(t < 100 * 60) {
s.sprintf("%2d:%02d", t / 60, t % 60);
} else if(t < 100 * 3600) {
int h = t / 3600;
t %= 3600;
s.sprintf("%2d:%02dh", h, t / 60);
} else {
int d = t / 86400;
t %= 86400;
s.sprintf("%dd%dh", d, t / 3600);
}
return s;
}
int Cat_time::compare(Procinfo *a, Procinfo *b)
{
int at = a->utime, bt = b->utime;
if(Qps::cumulative) {
at += a->cutime;
bt += b->cutime;
}
return bt - at;
}
Cat_start::Cat_start(const char *heading, const char *explain)
: Category(heading, explain)
{}
QString Cat_start::string(Procinfo *p)
{
#ifdef SOLARIS
if(p->state == 'Z')
return "-"; // Solaris zombies have no valid start time
#endif
time_t start = p->boot_time + p->starttime / (unsigned)HZ;
QString s;
char *ct = ctime(&start);
if(p->tv.tv_sec - start < 86400) {
ct[16] = '\0';
s = ct + 11;
} else {
ct[10] = '\0';
s = ct + 4;
}
return s;
}
int Cat_start::compare(Procinfo *a, Procinfo *b)
{
unsigned long bs = b->starttime, as = a->starttime;
return bs >= as ? (bs == as ? 0 : 1) : -1;
}
Cat_percent::Cat_percent(const char *heading, const char *explain, int w,
float Procinfo::*member)
: Category(heading, explain), float_member(member), field_width(w)
{}
QString Cat_percent::string(Procinfo *p)
{
QString s;
s.sprintf("%01.2f", (double)(p->*float_member));
return s;
}
int Cat_percent::compare(Procinfo *a, Procinfo *b)
{
float at = a->*float_member, bt = b->*float_member;
return at < bt ? 1 : (at > bt ? -1 : 0);
}
Cat_tty::Cat_tty(const char *heading, const char *explain)
: Cat_string(heading, explain)
{}
QString Cat_tty::string(Procinfo *p)
{
return Ttystr::name(p->tty);
}
Proc::Proc()
{
// Note: When adding/removing/changing the fields, the save file
// version must be increased!
allcats.set(F_PID, new Cat_int("PID", "Process ID", 6, &Procinfo::pid));
allcats.set(F_PPID, new Cat_int("PPID", "Parent process ID", 6,
&Procinfo::ppid));
allcats.set(F_PGID, new Cat_int("PGID", "Process group ID", 6,
&Procinfo::pgrp));
allcats.set(F_SID, new Cat_int("SID", "Session ID", 6,
&Procinfo::session));
allcats.set(F_TTY, new Cat_tty("TTY", "Controlling tty"));
#ifdef LINUX
allcats.set(F_TPGID, new Cat_int("TPGID", "Process group ID of tty owner",
6, &Procinfo::tpgid));
#endif
allcats.set(F_USER, new Cat_user("USER",
"Owner (*=suid root, +=suid other user)"));
allcats.set(F_GROUP, new Cat_group("GROUP", "Group name (*=sgid other)"));
allcats.set(F_UID, new Cat_int("UID", "Real user ID", 6, &Procinfo::uid));
allcats.set(F_EUID, new Cat_int("EUID", "Effective user ID", 6,
&Procinfo::euid));
#ifdef LINUX
allcats.set(F_SUID, new Cat_int("SUID", "Saved user ID (Posix)", 6,
&Procinfo::suid));
allcats.set(F_FSUID, new Cat_int("FSUID", "File system user ID", 6,
&Procinfo::fsuid));
#endif
allcats.set(F_GID, new Cat_int("GID", "Real group ID", 6, &Procinfo::gid));
allcats.set(F_EGID, new Cat_int("EGID", "Effective group ID", 6,
&Procinfo::egid));
#ifdef LINUX
allcats.set(F_SGID, new Cat_int("SGID", "Saved group ID (Posix)", 6,
&Procinfo::sgid));
allcats.set(F_FSGID, new Cat_int("FSGID", "File system group ID", 6,
&Procinfo::fsgid));
#endif
allcats.set(F_PRI, new Cat_int("PRI", "Dynamic priority", 4,
&Procinfo::priority));
allcats.set(F_NICE, new Cat_int("NICE",
"Scheduling favour (higher -> less cpu time)",
4, &Procinfo::nice));
allcats.set(F_PLCY, new Cat_policy("PLCY",
"Scheduling policy"));
allcats.set(F_RPRI, new Cat_rtprio("RPRI",
"Realtime priority (0-99, more is better)"));
#ifdef SOLARIS
allcats.set(F_NLWP, new Cat_int("NLWP", "Number of threads in process",
5, &Procinfo::nthreads));
#endif
allcats.set(F_MAJFLT, new Cat_uintl("MAJFLT",
"Number of major faults (disk access)",
8, &Procinfo::majflt));
allcats.set(F_MINFLT, new Cat_uintl("MINFLT",
"Number of minor faults (no disk access)",
8, &Procinfo::minflt));
#ifdef LINUX
allcats.set(F_TRS, new Cat_uintl("TRS", "Text resident set size in Kbytes",
8, &Procinfo::trs));
allcats.set(F_DRS, new Cat_uintl("DRS", "Data resident set size in Kbytes",
8, &Procinfo::drs));
#endif
allcats.set(F_SIZE, new Cat_uintl("SIZE",
"Virtual image size of process in Kbytes",
8, &Procinfo::size));
allcats.set(F_SWAP, new Cat_swap("SWAP", "Kbytes on swap device"));
allcats.set(F_RSS, new Cat_uintl("RSS",
"Resident set size; Kbytes of program "
"in memory", 8, &Procinfo::resident));
#ifdef LINUX
allcats.set(F_SHARE, new Cat_uintl("SHARE", "Shared memory in Kbytes",
8, &Procinfo::share));
allcats.set(F_DT, new Cat_uintl("DT",
"Number of dirty (non-written) pages",
7, &Procinfo::dt));
#endif
allcats.set(F_STAT, new Cat_state("STAT", "State of the process"));
allcats.set(F_FLAGS, new Cat_hex("FLAGS", "Process flags (hex)", 9,
&Procinfo::flags));
allcats.set(F_WCHAN, new Cat_wchan("WCHAN",
"Kernel function where process is sleeping"));
allcats.set(F_WCPU, new Cat_percent("%WCPU",
"Weighted percentage of CPU (30 s average)",
6, &Procinfo::wcpu));
allcats.set(F_CPU, new Cat_percent("%CPU",
"Percentage of CPU used since last update",
6, &Procinfo::pcpu));
allcats.set(F_MEM, new Cat_percent("%MEM",
"Percentage of memory used (RSS/total mem)",
6, &Procinfo::pmem));
allcats.set(F_START, new Cat_start("START", "Time process started"));
allcats.set(F_TIME, new Cat_time("TIME",
"Total CPU time used since start"));
allcats.set(F_CPUNUM, new Cat_int("CPU", "CPU the process is executing on",
3, &Procinfo::which_cpu));
allcats.set(F_COMM, new Cat_string("COMM",
"Command that started the process",
&Procinfo::comm));
allcats.set(F_CWD, new Cat_dir("CWD", "Current working directory",
"cwd", &Procinfo::cwd));
allcats.set(F_ROOT, new Cat_dir("ROOT", "Root directory of process",
"root", &Procinfo::root));
allcats.set(F_CMDLINE, new Cat_cmdline("CMDLINE",
"Command line that started the process"));
for(int i = 0; i < allcats.size(); i++)
allcats[i]->index = i;
Procinfo::init_static();
}
void Proc::newproc(Procinfo *p)
{
Procinfo *oldp = procs[p->pid];
if(oldp) {
#ifdef LINUX
// calculate pcpu and wcpu from previous procinfo
int dt = (p->tv.tv_usec - oldp->tv.tv_usec) / (1000000 / HZ)
+ (p->tv.tv_sec - oldp->tv.tv_sec) * HZ;
int dcpu = p->utime - oldp->utime;
p->pcpu = 100.0 * dcpu / dt;
if(p->pcpu > 99.99) p->pcpu = 99.99;
#endif
const float a = Procview::avg_factor;
p->wcpu = a * oldp->wcpu + (1 - a) * p->pcpu;
// propagate some fields to new incarnation
p->selected = oldp->selected;
p->details = oldp->details;
p->hidekids = oldp->hidekids;
oldp->details = 0;
if(p->details)
p->details->set_procinfo(p);
#ifdef LINUX
if(Procinfo::num_cpus > 1) {
// SMP: see which processor was used the most
int best_cpu = -1;
unsigned long most = 0;
for(unsigned cpu = 0; cpu < Procinfo::num_cpus; cpu++) {
unsigned long delta =
p->per_cpu_times[cpu] - oldp->per_cpu_times[cpu];
if(delta > most) {
most = delta;
best_cpu = cpu;
}
// if no cpu time has been spent, use previous value
p->which_cpu = (best_cpu >= 0) ? best_cpu : oldp->which_cpu;
}
}
#endif
oldp->deref();
} else {
// New process
#ifdef LINUX
// %cpu first time = (cpu time since start) / (time since start)
int jiffies_since_boot = p->tv.tv_usec / (1000000 / HZ)
+ (p->tv.tv_sec - p->boot_time) * HZ;
int dt = jiffies_since_boot - p->starttime;
int dcpu = p->utime;
p->pcpu = 100.0 * dcpu / dt;
if(dt == 0 || p->pcpu > 99.99 || p->pcpu < 0)
p->pcpu = 0.0;
p->selected = FALSE;
#endif
p->wcpu = p->pcpu; // just a start
#ifdef LINUX
if(Procinfo::num_cpus > 1) {
// first tick: count times from 0
unsigned long most = 0;
for(unsigned cpu = 0; cpu < Procinfo::num_cpus; cpu++) {
unsigned long t = p->per_cpu_times[cpu];
if(t > most) {
most = t;
p->which_cpu = cpu;
}
}
}
#endif
}
procs.replace(p->pid, p);
if(procs.count() > procs.size())
procs.resize(procs.count() * 2 - 1);
}
// update the process list
void Proc::refresh()
{
static bool current_gen;
current_gen = !current_gen;
Procinfo::read_common();
DIR *d = opendir(procdir);
struct dirent *e;
while((e = readdir(d)) != 0) {
if(e->d_name[0] >= '0' && e->d_name[0] <= '9') {
Procinfo *pi = new Procinfo(atoi(e->d_name));
if(pi->pid == -1)
delete pi; // already gone
else {
pi->generation = current_gen;
newproc(pi);
}
}
}
closedir(d);
// remove Procinfos of nonexisting processes
for(QIntDictIterator<Procinfo> it(procs); it.current();) {
Procinfo *p = it.current();
if(p->generation != current_gen) {
procs.remove(p->pid);
p->deref();
} else
++it;
}
}
int Procview::user_fields[] = {F_PID, F_TTY, F_USER, F_NICE,
#ifdef SOLARIS
F_NLWP,
#endif
F_SIZE, F_RSS,
F_STAT, F_CPU, F_START, F_TIME,
F_CMDLINE, F_END};
int Procview::jobs_fields[] = {F_PID, F_PPID, F_PGID, F_SID, F_TTY,
#ifdef LINUX
F_TPGID,
#endif
F_STAT, F_UID, F_TIME, F_CMDLINE, F_END};
int Procview::mem_fields[] = {F_PID, F_TTY, F_MAJFLT, F_MINFLT,
#ifdef LINUX
F_TRS, F_DRS,
#endif
F_SIZE, F_SWAP, F_RSS,
#ifdef LINUX
F_SHARE, F_DT,
#endif
F_CMDLINE,
F_END};
float Procview::avg_factor = 1.0;
Procview::Procview(Proc *p)
: proc(p)
{
sortcat = p->allcats[F_WCPU];
reversed = FALSE;
viewproc = ALL;
viewfields = USER;
treeview = FALSE;
set_fields();
}
// read new process info
void Procview::refresh()
{
for(int i = 0; i < old_procs.size(); i++)
old_procs[i]->deref();
old_procs = procs;
procs.clear();
proc->refresh();
rebuild();
}
bool Procview::accept_proc(Procinfo *p)
{
static int my_uid = getuid();
return viewproc == ALL
|| viewproc == OWNED && p->uid == my_uid
|| viewproc == NROOT && p->uid != 0
|| viewproc == RUNNING && strchr("ORDW", p->state) != 0;
}
void Procview::build_tree()
{
if(root_procs.size() > 0) {
Procinfo *p;
for(QIntDictIterator<Procinfo> it(proc->procs); (p = it.current());
++it)
if(p->children)
p->children->clear();
root_procs.clear();
}
Procinfo *p;
for(QIntDictIterator<Procinfo> it(proc->procs); (p = it.current()); ++it) {
if(accept_proc(p)) {
Procinfo *parent = 0;
if(p->ppid && (parent = proc->procs[p->ppid])
&& accept_proc(parent)) {
if(!parent->children)
parent->children = new Svec<Procinfo *>(4);
parent->children->add(p);
} else
root_procs.add(p);
} else
p->selected = FALSE;
}
}
// re-sort the process info
void Procview::rebuild()
{
for(int i = 0; i < procs.size(); i++)
procs[i]->deref();
procs.clear();
if(treeview) {
build_tree();
parent_rows.clear();
linearize_tree(&root_procs, 0, -1);
} else {
for(QIntDictIterator<Procinfo> it(proc->procs); it.current(); ++it) {
Procinfo *p = it.current();
if(accept_proc(p))
procs.add(p->ref());
else
p->selected = FALSE;
}
static_sortcat = sortcat;
procs.sort(reversed ? compare_backwards : compare);
}
}
void Procview::linearize_tree(Svec<Procinfo *> *ps, int level, int prow)
{
static_sortcat = sortcat;
ps->sort(reversed ? compare_backwards : compare);
for(int i = 0; i < ps->size(); i++) {
Procinfo *p = (*ps)[i];
p->level = level;
p->lastchild = FALSE;
procs.add(p->ref());
parent_rows.add(prow);
if(p->children && !p->hidekids)
linearize_tree(p->children, level + 1, procs.size() - 1);
}
if(ps->size() > 0)
(*ps)[ps->size() - 1]->lastchild = TRUE;
}
void Procview::set_fields_list(int fields[])
{
cats.clear();
for(int i = 0; fields[i] != F_END; i++)
cats.add(proc->allcats[fields[i]]);
}
void Procview::set_fields()
{
switch(viewfields) {
case USER:
set_fields_list(user_fields);
break;
case JOBS:
set_fields_list(jobs_fields);
break;
case MEM:
set_fields_list(mem_fields);
break;
case CUSTOM:
;
}
}
// return the column number of a field, or -1 if not displayed
int Procview::findCol(int field)
{
for(int i = 0; i < cats.size(); i++)
if(cats[i] == proc->allcats[field])
return i;
return -1;
}
// add a category (last)
void Procview::add_cat(Category *c)
{
cats.add(c);
}
void Procview::remove_cat(int index)
{
cats.remove(index);
}
Category *Procview::static_sortcat = 0;
int Procview::compare(Procinfo *const *a, Procinfo *const *b)
{
int r = static_sortcat->compare(*a, *b);
return (r == 0) ? ((*a)->pid > (*b)->pid ? 1 : -1) : r;
}
int Procview::compare_backwards(Procinfo *const *a, Procinfo *const *b)
{
int r = static_sortcat->compare(*b, *a);
return (r == 0) ? ((*b)->pid > (*a)->pid ? 1 : -1) : r;
}
|