~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/bpf/tracex3_kern.c

Version: ~ [ linux-5.5-rc6 ] ~ [ linux-5.4.11 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.95 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.164 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.209 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.209 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of version 2 of the GNU General Public
  5  * License as published by the Free Software Foundation.
  6  */
  7 #include <linux/skbuff.h>
  8 #include <linux/netdevice.h>
  9 #include <linux/version.h>
 10 #include <uapi/linux/bpf.h>
 11 #include "bpf_helpers.h"
 12 
 13 struct bpf_map_def SEC("maps") my_map = {
 14         .type = BPF_MAP_TYPE_HASH,
 15         .key_size = sizeof(long),
 16         .value_size = sizeof(u64),
 17         .max_entries = 4096,
 18 };
 19 
 20 /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
 21  * example will no longer be meaningful
 22  */
 23 SEC("kprobe/blk_start_request")
 24 int bpf_prog1(struct pt_regs *ctx)
 25 {
 26         long rq = PT_REGS_PARM1(ctx);
 27         u64 val = bpf_ktime_get_ns();
 28 
 29         bpf_map_update_elem(&my_map, &rq, &val, BPF_ANY);
 30         return 0;
 31 }
 32 
 33 static unsigned int log2l(unsigned long long n)
 34 {
 35 #define S(k) if (n >= (1ull << k)) { i += k; n >>= k; }
 36         int i = -(n == 0);
 37         S(32); S(16); S(8); S(4); S(2); S(1);
 38         return i;
 39 #undef S
 40 }
 41 
 42 #define SLOTS 100
 43 
 44 struct bpf_map_def SEC("maps") lat_map = {
 45         .type = BPF_MAP_TYPE_PERCPU_ARRAY,
 46         .key_size = sizeof(u32),
 47         .value_size = sizeof(u64),
 48         .max_entries = SLOTS,
 49 };
 50 
 51 SEC("kprobe/blk_account_io_completion")
 52 int bpf_prog2(struct pt_regs *ctx)
 53 {
 54         long rq = PT_REGS_PARM1(ctx);
 55         u64 *value, l, base;
 56         u32 index;
 57 
 58         value = bpf_map_lookup_elem(&my_map, &rq);
 59         if (!value)
 60                 return 0;
 61 
 62         u64 cur_time = bpf_ktime_get_ns();
 63         u64 delta = cur_time - *value;
 64 
 65         bpf_map_delete_elem(&my_map, &rq);
 66 
 67         /* the lines below are computing index = log10(delta)*10
 68          * using integer arithmetic
 69          * index = 29 ~ 1 usec
 70          * index = 59 ~ 1 msec
 71          * index = 89 ~ 1 sec
 72          * index = 99 ~ 10sec or more
 73          * log10(x)*10 = log2(x)*10/log2(10) = log2(x)*3
 74          */
 75         l = log2l(delta);
 76         base = 1ll << l;
 77         index = (l * 64 + (delta - base) * 64 / base) * 3 / 64;
 78 
 79         if (index >= SLOTS)
 80                 index = SLOTS - 1;
 81 
 82         value = bpf_map_lookup_elem(&lat_map, &index);
 83         if (value)
 84                 *value += 1;
 85 
 86         return 0;
 87 }
 88 char _license[] SEC("license") = "GPL";
 89 u32 _version SEC("version") = LINUX_VERSION_CODE;
 90 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp