12a7c2fffSYonghong Song // SPDX-License-Identifier: GPL-2.0
22a7c2fffSYonghong Song /* Copyright (c) 2020 Facebook */
32a7c2fffSYonghong Song #include "bpf_iter.h"
42a7c2fffSYonghong Song #include <bpf/bpf_helpers.h>
52a7c2fffSYonghong Song #include <bpf/bpf_tracing.h>
62a7c2fffSYonghong Song 
72a7c2fffSYonghong Song char _license[] SEC("license") = "GPL";
82a7c2fffSYonghong Song 
92a7c2fffSYonghong Song struct key_t {
102a7c2fffSYonghong Song 	int a;
112a7c2fffSYonghong Song 	int b;
122a7c2fffSYonghong Song 	int c;
132a7c2fffSYonghong Song };
142a7c2fffSYonghong Song 
152a7c2fffSYonghong Song struct {
162a7c2fffSYonghong Song 	__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
172a7c2fffSYonghong Song 	__uint(max_entries, 3);
182a7c2fffSYonghong Song 	__type(key, struct key_t);
192a7c2fffSYonghong Song 	__type(value, __u32);
202a7c2fffSYonghong Song } hashmap1 SEC(".maps");
212a7c2fffSYonghong Song 
222a7c2fffSYonghong Song /* will set before prog run */
232a7c2fffSYonghong Song volatile const __u32 num_cpus = 0;
242a7c2fffSYonghong Song 
252a7c2fffSYonghong Song /* will collect results during prog run */
262a7c2fffSYonghong Song __u32 key_sum_a = 0, key_sum_b = 0, key_sum_c = 0;
272a7c2fffSYonghong Song __u32 val_sum = 0;
282a7c2fffSYonghong Song 
292a7c2fffSYonghong Song SEC("iter/bpf_map_elem")
dump_bpf_percpu_hash_map(struct bpf_iter__bpf_map_elem * ctx)302a7c2fffSYonghong Song int dump_bpf_percpu_hash_map(struct bpf_iter__bpf_map_elem *ctx)
312a7c2fffSYonghong Song {
322a7c2fffSYonghong Song 	struct key_t *key = ctx->key;
332a7c2fffSYonghong Song 	void *pptr = ctx->value;
342a7c2fffSYonghong Song 	__u32 step;
352a7c2fffSYonghong Song 	int i;
362a7c2fffSYonghong Song 
372a7c2fffSYonghong Song 	if (key == (void *)0 || pptr == (void *)0)
382a7c2fffSYonghong Song 		return 0;
392a7c2fffSYonghong Song 
402a7c2fffSYonghong Song 	key_sum_a += key->a;
412a7c2fffSYonghong Song 	key_sum_b += key->b;
422a7c2fffSYonghong Song 	key_sum_c += key->c;
432a7c2fffSYonghong Song 
442a7c2fffSYonghong Song 	step = 8;
452a7c2fffSYonghong Song 	for (i = 0; i < num_cpus; i++) {
462a7c2fffSYonghong Song 		val_sum += *(__u32 *)pptr;
472a7c2fffSYonghong Song 		pptr += step;
482a7c2fffSYonghong Song 	}
492a7c2fffSYonghong Song 	return 0;
502a7c2fffSYonghong Song }
51