github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/examples/tracepoint_in_c/tracepoint.c (about)

     1  //go:build ignore
     2  
     3  #include "common.h"
     4  
     5  char __license[] SEC("license") = "Dual MIT/GPL";
     6  
     7  struct bpf_map_def SEC("maps") counting_map = {
     8  	.type        = BPF_MAP_TYPE_ARRAY,
     9  	.key_size    = sizeof(u32),
    10  	.value_size  = sizeof(u64),
    11  	.max_entries = 1,
    12  };
    13  
    14  // This struct is defined according to the following format file:
    15  // /sys/kernel/tracing/events/kmem/mm_page_alloc/format
    16  struct alloc_info {
    17  	/* The first 8 bytes is not allowed to read */
    18  	unsigned long pad;
    19  
    20  	unsigned long pfn;
    21  	unsigned int order;
    22  	unsigned int gfp_flags;
    23  	int migratetype;
    24  };
    25  
    26  // This tracepoint is defined in mm/page_alloc.c:__alloc_pages_nodemask()
    27  // Userspace pathname: /sys/kernel/tracing/events/kmem/mm_page_alloc
    28  SEC("tracepoint/kmem/mm_page_alloc")
    29  int mm_page_alloc(struct alloc_info *info) {
    30  	u32 key     = 0;
    31  	u64 initval = 1, *valp;
    32  
    33  	valp = bpf_map_lookup_elem(&counting_map, &key);
    34  	if (!valp) {
    35  		bpf_map_update_elem(&counting_map, &key, &initval, BPF_ANY);
    36  		return 0;
    37  	}
    38  	__sync_fetch_and_add(valp, 1);
    39  	return 0;
    40  }