Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magic_eye:加入kvm_watcher子项目 #913

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MagicEyes/src/backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ if (BUILD_ALL)
add_subdirectory(process/cpu_watcher)
add_subdirectory(process/proc_image)
# TODO(fzy, 2024.03.13) 没有kvm的环境
# add_subdirectory(hypervisor/kvm_watcher)
add_subdirectory(hypervisor/kvm_watcher)
add_subdirectory(system_diagnosis/stack_analyzer)
else ()
message(STATUS "----------------------------------------")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ set(CPU_WATCHER_INSTALL_DIR backend/${TOOL_BELONG_TO_MODULE}/${TOOL_NAME})
# 安装可执行文件到 ${CPU_WATCHER_INSTALL_DIR}/bin
install(TARGETS ${TOOL_NAME}
RUNTIME DESTINATION ${CPU_WATCHER_INSTALL_DIR}/bin)
# 安装配置文件到 ${CPU_WATCHER_INSTALL_DIR}/etc
# 安装配置文件到 ${CPU_WATCHER_INSTALL_DIR}/etc
113 changes: 75 additions & 38 deletions MagicEyes/src/backend/hypervisor/kvm_watcher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@

## 一、项目简介

`kvm_watcher` 是一个基于 eBPF 技术的项目,旨在在宿主机侧监控和提取 KVM 虚拟机的性能指标,同时对宿主机性能影响较小。该项目基于 eBPF 的实时监控方案,通过在宿主机中执行eBPF程序,实时捕获有关 KVM 虚拟机的关键性能数据和性能事件,提供全面的性能数据,帮助管理员优化虚拟化环境,改善虚拟机的运行效率和响应性,并且允许用户根据实际需求选择监控的指标和事件,实现个性化配置。
`kvm_watcher` 是一个基于 eBPF 技术的项目,旨在在宿主机侧监控和提取 KVM 虚拟机的性能指标,用于诊断对 `kvm` 可见的客户机行为,特别是与客户机相关的问题。同时对宿主机性能影响较小。该项目基于 eBPF 的实时监控方案,通过在宿主机中执行eBPF程序,实时捕获有关 KVM 虚拟机的关键性能数据和性能事件,提供全面的性能数据,帮助管理员优化虚拟化环境,改善虚拟机的运行效率和响应性,并且允许用户根据实际需求选择监控的指标和事件,实现个性化配置。

> 局限:鉴于不同体系结构的硬件辅助虚拟化技术,目前我们只适用于intel中的vmx技术。

## 二、功能介绍

`kvm_watcher`是一款基于eBPF的kvm检测工具,其旨在使用户方便快捷在宿主机侧获取kvm虚拟机中的各种信息。
`kvm_watcher`是一款基于eBPF的kvm虚拟机检测工具,其旨在使用户方便快捷在宿主机侧获取kvm虚拟机中的各种信息,报告所有正在运行的guest行为。

![kvm watcher项目框图](https://gitee.com/nan-shuaibo/image/raw/master/202404251704350.png)

目前,其实现的功能主要包括:

- **VM Exit 事件分析:**
- 捕获 VM Exit 事件,包括发生的时间戳、原因、次数以及处理时延等信息。
- **KVM mmu事件分析:**
- 监控 KVM 中的 mmu page fault 和mmio page fault 事件,记录gva、hva、pfn、错误类型和处理时延等关键信息。
- 实时监控kvm虚拟机中产生的dirty page,记录脏页地址、变脏时间、变脏次数和memslot等相关信息。
- **vCPU相关指标分析:**
- 记录有关vCPU的性能指标,包括唤醒时的时间戳,halt持续时间,vCPU id等相关信息。
- 实时监控vCPU的halt-polling时间的变化信息,包括vCPU的线程tid,变化类型,变化前后的halt-polling时间等信息。
- **kvm中中断注入时相关信息:**
- PIC:实时记录PIC芯片类型,中断引脚编号,中断触发方式,是否可屏蔽,处理延时,是否发生合并等信息。
- IOAPIC:
- MSI:
- **[VM Exit 事件分析](./docs/kvm_exit.md)**
- **[KVM mmu事件分析](./docs/kvm_mmu.md)**
- **[vCPU相关指标分析](./docs/kvm_vcpu.md)**
- **[kvm中中断注入记录](./docs/kvm_irq.md)**
- **[hypercall信息统计](./docs/kvm_hypercall.md)**

## 三、使用方法

Expand All @@ -36,14 +33,14 @@
**安装依赖:**

```
sudo apt install clang libelf1 libelf-dev zlib1g-dev libbpf-dev linux-tools-$(uname -r) linux-cloud-tools-$(uname -r)
sudo modprobe kvm && sudo modprobe kvm-intel //加载kvm模块
make deps
```


**编译运行:**

```
make
make bpf
sudo ./kvm_watcher [options]
make clean
```
Expand All @@ -53,61 +50,101 @@ make clean
`kvm_watcher`通过一系列命令参数来控制其具体行为:

```
_ ____ ____ __ __ ___ _____ ____ _ _ _____ ____
| |/ /\ \ / / \/ | \ \ / / \|_ _/ ___| | | | ____| _ \
| ' / \ \ / /| |\/| | \ \ /\ / / _ \ | || | | |_| | _| | |_) |
| . \ \ V / | | | | \ V V / ___ \| || |___| _ | |___| _ <
|_|\_\ \_/ |_| |_| \_/\_/_/ \_\_| \____|_| |_|_____|_| \_|
Usage: kvm_watcher [OPTION...]
BPF program used for monitoring KVM event

-c, --kvm_irqchip Monitor the irqchip setting information in KVM
VM.
-d, --mark_page_dirty Monitor virtual machine dirty page information.
-e, --vm_exit Monitoring the event of vm exit.
-e, --vm_exit Monitoring the event of vm exit(including exiting
to KVM and user mode).
-f, --kvmmmu_page_fault Monitoring the data of kvmmmu page fault.
-c, --kvm_irq Monitor the interrupt information in KVM VM.
-m, --mmio Monitoring the data of mmio page fault..(The -f option must be specified.)
-n, --halt_poll_ns Monitoring the variation in vCPU halt-polling time.
-h, --hypercall Monitor the hypercall information in KVM VM
-i, --irq_inject Monitor the virq injection information in KVM VM
-l, --kvm_ioctl Monitoring the KVM IOCTL.
-m, --mmio Monitoring the data of mmio page fault.(The -f
option must be specified.)
-n, --halt_poll_ns Monitoring the variation in vCPU halt-polling
time.
-o, --vcpu_load Monitoring the load of vcpu.
-p, --vm_pid=PID Specify the virtual machine pid to monitor.
-s, --stat Display statistical data.(The -e option must be specified.)
-t, --monitoring_time=SEC Time for monitoring.
-T, --kvm_timer Monitoring the KVM hv or software timer.
-v, --verbose Verbose debug output.
-w, --vcpu_wakeup Monitoring the wakeup of vcpu.
-?, --help Give this help list
--usage Give a short usage message
-V, --version Print program version
```

`-h`:输出帮助信息
Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Report bugs to <[email protected]>.
```

`-e`:记录vm exit事件信息
`-H`:输出帮助信息

`-s`:输出最后的vm exit事件统计信息(需要和`-e`一同使用)
`-e`:统计vm exit事件信息

`-f`:记录kvmmmu缺页信息

`-c:记录kvm中断芯片设置相关信息
`-c`:记录kvm中断芯片设置相关信息

`-h`:统计hypercall发生的信息

`-m`:记录mmio缺页信息(需要和`-f`一同使用)

`-d`:记录kvm脏页信息

`-h`:记录hypercall超级调用信息

`-n`:记录vcpu的halt-polling相关信息

`-w`:记录vcpu唤醒时的相关信息

`-l`:记录kvm相关ioctl系统调用命令字

`-o`:统计VCPU在PCPU上的运行时间及相关信息

`-p`:指定kvm虚拟机进程pid

`-t`:监控时间

## 四、代码结构

```
├── docs //功能模块说明文档
│ ├── kvm_exit.md
│ ├── kvm_hypercall.md
│ ├── kvm_irq.md
│ ├── kvm_mmu.md
│ └── kvm_vcpu.md
├── include
│ ├── kvm_exits.h //vm exit事件相关的内核bpf程序
│ ├── kvm_mmu.h //kvmmmu相关的内核bpf程序
│ ├── kvm_irq.h //kvm中断相关内核bpf程序
│ ├── kvm_vcpu.h //vcpu相关内核bpf程序
│ └── kvm_watcher.h //项目公用头文件
├── Makefile //编译脚本
│ ├── bpf //内核态bpf程序
│ │ ├── kvm_exits.h
│ │ ├── kvm_hypercall.h
│ │ ├── kvm_ioctl.h
│ │ ├── kvm_irq.h
│ │ ├── kvm_mmu.h
│ │ └── kvm_vcpu.h
│ ├── common.h //内核态和用户态公共头文件
│ └── helpers //用户态帮助函数
│ ├── trace_helpers.h
│ └── uprobe_helpers.h
├── Makefile //编译脚本
├── README.md
├── src
│ ├── kvm_watcher.bpf.c //内核态bpf入口程序
│ └── kvm_watcher.c //用户态bpf程序
└── temp
└── dirty_temp //脏页临时文件
│ ├── helpers //用户态帮助函数
│ │ ├── trace_helpers.c
│ │ └── uprobe_helpers.c
│ ├── kvm_watcher.bpf.c //内核态bpf程序入口
│ └── kvm_watcher.c //用户态bpf程序
└── temp //临时文件目录
```

## 五、测试
Expand All @@ -132,7 +169,7 @@ graph TD;
要运行测试,请执行以下命令:

```
make test
make deps test
```

这将自动执行上述测试流程,并在结束后提供测试结果。
Loading
Loading