2 from time import sleep, strftime
5 from bcc import BPF, USDT
9 # Parse command line arguments
10 parser = argparse.ArgumentParser(description="Trace the moving average of the latency of an operation using usdt probes.",
11 formatter_class=argparse.RawDescriptionHelpFormatter)
12 parser.add_argument("-p", "--pid", type=int, help="The id of the process to trace.")
13 parser.add_argument("-i", "--interval", type=int, help="The interval in seconds on which to report the latency distribution.")
14 parser.add_argument("-c", "--count", type=int, default=16, help="The count of samples over which to calculate the moving average.")
15 parser.add_argument("-f", "--filterstr", type=str, default="", help="The prefix filter for the operation input. If specified, only operations for which the input string starts with the filterstr are traced.")
16 parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", help="If true, will output verbose logging information.")
17 parser.set_defaults(verbose=False)
18 args = parser.parse_args()
19 this_pid = int(args.pid)
20 this_interval = int(args.interval)
21 this_count = int(args.count)
22 this_filter = str(args.filterstr)
25 print("Invalid value for interval, using 1.")
29 print("Invalid value for count, using 1.")
37 bpf_text_shared = "%s/bpf_text_shared.c" % os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
38 bpf_text = open(bpf_text_shared, 'r').read()
41 const u32 MAX_SAMPLES = SAMPLE_COUNT;
56 * @brief Contains the averages for the operation latencies by operation input.
58 BPF_HASH(lat_hash, struct hash_key_t, struct hash_leaf_t, 512);
61 * @brief Reads the operation response arguments, calculates the latency, and stores it in the histogram.
62 * @param ctx The BPF context.
64 int trace_operation_end(struct pt_regs* ctx)
67 bpf_usdt_readarg(1, ctx, &operation_id);
69 struct start_data_t* start_data = start_hash.lookup(&operation_id);
70 if (0 == start_data) {
74 u64 duration = bpf_ktime_get_ns() - start_data->start;
75 struct hash_key_t hash_key = {};
76 __builtin_memcpy(&hash_key.input, start_data->input, sizeof(hash_key.input));
77 start_hash.delete(&operation_id);
79 struct hash_leaf_t zero = {};
80 struct hash_leaf_t* hash_leaf = lat_hash.lookup_or_init(&hash_key, &zero);
85 if (hash_leaf->count < MAX_SAMPLES) {
88 hash_leaf->total -= hash_leaf->average;
91 hash_leaf->total += duration;
92 hash_leaf->average = hash_leaf->total / hash_leaf->count;
98 bpf_text = bpf_text.replace("SAMPLE_COUNT", str(this_count))
99 bpf_text = bpf_text.replace("FILTER_STRING", this_filter)
101 bpf_text = bpf_text.replace("FILTER", "if (!filter(start_data.input)) { return 0; }")
103 bpf_text = bpf_text.replace("FILTER", "")
105 # Create USDT context
106 print("Attaching probes to pid %d" % this_pid)
107 usdt_ctx = USDT(pid=this_pid)
108 usdt_ctx.enable_probe(probe="operation_start", fn_name="trace_operation_start")
109 usdt_ctx.enable_probe(probe="operation_end", fn_name="trace_operation_end")
111 # Create BPF context, load BPF program
112 bpf_ctx = BPF(text=bpf_text, usdt_contexts=[usdt_ctx], debug=debugLevel)
114 print("Tracing... Hit Ctrl-C to end.")
116 lat_hash = bpf_ctx.get_table("lat_hash")
120 except KeyboardInterrupt:
123 print("[%s]" % strftime("%H:%M:%S"))
124 print("%-64s %8s %16s" % ("input", "count", "latency (us)"))
125 for k, v in lat_hash.items():
126 print("%-64s %8d %16d" % (k.input, v.count, v.average / 1000))