GpuDashboard.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <div class="gpu-monitor-wrapper">
  3. <div class="monitor-header">
  4. <div class="header-left">
  5. <h3 class="page-title"><i class="el-icon-odometer"></i> 算力资源实时监控</h3>
  6. <span class="refresh-tip">
  7. <i class="el-icon-loading" v-if="loading"></i>
  8. <i class="el-icon-time" v-else></i>
  9. 自动刷新中(10s/次)
  10. </span>
  11. </div>
  12. <el-button type="primary" size="small" round icon="el-icon-refresh" :loading="loading" @click="fetchGpuData">立即同步</el-button>
  13. </div>
  14. <el-row :gutter="20">
  15. <el-col :span="14">
  16. <el-card shadow="never" class="info-card">
  17. <div slot="header" class="card-header">
  18. <span><i class="el-icon-processor"></i> 显卡硬件:{{ gpuData ? gpuData.gpu.model : '检测中...' }}</span>
  19. <el-tag type="success" size="mini" effect="dark">在线</el-tag>
  20. </div>
  21. <div v-if="gpuData" class="gpu-usage-flex">
  22. <div class="gauge-section">
  23. <el-progress
  24. type="dashboard"
  25. :percentage="calculatePercentage(gpuData.gpu.mem_used, gpuData.gpu.mem_total)"
  26. :stroke-width="12"
  27. :width="160"
  28. :color="customColors"
  29. ></el-progress>
  30. <div class="gauge-label">总显存占用率</div>
  31. </div>
  32. <div class="details-section">
  33. <div class="detail-item">
  34. <span class="label">已使用 (Used)</span>
  35. <span class="value used">{{ gpuData.gpu.mem_used }}</span>
  36. </div>
  37. <el-progress :percentage="calculatePercentage(gpuData.gpu.mem_used, gpuData.gpu.mem_total)" :show-text="false" :stroke-width="8" :color="customColors"></el-progress>
  38. <el-divider></el-divider>
  39. <div class="mini-stats">
  40. <div class="mini-item">
  41. <div class="m-label">空闲 (Free)</div>
  42. <div class="m-value">{{ gpuData.gpu.mem_free }}</div>
  43. </div>
  44. <div class="mini-item">
  45. <div class="m-label">总量 (Total)</div>
  46. <div class="m-value">{{ gpuData.gpu.mem_total }}</div>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. </el-card>
  52. </el-col>
  53. <el-col :span="10">
  54. <el-card shadow="never" class="info-card">
  55. <div slot="header" class="card-header">
  56. <span><i class="el-icon-connection"></i> PyTorch 显存池</span>
  57. </div>
  58. <div v-if="gpuData" class="torch-container">
  59. <div class="torch-box reserved">
  60. <div class="t-label">Reserved (预留)</div>
  61. <div class="t-value">{{ gpuData.torch.reserved }}</div>
  62. <div class="t-desc">Torch 预先向系统申请的缓存</div>
  63. </div>
  64. <div class="torch-box allocated">
  65. <div class="t-label">Allocated (分配)</div>
  66. <div class="t-value">{{ gpuData.torch.allocated }}</div>
  67. <div class="t-desc">当前 AI 模型实际占用的显存</div>
  68. </div>
  69. </div>
  70. </el-card>
  71. </el-col>
  72. </el-row>
  73. <el-card shadow="never" class="m-t-20 table-card">
  74. <div slot="header" class="card-header">
  75. <span><i class="el-icon-collection"></i> Ollama 推理引擎:运行中模型</span>
  76. </div>
  77. <el-table :data="gpuData ? gpuData.ollama : []" size="medium" style="width: 100%">
  78. <el-table-column label="模型名称" min-width="150">
  79. <template slot-scope="scope">
  80. <div class="model-cell">
  81. <div class="model-icon"><i class="el-icon-box"></i></div>
  82. <span class="model-name-text">{{ scope.row.model_name }}</span>
  83. </div>
  84. </template>
  85. </el-table-column>
  86. <el-table-column prop="size" label="磁盘大小" width="120"></el-table-column>
  87. <el-table-column prop="size_vram" label="显存占用 (VRAM)" width="150">
  88. <template slot-scope="scope">
  89. <span class="vram-text">{{ scope.row.size_vram }}</span>
  90. </template>
  91. </el-table-column>
  92. <el-table-column label="GPU 负载">
  93. <template slot-scope="scope">
  94. <el-progress
  95. :percentage="Number(scope.row.gpu_percentage)"
  96. :color="scope.row.gpu_percentage > 90 ? '#67c23a' : '#e6a23c'"
  97. :stroke-width="10"
  98. stroke-linecap="round"
  99. ></el-progress>
  100. </template>
  101. </el-table-column>
  102. <el-table-column prop="stat" label="活跃状态" width="120" align="center">
  103. <template slot-scope="scope">
  104. <span class="status-dot" :class="{ 'is-active': scope.row.stat.includes('OK') }"></span>
  105. {{ scope.row.stat }}
  106. </template>
  107. </el-table-column>
  108. </el-table>
  109. </el-card>
  110. </div>
  111. </template>
  112. <script>
  113. import axios from 'axios';
  114. export default {
  115. name: 'GpuDashboard',
  116. data() {
  117. return {
  118. gpuData: null,
  119. loading: false,
  120. timer: null,
  121. customColors: [
  122. {color: '#67c23a', percentage: 40},
  123. {color: '#e6a23c', percentage: 75},
  124. {color: '#f56c6c', percentage: 90}
  125. ]
  126. };
  127. },
  128. mounted() {
  129. this.fetchGpuData();
  130. this.timer = setInterval(this.fetchGpuData, 10000);
  131. },
  132. beforeDestroy() {
  133. if (this.timer) clearInterval(this.timer);
  134. },
  135. methods: {
  136. async fetchGpuData() {
  137. this.loading = true;
  138. try {
  139. const response = await axios.get('/api1/gpu/info');
  140. this.gpuData = response.data;
  141. } catch (error) {
  142. this.$message.error('获取 GPU 监控数据失败');
  143. } finally {
  144. setTimeout(() => { this.loading = false; }, 500);
  145. }
  146. },
  147. calculatePercentage(usedStr, totalStr) {
  148. const used = parseFloat(usedStr) || 0;
  149. const total = parseFloat(totalStr) || 1;
  150. return Math.min(Math.round((used / total) * 100), 100);
  151. }
  152. }
  153. };
  154. </script>
  155. <style scoped>
  156. .gpu-monitor-wrapper { padding: 0; }
  157. .m-t-20 { margin-top: 20px; }
  158. /* 头部样式 */
  159. .monitor-header {
  160. display: flex;
  161. justify-content: space-between;
  162. align-items: center;
  163. margin-bottom: 20px;
  164. }
  165. .page-title { margin: 0; font-size: 18px; color: #303133; display: flex; align-items: center; }
  166. .page-title i { margin-right: 8px; color: #409EFF; }
  167. .refresh-tip { font-size: 12px; color: #909399; margin-left: 15px; }
  168. /* 卡片通用 */
  169. .info-card { border: none; border-radius: 8px; }
  170. .card-header { display: flex; justify-content: space-between; align-items: center; font-weight: bold; }
  171. /* GPU 显存布局 */
  172. .gpu-usage-flex {
  173. display: flex;
  174. align-items: center;
  175. padding: 10px 0;
  176. }
  177. .gauge-section { width: 200px; text-align: center; border-right: 1px solid #f0f2f5; }
  178. .gauge-label { font-size: 13px; color: #909399; margin-top: -15px; }
  179. .details-section { flex: 1; padding-left: 30px; }
  180. .detail-item { display: flex; justify-content: space-between; margin-bottom: 8px; align-items: flex-end; }
  181. .detail-item .label { font-size: 13px; color: #606266; }
  182. .detail-item .value { font-size: 20px; font-weight: bold; font-family: 'PingFang SC', sans-serif; }
  183. .value.used { color: #f56c6c; }
  184. .mini-stats { display: flex; gap: 40px; }
  185. .m-label { font-size: 12px; color: #909399; margin-bottom: 4px; }
  186. .m-value { font-size: 14px; font-weight: 600; color: #303133; }
  187. /* PyTorch 盒子样式 */
  188. .torch-container { display: flex; flex-direction: column; gap: 12px; }
  189. .torch-box {
  190. padding: 15px;
  191. border-radius: 6px;
  192. position: relative;
  193. overflow: hidden;
  194. }
  195. .torch-box.reserved { background: #fdf6ec; border: 1px solid #faecd8; }
  196. .torch-box.allocated { background: #ecf5ff; border: 1px solid #d9ecff; }
  197. .t-label { font-size: 12px; color: #909399; }
  198. .t-value { font-size: 20px; font-weight: bold; margin: 5px 0; color: #303133; }
  199. .t-desc { font-size: 11px; color: #a8abb2; }
  200. /* 表格样式 */
  201. .table-card { border: none; }
  202. .model-cell { display: flex; align-items: center; }
  203. .model-icon {
  204. width: 32px;
  205. height: 32px;
  206. background: #f0f2f5;
  207. border-radius: 4px;
  208. display: flex;
  209. align-items: center;
  210. justify-content: center;
  211. margin-right: 10px;
  212. color: #409EFF;
  213. }
  214. .model-name-text { font-weight: 600; color: #303133; }
  215. .vram-text { color: #409EFF; font-family: monospace; font-weight: bold; }
  216. /* 状态小点 */
  217. .status-dot {
  218. display: inline-block;
  219. width: 8px;
  220. height: 8px;
  221. background: #909399;
  222. border-radius: 50%;
  223. margin-right: 6px;
  224. }
  225. .status-dot.is-active {
  226. background: #67c23a;
  227. box-shadow: 0 0 6px #67c23a;
  228. }
  229. </style>