{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "856ee2ad",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "# 1. 环境依赖安装,首次安装后无需重复安装\n",
    "!pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07af41c5",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "# 2. CPU、NPU / CPU、NPU、NUMA拓扑关系可视化\n",
    "!python3 topology_visualizer.py\n",
    "\n",
    "from IPython.display import IFrame\n",
    "IFrame(\"ascend_topo.html\", width=\"100%\", height=\"850px\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e9ab67b",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "# 3. 当前CPU间模型主要运行线程可视化\n",
    "from key_pstree_visualizer import KeyPstreeVisualizer\n",
    "\n",
    "keyPstreeVisualizer = KeyPstreeVisualizer()\n",
    "# 用户可以输入 PID 或进程名/正则,例如:roots = keyPstreeVisualizer.build_pstree(extra_input=[\"python\", 1234])\n",
    "roots = keyPstreeVisualizer.build_pstree()\n",
    "\n",
    "print(\"=== 全部进程树 ===\")\n",
    "keyPstreeVisualizer.print_tree(roots)\n",
    "\n",
    "# 交互式搜索\n",
    "keyPstreeVisualizer.interactive_search(roots)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9269b465",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "# 4. 模型主要运行线程绑核建议\n",
    "from cpu_binding_suggestion import CpuBindingSuggestion\n",
    "from IPython.display import Markdown, display\n",
    "\n",
    "display(Markdown(CpuBindingSuggestion.generate_markdown()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a18f411",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "# 5. 绑核后模型主要运行线程可视化校验\n",
    "'''\n",
    "usage: cpu_affinity_data_collection.py [-h] [--csv]\n",
    "                                       [--npu-process [NPU_PROCESS ...]]\n",
    "                                       [--datawork-process [DATAWORK_PROCESS ...]]\n",
    "\n",
    "CPU Binding Validation\n",
    "\n",
    "options:\n",
    "  -h, --help            show this help message and exit\n",
    "  --csv                 输出 CSV 格式\n",
    "  --npu-process [NPU_PROCESS ...]\n",
    "                        npu进程额外关注的线程名\n",
    "  --datawork-process [DATAWORK_PROCESS ...]\n",
    "                        datawork要扫描的进程/线程关键词 (不输入则不扫描)\n",
    "'''\n",
    "# 导出为csv格式:!python3 cpu_affinity_data_collection.py --csv > test.csv\n",
    "# 直接打屏输出:!python3 cpu_affinity_data_collection.py --npu-process hccp_connect --datawork-process hccp_epoll\n",
    "\n",
    "import os\n",
    "from cpu_affinity_data_visualizer import run_notebook_app\n",
    "\n",
    "# ==========================================================\n",
    "# 快捷配置区:只需修改这里\n",
    "# ==========================================================\n",
    "# 1. NPU 相关线程关键字 (例如: 'hccp',不填则只扫描默认内容)\n",
    "NPU_PROCESS = \"CommWorker DataWorker\" \n",
    "\n",
    "# 2. 业务进程/线程关键字 (不填则不扫描)\n",
    "DATAWORK_PROCESS = \"\" \n",
    "\n",
    "# 3. 数据保存文件名\n",
    "OUTPUT_FILE = \"affinity_data.csv\"\n",
    "# ==========================================================\n",
    "npu_part = f\"--npu-process {NPU_PROCESS}\" if NPU_PROCESS else \"\"\n",
    "dw_part = f\"--datawork-process {DATAWORK_PROCESS}\" if DATAWORK_PROCESS else \"\"\n",
    "collect_cmd = f\"python3 cpu_affinity_data_collection.py --csv {npu_part} {dw_part} > {OUTPUT_FILE}\"\n",
    "\n",
    "print(f\"开始执行采集...\")\n",
    "print(f\"指令: {collect_cmd}\")\n",
    "os.system(collect_cmd)\n",
    "\n",
    "if os.path.exists(OUTPUT_FILE) and os.path.getsize(OUTPUT_FILE) > 0:\n",
    "    print(f\"采集成功!正在加载可视化界面...\")\n",
    "    run_notebook_app(OUTPUT_FILE)\n",
    "else:\n",
    "    print(f\"错误:未能生成数据\")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}