summaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@fb.com>2016-02-02 01:39:58 -0500
committerDavid S. Miller <davem@davemloft.net>2016-02-06 03:34:36 -0500
commit3059303f59cf90a84e7fdef154ff0b215bcfaa97 (patch)
treebacdf1b8025256fc591b1fd935a99f0b6af7466f /samples
parentdf570f577231407d929bdc6f59ae2f53e0028e8a (diff)
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>
Diffstat (limited to 'samples')
-rw-r--r--samples/bpf/tracex2_kern.c2
-rw-r--r--samples/bpf/tracex2_user.c7
-rw-r--r--samples/bpf/tracex3_kern.c8
-rw-r--r--samples/bpf/tracex3_user.c21
4 files changed, 25 insertions, 13 deletions
diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c
index b32367cfbff4..09c1adc27d42 100644
--- a/samples/bpf/tracex2_kern.c
+++ b/samples/bpf/tracex2_kern.c
@@ -70,7 +70,7 @@ struct hist_key {
70}; 70};
71 71
72struct bpf_map_def SEC("maps") my_hist_map = { 72struct bpf_map_def SEC("maps") my_hist_map = {
73 .type = BPF_MAP_TYPE_HASH, 73 .type = BPF_MAP_TYPE_PERCPU_HASH,
74 .key_size = sizeof(struct hist_key), 74 .key_size = sizeof(struct hist_key),
75 .value_size = sizeof(long), 75 .value_size = sizeof(long),
76 .max_entries = 1024, 76 .max_entries = 1024,
diff --git a/samples/bpf/tracex2_user.c b/samples/bpf/tracex2_user.c
index cd0241c1447a..ab5b19e68acf 100644
--- a/samples/bpf/tracex2_user.c
+++ b/samples/bpf/tracex2_user.c
@@ -37,6 +37,8 @@ struct hist_key {
37static void print_hist_for_pid(int fd, void *task) 37static void print_hist_for_pid(int fd, void *task)
38{ 38{
39 struct hist_key key = {}, next_key; 39 struct hist_key key = {}, next_key;
40 unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
41 long values[nr_cpus];
40 char starstr[MAX_STARS]; 42 char starstr[MAX_STARS];
41 long value; 43 long value;
42 long data[MAX_INDEX] = {}; 44 long data[MAX_INDEX] = {};
@@ -49,7 +51,10 @@ static void print_hist_for_pid(int fd, void *task)
49 key = next_key; 51 key = next_key;
50 continue; 52 continue;
51 } 53 }
52 bpf_lookup_elem(fd, &next_key, &value); 54 bpf_lookup_elem(fd, &next_key, values);
55 value = 0;
56 for (i = 0; i < nr_cpus; i++)
57 value += values[i];
53 ind = next_key.index; 58 ind = next_key.index;
54 data[ind] = value; 59 data[ind] = value;
55 if (value && ind > max_ind) 60 if (value && ind > max_ind)
diff --git a/samples/bpf/tracex3_kern.c b/samples/bpf/tracex3_kern.c
index bf337fbb0947..9974c3d7c18b 100644
--- a/samples/bpf/tracex3_kern.c
+++ b/samples/bpf/tracex3_kern.c
@@ -20,7 +20,7 @@ struct bpf_map_def SEC("maps") my_map = {
20/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe 20/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
21 * example will no longer be meaningful 21 * example will no longer be meaningful
22 */ 22 */
23SEC("kprobe/blk_mq_start_request") 23SEC("kprobe/blk_start_request")
24int bpf_prog1(struct pt_regs *ctx) 24int bpf_prog1(struct pt_regs *ctx)
25{ 25{
26 long rq = PT_REGS_PARM1(ctx); 26 long rq = PT_REGS_PARM1(ctx);
@@ -42,13 +42,13 @@ static unsigned int log2l(unsigned long long n)
42#define SLOTS 100 42#define SLOTS 100
43 43
44struct bpf_map_def SEC("maps") lat_map = { 44struct bpf_map_def SEC("maps") lat_map = {
45 .type = BPF_MAP_TYPE_ARRAY, 45 .type = BPF_MAP_TYPE_PERCPU_ARRAY,
46 .key_size = sizeof(u32), 46 .key_size = sizeof(u32),
47 .value_size = sizeof(u64), 47 .value_size = sizeof(u64),
48 .max_entries = SLOTS, 48 .max_entries = SLOTS,
49}; 49};
50 50
51SEC("kprobe/blk_update_request") 51SEC("kprobe/blk_account_io_completion")
52int bpf_prog2(struct pt_regs *ctx) 52int bpf_prog2(struct pt_regs *ctx)
53{ 53{
54 long rq = PT_REGS_PARM1(ctx); 54 long rq = PT_REGS_PARM1(ctx);
@@ -81,7 +81,7 @@ int bpf_prog2(struct pt_regs *ctx)
81 81
82 value = bpf_map_lookup_elem(&lat_map, &index); 82 value = bpf_map_lookup_elem(&lat_map, &index);
83 if (value) 83 if (value)
84 __sync_fetch_and_add((long *)value, 1); 84 *value += 1;
85 85
86 return 0; 86 return 0;
87} 87}
diff --git a/samples/bpf/tracex3_user.c b/samples/bpf/tracex3_user.c
index 0aaa933ab938..48716f7f0d8b 100644
--- a/samples/bpf/tracex3_user.c
+++ b/samples/bpf/tracex3_user.c
@@ -20,11 +20,13 @@
20 20
21static void clear_stats(int fd) 21static void clear_stats(int fd)
22{ 22{
23 unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
24 __u64 values[nr_cpus];
23 __u32 key; 25 __u32 key;
24 __u64 value = 0;
25 26
27 memset(values, 0, sizeof(values));
26 for (key = 0; key < SLOTS; key++) 28 for (key = 0; key < SLOTS; key++)
27 bpf_update_elem(fd, &key, &value, BPF_ANY); 29 bpf_update_elem(fd, &key, values, BPF_ANY);
28} 30}
29 31
30const char *color[] = { 32const char *color[] = {
@@ -75,15 +77,20 @@ static void print_banner(void)
75 77
76static void print_hist(int fd) 78static void print_hist(int fd)
77{ 79{
78 __u32 key; 80 unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
79 __u64 value;
80 __u64 cnt[SLOTS];
81 __u64 max_cnt = 0;
82 __u64 total_events = 0; 81 __u64 total_events = 0;
82 long values[nr_cpus];
83 __u64 max_cnt = 0;
84 __u64 cnt[SLOTS];
85 __u64 value;
86 __u32 key;
87 int i;
83 88
84 for (key = 0; key < SLOTS; key++) { 89 for (key = 0; key < SLOTS; key++) {
90 bpf_lookup_elem(fd, &key, values);
85 value = 0; 91 value = 0;
86 bpf_lookup_elem(fd, &key, &value); 92 for (i = 0; i < nr_cpus; i++)
93 value += values[i];
87 cnt[key] = value; 94 cnt[key] = value;
88 total_events += value; 95 total_events += value;
89 if (value > max_cnt) 96 if (value > max_cnt)