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 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
|
/*
* Copyright (C) 2011-2021 Canonical
* Copyright (C) 2021-2024 Colin Ian King
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* eventstat by Colin Ian King <colin.i.king@gmail.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <inttypes.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <math.h>
#include <float.h>
#include <ncurses.h>
#include <ctype.h>
#include <dirent.h>
#define TABLE_SIZE (1009) /* Should be a prime */
#define SIZEOF_ARRAY(a) (sizeof(a) / sizeof(a[0]))
#define OPT_QUIET (0x00000001)
#define OPT_CUMULATIVE (0x00000002)
#define OPT_CMD_SHORT (0x00000004)
#define OPT_CMD_LONG (0x00000008)
#define OPT_DIRNAME_STRIP (0x00000010)
#define OPT_SAMPLE_COUNT (0x00000020)
#define OPT_RESULT_STATS (0x00000040)
#define OPT_BRIEF (0x00000080)
#define OPT_KERNEL (0x00000100)
#define OPT_USER (0x00000200)
#define OPT_SHOW_WHENCE (0x00000400)
#define OPT_TOP (0x00000800)
#define OPT_TIMER_ID (0x00001000)
#define OPT_CMD (OPT_CMD_SHORT | OPT_CMD_LONG)
#define EVENT_BUF_SIZE (64 * 1024)
#define TIMER_REAP_AGE (600) /* Age of timer before it is reaped */
#define TIMER_REAP_THRESHOLD (30)
#define EVENTS_WIDTH (8)
#define TASK_WIDTH (15)
#define TIMER_ID_WIDTH (16)
#define FUNC_WIDTH (24)
#define FUNC_WIDTH_MAX (30)
#define _VER_(major, minor, patchlevel) \
((major * 10000) + (minor * 100) + patchlevel)
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
#if defined(__GNUC_PATCHLEVEL__)
#define NEED_GNUC(major, minor, patchlevel) \
_VER_(major, minor, patchlevel) <= \
_VER_(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
#else
#define NEED_GNUC(major, minor, patchlevel) \
_VER_(major, minor, patchlevel) <= _VER_(__GNUC__, __GNUC_MINOR__, 0)
#endif
#else
#define NEED_GNUC(major, minor, patchlevel) (0)
#endif
#if defined(__GNUC__) && NEED_GNUC(4,6,0)
#define HOT __attribute__ ((hot))
#else
#define HOT
#endif
#if defined(__GNUC__) && !defined(__clang__) && NEED_GNUC(4,6,0)
#define OPTIMIZE3 __attribute__((optimize("-O3")))
#else
#define OPTIMIZE3
#endif
#if defined(__GNUC__)
#define LIKELY(x) __builtin_expect((x),1)
#define UNLIKELY(x) __builtin_expect((x),0)
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif
#define FLOAT_TINY (0.0000001)
#define FLOAT_CMP(a, b) (fabs(a - b) < FLOAT_TINY)
/*
* timer_info_t contains per task timer infos.
*/
typedef struct timer_info {
struct timer_info *next; /* Next in list */
struct timer_info *hash_next; /* Next in hash list */
pid_t pid;
uint32_t ref_count; /* Timer stat reference count */
char *cmdline; /* From /proc/$pid/cmdline */
char *comm; /* from /proc/$pid/comm */
char *func; /* Kernel waiting func */
char *ident; /* Unique identity */
uint64_t timer; /* Timer ID */
uint64_t total_events; /* Total number of events */
uint64_t delta_events; /* Events in one time period */
uint16_t cpu_rt_prio; /* process priority level */
int16_t cpu_nice; /* process nice level */
uint32_t cpu_tasks; /* Number of tasks sharing ticks */
uint64_t cpu_ticks; /* CPU utilization ticks */
double cpu_ticks_time; /* CPU utilization ticks, last read */
double time_total; /* Total time */
double last_used; /* Last referenced */
double prev_used; /* Previous time used */
bool kernel_thread; /* True if task is a kernel thread */
bool just_added; /* True if recently added */
} timer_info_t;
typedef struct timer_stat {
struct timer_stat *next; /* Next timer stat in hash table */
struct timer_stat *sorted_freq_next; /* Next timer stat in event */
/* frequency sorted list */
timer_info_t *info; /* Timer info */
} timer_stat_t;
/* sample delta item as an element of the sample_delta_list_t */
typedef struct sample_delta_item {
struct sample_delta_item *next; /* next in list */
int64_t delta_events; /* delta in events */
double time_delta; /* difference in time between old */
/* and new */
timer_info_t *info; /* timer this refers to */
} sample_delta_item_t;
/* list of sample_delta_items */
typedef struct sample_delta_list {
struct sample_delta_list *next; /* next in list */
struct sample_delta_item *list; /* list of sample delta items */
double whence; /* when the sample was taken */
} sample_delta_list_t;
typedef struct {
char *task; /* Name of kernel task */
size_t len; /* Length */
} kernel_task_info;
#define KERN_TASK_INFO(str) { str, sizeof(str) - 1 }
static const char * const g_app_name = "eventstat";
static const char * const g_sys_tracing_enable =
"/sys/kernel/debug/tracing/events/timer/hrtimer_start/enable";
static const char * const g_sys_tracing_pipe =
"/sys/kernel/debug/tracing/trace_pipe";
static const char * const g_sys_tracing_set_event =
"/sys/kernel/debug/tracing/set_event";
static const char * const g_sys_tracing_filter =
"/sys/kernel/debug/tracing/events/timer/filter";
static void es_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
static timer_stat_t *g_timer_stat_free_list; /* free list of timer stats */
static timer_info_t *g_timer_info_list; /* cache list of timer_info */
static timer_info_t *g_timer_info_hash[TABLE_SIZE]; /* hash of timer_info */
/* head of list of samples, sorted in sample time order */
static sample_delta_list_t *g_sample_delta_list_head;
/* tail of list of samples, sorted in sample time order */
static sample_delta_list_t *g_sample_delta_list_tail;
/* ignore samples with event delta less than this */
static double g_opt_threshold;
static char *g_csv_results; /* results in comma separated values */
static uint32_t g_timer_info_list_length; /* length of timer_info_list */
static uint32_t g_opt_flags; /* option flags */
static volatile bool g_stop_eventstat = false; /* set by sighandler */
static bool g_sane_procs; /* false if we are in a container */
static bool g_resized; /* window resized */
static bool g_curses_init; /* curses initialised */
static int g_rows = 25; /* tty size, rows */
static int g_cols = 80; /* tty size, columns */
static uint64_t g_clock_tick_rate; /* system clock tick rate */
/*
* Attempt to catch a range of signals so
* we can clean
*/
static const int g_signals[] = {
/* POSIX.1-1990 */
#ifdef SIGHUP
SIGHUP,
#endif
#ifdef SIGINT
SIGINT,
#endif
#ifdef SIGQUIT
SIGQUIT,
#endif
#ifdef SIGFPE
SIGFPE,
#endif
#ifdef SIGTERM
SIGTERM,
#endif
#ifdef SIGUSR1
SIGUSR1,
#endif
#ifdef SIGUSR2
SIGUSR2,
/* POSIX.1-2001 */
#endif
#ifdef SIGXCPU
SIGXCPU,
#endif
#ifdef SIGXFSZ
SIGXFSZ,
#endif
/* Linux various */
#ifdef SIGIOT
SIGIOT,
#endif
#ifdef SIGSTKFLT
SIGSTKFLT,
#endif
#ifdef SIGPWR
SIGPWR,
#endif
#ifdef SIGINFO
SIGINFO,
#endif
#ifdef SIGVTALRM
SIGVTALRM,
#endif
};
/*
* pid_max_digits()
* determine (or guess) maximum digits of pids
*/
static int pid_max_digits(void)
{
static int max_digits;
ssize_t n;
int fd;
const int default_digits = 6;
const int min_digits = 5;
char buf[32];
if (LIKELY(max_digits))
goto ret;
max_digits = default_digits;
fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
if (fd < 0)
goto ret;
n = read(fd, buf, sizeof(buf) - 1);
(void)close(fd);
if (n < 0)
goto ret;
buf[n] = '\0';
max_digits = 0;
while ((max_digits < n) && (buf[max_digits] >= '0') && (buf[max_digits] <= '9'))
max_digits++;
if (max_digits < min_digits)
max_digits = min_digits;
ret:
return max_digits;
}
/*
* hash_djb2a()
* Hash a string, from Dan Bernstein comp.lang.c (xor version)
*/
static HOT OPTIMIZE3 uint32_t hash_djb2a(const char *str)
{
register uint32_t hash = 5381;
register int c;
while ((c = *str++)) {
/* (hash * 33) ^ c */
hash = ((hash << 5) + hash) ^ c;
hash = (hash * 33) ^ c;
}
return hash % TABLE_SIZE;
}
/*
* eventstat_winsize()
* get tty size
*/
static void eventstat_winsize(void)
{
struct winsize ws;
(void)memset(&ws, 0, sizeof(ws));
if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) {
g_rows = ws.ws_row;
g_cols = ws.ws_col;
}
}
/*
* eventstat_clear()
* clear screen if in top mode
*/
static inline void eventstat_clear(void)
{
if (g_curses_init)
(void)clear();
}
/*
* eventstat_refresh()
* refresh screen if in top mode
*/
static inline void eventstat_refresh(void)
{
if (g_curses_init)
(void)refresh();
}
/*
* eventstat_move()
* move cursor if in top mode
*/
static inline void eventstat_move(const int y, const int x)
{
if (g_curses_init)
(void)move(y, x);
}
/*
* eventstat_endwin()
* call endwin if in top mode
*/
static void eventstat_endwin(void)
{
if (g_curses_init) {
(void)clear();
(void)endwin();
}
}
/*
* err_abort()
* print an error and exit
*/
static void __attribute__ ((noreturn)) __attribute__((format(printf, 1, 2)))
err_abort(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
eventstat_endwin();
(void)vfprintf(stderr,fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
/*
* set_tracing()
* enable/disable timer stat
*/
static void set_tracing(const char *path, const char *str, const bool carp)
{
int fd;
const ssize_t len = (ssize_t)strlen(str);
if ((fd = open(path, O_WRONLY, S_IRUSR | S_IWUSR)) < 0) {
if (carp) {
err_abort("Cannot open %s, errno=%d (%s)\n",
path, errno, strerror(errno));
}
return;
}
if (write(fd, str, len) != len) {
(void)close(fd);
if (carp) {
err_abort("Cannot write to %s, errno=%d (%s)\n",
path, errno, strerror(errno));
}
return;
}
(void)close(fd);
}
/*
* set_tracing_enable()
* enable/disable timer stat
*/
static inline void set_tracing_enable(const char *str, const bool carp)
{
set_tracing(g_sys_tracing_enable, str, carp);
}
static void set_tracing_event(void)
{
char buffer[64];
set_tracing(g_sys_tracing_set_event, "\n", true);
set_tracing(g_sys_tracing_set_event, "hrtimer_start", true);
set_tracing(g_sys_tracing_filter, "0", true);
/* Ignore event stat and idle events */
(void)snprintf(buffer, sizeof(buffer),
"common_pid != %d && common_pid != 0", getpid());
set_tracing(g_sys_tracing_filter, buffer, true);
}
/*
* eventstat_exit()
* exit and set timer stat to 0
*/
static void __attribute__ ((noreturn)) eventstat_exit(const int status)
{
set_tracing_enable("0\n", false);
exit(status);
}
/*
* timeval_to_double
* timeval to a double (in seconds)
*/
static inline double timeval_to_double(const struct timeval *const tv)
{
return (double)tv->tv_sec + ((double)tv->tv_usec / 1000000.0);
}
/*
* double_to_timeval
* seconds in double to timeval
*/
static inline struct timeval double_to_timeval(const double val)
{
struct timeval tv;
tv.tv_sec = val;
tv.tv_usec = (val - (time_t)val) * 1000000.0;
return tv;
}
/*
* gettime_to_double()
* get time as a double
*/
static double gettime_to_double(void)
{
struct timeval tv;
if (UNLIKELY(gettimeofday(&tv, NULL) < 0))
err_abort("gettimeofday failed: errno=%d (%s)\n",
errno, strerror(errno));
return timeval_to_double(&tv);
}
/*
* sane_proc_pid_info()
* detect if proc info mapping from /proc/timer_stats
* maps to proc pids OK. If we are in a container or
* we can't tell, return false.
*/
static bool sane_proc_pid_info(void)
{
FILE *fp;
static const char pattern[] = "container=";
const char *ptr = pattern;
bool ret = true;
/* Fast check */
if (access("/run/systemd/container", R_OK) == 0)
return false;
fp = fopen("/proc/1/environ", "r");
if (!fp)
return false;
while (!feof(fp)) {
int ch = getc(fp);
if (*ptr == ch) {
ptr++;
/* Match? So we're inside a container */
if (*ptr == '\0') {
ret = false;
break;
}
} else {
/* No match, skip to end of var and restart scan */
do {
ch = getc(fp);
} while ((ch != EOF) && (ch != '\0'));
ptr = pattern;
}
}
(void)fclose(fp);
return ret;
}
/*
* handle_sig()
* catch signal, flag a stop and restore timer stat
*/
static void handle_sig(int dummy)
{
(void)dummy; /* Stop unused parameter warning with -Wextra */
g_stop_eventstat = true;
set_tracing_enable("0\n", false);
}
static uint32_t get_proc_cpu_tasks(const pid_t pid)
{
char path[PATH_MAX];
DIR *dir;
struct dirent *d;
uint32_t n = 0;
(void)snprintf(path, sizeof(path), "/proc/%d/task", pid);
dir = opendir(path);
if (!dir)
return 1;
while ((d = readdir(dir)) != NULL) {
if (d->d_name[0] != '.')
n++;
}
(void)closedir(dir);
/* We should always have at least 1 task! */
return (n < 1) ? 1 : n;
}
#define SKIP_FIELDS(n) \
skip = n; \
while (skip > 0 && *ptr) { \
if (*ptr == ' ') \
skip--; \
ptr++; \
} \
if (UNLIKELY(*ptr == '\0')) \
return -1; \
/*
* get_proc_cpu_ticks()
* get cpu ticks for a given pid
* unique tasks
*/
static int get_proc_cpu_ticks(
const pid_t pid,
uint64_t *ticks,
uint16_t *rt_prio,
int16_t *niceness)
{
char buffer[4096], path[PATH_MAX], *ptr = buffer, *endptr, *tmp;
int fd, skip;
uint64_t utime, stime;
ssize_t len;
(void)snprintf(path, sizeof(path), "/proc/%d/stat", pid);
*ticks = 0;
if ((fd = open(path, O_RDONLY)) < 0)
return -1;
len = read(fd, buffer, sizeof(buffer) - 1);
(void)close(fd);
if (UNLIKELY(len <= 1))
return -1;
buffer[len] = '\0';
/* 3173 (a.out) R 3093 3173 3093 34818 3173 4202496 165 0 0 0 3194 0 */
/*
* We used to use scanf but this is really expensive and it
* is far faster to parse the data via a more tedious means
* of scanning down the buffer manually..
*/
if ((pid_t)strtoul(ptr, &endptr, 10) != pid)
return -1;
if (endptr == ptr)
return -1;
ptr = endptr;
if (UNLIKELY(*ptr != ' '))
return -1;
ptr++;
if (UNLIKELY((*ptr != '(')))
return -1;
ptr++;
/* parse comm field */
for (tmp = ptr; *tmp; tmp++) {
if (*tmp == ')')
ptr = tmp;
}
ptr++;
if (UNLIKELY(*ptr != ' '))
return -1;
/* skip over state field */
ptr+=2 ;
/* Skip over fields to the 14th field (utime) */
SKIP_FIELDS(11)
/* Field 14, utime */
utime = strtoull(ptr, &endptr, 10);
if (UNLIKELY(endptr == ptr))
return -1;
ptr = endptr;
if (UNLIKELY(*ptr != ' '))
return -1;
ptr++;
/* Field 15, stime */
stime = strtoull(ptr, &endptr, 10);
if (UNLIKELY(endptr == ptr))
return -1;
/* Skip over fields to the 19th field (utime) */
SKIP_FIELDS(4)
/* Field 19, niceness */
*niceness = strtoul(ptr, &endptr, 10);
if (UNLIKELY(endptr == ptr))
return -1;
SKIP_FIELDS(21)
/* Field 40, rt_priority */
*rt_prio = strtol(ptr, &endptr, 10);
if (UNLIKELY(endptr == ptr))
return -1;
*ticks = utime + stime;
return 0;
}
/*
* samples_free()
* free collected samples
*/
static inline void samples_free(void)
{
sample_delta_list_t *sdl = g_sample_delta_list_head;
while (sdl) {
sample_delta_list_t *sdl_next = sdl->next;
sample_delta_item_t *sdi = sdl->list;
while (sdi) {
sample_delta_item_t *sdi_next = sdi->next;
free(sdi);
sdi = sdi_next;
}
free(sdl);
sdl = sdl_next;
}
}
/*
* sample_add()
* add a timer_stat's delta and info field to a
* list at time position whence
*/
static void sample_add(timer_stat_t *timer_stat, const double whence)
{
bool found = false;
sample_delta_list_t *sdl;
sample_delta_item_t *sdi;
if (g_csv_results == NULL) /* No need if not enabled */
return;
for (sdl = g_sample_delta_list_head; sdl; sdl = sdl->next) {
if (FLOAT_CMP(sdl->whence, whence)) {
found = true;
break;
}
}
/*
* New time period, need new sdl, we assume it goes at the end of the
* list since time is assumed to be increasing
*/
if (!found) {
sdl = calloc(1, sizeof(*sdl));
if (UNLIKELY(!sdl))
err_abort("Cannot allocate sample delta list\n");
sdl->whence = whence;
if (g_sample_delta_list_tail) {
g_sample_delta_list_tail->next = sdl;
g_sample_delta_list_tail = sdl;
} else {
g_sample_delta_list_head = sdl;
g_sample_delta_list_tail = sdl;
}
}
/* Now append the sdi onto the list */
sdi = calloc(1, sizeof(*sdi));
if (UNLIKELY(!sdi))
err_abort("Cannot allocate sample delta item\n");
sdi->delta_events = timer_stat->info->delta_events;
sdi->time_delta = timer_stat->info->last_used -
timer_stat->info->prev_used;
sdi->info = timer_stat->info;
sdi->next = sdl->list;
sdl->list = sdi;
}
/*
* sample_find()
* scan through a sample_delta_list for timer info,
* return NULL if not found
*/
inline HOT static sample_delta_item_t *sample_find(
sample_delta_list_t *sdl,
const timer_info_t *info)
{
sample_delta_item_t *sdi;
for (sdi = sdl->list; sdi; sdi = sdi->next) {
if (sdi->info == info)
return sdi;
}
return NULL;
}
/*
* info_compare_total()
* used by qsort to sort array in sample event total order
*/
static int info_compare_total(const void *item1, const void *item2)
{
timer_info_t *const *info1 = (timer_info_t *const *)item1;
timer_info_t *const *info2 = (timer_info_t *const *)item2;
if (UNLIKELY((*info2)->total_events == (*info1)->total_events))
return 0;
return ((*info2)->total_events > (*info1)->total_events) ? 1 : -1;
}
static bool pid_a_kernel_thread_guess(const char *task)
{
/*
* This is not exactly accurate, but if we can't look up
* a process then try and infer something from the comm field.
* Until we have better kernel support to map /proc/timer_stats
* pids to containerised pids this is the best we can do.
*/
static const kernel_task_info kernel_tasks[] = {
KERN_TASK_INFO("swapper/"),
KERN_TASK_INFO("kworker/"),
KERN_TASK_INFO("ksoftirqd/"),
KERN_TASK_INFO("watchdog/"),
KERN_TASK_INFO("migration/"),
KERN_TASK_INFO("irq/"),
KERN_TASK_INFO("mmcqd/"),
KERN_TASK_INFO("jbd2/"),
KERN_TASK_INFO("kthreadd"),
KERN_TASK_INFO("kthrotld"),
KERN_TASK_INFO("kswapd"),
KERN_TASK_INFO("ecryptfs-kthrea"),
KERN_TASK_INFO("kauditd"),
KERN_TASK_INFO("kblockd"),
KERN_TASK_INFO("kcryptd"),
KERN_TASK_INFO("kdevtmpfs"),
KERN_TASK_INFO("khelper"),
KERN_TASK_INFO("khubd"),
KERN_TASK_INFO("khugepaged"),
KERN_TASK_INFO("khungtaskd"),
KERN_TASK_INFO("flush-"),
KERN_TASK_INFO("bdi-default-"),
{ NULL, 0 }
};
size_t i;
for (i = 0; i < SIZEOF_ARRAY(kernel_tasks); i++) {
if (!strncmp(task, kernel_tasks[i].task, kernel_tasks[i].len))
return true;
}
return false;
}
/*
* pid_a_kernel_thread
*
*/
static bool pid_a_kernel_thread(const char *task, const pid_t id)
{
const pid_t pgid = (id == 0) ? 0 : getpgid(id);
/* We are either in a container, or with a task with a NULL cmdline */
if (LIKELY(g_sane_procs || (id >= 0)))
return (pgid == 0);
/* Can't get pgid on that pid, so make a guess */
return pid_a_kernel_thread_guess(task);
}
/*
* unknown_comm()
* return an unknonw comm field replacement
*/
static char *unknown_comm(void)
{
return strdup("<unknown>");
}
/*
* get_pid_comm
* get process /proc/pid/comm field info
*/
static char *get_pid_comm(const pid_t id, bool kernel_thread)
{
char buffer[256];
ssize_t ret;
char *comm, *ptr;
int fd;
(void)snprintf(buffer, sizeof(buffer), "/proc/%d/comm", id);
if ((fd = open(buffer, O_RDONLY)) < 0)
return unknown_comm();
ret = read(fd, buffer, sizeof(buffer));
(void)close(fd);
if (ret <= 0)
return unknown_comm();
buffer[sizeof(buffer)-1] = '\0';
for (ptr = buffer; *ptr; ptr++) {
if (*ptr == '\n') {
*ptr = '\0';
break;
}
}
if (kernel_thread) {
size_t len = strlen(buffer) + 3;
comm = malloc(len);
if (UNLIKELY(!comm))
return unknown_comm();
snprintf(comm, len, "[%s]", buffer);
} else {
comm = strdup(buffer);
if (!comm)
return unknown_comm();
}
return comm;
}
/*
* get_pid_cmdline
* get process /proc/pid/cmdline
*/
static char *get_pid_cmdline(const pid_t id)
{
char buffer[4096];
char *ptr;
int fd;
ssize_t ret;
(void)snprintf(buffer, sizeof(buffer), "/proc/%d/cmdline", id);
if ((fd = open(buffer, O_RDONLY)) < 0)
return NULL;
ret = read(fd, buffer, sizeof(buffer));
(void)close(fd);
if (ret < 0)
return NULL;
if (ret == 0)
return strdup("");
buffer[sizeof(buffer)-1] = '\0';
/*
* OPT_CMD_LONG option we get the full cmdline args
*/
if (g_opt_flags & OPT_CMD_LONG) {
for (ptr = buffer; ptr < buffer + ret - 1; ptr++) {
if (*ptr == '\0')
*ptr = ' ';
}
*ptr = '\0';
}
/*
* OPT_CMD_SHORT option we discard anything after a space
*/
if (g_opt_flags & OPT_CMD_SHORT) {
for (ptr = buffer; *ptr && (ptr < buffer + ret); ptr++) {
if (*ptr == ' ')
*ptr = '\0';
}
}
if (g_opt_flags & OPT_DIRNAME_STRIP) {
char *base = basename(buffer);
/* Should always be true */
if (base)
return strdup(base);
}
return strdup(buffer);
}
static inline double duration_round(const double duration)
{
return floor((duration * 100.0) + 0.5) / 100.0;
}
/*
* samples_dump()
* dump out collected sample information
*/
static void samples_dump(const char *filename)
{
timer_info_t **sorted_timer_infos;
size_t i, n;
FILE *fp;
uint64_t count = 0;
double first_time = -1.0;
timer_info_t *info;
sample_delta_list_t *sdl;
if (filename == NULL)
return;
if ((fp = fopen(filename, "w")) == NULL) {
(void)fprintf(stderr, "Cannot write to file %s\n", filename);
return;
}
sorted_timer_infos = calloc(g_timer_info_list_length,
sizeof(*sorted_timer_infos));
if (UNLIKELY(!sorted_timer_infos))
err_abort("Cannot allocate buffer for sorting timer_infos\n");
/* Just want the timers with some non-zero total */
for (n = 0, info = g_timer_info_list; info; info = info->next) {
if (info->total_events > 0)
sorted_timer_infos[n++] = info;
}
qsort(sorted_timer_infos, n,
sizeof(timer_info_t *), info_compare_total);
fprintf(fp, "Time:,Task:");
for (i = 0; i < n; i++) {
char *task;
if (g_opt_flags & OPT_CMD)
task = sorted_timer_infos[i]->cmdline;
else
task = sorted_timer_infos[i]->comm;
(void)fprintf(fp, ",%s", task);
}
(void)fprintf(fp, "\n");
(void)fprintf(fp, ",Kernel Init Function:");
for (i = 0; i < n; i++)
(void)fprintf(fp, ",%s", sorted_timer_infos[i]->func);
(void)fprintf(fp, "\n");
(void)fprintf(fp, ",Total:");
for (i = 0; i < n; i++)
(void)fprintf(fp, ",%" PRIu64,
sorted_timer_infos[i]->total_events);
(void)fprintf(fp, "\n");
for (sdl = g_sample_delta_list_head; sdl; sdl = sdl->next) {
time_t t = (time_t)sdl->whence;
struct tm *tm;
count++;
tm = localtime(&t);
if (tm) {
(void)fprintf(fp, "%2.2d:%2.2d:%2.2d",
tm->tm_hour, tm->tm_min, tm->tm_sec);
} else {
(void)fprintf(fp, "--:--:--");
}
if (first_time < 0)
first_time = sdl->whence;
(void)fprintf(fp, ",%f",
duration_round(sdl->whence - first_time));
/*
* Scan in timer info order to be consistent for all sdl rows
*/
for (i = 0; i < n; i++) {
sample_delta_item_t *sdi =
sample_find(sdl, sorted_timer_infos[i]);
/*
* duration - if -C option is used then don't scale
* by the per sample duration time, instead give the
* raw sample count by scaling by 1.0 (i.e. no scaling)
*/
if (sdi) {
double duration = duration_round((g_opt_flags & OPT_SAMPLE_COUNT) ? 1.0 : sdi->time_delta);
(void)fprintf(fp, ",%f",
FLOAT_CMP(duration, 0.0) ? -99.99 :
(double)sdi->delta_events / duration);
} else
(void)fprintf(fp, ",%f", 0.0);
}
(void)fprintf(fp, "\n");
}
/*
* -S option - some statistics, min, max, average, std.dev.
*/
if (g_opt_flags & OPT_RESULT_STATS) {
(void)fprintf(fp, ",Min:");
for (i = 0; i < n; i++) {
double min = DBL_MAX;
for (sdl = g_sample_delta_list_head; sdl; sdl = sdl->next) {
sample_delta_item_t *sdi =
sample_find(sdl, sorted_timer_infos[i]);
if (sdi) {
double duration = duration_round((g_opt_flags & OPT_SAMPLE_COUNT) ? 1.0 : sdi->time_delta);
double val = FLOAT_CMP(duration, 0.0) ?
0.0 : sdi->delta_events / duration;
if (min > val)
min = val;
}
}
(void)fprintf(fp, ",%f", min);
}
(void)fprintf(fp, "\n");
(void)fprintf(fp, ",Max:");
for (i = 0; i < n; i++) {
double max = DBL_MIN;
for (sdl = g_sample_delta_list_head; sdl; sdl = sdl->next) {
sample_delta_item_t *sdi =
sample_find(sdl, sorted_timer_infos[i]);
if (sdi) {
double duration = duration_round((g_opt_flags & OPT_SAMPLE_COUNT) ? 1.0 : sdi->time_delta);
double val = FLOAT_CMP(duration, 0.0) ?
0.0 : sdi->delta_events / duration;
if (max < val)
max = val;
}
}
(void)fprintf(fp, ",%f", max);
}
(void)fprintf(fp, "\n");
(void)fprintf(fp, ",Average:");
for (i = 0; i < n; i++)
(void)fprintf(fp, ",%f", count == 0 ? 0.0 :
(double)sorted_timer_infos[i]->total_events / count);
(void)fprintf(fp, "\n");
/*
* population standard deviation
*/
(void)fprintf(fp, ",Std.Dev.:");
for (i = 0; i < n; i++) {
double average = (double)
sorted_timer_infos[i]->total_events / (double)count;
double sum = 0.0;
for (sdl = g_sample_delta_list_head; sdl; sdl = sdl->next) {
sample_delta_item_t *sdi =
sample_find(sdl, sorted_timer_infos[i]);
if (sdi) {
double duration = duration_round((g_opt_flags & OPT_SAMPLE_COUNT) ? 1.0 : sdi->time_delta);
double diff = FLOAT_CMP(duration, 0.0) ? 0.0 :
((double)sdi->delta_events - average) / duration;
diff = diff * diff;
sum += diff;
}
}
sum = sum / (double)count;
(void)fprintf(fp, ",%f", sqrt(sum));
}
(void)fprintf(fp, "\n");
}
free(sorted_timer_infos);
(void)fclose(fp);
}
/*
* timer_info_find()
* try to find existing timer info in cache, and to the cache
* if it is new.
*/
static HOT timer_info_t *timer_info_find(
const timer_info_t *new_info,
const char *ident,
const double time_now,
const double duration)
{
timer_info_t *info;
const uint32_t h = hash_djb2a(ident);
for (info = g_timer_info_hash[h]; info; info = info->hash_next) {
if (UNLIKELY(strcmp(ident, info->ident) == 0)) {
info->prev_used = info->last_used;
info->last_used = time_now;
return info;
}
}
info = calloc(1, sizeof(*info));
if (UNLIKELY(!info))
err_abort("Cannot allocate timer info\n");
info->pid = new_info->pid;
info->comm = new_info->comm ? strdup(new_info->comm) : unknown_comm();
info->cmdline = strdup(new_info->cmdline);
info->func = strdup(new_info->func);
info->ident = strdup(ident);
info->kernel_thread = new_info->kernel_thread;
info->time_total = new_info->time_total;
info->timer = new_info->timer;
info->ref_count = 0;
info->prev_used = time_now - duration; /* Fake previous time */
info->last_used = time_now;
info->total_events = 1;
info->delta_events = 1;
get_proc_cpu_ticks(info->pid, &info->cpu_ticks, &info->cpu_rt_prio, &info->cpu_nice);
info->cpu_ticks_time = time_now;
if (UNLIKELY(info->comm == NULL ||
info->cmdline == NULL ||
info->func == NULL ||
info->ident == NULL)) {
err_abort("Out of memory allocating a timer stat fields\n");
}
/* Does not exist in list, append it */
info->next = g_timer_info_list;
g_timer_info_list = info;
g_timer_info_list_length++;
info->hash_next = g_timer_info_hash[h];
g_timer_info_hash[h] = info;
return info;
}
/*
* timer_info_free()
* free up timer_info
*/
static void timer_info_free(void *data)
{
timer_info_t *info = (timer_info_t*)data;
free(info->comm);
free(info->cmdline);
free(info->func);
free(info->ident);
free(info);
}
/*
* timer_info_purge_old_from_timer_list()
* remove old timer info from the timer list
*/
static void timer_info_purge_old_from_timer_list(
timer_info_t **list,
const double time_now)
{
timer_info_t *prev = NULL, *info = *list;
while (info) {
timer_info_t *next = info->next;
/*
* Only remove from list once all timer
* stats no longer reference it
*/
if ((info->ref_count == 0) &&
(info->last_used + TIMER_REAP_AGE < time_now)) {
if (prev == NULL)
*list = next;
else
prev->next = next;
g_timer_info_list_length--;
} else {
prev = info;
}
info = next;
}
}
/*
* timer_info_purge_old_from_hash_list()
* remove old timer info from a hash list
*/
static void timer_info_purge_old_from_hash_list(
timer_info_t **list,
const double time_now)
{
timer_info_t *prev = NULL, *info = *list;
while (info) {
timer_info_t *next = info->hash_next;
/*
* Only remove and free once all timer stats no
* longer reference it
*/
if ((info->ref_count == 0) &&
(info->last_used + TIMER_REAP_AGE < time_now)) {
if (prev == NULL)
*list = next;
else
prev->hash_next = next;
timer_info_free(info);
} else {
prev = info;
}
info = next;
}
}
/*
* timer_info_purge_old()
* clean out old timer infos
*/
static inline void timer_info_purge_old(const double time_now)
{
static uint16_t count = 0;
count++;
if (count > TIMER_REAP_THRESHOLD) {
size_t i;
count = 0;
timer_info_purge_old_from_timer_list(&g_timer_info_list,
time_now);
for (i = 0; i < TABLE_SIZE; i++)
timer_info_purge_old_from_hash_list(&g_timer_info_hash[i], time_now);
}
}
/*
* timer_info_list_free()
* free up all unique timer infos
*/
static inline void timer_info_list_free(void)
{
timer_info_t *info = g_timer_info_list;
/* Free list and timers on list */
while (info) {
timer_info_t *next = info->next;
timer_info_free(info);
info = next;
}
}
/*
* make_hash_ident()
*/
static char *make_hash_ident(const timer_info_t *info)
{
static char ident[128];
if (g_opt_flags & OPT_TIMER_ID) {
(void)snprintf(ident, sizeof(ident), "%x%s%8.8s%" PRIx64,
info->pid, info->comm, info->func, info->timer);
} else {
(void)snprintf(ident, sizeof(ident), "%x%s%8.8s",
info->pid, info->comm, info->func);
}
return ident;
}
/*
* timer_stat_free_list_free()
* free up the timer stat free list
*/
static void timer_stat_free_list_free(void)
{
timer_stat_t *ts = g_timer_stat_free_list;
while (ts) {
timer_stat_t *next = ts->next;
free(ts);
ts = next;
}
g_timer_stat_free_list = NULL;
}
/*
* timer_stat_free_contents()
* Free timers from a hash table
*/
static void timer_stat_free_contents(timer_stat_t *timer_stats[])
{
size_t i;
for (i = 0; i < TABLE_SIZE; i++) {
timer_stat_t *ts = timer_stats[i];
while (ts) {
timer_stat_t *next = ts->next;
/* Decrement info ref count */
ts->info->ref_count--;
/* Add it onto the timer stat free list */
ts->next = g_timer_stat_free_list;
g_timer_stat_free_list = ts;
ts = next;
}
timer_stats[i] = NULL;
}
}
/*
* timer_stat_add()
* add timer stats to a hash table if it is new, otherwise just
* accumulate the event count.
*/
static void timer_stat_add(
timer_stat_t *timer_stats[], /* timer stat hash table */
timer_info_t *info, /* timer info to be added */
const double time_now, /* time sample was taken */
const double duration) /* duration of a sample */
{
const char *ident = make_hash_ident(info);
const uint32_t h = hash_djb2a(ident);
timer_stat_t *ts, *ts_new;
for (ts = timer_stats[h]; ts; ts = ts->next) {
if (UNLIKELY(strcmp(ts->info->ident, ident) == 0)) {
ts->info->total_events++;
ts->info->delta_events++;
sample_add(ts, time_now);
return;
}
}
/* Not found, it is new */
if (g_timer_stat_free_list) {
/* Get new one from free list */
ts_new = g_timer_stat_free_list;
g_timer_stat_free_list = g_timer_stat_free_list->next;
} else {
/* Get one from heap */
ts_new = malloc(sizeof(*ts_new));
if (UNLIKELY(!ts_new))
err_abort("Out of memory allocating a timer stat\n");
}
ts_new->info = timer_info_find(info, ident, time_now, duration);
ts_new->info->ref_count++;
ts_new->next = timer_stats[h];
ts_new->sorted_freq_next = NULL;
timer_stats[h] = ts_new;
sample_add(ts_new, time_now);
}
/*
* timer_stat_sort_freq_add()
* add a timer stat to a sorted list of timer stats
*/
static void timer_stat_sort_freq_add(
timer_stat_t **sorted, /* timer stat sorted list */
timer_stat_t *new) /* timer stat to add */
{
while (*sorted) {
if (UNLIKELY(g_opt_flags & OPT_CUMULATIVE)) {
if ((*sorted)->info->total_events < new->info->total_events) {
new->sorted_freq_next = *(sorted);
break;
}
} else {
if ((*sorted)->info->delta_events < new->info->delta_events) {
new->sorted_freq_next = *(sorted);
break;
}
}
sorted = &(*sorted)->sorted_freq_next;
}
*sorted = new;
}
/*
* es_printf()
* eventstat printf - print to stdout or ncurses
* print depending on the mode
*/
static void es_printf(const char *fmt, ...)
{
va_list ap;
static int col = 0;
int n;
char buf[256], *eol;
va_start(ap, fmt);
n = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
eol = strchr(buf, '\n');
if (n + col >= g_cols) {
n = g_cols - col;
buf[n > 0 ? n : 0] = '\0';
}
(void)(g_curses_init ? printw : printf)("%s", buf);
col = eol ? 0 : col + n;
}
/*
* timer_stat_dump()
*/
static OPTIMIZE3 void timer_stat_dump(
const double duration, /* time between each sample */
const double time_delta, /* how long been running sofar */
const int32_t n_lines, /* number of lines to output */
const double whence, /* nth sample */
timer_stat_t *timer_stats[]) /* timer stats samples */
{
size_t i;
timer_stat_t *sorted = NULL;
double now = gettime_to_double();
for (i = 0; i < TABLE_SIZE; i++) {
timer_stat_t *ts;
for (ts = timer_stats[i]; ts; ts = ts->next)
timer_stat_sort_freq_add(&sorted, ts);
}
if (!(g_opt_flags & OPT_QUIET)) {
uint64_t total = 0UL, kt_total = 0UL;
int32_t j = 0;
const int pid_size = pid_max_digits();
int cols, sz, ta_size, fn_size = 0;
int min_width;
eventstat_winsize();
if (UNLIKELY(g_resized && g_curses_init)) {
(void)resizeterm(g_rows, g_cols);
(void)refresh();
g_resized = false;
}
/* Minimum width w/o task or func info */
min_width = EVENTS_WIDTH + 1 + \
1 + \
pid_size + 1 + 6 + 4 + 4;
if (!(g_opt_flags & OPT_BRIEF)) {
if (g_opt_flags & OPT_TIMER_ID)
min_width += TIMER_ID_WIDTH + 1;
fn_size = FUNC_WIDTH;
}
if (!(g_opt_flags & OPT_CMD_LONG) && g_cols > 80)
cols = 80;
else
cols = g_cols;
sz = cols - min_width;
sz = (sz < 0) ? 0 : sz;
if (fn_size) {
fn_size += (sz >> 1);
if (fn_size > FUNC_WIDTH_MAX)
fn_size = FUNC_WIDTH_MAX;
min_width += fn_size;
sz = cols - min_width;
sz = (sz < 0) ? 0 : sz;
}
ta_size = sz;
if (ta_size < TASK_WIDTH)
ta_size = TASK_WIDTH;
if (g_opt_flags & OPT_BRIEF) {
es_printf("%*.*s %-*.*s %-*.*s",
EVENTS_WIDTH, EVENTS_WIDTH,
(g_opt_flags & OPT_CUMULATIVE) ?
"Events" : "Event/s",
pid_size, pid_size, "PID",
ta_size, ta_size, "Task");
} else {
es_printf("%*.*s %-*.*s %5s %3.3s %3.3s %-*.*s",
EVENTS_WIDTH, EVENTS_WIDTH,
(g_opt_flags & OPT_CUMULATIVE) ?
"Events" : "Event/s",
pid_size, pid_size, "PID",
"%CPU", "PR", "NI",
ta_size, ta_size, "Task");
if (g_opt_flags & OPT_TIMER_ID) {
es_printf(" %16.16s", "Timer ID");
}
es_printf("%-*.*s", fn_size, fn_size,
" Kernel Init Function");
}
es_printf("\n");
while (sorted) {
uint64_t events;
if (g_opt_flags & OPT_CUMULATIVE)
events = sorted->info->total_events;
else
events = sorted->info->delta_events;
if (((n_lines == -1) || (j < n_lines)) && (events != 0)) {
char *task = (g_opt_flags & OPT_CMD) ?
sorted->info->cmdline :
sorted->info->comm;
j++;
if (g_opt_flags & OPT_CUMULATIVE) {
es_printf("%*" PRIu64 " ",
EVENTS_WIDTH, events);
} else {
es_printf("%*.2f ",
EVENTS_WIDTH,
(duration > 0.0) ? (double)events / duration : 0.0);
}
if (g_opt_flags & OPT_BRIEF) {
es_printf("%*d %s\n",
pid_size, sorted->info->pid,
task);
} else {
double tick_time = now - sorted->info->cpu_ticks_time;
uint32_t tasks = get_proc_cpu_tasks(sorted->info->pid);
uint64_t cpu_ticks;
uint16_t cpu_rt_prio;
int16_t cpu_nice;
double cpu;
get_proc_cpu_ticks(sorted->info->pid, &cpu_ticks,
&cpu_rt_prio, &cpu_nice);
if (cpu_ticks && sorted->info->cpu_ticks) {
uint64_t ticks = cpu_ticks - sorted->info->cpu_ticks;
cpu = (100.0 * (double)ticks) / (tick_time * (double)g_clock_tick_rate);
} else {
cpu = 0.0;
}
sorted->info->cpu_ticks = cpu_ticks;
sorted->info->cpu_ticks_time = now;
es_printf("%*d %5.1f %3d %3d %-*.*s",
pid_size, sorted->info->pid,
cpu / (double)tasks,
sorted->info->cpu_rt_prio,
sorted->info->cpu_nice,
ta_size, ta_size, task);
if (g_opt_flags & OPT_TIMER_ID) {
es_printf(" %16" PRIx64,
sorted->info->timer);
}
es_printf(" %-*.*s\n",
fn_size - 1, fn_size - 1,
sorted->info->func);
}
}
total += sorted->info->delta_events;
if (sorted->info->kernel_thread)
kt_total += sorted->info->delta_events;
sorted->info->delta_events = 0;
sorted = sorted->sorted_freq_next;
}
eventstat_move(LINES - 1, 0);
es_printf("%" PRIu64 " Total events, %5.2f events/sec "
"(kernel: %5.2f, userspace: %5.2f)\n",
total,
(duration > 0.0) ? (double)total / duration : 0.0,
(duration > 0.0) ? (double)kt_total / duration : 0.0,
(duration > 0.0) ? (double)(total - kt_total) / duration : 0.0);
if ((g_opt_flags & OPT_SHOW_WHENCE) && !g_curses_init) {
time_t t = (time_t)whence;
char *timestr = ctime(&t);
char *pos;
if (timestr) {
pos = strchr(timestr, '\n');
} else {
pos = "<unknown>";
}
if (pos)
*pos = '\0';
es_printf("Timestamp: %s, Total Run Duration: "
"%.1f secs\n", timestr, time_delta);
}
if (!g_sane_procs)
es_printf("Note: this was run inside a container, "
"kernel tasks were guessed.\n");
es_printf("\n");
}
}
/*
* read_events()
* read in events data into a global read buffer.
* the buffer is auto-expanded where necessary and
* only free'd at exit time. This way we can parse
* the data a little faster.
*/
static char *read_events(const double time_end, char **get_events_buf)
{
int fd;
static size_t get_events_size;
size_t size;
if (UNLIKELY(*get_events_buf == NULL)) {
*get_events_buf = calloc(EVENT_BUF_SIZE << 1, sizeof(char));
if (UNLIKELY(!*get_events_buf)) {
err_abort("Cannot read %s, out of memory\n",
g_sys_tracing_pipe);
}
get_events_size = (EVENT_BUF_SIZE << 1);
}
if ((fd = open(g_sys_tracing_pipe, O_RDONLY)) < 0)
err_abort("Cannot open %s\n", g_sys_tracing_pipe);
size = 0;
while (!g_stop_eventstat) {
ssize_t ret;
int rc;
static char buffer[EVENT_BUF_SIZE];
const double duration = time_end - gettime_to_double();
struct timeval tv;
fd_set rfds;
if (UNLIKELY(duration < 0.0))
break;
tv = double_to_timeval(duration);
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
errno = 0;
rc = select(fd + 1, &rfds, NULL, NULL, &tv);
if (UNLIKELY(rc <= 0)) {
if (errno == EINTR)
continue;
break;
}
if (!FD_ISSET(fd, &rfds))
continue;
ret = read(fd, buffer, sizeof(buffer));
if (ret == 0)
continue;
if (UNLIKELY(ret < 0)) {
if (!g_stop_eventstat &&
((errno == EINTR) ||
(errno != EAGAIN))) {
continue;
}
break;
}
/* Do we need to expand the global buffer? */
if (size + ret >= get_events_size) {
char *tmpptr;
get_events_size += (EVENT_BUF_SIZE << 1);
tmpptr = realloc(*get_events_buf, get_events_size + 1);
if (UNLIKELY(!tmpptr)) {
(void)close(fd);
err_abort("Cannot read %s, out of memory\n",
g_sys_tracing_pipe);
}
*get_events_buf = tmpptr;
}
(void)memcpy((*get_events_buf) + size, buffer, ret);
size += ret;
*(*get_events_buf + size) = '\0';
}
(void)close(fd);
return *get_events_buf;
}
/*
* get_events()
* parse /sys/kernel/debug/tracing/trace_pipe and populate
* a timer stat hash table with unique events
*/
static void get_events(
timer_stat_t *timer_stats[],
char **get_events_buf,
const double time_now,
const double duration)
{
const size_t app_name_len = strlen(g_app_name);
const double time_end = time_now + duration - 0.05;
char *tmpptr;
timer_info_t *pinfo;
tmpptr = read_events(time_end, get_events_buf);
if (!tmpptr)
return;
if (g_opt_flags & OPT_CUMULATIVE) {
for (pinfo = g_timer_info_list; pinfo; pinfo = pinfo->next)
pinfo->just_added = false;
}
while (*tmpptr) {
char *ptr, *eol = tmpptr;
char func[64], task[64];
char *cmdline, *comm;
int mask;
timer_info_t info;
/* Find the end of a line */
while (*eol) {
if (UNLIKELY(*eol == '\n')) {
*eol = '\0';
eol++;
break;
}
eol++;
}
if (strstr(tmpptr, "hrtimer_start")) {
(void)memset(task, 0, sizeof(task));
(void)memset(&info, 0, sizeof(info));
(void)memset(func, 0, sizeof(func));
/*
* Parse something like the following:
* gnome-shell-3515 [003] d.h. 101499.108349: hrtimer_start: hrtimer=ffff99979e2d4600 function=tick_sched_timer expires=101497144000000 softexpires=101497144000000
*/
if (sscanf(tmpptr, "%63s %*s %*s %*f: hrtimer_start: hrtimer=%" PRIx64 " function=%63s", task, &info.timer, func) != 3)
goto next;
} else {
goto next;
}
/*
* task name in form: gnome-shell-3515, scan to end of
* string, then scan back to find start of PID
*/
ptr = task;
while (*ptr)
ptr++;
ptr--;
while (ptr >= task && (*ptr >= '0' && *ptr <= '9'))
ptr--;
*ptr = '\0';
ptr++;
if (sscanf(ptr, "%10d\n", &info.pid) != 1)
goto next;
if (UNLIKELY(info.pid == 0))
goto next;
/* Processes without a command line are kernel threads */
cmdline = get_pid_cmdline(info.pid);
info.kernel_thread = pid_a_kernel_thread(task, info.pid);
comm = get_pid_comm(info.pid, info.kernel_thread);
if (UNLIKELY(!comm))
goto free_next;
/* Swapper is special, like all corner cases */
if (UNLIKELY(strncmp(task, "swapper", 6) == 0))
info.kernel_thread = true;
mask = info.kernel_thread ? OPT_KERNEL : OPT_USER;
if (!(g_opt_flags & mask))
goto free_next;
if (strncmp(task, g_app_name, app_name_len)) {
info.cmdline = cmdline ? cmdline : comm;
info.comm = comm;
info.func = func;
info.time_total = 0.0;
info.total_events = 1;
info.ident = make_hash_ident(&info);
info.just_added = true;
timer_stat_add(timer_stats, &info, time_now, duration);
}
free_next:
free(cmdline);
free(comm);
next:
tmpptr = eol;
}
if (g_opt_flags & OPT_CUMULATIVE) {
for (pinfo = g_timer_info_list; pinfo; pinfo = pinfo->next) {
if (!pinfo->just_added)
timer_stat_add(timer_stats, pinfo, time_now, duration);
}
}
}
/*
* show_usage()
* show how to use
*/
static void show_usage(void)
{
(void)printf("%s, version %s\n\n", g_app_name, VERSION);
(void)printf("Usage: %s [options] [duration] [count]\n", g_app_name);
(void)printf("Options are:\n"
" -c\t\treport cumulative events rather than events per second.\n"
" -C\t\treport event count rather than event per second in CSV output.\n"
" -d\t\tremove pathname from long process name in CSV output.\n"
" -h\t\tprint this help.\n"
" -l\t\tuse long cmdline text from /proc/pid/cmdline for process name.\n"
" -n events\tspecifies number of events to display.\n"
" -q\t\trun quietly, useful with option -r.\n"
" -r filename\tspecifies a comma separated values (CSV) output file\n"
"\t\tto dump samples into.\n"
" -s\t\tuse short process name from /proc/pid/cmdline for process name.\n"
" -S\t\tcalculate min, max, average and standard deviation in CSV\n"
"\t\toutput.\n"
" -t threshold\tsamples less than the specified threshold are ignored.\n"
" -T\t\tenable \'top\' mode rather than a scrolling output.\n"
" -w\t\tadd time stamp (when events occurred) to output.\n");
}
/*
* handle_sigwinch()
* flag window resize on SIGWINCH
*/
static void handle_sigwinch(int sig)
{
(void)sig;
eventstat_winsize();
g_resized = true;
}
int main(int argc, char **argv)
{
timer_stat_t **timer_stats;
char *get_events_buf = NULL;
double duration_secs = 1.0, time_start, time_now;
int64_t count = 1, t = 1;
int32_t n_lines = -1;
bool forever = true;
bool redo = false;
struct sigaction new_action;
size_t i;
for (;;) {
int c = getopt(argc, argv, "bcCdksSlhin:qr:t:Tuw");
if (c == -1)
break;
switch (c) {
case 'b':
g_opt_flags |= OPT_BRIEF;
break;
case 'c':
g_opt_flags |= OPT_CUMULATIVE;
break;
case 'C':
g_opt_flags |= OPT_SAMPLE_COUNT;
break;
case 'd':
g_opt_flags |= OPT_DIRNAME_STRIP;
break;
case 'h':
show_usage();
eventstat_exit(EXIT_SUCCESS);
case 'i':
g_opt_flags |= OPT_TIMER_ID;
break;
case 'n':
errno = 0;
n_lines = (int32_t)strtol(optarg, NULL, 10);
if (errno)
err_abort("Invalid value for number "
"of events to display\n");
if (n_lines < 1)
err_abort("-n option must be greater than 0\n");
break;
case 'S':
g_opt_flags |= OPT_RESULT_STATS;
break;
case 't':
g_opt_threshold = strtoull(optarg, NULL, 10);
if (g_opt_threshold < 1)
err_abort("-t threshold must be 1 or more.\n");
break;
case 'T':
g_opt_flags |= OPT_TOP;
break;
case 'q':
g_opt_flags |= OPT_QUIET;
break;
case 'r':
g_csv_results = optarg;
break;
case 's':
g_opt_flags |= OPT_CMD_SHORT;
break;
case 'l':
g_opt_flags |= OPT_CMD_LONG;
break;
case 'k':
g_opt_flags |= OPT_KERNEL;
break;
case 'u':
g_opt_flags |= OPT_USER;
break;
case 'w':
g_opt_flags |= OPT_SHOW_WHENCE;
break;
default:
show_usage();
eventstat_exit(EXIT_FAILURE);
}
}
if (!(g_opt_flags & (OPT_KERNEL | OPT_USER)))
g_opt_flags |= (OPT_KERNEL | OPT_USER);
if (optind < argc) {
duration_secs = atof(argv[optind++]);
if (duration_secs < 0.5)
err_abort("Duration must 0.5 or more.\n");
}
if (optind < argc) {
forever = false;
errno = 0;
count = (int64_t)strtoll(argv[optind++], NULL, 10);
if (errno)
err_abort("Invalid count value\n");
if (count < 1)
err_abort("Count must be > 0\n");
}
g_opt_threshold *= duration_secs;
if (geteuid() != 0)
err_abort("%s requires root privileges to gather "
"trace event data\n", g_app_name);
g_sane_procs = sane_proc_pid_info();
if (!g_sane_procs)
g_opt_flags &= ~(OPT_CMD_SHORT | OPT_CMD_LONG);
(void)memset(&new_action, 0, sizeof(new_action));
for (i = 0; i < SIZEOF_ARRAY(g_signals); i++) {
new_action.sa_handler = handle_sig;
(void)sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
if (sigaction(g_signals[i], &new_action, NULL) < 0)
err_abort("sigaction failed: errno=%d (%s)\n",
errno, strerror(errno));
}
timer_stats = calloc(TABLE_SIZE, sizeof(*timer_stats));
if (UNLIKELY(!timer_stats))
err_abort("Cannot allocate timer stats table\n");
/* Should really catch signals and set back to zero before we die */
set_tracing_enable("1\n", true);
set_tracing_event();
g_clock_tick_rate = (uint64_t)sysconf(_SC_CLK_TCK);
time_now = time_start = gettime_to_double();
if (g_opt_flags & OPT_TOP) {
struct sigaction sa;
(void)memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle_sigwinch;
if (sigaction(SIGWINCH, &sa, NULL) < 0)
err_abort("sigaction failed: errno=%d (%s)\n",
errno, strerror(errno));
(void)initscr();
(void)cbreak();
(void)noecho();
(void)nodelay(stdscr, 1);
(void)keypad(stdscr, 1);
(void)curs_set(0);
g_curses_init = true;
}
while (!g_stop_eventstat && (forever || count--)) {
double secs, duration, time_delta;
/* Timeout to wait for in the future for this sample */
secs = time_start + ((double)t * duration_secs) - time_now;
/* Play catch-up, probably been asleep */
if (secs < 0.0) {
t = ceil((time_now - time_start) / duration_secs);
secs = time_start +
((double)t * duration_secs) - time_now;
/* Really, it's impossible, but just in case.. */
if (secs < 0.0)
secs = 0.0;
} else {
if (!redo)
t++;
}
redo = false;
if (g_curses_init) {
fd_set rfds;
int ch, ret;
struct timeval tv;
(void)memset(&tv, 0, sizeof(tv));
FD_ZERO(&rfds);
FD_SET(fileno(stdin), &rfds);
ret = select(fileno(stdin) + 1, &rfds, NULL, NULL, &tv);
ch = getch();
if ((ch == 27) || (ch == 'q'))
break;
if (ret > 0)
redo = true;
if (ret < 0) {
if (errno != EINTR) {
eventstat_endwin();
(void)fprintf(stderr, "select() failed: "
"errno=%d (%s)\n",
errno, strerror(errno));
goto abort;
}
redo = true;
}
}
get_events(timer_stats, &get_events_buf, time_now, secs);
duration = gettime_to_double() - time_now;
duration = floor((duration * 1000.0) + 0.5) / 1000.0;
time_now = gettime_to_double();
time_delta = time_now - time_start;
eventstat_clear();
timer_stat_dump(duration, time_delta, n_lines,
time_now, timer_stats);
eventstat_refresh();
timer_stat_free_contents(timer_stats);
timer_info_purge_old(time_now);
}
eventstat_endwin();
abort:
samples_dump(g_csv_results);
timer_stat_free_contents(timer_stats);
free(timer_stats);
samples_free();
timer_info_list_free();
timer_stat_free_list_free();
free(get_events_buf);
eventstat_exit(EXIT_SUCCESS);
}
|