-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
175 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// author: [email protected] [email protected] [email protected] | ||
// author: [email protected] | ||
|
||
#include <vmlinux.h> | ||
#include <bpf/bpf_helpers.h> | ||
|
@@ -22,57 +22,70 @@ | |
|
||
char LICENSE[] SEC("license") = "Dual BSD/GPL"; | ||
|
||
BPF_HASH(mutex_info_map,u64,struct mutex_info, 1024); | ||
|
||
const int ctrl_key = 0; | ||
BPF_HASH(mutex_info_map, u64, struct mutex_info_kernel, 1024); | ||
BPF_ARRAY(mu_ctrl_map, int, struct mu_ctrl, 1); | ||
struct { | ||
__uint(type, BPF_MAP_TYPE_RINGBUF); | ||
__uint(max_entries, 256 * 1024); | ||
} rb SEC(".maps"); | ||
|
||
static inline struct mu_ctrl *get_mu_ctrl(void) { | ||
struct mu_ctrl *mu_ctrl; | ||
mu_ctrl = bpf_map_lookup_elem(&mu_ctrl_map, &ctrl_key); | ||
if (!mu_ctrl || !mu_ctrl->mu_func) { | ||
return NULL; | ||
} | ||
return mu_ctrl; | ||
} | ||
|
||
/*----------------------------------------------*/ | ||
/* 内核态互斥锁 */ | ||
/*----------------------------------------------*/ | ||
|
||
SEC("kprobe/mutex_lock") | ||
int BPF_KPROBE(trace_mutex_lock, struct mutex *lock) { | ||
u64 lock_addr = (u64)lock; // 获取锁地址 | ||
u64 ts = bpf_ktime_get_ns(); | ||
struct mutex_info *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr); | ||
u64 lock_addr = (u64)lock; // 获取锁地址 | ||
u64 ts = bpf_ktime_get_ns(); | ||
struct mutex_info_kernel *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr); | ||
if (info) { | ||
info->acquire_time = ts; // 保存锁获取时间 | ||
info->acquire_time = ts; // 保存锁获取时间 | ||
} else { | ||
struct mutex_info new_info = { | ||
struct mutex_info_kernel new_info = { | ||
.locked_total = 0, | ||
.locked_max = 0, | ||
.contended_total = 0, | ||
.count = 0, | ||
.last_owner = 0, | ||
.acquire_time = ts, | ||
.ptr = lock_addr | ||
}; | ||
bpf_map_update_elem(&mutex_info_map, &lock_addr, &new_info, BPF_ANY); | ||
__builtin_memset(new_info.last_name, 0, sizeof(new_info.last_name)); | ||
bpf_map_update_elem(&mutex_info_map, &lock_addr, &new_info, BPF_ANY); | ||
} | ||
return 0; | ||
} | ||
|
||
SEC("kprobe/mutex_trylock") | ||
int BPF_KPROBE(trace_mutex_trylock, struct mutex *lock) { | ||
int ret = PT_REGS_RC(ctx); | ||
if (ret == 0) { // 成功获取锁 | ||
u64 lock_addr = (u64)lock; // 获取锁地址 | ||
u64 ts = bpf_ktime_get_ns(); | ||
struct mutex_info *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr); | ||
if (ret != 0) { // 成功获取锁 | ||
u64 lock_addr = (u64)lock; // 获取锁地址 | ||
u64 ts = bpf_ktime_get_ns(); | ||
struct mutex_info_kernel *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr); | ||
if (info) { | ||
info->acquire_time = ts; | ||
info->acquire_time = ts; | ||
} else { | ||
struct mutex_info new_info = { | ||
struct mutex_info_kernel new_info = { | ||
.locked_total = 0, | ||
.locked_max = 0, | ||
.contended_total = 0, | ||
.count = 0, | ||
.last_owner = 0, | ||
.acquire_time = ts, | ||
.ptr = lock_addr | ||
}; | ||
__builtin_memset(new_info.last_name, 0, sizeof(new_info.last_name)); | ||
bpf_map_update_elem(&mutex_info_map, &lock_addr, &new_info, BPF_ANY); | ||
} | ||
} | ||
|
@@ -81,6 +94,7 @@ int BPF_KPROBE(trace_mutex_trylock, struct mutex *lock) { | |
|
||
SEC("kprobe/__mutex_lock_slowpath") | ||
int BPF_KPROBE(trace_mutex_lock_slowpath, struct mutex *lock) { | ||
struct mu_ctrl *mu_ctrl = get_mu_ctrl(); | ||
struct mutex_contention_event *e; | ||
struct task_struct *owner_task; | ||
struct task_struct *contender_task; | ||
|
@@ -98,7 +112,7 @@ int BPF_KPROBE(trace_mutex_lock_slowpath, struct mutex *lock) { | |
bpf_probe_read_kernel(&owner, sizeof(owner), &lock->owner); | ||
owner_task = (struct task_struct *)(owner & ~0x1L); | ||
contender_task = (struct task_struct *)bpf_get_current_task(); | ||
bpf_probe_read_kernel(&e->contender_prio, sizeof(e->contender_prio), &contender_task->prio); | ||
bpf_probe_read_kernel(&e->contender_prio, sizeof(e->contender_prio), &contender_task->prio); | ||
if (owner_task) { | ||
bpf_probe_read_kernel(&e->owner_pid, sizeof(e->owner_pid), &owner_task->pid); | ||
bpf_probe_read_kernel_str(&e->owner_name, sizeof(e->owner_name), owner_task->comm); | ||
|
@@ -107,18 +121,22 @@ int BPF_KPROBE(trace_mutex_lock_slowpath, struct mutex *lock) { | |
e->owner_pid = 0; | ||
__builtin_memset(e->owner_name, 0, sizeof(e->owner_name)); | ||
} | ||
struct mutex_info *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr); | ||
struct mutex_info_kernel *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr); | ||
if (info) { | ||
info->contended_total += ts - info->acquire_time; | ||
u64 contention_start = ts; | ||
info->contended_total += (contention_start - info->acquire_time); // 更新争用时间 | ||
info->count++; // 更新争用次数 | ||
} else { | ||
struct mutex_info new_info = { | ||
struct mutex_info_kernel new_info = { | ||
.locked_total = 0, | ||
.locked_max = 0, | ||
.contended_total = ts, | ||
.contended_total = 0, | ||
.count = 1, // 初始化争用次数 | ||
.last_owner = 0, | ||
.acquire_time = 0, | ||
.acquire_time = ts, // 初始化获取时间 | ||
.ptr = lock_addr | ||
}; | ||
__builtin_memset(new_info.last_name, 0, sizeof(new_info.last_name)); | ||
bpf_map_update_elem(&mutex_info_map, &lock_addr, &new_info, BPF_ANY); | ||
} | ||
bpf_ringbuf_submit(e, 0); | ||
|
@@ -130,43 +148,46 @@ int BPF_KPROBE(trace_mutex_unlock, struct mutex *lock) { | |
u64 lock_addr = (u64)lock; | ||
u64 ts = bpf_ktime_get_ns(); | ||
pid_t pid = bpf_get_current_pid_tgid(); | ||
struct mutex_info *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr); | ||
struct mutex_info_kernel *info = bpf_map_lookup_elem(&mutex_info_map, &lock_addr); | ||
if (info) { | ||
u64 held_time = ts - info->acquire_time; // 计算锁被持有的时间 | ||
info->locked_total += held_time; // 更新锁被持有的总时间 | ||
if (held_time > info->locked_max) { | ||
info->locked_max = held_time; // 更新锁被持有的最长时间 | ||
} | ||
info->last_owner = pid; // 更新最后一次持有该锁的线程ID | ||
bpf_get_current_comm(&info->last_name, sizeof(info->last_name)); // 更新最后一次持有该锁的线程名称 | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
|
||
/*----------------------------------------------*/ | ||
/* 用户态互斥锁 */ | ||
/*----------------------------------------------*/ | ||
|
||
// SEC("uprobe") | ||
// int BPF_KPROBE(pthread_mutex_lock_init, pthread_mutex_t *mutex){ | ||
SEC("uprobe") | ||
int BPF_KPROBE(pthread_mutex_lock_init, void *__mutex){ | ||
|
||
// } | ||
} | ||
|
||
// SEC("uprobe") | ||
// int BPF_KPROBE(pthread_mutex_lock,pthread_mutex_t *mutex){ | ||
SEC("uprobe") | ||
int BPF_KPROBE(pthread_mutex_lock,void *__mutex){ | ||
|
||
// } | ||
} | ||
|
||
// SEC("uprobe") | ||
// int BPF_KPROBE(pthread_mutex_try, pthread_mutex_t *mutex){ | ||
SEC("uprobe") | ||
int BPF_KPROBE(pthread_mutex_trylock, void *__mutex){ | ||
|
||
// } | ||
} | ||
|
||
// SEC("uprobe") | ||
// int BPF_KPROBE(pthread_mutex_unlock, pthread_mutex_t *mutex){ | ||
SEC("uprobe") | ||
int BPF_KPROBE(pthread_mutex_unlock, void *__mutex){ | ||
|
||
// } | ||
} | ||
|
||
// SEC("uprobe") | ||
// int BPF_KPROBE(pthread_mutex_destroy, pthread_mutex_t *mutex){ | ||
SEC("uprobe") | ||
int BPF_KPROBE(pthread_mutex_destroy,void *__mutex){ | ||
|
||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.