Skip to content

Commit

Permalink
fix warns
Browse files Browse the repository at this point in the history
  • Loading branch information
elazarcoh committed Nov 24, 2021
1 parent abcc412 commit a948fb6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
19 changes: 13 additions & 6 deletions src/gpu-info-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import * as vscode from "vscode";
import parser = require("fast-xml-parser");
import { spawn } from "child_process";
import { CronJob } from "cron";
import { shallowEqual } from "./utils";
import { json, shallowEqual } from "./utils";
import { NVIDIA_SMI_FIELDS, resolveGpuInfoField } from "./nvidia-smi-fields";
import { configurations } from "./config";


type NvidiaSmiInfoJson = {
nvidia_smi_log: {
gpu: json[]
}
}

export type GpuInfo = {
id: number;
[key: string]: any;
[key: string]: string | number;
};

export type NvidiaSmiInfo = {
Expand Down Expand Up @@ -91,13 +98,13 @@ export class NvidiaSmiService implements vscode.Disposable {
const jsonObj = await nvidiaSmiAsJsonObject();
const gpus: GpuInfo[] = [];
for (const [gpuId, gpuInfo] of jsonObj.nvidia_smi_log.gpu.entries()) {
const gpuInfoFields: Record<string, any> = {};
const gpuInfoFields: Record<string, number | string> = {};
for (const [name, field] of Object.entries(NVIDIA_SMI_FIELDS)) {
gpuInfoFields[name] = resolveGpuInfoField(
gpuInfo,
field,
gpuInfoFields
);
) ?? "null";
}
gpus.push({
id: gpuId,
Expand All @@ -119,15 +126,15 @@ export class NvidiaSmiService implements vscode.Disposable {
}
}

export async function nvidiaSmiAsJsonObject(): Promise<any> {
export async function nvidiaSmiAsJsonObject(): Promise<NvidiaSmiInfoJson> {
const exec = configurations.get("executablePath", undefined, "nvidia-smi");

const child = spawn(exec, ["-q", "-x"]);
let xmlData = "";
for await (const data of child.stdout) {
xmlData += data.toString();
}
const jsonObj = parser.parse(xmlData, {}, true);
const jsonObj: NvidiaSmiInfoJson = parser.parse(xmlData, {}, true);

// for a system with a single GPU
if (!Array.isArray(jsonObj.nvidia_smi_log.gpu)) {
Expand Down
6 changes: 3 additions & 3 deletions src/gpu-treeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class GPUItem extends vscode.TreeItem {
}
}

function itemLabel(itemId: string, _itemValue: any): string {
function itemLabel(itemId: string, _itemValue: string | number): string {
const field = NVIDIA_SMI_FIELDS[itemId];
return `${field.label}`;
}

function itemDescription(itemId: string, itemValue: any): string {
function itemDescription(itemId: string, itemValue: string | number): string {
return `${itemValue}`;
}

Expand Down Expand Up @@ -62,7 +62,7 @@ function gpusInfo(info: NvidiaSmiInfo): GPUItem[] {
`GPU ${gpu.id}`,
vscode.TreeItemCollapsibleState.Collapsed,
GPUTreeItemType.gpuItem,
(gpuMainDescription ? gpu[gpuMainDescription] : undefined) ?? "",
(gpuMainDescription ? itemDescription(gpuMainDescription, gpu[gpuMainDescription]) : undefined) ?? "",
undefined,
gpuInfoItems(gpu)
)
Expand Down
23 changes: 19 additions & 4 deletions src/nvidia-smi-fields.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// keep the NVIDIA_SMI_FIELDS records in snake_case to match the configuration style
/* eslint-disable @typescript-eslint/naming-convention */
import * as vscode from 'vscode';
import { replaceAll } from './utils';
import { json, replaceAll } from './utils';

enum InfoFieldType {
expr,
Expand Down Expand Up @@ -37,12 +37,27 @@ export const NVIDIA_SMI_FIELDS: Record<string, InfoField> = {
};


export function resolveGpuInfoField(gpuInfo: any, field: InfoField, values: Record<string, any>): any {
export function resolveGpuInfoField(gpuInfo: json, field: InfoField, values: Record<string, string | number>): string | number | undefined {
switch (field.type) {
case InfoFieldType.value:
let val = gpuInfo;
for (const key of field.accessor) { val = val[key]; }
return val;
for (const key of field.accessor) {
try {
// @ts-expect-error. The accessor should be valid by definition.
val = val[key];
}
catch (e) {
console.log(`evaluation failed`, e);
return undefined;
}
}
if(typeof val === 'string' || typeof val === 'number') {
return val;
}
else {
console.log(`evaluation failed`, "expected a number or string");
return undefined;
}
case InfoFieldType.expr:
if (typeof field.accessor !== 'string') { return "!Error!"; }
let str = field.accessor;
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ export function shallowEqual(object1: Record<string, unknown>, object2: Record<s
export function replaceAll(string: string, search: string | RegExp, replace: string): string {
return string.split(search).join(replace);
}

export type json =
| string
| number
| boolean
| null
| json[]
| { [key: string]: json }

0 comments on commit a948fb6

Please sign in to comment.