mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-05-08 06:42:16 +08:00
fix hardirqs
This commit is contained in:
31
10-hardirqs/bits.bpf.h
Normal file
31
10-hardirqs/bits.bpf.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
|
||||||
|
#ifndef __BITS_BPF_H
|
||||||
|
#define __BITS_BPF_H
|
||||||
|
|
||||||
|
#define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
|
||||||
|
#define WRITE_ONCE(x, val) ((*(volatile typeof(x) *)&(x)) = val)
|
||||||
|
|
||||||
|
static __always_inline u64 log2(u32 v)
|
||||||
|
{
|
||||||
|
u32 shift, r;
|
||||||
|
|
||||||
|
r = (v > 0xFFFF) << 4; v >>= r;
|
||||||
|
shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
|
||||||
|
shift = (v > 0xF) << 2; v >>= shift; r |= shift;
|
||||||
|
shift = (v > 0x3) << 1; v >>= shift; r |= shift;
|
||||||
|
r |= (v >> 1);
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static __always_inline u64 log2l(u64 v)
|
||||||
|
{
|
||||||
|
u32 hi = v >> 32;
|
||||||
|
|
||||||
|
if (hi)
|
||||||
|
return log2(hi) + 32;
|
||||||
|
else
|
||||||
|
return log2(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* __BITS_BPF_H */
|
||||||
@@ -15,6 +15,10 @@ const volatile bool targ_dist = false;
|
|||||||
const volatile bool targ_ns = false;
|
const volatile bool targ_ns = false;
|
||||||
const volatile bool do_count = false;
|
const volatile bool do_count = false;
|
||||||
|
|
||||||
|
struct irq_key {
|
||||||
|
char name[32];
|
||||||
|
};
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
|
__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
|
||||||
__type(key, u32);
|
__type(key, u32);
|
||||||
@@ -29,6 +33,7 @@ struct {
|
|||||||
__type(value, u64);
|
__type(value, u64);
|
||||||
} start SEC(".maps");
|
} start SEC(".maps");
|
||||||
|
|
||||||
|
/// @sample {"interval": 1000, "type" : "log2_hist"}
|
||||||
struct {
|
struct {
|
||||||
__uint(type, BPF_MAP_TYPE_HASH);
|
__uint(type, BPF_MAP_TYPE_HASH);
|
||||||
__uint(max_entries, MAX_ENTRIES);
|
__uint(max_entries, MAX_ENTRIES);
|
||||||
@@ -127,4 +132,4 @@ int BPF_PROG(irq_handler_exit, int irq, struct irqaction *action)
|
|||||||
return handle_exit(irq, action);
|
return handle_exit(irq, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
char LICENSE[] SEC("license") = "GPL";
|
char LICENSE[] SEC("license") = "GPL";
|
||||||
|
|||||||
@@ -4,10 +4,6 @@
|
|||||||
|
|
||||||
#define MAX_SLOTS 20
|
#define MAX_SLOTS 20
|
||||||
|
|
||||||
struct irq_key {
|
|
||||||
char name[32];
|
|
||||||
};
|
|
||||||
|
|
||||||
struct info {
|
struct info {
|
||||||
__u64 count;
|
__u64 count;
|
||||||
__u32 slots[MAX_SLOTS];
|
__u32 slots[MAX_SLOTS];
|
||||||
26
10-hardirqs/maps.bpf.h
Normal file
26
10-hardirqs/maps.bpf.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
|
||||||
|
// Copyright (c) 2020 Anton Protopopov
|
||||||
|
#ifndef __MAPS_BPF_H
|
||||||
|
#define __MAPS_BPF_H
|
||||||
|
|
||||||
|
#include <bpf/bpf_helpers.h>
|
||||||
|
#include <asm-generic/errno.h>
|
||||||
|
|
||||||
|
static __always_inline void *
|
||||||
|
bpf_map_lookup_or_try_init(void *map, const void *key, const void *init)
|
||||||
|
{
|
||||||
|
void *val;
|
||||||
|
long err;
|
||||||
|
|
||||||
|
val = bpf_map_lookup_elem(map, key);
|
||||||
|
if (val)
|
||||||
|
return val;
|
||||||
|
|
||||||
|
err = bpf_map_update_elem(map, key, init, BPF_NOEXIST);
|
||||||
|
if (err && err != -EEXIST)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return bpf_map_lookup_elem(map, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* __MAPS_BPF_H */
|
||||||
Reference in New Issue
Block a user