mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	samples/bpf: update tracex[23] examples to use per-cpu maps
Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
		
							parent
							
								
									df570f5772
								
							
						
					
					
						commit
						3059303f59
					
				
					 4 changed files with 25 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -70,7 +70,7 @@ struct hist_key {
 | 
			
		|||
};
 | 
			
		||||
 | 
			
		||||
struct bpf_map_def SEC("maps") my_hist_map = {
 | 
			
		||||
	.type = BPF_MAP_TYPE_HASH,
 | 
			
		||||
	.type = BPF_MAP_TYPE_PERCPU_HASH,
 | 
			
		||||
	.key_size = sizeof(struct hist_key),
 | 
			
		||||
	.value_size = sizeof(long),
 | 
			
		||||
	.max_entries = 1024,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -37,6 +37,8 @@ struct hist_key {
 | 
			
		|||
static void print_hist_for_pid(int fd, void *task)
 | 
			
		||||
{
 | 
			
		||||
	struct hist_key key = {}, next_key;
 | 
			
		||||
	unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
 | 
			
		||||
	long values[nr_cpus];
 | 
			
		||||
	char starstr[MAX_STARS];
 | 
			
		||||
	long value;
 | 
			
		||||
	long data[MAX_INDEX] = {};
 | 
			
		||||
| 
						 | 
				
			
			@ -49,7 +51,10 @@ static void print_hist_for_pid(int fd, void *task)
 | 
			
		|||
			key = next_key;
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		bpf_lookup_elem(fd, &next_key, &value);
 | 
			
		||||
		bpf_lookup_elem(fd, &next_key, values);
 | 
			
		||||
		value = 0;
 | 
			
		||||
		for (i = 0; i < nr_cpus; i++)
 | 
			
		||||
			value += values[i];
 | 
			
		||||
		ind = next_key.index;
 | 
			
		||||
		data[ind] = value;
 | 
			
		||||
		if (value && ind > max_ind)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,7 +20,7 @@ struct bpf_map_def SEC("maps") my_map = {
 | 
			
		|||
/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
 | 
			
		||||
 * example will no longer be meaningful
 | 
			
		||||
 */
 | 
			
		||||
SEC("kprobe/blk_mq_start_request")
 | 
			
		||||
SEC("kprobe/blk_start_request")
 | 
			
		||||
int bpf_prog1(struct pt_regs *ctx)
 | 
			
		||||
{
 | 
			
		||||
	long rq = PT_REGS_PARM1(ctx);
 | 
			
		||||
| 
						 | 
				
			
			@ -42,13 +42,13 @@ static unsigned int log2l(unsigned long long n)
 | 
			
		|||
#define SLOTS 100
 | 
			
		||||
 | 
			
		||||
struct bpf_map_def SEC("maps") lat_map = {
 | 
			
		||||
	.type = BPF_MAP_TYPE_ARRAY,
 | 
			
		||||
	.type = BPF_MAP_TYPE_PERCPU_ARRAY,
 | 
			
		||||
	.key_size = sizeof(u32),
 | 
			
		||||
	.value_size = sizeof(u64),
 | 
			
		||||
	.max_entries = SLOTS,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
SEC("kprobe/blk_update_request")
 | 
			
		||||
SEC("kprobe/blk_account_io_completion")
 | 
			
		||||
int bpf_prog2(struct pt_regs *ctx)
 | 
			
		||||
{
 | 
			
		||||
	long rq = PT_REGS_PARM1(ctx);
 | 
			
		||||
| 
						 | 
				
			
			@ -81,7 +81,7 @@ int bpf_prog2(struct pt_regs *ctx)
 | 
			
		|||
 | 
			
		||||
	value = bpf_map_lookup_elem(&lat_map, &index);
 | 
			
		||||
	if (value)
 | 
			
		||||
		__sync_fetch_and_add((long *)value, 1);
 | 
			
		||||
		*value += 1;
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,11 +20,13 @@
 | 
			
		|||
 | 
			
		||||
static void clear_stats(int fd)
 | 
			
		||||
{
 | 
			
		||||
	unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
 | 
			
		||||
	__u64 values[nr_cpus];
 | 
			
		||||
	__u32 key;
 | 
			
		||||
	__u64 value = 0;
 | 
			
		||||
 | 
			
		||||
	memset(values, 0, sizeof(values));
 | 
			
		||||
	for (key = 0; key < SLOTS; key++)
 | 
			
		||||
		bpf_update_elem(fd, &key, &value, BPF_ANY);
 | 
			
		||||
		bpf_update_elem(fd, &key, values, BPF_ANY);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *color[] = {
 | 
			
		||||
| 
						 | 
				
			
			@ -75,15 +77,20 @@ static void print_banner(void)
 | 
			
		|||
 | 
			
		||||
static void print_hist(int fd)
 | 
			
		||||
{
 | 
			
		||||
	__u32 key;
 | 
			
		||||
	__u64 value;
 | 
			
		||||
	__u64 cnt[SLOTS];
 | 
			
		||||
	__u64 max_cnt = 0;
 | 
			
		||||
	unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
 | 
			
		||||
	__u64 total_events = 0;
 | 
			
		||||
	long values[nr_cpus];
 | 
			
		||||
	__u64 max_cnt = 0;
 | 
			
		||||
	__u64 cnt[SLOTS];
 | 
			
		||||
	__u64 value;
 | 
			
		||||
	__u32 key;
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	for (key = 0; key < SLOTS; key++) {
 | 
			
		||||
		bpf_lookup_elem(fd, &key, values);
 | 
			
		||||
		value = 0;
 | 
			
		||||
		bpf_lookup_elem(fd, &key, &value);
 | 
			
		||||
		for (i = 0; i < nr_cpus; i++)
 | 
			
		||||
			value += values[i];
 | 
			
		||||
		cnt[key] = value;
 | 
			
		||||
		total_events += value;
 | 
			
		||||
		if (value > max_cnt)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue