|
1603 | 1603 | Hits: 489 |
1604 | 1604 | Entries: 7 |
1605 | 1605 | Dropped: 0 |
| 1606 | + |
| 1607 | + |
| 1608 | +2.2 Inter-event hist triggers |
| 1609 | +----------------------------- |
| 1610 | + |
| 1611 | +Inter-event hist triggers are hist triggers that combine values from |
| 1612 | +one or more other events and create a histogram using that data. Data |
| 1613 | +from an inter-event histogram can in turn become the source for |
| 1614 | +further combined histograms, thus providing a chain of related |
| 1615 | +histograms, which is important for some applications. |
| 1616 | + |
| 1617 | +The most important example of an inter-event quantity that can be used |
| 1618 | +in this manner is latency, which is simply a difference in timestamps |
| 1619 | +between two events. Although latency is the most important |
| 1620 | +inter-event quantity, note that because the support is completely |
| 1621 | +general across the trace event subsystem, any event field can be used |
| 1622 | +in an inter-event quantity. |
| 1623 | + |
| 1624 | +An example of a histogram that combines data from other histograms |
| 1625 | +into a useful chain would be a 'wakeupswitch latency' histogram that |
| 1626 | +combines a 'wakeup latency' histogram and a 'switch latency' |
| 1627 | +histogram. |
| 1628 | + |
| 1629 | +Normally, a hist trigger specification consists of a (possibly |
| 1630 | +compound) key along with one or more numeric values, which are |
| 1631 | +continually updated sums associated with that key. A histogram |
| 1632 | +specification in this case consists of individual key and value |
| 1633 | +specifications that refer to trace event fields associated with a |
| 1634 | +single event type. |
| 1635 | + |
| 1636 | +The inter-event hist trigger extension allows fields from multiple |
| 1637 | +events to be referenced and combined into a multi-event histogram |
| 1638 | +specification. In support of this overall goal, a few enabling |
| 1639 | +features have been added to the hist trigger support: |
| 1640 | + |
| 1641 | + - In order to compute an inter-event quantity, a value from one |
| 1642 | + event needs to saved and then referenced from another event. This |
| 1643 | + requires the introduction of support for histogram 'variables'. |
| 1644 | + |
| 1645 | + - The computation of inter-event quantities and their combination |
| 1646 | + require some minimal amount of support for applying simple |
| 1647 | + expressions to variables (+ and -). |
| 1648 | + |
| 1649 | + - A histogram consisting of inter-event quantities isn't logically a |
| 1650 | + histogram on either event (so having the 'hist' file for either |
| 1651 | + event host the histogram output doesn't really make sense). To |
| 1652 | + address the idea that the histogram is associated with a |
| 1653 | + combination of events, support is added allowing the creation of |
| 1654 | + 'synthetic' events that are events derived from other events. |
| 1655 | + These synthetic events are full-fledged events just like any other |
| 1656 | + and can be used as such, as for instance to create the |
| 1657 | + 'combination' histograms mentioned previously. |
| 1658 | + |
| 1659 | + - A set of 'actions' can be associated with histogram entries - |
| 1660 | + these can be used to generate the previously mentioned synthetic |
| 1661 | + events, but can also be used for other purposes, such as for |
| 1662 | + example saving context when a 'max' latency has been hit. |
| 1663 | + |
| 1664 | + - Trace events don't have a 'timestamp' associated with them, but |
| 1665 | + there is an implicit timestamp saved along with an event in the |
| 1666 | + underlying ftrace ring buffer. This timestamp is now exposed as a |
| 1667 | + a synthetic field named 'common_timestamp' which can be used in |
| 1668 | + histograms as if it were any other event field; it isn't an actual |
| 1669 | + field in the trace format but rather is a synthesized value that |
| 1670 | + nonetheless can be used as if it were an actual field. By default |
| 1671 | + it is in units of nanoseconds; appending '.usecs' to a |
| 1672 | + common_timestamp field changes the units to microseconds. |
| 1673 | + |
| 1674 | +These features are decribed in more detail in the following sections. |
| 1675 | + |
| 1676 | +2.2.1 Histogram Variables |
| 1677 | +------------------------- |
| 1678 | + |
| 1679 | +Variables are simply named locations used for saving and retrieving |
| 1680 | +values between matching events. A 'matching' event is defined as an |
| 1681 | +event that has a matching key - if a variable is saved for a histogram |
| 1682 | +entry corresponding to that key, any subsequent event with a matching |
| 1683 | +key can access that variable. |
| 1684 | + |
| 1685 | +A variable's value is normally available to any subsequent event until |
| 1686 | +it is set to something else by a subsequent event. The one exception |
| 1687 | +to that rule is that any variable used in an expression is essentially |
| 1688 | +'read-once' - once it's used by an expression in a subsequent event, |
| 1689 | +it's reset to its 'unset' state, which means it can't be used again |
| 1690 | +unless it's set again. This ensures not only that an event doesn't |
| 1691 | +use an uninitialized variable in a calculation, but that that variable |
| 1692 | +is used only once and not for any unrelated subsequent match. |
| 1693 | + |
| 1694 | +The basic syntax for saving a variable is to simply prefix a unique |
| 1695 | +variable name not corresponding to any keyword along with an '=' sign |
| 1696 | +to any event field. |
| 1697 | + |
| 1698 | +Either keys or values can be saved and retrieved in this way. This |
| 1699 | +creates a variable named 'ts0' for a histogram entry with the key |
| 1700 | +'next_pid': |
| 1701 | + |
| 1702 | + # echo 'hist:keys=next_pid:vals=$ts0:ts0=common_timestamp ... >> \ |
| 1703 | + event/trigger |
| 1704 | + |
| 1705 | +The ts0 variable can be accessed by any subsequent event having the |
| 1706 | +same pid as 'next_pid'. |
| 1707 | + |
| 1708 | +Variable references are formed by prepending the variable name with |
| 1709 | +the '$' sign. Thus for example, the ts0 variable above would be |
| 1710 | +referenced as '$ts0' in expressions. |
| 1711 | + |
| 1712 | +Because 'vals=' is used, the common_timestamp variable value above |
| 1713 | +will also be summed as a normal histogram value would (though for a |
| 1714 | +timestamp it makes little sense). |
| 1715 | + |
| 1716 | +The below shows that a key value can also be saved in the same way: |
| 1717 | + |
| 1718 | + # echo 'hist:timer_pid=common_pid:key=timer_pid ...' >> event/trigger |
| 1719 | + |
| 1720 | +If a variable isn't a key variable or prefixed with 'vals=', the |
| 1721 | +associated event field will be saved in a variable but won't be summed |
| 1722 | +as a value: |
| 1723 | + |
| 1724 | + # echo 'hist:keys=next_pid:ts1=common_timestamp ... >> event/trigger |
| 1725 | + |
| 1726 | +Multiple variables can be assigned at the same time. The below would |
| 1727 | +result in both ts0 and b being created as variables, with both |
| 1728 | +common_timestamp and field1 additionally being summed as values: |
| 1729 | + |
| 1730 | + # echo 'hist:keys=pid:vals=$ts0,$b:ts0=common_timestamp,b=field1 ... >> \ |
| 1731 | + event/trigger |
| 1732 | + |
| 1733 | +Note that variable assignments can appear either preceding or |
| 1734 | +following their use. The command below behaves identically to the |
| 1735 | +command above: |
| 1736 | + |
| 1737 | + # echo 'hist:keys=pid:ts0=common_timestamp,b=field1:vals=$ts0,$b ... >> \ |
| 1738 | + event/trigger |
| 1739 | + |
| 1740 | +Any number of variables not bound to a 'vals=' prefix can also be |
| 1741 | +assigned by simply separating them with colons. Below is the same |
| 1742 | +thing but without the values being summed in the histogram: |
| 1743 | + |
| 1744 | + # echo 'hist:keys=pid:ts0=common_timestamp:b=field1 ... >> event/trigger |
| 1745 | + |
| 1746 | +Variables set as above can be referenced and used in expressions on |
| 1747 | +another event. |
| 1748 | + |
| 1749 | +For example, here's how a latency can be calculated: |
| 1750 | + |
| 1751 | + # echo 'hist:keys=pid,prio:ts0=common_timestamp ... >> event1/trigger |
| 1752 | + # echo 'hist:keys=next_pid:wakeup_lat=common_timestamp-$ts0 ... >> event2/trigger |
| 1753 | + |
| 1754 | +In the first line above, the event's timetamp is saved into the |
| 1755 | +variable ts0. In the next line, ts0 is subtracted from the second |
| 1756 | +event's timestamp to produce the latency, which is then assigned into |
| 1757 | +yet another variable, 'wakeup_lat'. The hist trigger below in turn |
| 1758 | +makes use of the wakeup_lat variable to compute a combined latency |
| 1759 | +using the same key and variable from yet another event: |
| 1760 | + |
| 1761 | + # echo 'hist:key=pid:wakeupswitch_lat=$wakeup_lat+$switchtime_lat ... >> event3/trigger |
| 1762 | + |
| 1763 | +2.2.2 Synthetic Events |
| 1764 | +---------------------- |
| 1765 | + |
| 1766 | +Synthetic events are user-defined events generated from hist trigger |
| 1767 | +variables or fields associated with one or more other events. Their |
| 1768 | +purpose is to provide a mechanism for displaying data spanning |
| 1769 | +multiple events consistent with the existing and already familiar |
| 1770 | +usage for normal events. |
| 1771 | + |
| 1772 | +To define a synthetic event, the user writes a simple specification |
| 1773 | +consisting of the name of the new event along with one or more |
| 1774 | +variables and their types, which can be any valid field type, |
| 1775 | +separated by semicolons, to the tracing/synthetic_events file. |
| 1776 | + |
| 1777 | +For instance, the following creates a new event named 'wakeup_latency' |
| 1778 | +with 3 fields: lat, pid, and prio. Each of those fields is simply a |
| 1779 | +variable reference to a variable on another event: |
| 1780 | + |
| 1781 | + # echo 'wakeup_latency \ |
| 1782 | + u64 lat; \ |
| 1783 | + pid_t pid; \ |
| 1784 | + int prio' >> \ |
| 1785 | + /sys/kernel/debug/tracing/synthetic_events |
| 1786 | + |
| 1787 | +Reading the tracing/synthetic_events file lists all the currently |
| 1788 | +defined synthetic events, in this case the event defined above: |
| 1789 | + |
| 1790 | + # cat /sys/kernel/debug/tracing/synthetic_events |
| 1791 | + wakeup_latency u64 lat; pid_t pid; int prio |
| 1792 | + |
| 1793 | +An existing synthetic event definition can be removed by prepending |
| 1794 | +the command that defined it with a '!': |
| 1795 | + |
| 1796 | + # echo '!wakeup_latency u64 lat pid_t pid int prio' >> \ |
| 1797 | + /sys/kernel/debug/tracing/synthetic_events |
| 1798 | + |
| 1799 | +At this point, there isn't yet an actual 'wakeup_latency' event |
| 1800 | +instantiated in the event subsytem - for this to happen, a 'hist |
| 1801 | +trigger action' needs to be instantiated and bound to actual fields |
| 1802 | +and variables defined on other events (see Section 6.3.3 below). |
| 1803 | + |
| 1804 | +Once that is done, an event instance is created, and a histogram can |
| 1805 | +be defined using it: |
| 1806 | + |
| 1807 | + # echo 'hist:keys=pid,prio,lat.log2:sort=pid,lat' >> \ |
| 1808 | + /sys/kernel/debug/tracing/events/synthetic/wakeup_latency/trigger |
| 1809 | + |
| 1810 | +The new event is created under the tracing/events/synthetic/ directory |
| 1811 | +and looks and behaves just like any other event: |
| 1812 | + |
| 1813 | + # ls /sys/kernel/debug/tracing/events/synthetic/wakeup_latency |
| 1814 | + enable filter format hist id trigger |
| 1815 | + |
| 1816 | +Like any other event, once a histogram is enabled for the event, the |
| 1817 | +output can be displayed by reading the event's 'hist' file. |
| 1818 | + |
| 1819 | +2.2.3 Hist trigger 'actions' |
| 1820 | +---------------------------- |
| 1821 | + |
| 1822 | +A hist trigger 'action' is a function that's executed whenever a |
| 1823 | +histogram entry is added or updated. |
| 1824 | + |
| 1825 | +The default 'action' if no special function is explicity specified is |
| 1826 | +as it always has been, to simply update the set of values associated |
| 1827 | +with an entry. Some applications, however, may want to perform |
| 1828 | +additional actions at that point, such as generate another event, or |
| 1829 | +compare and save a maximum. |
| 1830 | + |
| 1831 | +The following additional actions are available. To specify an action |
| 1832 | +for a given event, simply specify the action between colons in the |
| 1833 | +hist trigger specification. |
| 1834 | + |
| 1835 | + - onmatch(matching.event).<synthetic_event_name>(param list) |
| 1836 | + |
| 1837 | + The 'onmatch(matching.event).<synthetic_event_name>(params)' hist |
| 1838 | + trigger action is invoked whenever an event matches and the |
| 1839 | + histogram entry would be added or updated. It causes the named |
| 1840 | + synthetic event to be generated with the values given in the |
| 1841 | + 'param list'. The result is the generation of a synthetic event |
| 1842 | + that consists of the values contained in those variables at the |
| 1843 | + time the invoking event was hit. |
| 1844 | + |
| 1845 | + The 'param list' consists of one or more parameters which may be |
| 1846 | + either variables or fields defined on either the 'matching.event' |
| 1847 | + or the target event. The variables or fields specified in the |
| 1848 | + param list may be either fully-qualified or unqualified. If a |
| 1849 | + variable is specified as unqualified, it must be unique between |
| 1850 | + the two events. A field name used as a param can be unqualified |
| 1851 | + if it refers to the target event, but must be fully qualified if |
| 1852 | + it refers to the matching event. A fully-qualified name is of the |
| 1853 | + form 'system.event_name.$var_name' or 'system.event_name.field'. |
| 1854 | + |
| 1855 | + The 'matching.event' specification is simply the fully qualified |
| 1856 | + event name of the event that matches the target event for the |
| 1857 | + onmatch() functionality, in the form 'system.event_name'. |
| 1858 | + |
| 1859 | + Finally, the number and type of variables/fields in the 'param |
| 1860 | + list' must match the number and types of the fields in the |
| 1861 | + synthetic event being generated. |
| 1862 | + |
| 1863 | + As an example the below defines a simple synthetic event and uses |
| 1864 | + a variable defined on the sched_wakeup_new event as a parameter |
| 1865 | + when invoking the synthetic event. Here we define the synthetic |
| 1866 | + event: |
| 1867 | + |
| 1868 | + # echo 'wakeup_new_test pid_t pid' >> \ |
| 1869 | + /sys/kernel/debug/tracing/synthetic_events |
| 1870 | + |
| 1871 | + # cat /sys/kernel/debug/tracing/synthetic_events |
| 1872 | + wakeup_new_test pid_t pid |
| 1873 | + |
| 1874 | + The following hist trigger both defines the missing testpid |
| 1875 | + variable and specifies an onmatch() action that generates a |
| 1876 | + wakeup_new_test synthetic event whenever a sched_wakeup_new event |
| 1877 | + occurs, which because of the 'if comm == "cyclictest"' filter only |
| 1878 | + happens when the executable is cyclictest: |
| 1879 | + |
| 1880 | + # echo 'hist:keys=$testpid:testpid=pid:onmatch(sched.sched_wakeup_new).\ |
| 1881 | + wakeup_new_test($testpid) if comm=="cyclictest"' >> \ |
| 1882 | + /sys/kernel/debug/tracing/events/sched/sched_wakeup_new/trigger |
| 1883 | + |
| 1884 | + Creating and displaying a histogram based on those events is now |
| 1885 | + just a matter of using the fields and new synthetic event in the |
| 1886 | + tracing/events/synthetic directory, as usual: |
| 1887 | + |
| 1888 | + # echo 'hist:keys=pid:sort=pid' >> \ |
| 1889 | + /sys/kernel/debug/tracing/events/synthetic/wakeup_new_test/trigger |
| 1890 | + |
| 1891 | + Running 'cyclictest' should cause wakeup_new events to generate |
| 1892 | + wakeup_new_test synthetic events which should result in histogram |
| 1893 | + output in the wakeup_new_test event's hist file: |
| 1894 | + |
| 1895 | + # cat /sys/kernel/debug/tracing/events/synthetic/wakeup_new_test/hist |
| 1896 | + |
| 1897 | + A more typical usage would be to use two events to calculate a |
| 1898 | + latency. The following example uses a set of hist triggers to |
| 1899 | + produce a 'wakeup_latency' histogram: |
| 1900 | + |
| 1901 | + First, we define a 'wakeup_latency' synthetic event: |
| 1902 | + |
| 1903 | + # echo 'wakeup_latency u64 lat; pid_t pid; int prio' >> \ |
| 1904 | + /sys/kernel/debug/tracing/synthetic_events |
| 1905 | + |
| 1906 | + Next, we specify that whenever we see a sched_waking event for a |
| 1907 | + cyclictest thread, save the timestamp in a 'ts0' variable: |
| 1908 | + |
| 1909 | + # echo 'hist:keys=$saved_pid:saved_pid=pid:ts0=common_timestamp.usecs \ |
| 1910 | + if comm=="cyclictest"' >> \ |
| 1911 | + /sys/kernel/debug/tracing/events/sched/sched_waking/trigger |
| 1912 | + |
| 1913 | + Then, when the corresponding thread is actually scheduled onto the |
| 1914 | + CPU by a sched_switch event, calculate the latency and use that |
| 1915 | + along with another variable and an event field to generate a |
| 1916 | + wakeup_latency synthetic event: |
| 1917 | + |
| 1918 | + # echo 'hist:keys=next_pid:wakeup_lat=common_timestamp.usecs-$ts0:\ |
| 1919 | + onmatch(sched.sched_waking).wakeup_latency($wakeup_lat,\ |
| 1920 | + $saved_pid,next_prio) if next_comm=="cyclictest"' >> \ |
| 1921 | + /sys/kernel/debug/tracing/events/sched/sched_switch/trigger |
| 1922 | + |
| 1923 | + We also need to create a histogram on the wakeup_latency synthetic |
| 1924 | + event in order to aggregate the generated synthetic event data: |
| 1925 | + |
| 1926 | + # echo 'hist:keys=pid,prio,lat:sort=pid,lat' >> \ |
| 1927 | + /sys/kernel/debug/tracing/events/synthetic/wakeup_latency/trigger |
| 1928 | + |
| 1929 | + Finally, once we've run cyclictest to actually generate some |
| 1930 | + events, we can see the output by looking at the wakeup_latency |
| 1931 | + synthetic event's hist file: |
| 1932 | + |
| 1933 | + # cat /sys/kernel/debug/tracing/events/synthetic/wakeup_latency/hist |
| 1934 | + |
| 1935 | + - onmax(var).save(field,.. .) |
| 1936 | + |
| 1937 | + The 'onmax(var).save(field,...)' hist trigger action is invoked |
| 1938 | + whenever the value of 'var' associated with a histogram entry |
| 1939 | + exceeds the current maximum contained in that variable. |
| 1940 | + |
| 1941 | + The end result is that the trace event fields specified as the |
| 1942 | + onmax.save() params will be saved if 'var' exceeds the current |
| 1943 | + maximum for that hist trigger entry. This allows context from the |
| 1944 | + event that exhibited the new maximum to be saved for later |
| 1945 | + reference. When the histogram is displayed, additional fields |
| 1946 | + displaying the saved values will be printed. |
| 1947 | + |
| 1948 | + As an example the below defines a couple of hist triggers, one for |
| 1949 | + sched_waking and another for sched_switch, keyed on pid. Whenever |
| 1950 | + a sched_waking occurs, the timestamp is saved in the entry |
| 1951 | + corresponding to the current pid, and when the scheduler switches |
| 1952 | + back to that pid, the timestamp difference is calculated. If the |
| 1953 | + resulting latency, stored in wakeup_lat, exceeds the current |
| 1954 | + maximum latency, the values specified in the save() fields are |
| 1955 | + recoreded: |
| 1956 | + |
| 1957 | + # echo 'hist:keys=pid:ts0=common_timestamp.usecs \ |
| 1958 | + if comm=="cyclictest"' >> \ |
| 1959 | + /sys/kernel/debug/tracing/events/sched/sched_waking/trigger |
| 1960 | + |
| 1961 | + # echo 'hist:keys=next_pid:\ |
| 1962 | + wakeup_lat=common_timestamp.usecs-$ts0:\ |
| 1963 | + onmax($wakeup_lat).save(next_comm,prev_pid,prev_prio,prev_comm) \ |
| 1964 | + if next_comm=="cyclictest"' >> \ |
| 1965 | + /sys/kernel/debug/tracing/events/sched/sched_switch/trigger |
| 1966 | + |
| 1967 | + When the histogram is displayed, the max value and the saved |
| 1968 | + values corresponding to the max are displayed following the rest |
| 1969 | + of the fields: |
| 1970 | + |
| 1971 | + # cat /sys/kernel/debug/tracing/events/sched/sched_switch/hist |
| 1972 | + { next_pid: 2255 } hitcount: 239 |
| 1973 | + common_timestamp-ts0: 0 |
| 1974 | + max: 27 |
| 1975 | + next_comm: cyclictest |
| 1976 | + prev_pid: 0 prev_prio: 120 prev_comm: swapper/1 |
| 1977 | + |
| 1978 | + { next_pid: 2256 } hitcount: 2355 |
| 1979 | + common_timestamp-ts0: 0 |
| 1980 | + max: 49 next_comm: cyclictest |
| 1981 | + prev_pid: 0 prev_prio: 120 prev_comm: swapper/0 |
| 1982 | + |
| 1983 | + Totals: |
| 1984 | + Hits: 12970 |
| 1985 | + Entries: 2 |
| 1986 | + Dropped: 0 |
0 commit comments