diff --git a/MagicEyes/CMakeLists.txt b/MagicEyes/CMakeLists.txt index 549a17dfa..948af1244 100644 --- a/MagicEyes/CMakeLists.txt +++ b/MagicEyes/CMakeLists.txt @@ -158,3 +158,4 @@ endif () add_subdirectory(src/backend) add_subdirectory(src/bridge) add_subdirectory(src/magic_eyes_cli) +add_subdirectory(src/visualization) diff --git a/MagicEyes/src/visualization/CMakeLists.txt b/MagicEyes/src/visualization/CMakeLists.txt new file mode 100644 index 000000000..5083fdb93 --- /dev/null +++ b/MagicEyes/src/visualization/CMakeLists.txt @@ -0,0 +1,74 @@ +# 设置最低 CMake 版本要求 +cmake_minimum_required(VERSION 3.10) + +# 定义项目名称 +project(DataVisual LANGUAGES NONE) +#目标目录,install/visualization +set(VISUALIZATION_INSTALL_DIR visualization) +# 定义变量 A +set(EBPF_PROMETHEUS ${CMAKE_CURRENT_SOURCE_DIR}/eBPF_prometheus) + + +# 直接执行命令:go mod tidy +execute_process( + COMMAND go mod tidy + WORKING_DIRECTORY ${EBPF_PROMETHEUS} + RESULT_VARIABLE GO_MOD_TIDY_RESULT + OUTPUT_VARIABLE GO_MOD_TIDY_OUTPUT + ERROR_VARIABLE GO_MOD_TIDY_ERROR +) +if(NOT GO_MOD_TIDY_RESULT EQUAL 0) + message(FATAL_ERROR "Failed to run 'go mod tidy': ${GO_MOD_TIDY_ERROR}") +endif() + +# 直接执行命令:go build +execute_process( + COMMAND go build -o ${EBPF_PROMETHEUS}/data-visual main.go + WORKING_DIRECTORY ${EBPF_PROMETHEUS} + RESULT_VARIABLE GO_BUILD_RESULT + OUTPUT_VARIABLE GO_BUILD_OUTPUT + ERROR_VARIABLE GO_BUILD_ERROR +) +if(NOT GO_BUILD_RESULT EQUAL 0) + message(FATAL_ERROR "Failed to build 'data-visual': ${GO_BUILD_ERROR}") +endif() +#将生成的data-visual可执行文件 安装到install/visualization目录下 +install(PROGRAMS ${EBPF_PROMETHEUS}/data-visual + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE + DESTINATION ${VISUALIZATION_INSTALL_DIR} +) +#将checker,collector,dao,prom_core安装到安装到install/visualization目录下 +install(DIRECTORY ${EBPF_PROMETHEUS}/checker + ${EBPF_PROMETHEUS}/dao + ${EBPF_PROMETHEUS}/prom_core + DESTINATION ${VISUALIZATION_INSTALL_DIR} +) +# 创建目录并生成文件 +file(MAKE_DIRECTORY ${VISUALIZATION_INSTALL_DIR}/.output/data) +file(WRITE ${VISUALIZATION_INSTALL_DIR}/.output/data/offcpu_stack.txt "") + + +# 安装脚本文件 runimages.sh 到指定目录,并设置安装后的权限 +install(PROGRAMS ${EBPF_PROMETHEUS}/runimages.sh + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE + DESTINATION ${VISUALIZATION_INSTALL_DIR} +) +# 执行脚本:runimages.sh +execute_process( + COMMAND ./runimages.sh + WORKING_DIRECTORY ${VISUALIZATION_INSTALL_DIR} + RESULT_VARIABLE RUNIMAGES_RESULT + OUTPUT_VARIABLE RUNIMAGES_OUTPUT + ERROR_VARIABLE RUNIMAGES_ERROR +) +# 将vis.sh安装到目标目录 +install(PROGRAMS ${EBPF_PROMETHEUS}/vis.sh + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE + DESTINATION ${VISUALIZATION_INSTALL_DIR} +) + +# 清理目标:可选 +add_custom_target(clean_data + COMMAND ${CMAKE_COMMAND} -E rm -rf ${EBPF_PROMETHEUS}/dao/data.db ${EBPF_PROMETHEUS}/r/.output/data + COMMENT "Cleaning up generated data and files" +) diff --git a/MagicEyes/src/visualization/eBPF_prometheus b/MagicEyes/src/visualization/eBPF_prometheus new file mode 120000 index 000000000..d2ba976bf --- /dev/null +++ b/MagicEyes/src/visualization/eBPF_prometheus @@ -0,0 +1 @@ +../../../eBPF_Visualization/eBPF_prometheus/ \ No newline at end of file diff --git a/MagicEyes/src/visualization/run_images.sh b/MagicEyes/src/visualization/run_images.sh deleted file mode 100644 index 6a4b6ce99..000000000 --- a/MagicEyes/src/visualization/run_images.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -# ------------------------------------------------------------ -# 拷贝自:https://github.com/linuxkerneltravel/lmp/blob/develop/eBPF_Visualization/eBPF_prometheus/runimages.sh -# ------------------------------------------------------------ -# 定义镜像名 -prometheus_image="prom/prometheus" -grafana_image="grafana/grafana-enterprise" - -# 使用 docker ps 命令列出所有容器的 ID,过滤出指定镜像的容器 -prometheus_info=$(sudo docker ps -a -q --filter "ancestor=$prometheus_image") -grafana_info=$(sudo docker ps -a -q --filter "ancestor=$grafana_image") - -# 检查 Prometheus 容器是否存在 -if [ -n "$prometheus_info" ]; then - # 如果容器存在,获取容器的 ID - # 即获取存储在 $prometheus_info 变量中的容器 ID 列表的第一个容器 ID,并将其存储到 container_id 变量中 - container_id=$(echo "$prometheus_info" | head -n 1) - echo "prometheus 容器存在,id为$container_id。启动容器..." - sudo docker start $container_id -else - echo "容器不存在,开始创建容器,并启动服务" - # 启动一个新的 Prometheus 容器,映射主机的端口 9090 到容器的端口 9090, - # 同时将主机上的 Prometheus 配置文件挂载到容器内,以便配置 Prometheus 服务 - sudo docker run \ - -p 9090:9090 \ - -v ./prom_core/prometheus.yaml:/etc/prometheus/prometheus.yml \ - --name=prometheus prom/prometheus & -fi - -if [ -n "$grafana_info" ]; then - # 如果容器存在,获取容器的 ID - # 即获取存储在 $grafana_info 变量中的容器 ID 列表的第一个容器 ID,并将其存储到 container_id 变量中 - container_id=$(echo "$grafana_info" | head -n 1) - echo "grafana 容器存在,id为$container_id。启动容器..." - sudo docker start $container_id -else - echo "grafana容器不存在,开始创建容器,并启动服务" - # 启动一个新的 Grafana Enterprise 容器,映射主机的端口 3000 到容器的端口 3000,并指定容器名称为 "grafana" - sudo docker run -d -p 3000:3000 --name=grafana grafana/grafana-enterprise & -fi diff --git a/eBPF_Supermarket/CPU_Subsystem/cpu_watcher/cpu_watcher.c b/eBPF_Supermarket/CPU_Subsystem/cpu_watcher/cpu_watcher.c index e50e23ab2..f59178c50 100644 --- a/eBPF_Supermarket/CPU_Subsystem/cpu_watcher/cpu_watcher.c +++ b/eBPF_Supermarket/CPU_Subsystem/cpu_watcher/cpu_watcher.c @@ -438,7 +438,7 @@ static int print_all() return 0; } -int count[25]={0};//定义一个count数组,用于汇总schedul()调度时间,以log2(时间间隔)为统计依据; +int count[25]={0} , cs_flag=0;//定义一个count数组,用于汇总schedul()调度时间,以log2(时间间隔)为统计依据; static int handle_event(void *ctx, void *data,unsigned long data_sz) { const struct event *e = data; @@ -449,6 +449,8 @@ static int handle_event(void *ctx, void *data,unsigned long data_sz) i ++; } count[i]++; + if(!cs_flag) + cs_flag = 1; return 0; } static int print_hstgram(int i,int max,int per_len) @@ -1101,7 +1103,11 @@ int main(int argc, char **argv) printf("Error polling perf buffer: %d\n", err); break; } - histogram(); + struct cs_ctrl *cs_ctrl; + int ctrl_key = 0 ; + if(cs_flag){ + histogram(); + } } else if(env.SYSCALL_DELAY){ err = ring_buffer__poll(rb, 100 /* timeout, ms */); //ring_buffer__poll(),轮询打开ringbuf缓冲区。如果有事件,handle_event函数会执行 diff --git a/eBPF_Visualization/eBPF_prometheus/vis.sh b/eBPF_Visualization/eBPF_prometheus/vis.sh new file mode 100755 index 000000000..ea6861b19 --- /dev/null +++ b/eBPF_Visualization/eBPF_prometheus/vis.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# 检查参数数量是否足够 +if [ $# -lt 4 ]; then + echo "Usage: $0 [other_args...]" + exit 1 +fi + +# 获取传递的参数 +PARAM1=$1 +PARAM2=$2 +PARAM3=$3 +PARAM4=$4 +OTHER_ARGS=${@:5} # 从第5个参数开始的所有额外参数 + +# 构建路径 +TARGET_DIR="../backend/${PARAM1}/${PARAM2}/bin" + +# 构建命令 +COMMAND="sudo ./data-visual collect ${TARGET_DIR}/${PARAM3} ${PARAM4} ${OTHER_ARGS}" + +# 显示即将执行的命令(可选) +echo "Executing command: ${COMMAND}" + +# 执行命令 +eval "${COMMAND}"