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

sort by key #36

Open
brendangregg opened this issue Nov 12, 2013 · 1 comment
Open

sort by key #36

brendangregg opened this issue Nov 12, 2013 · 1 comment

Comments

@brendangregg
Copy link

I've used sort_pairs() to sort by value, but I didn't see a way to sort by key.

The following example script uses an additional table to accomplish key sorting, as an example of what is desired.

#!/usr/bin/env ktap
#
# syslist.kp    syscall latency as a list with counts
#
#10-Nov-2013   Brendan Gregg   Created this

self = {}
lats = {}
order = {}  # a workaround for key sorting

print("Tracing syscalls... Hit Ctrl-C to end.")

trace syscalls:sys_enter_* {
    self[tid()] = gettimeofday_us()
}

trace syscalls:sys_exit_* {
    if (self[tid()] == nil) { return }
    delta = gettimeofday_us() - self[tid()]
    lats[delta] += 1
    order[delta] = delta
    self[tid()] = nil
}

trace_end {
    printf("   %8s %8s\n", "LAT(us)", "COUNT");
    function cmp(v1, v2) {
        return (v1 < v2)
    }
    for (lat, dummy in sort_pairs(order, cmp)) {
        printf("   %8d %8d\n", lat, lats[lat]);
    }
}
@ktap
Copy link
Owner

ktap commented Nov 12, 2013

Hi Brendan,

Yes, there don't have way to sort by key at present.

I see it would be useful to sort table/aggregation by key, so I will
implement it in future, not only in table sort dump, also support
it in printa built-in function.

And, shall I can commit this script to ktap tree to be a sample script
with your authorship? there may have more people like this script too. :)

Thanks for your reporting and script.

Jovi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants