Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hist: log2 histograms with finer granularity #2831

Merged
merged 1 commit into from
Dec 26, 2023

Commits on Nov 27, 2023

  1. hist: log2 histograms with finer granularity

    Allow a second optional argument in hist(n, k) to map each power
    of 2 into 2^k buckets, thus creating a logarithmic scale with finer
    granularity and modest runtime overhead (a couple of shifts and add/mask
    in addition to the original algorithm).
    
    Allowed values of k are 0..5, with 0 as default for backward compatibility.
    
    The implementation follows my earlier code in https://github.com/luigirizzo/lr-cstats
    
    Example below:
    
    $ sudo src/bpftrace -e 'kfunc:tick_do_update_jiffies64 { @ = hist((nsecs & 0xff),2); }'
    Attaching 2 probes...
    @:
    [0]                    4 |@                                                   |
    [1]                    1 |                                                    |
    [2]                    3 |@                                                   |
    [3]                    2 |                                                    |
    [4]                    3 |@                                                   |
    [5]                    0 |                                                    |
    [6]                    3 |@                                                   |
    [7]                    2 |                                                    |
    [8, 10)                5 |@                                                   |
    [10, 12)               7 |@@                                                  |
    [12, 14)               5 |@                                                   |
    [14, 16)               6 |@@                                                  |
    [16, 20)              11 |@@@                                                 |
    [20, 24)              14 |@@@@                                                |
    [24, 28)              20 |@@@@@@                                              |
    [28, 32)              13 |@@@@                                                |
    [32, 40)              40 |@@@@@@@@@@@@@                                       |
    [40, 48)              38 |@@@@@@@@@@@@@                                       |
    [48, 56)              35 |@@@@@@@@@@@                                         |
    [56, 64)              29 |@@@@@@@@@                                           |
    [64, 80)              72 |@@@@@@@@@@@@@@@@@@@@@@@@                            |
    [80, 96)              64 |@@@@@@@@@@@@@@@@@@@@@                               |
    [96, 112)             61 |@@@@@@@@@@@@@@@@@@@@                                |
    [112, 128)            67 |@@@@@@@@@@@@@@@@@@@@@@                              |
    [128, 160)           124 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@          |
    [160, 192)           130 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@        |
    [192, 224)           124 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@          |
    [224, 256)           152 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
    
    Performance:
    most of the cost of hist() in bpftrace is in the bpf hash lookup,
    and the cost of index calculation is negligible.
    One way to measure the overall cost is the following
    
    sudo taskset -c 1 src/bpftrace -e 'i:us:1 { $t = nsecs; @A = hist($t); $t = nsecs - $t; @ = lhist($t, 0, 5000, 100);} '
    
    and on my AMD 5800 most of the samples are in the 900-100us range;
    my estimate for index computation (from lr-cstats) is in the 10-20ns range.
    
    Remember to check `/proc/sys/kernel/perf_` to make sure that the code
    can generate a sufficient number of samples per second.
    luigirizzo committed Nov 27, 2023
    Configuration menu
    Copy the full SHA
    80c28f4 View commit details
    Browse the repository at this point in the history