-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Session usage monitor in react
- Loading branch information
1 parent
0479273
commit e85e6a4
Showing
2 changed files
with
266 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
import { iSizeToSize, toFixedFloorWithoutTrailingZeros } from '../helper'; | ||
import Flex from './Flex'; | ||
import { ResourceTypeIcon } from './ResourceNumber'; | ||
import { SessionUsageMonitorFragment$key } from './__generated__/SessionUsageMonitorFragment.graphql'; | ||
import { Col, Progress, ProgressProps, Row, Typography, theme } from 'antd'; | ||
import { createStyles } from 'antd-style'; | ||
import graphql from 'babel-plugin-relay/macro'; | ||
import _ from 'lodash'; | ||
import { useMemo } from 'react'; | ||
import { useFragment } from 'react-relay'; | ||
|
||
const useStyles = createStyles(({ css, token }) => ({ | ||
progress: css` | ||
.ant-progress-text { | ||
font-size: ${token.fontSizeSM} !important; | ||
} | ||
.ant-progress-inner { | ||
} | ||
.ant-typography { | ||
color: ${token.colorTextSecondary} !important; | ||
font-size: ${token.fontSizeSM}px !important; | ||
} | ||
`, | ||
})); | ||
|
||
interface SessionUsageMonitorProps extends ProgressProps { | ||
sessionFrgmt: SessionUsageMonitorFragment$key | null; | ||
col?: 1 | 2 | 3 | 4; | ||
size?: ProgressProps['size']; | ||
} | ||
|
||
// compute session node query returns accelerator type as `[device_name]_[util|mem]` | ||
// so we need to split via '_' and get the first part to map to the resource type in `resourceTypeIconSrcMap` | ||
|
||
type acceleratorType = | ||
| 'cuda' | ||
| 'rocm' | ||
| 'tpu' | ||
| 'ipu' | ||
| 'atom' | ||
| 'atom-plus' | ||
| 'gaudi2' | ||
| 'warboy' | ||
| 'rngd' | ||
| 'hyperaccel-lpu'; | ||
|
||
const resourceTypeMap: { | ||
[key in acceleratorType | 'cpu' | 'mem']: string; | ||
} = { | ||
cpu: 'cpu', | ||
mem: 'mem', | ||
cuda: 'cuda.device', | ||
rocm: 'rocm.device', | ||
tpu: 'tpu.device', | ||
ipu: 'ipu.device', | ||
atom: 'atom.device', | ||
'atom-plus': 'atom-plus.device', | ||
gaudi2: 'gaudi2.device', | ||
warboy: 'warboy.device', | ||
rngd: 'rngd.device', | ||
'hyperaccel-lpu': 'hyperaccel-lpu.device', | ||
}; | ||
|
||
const gridColSpanMap = { | ||
1: 24, | ||
2: 12, | ||
3: 8, | ||
4: 6, | ||
}; | ||
|
||
const SessionUsageMonitor: React.FC<SessionUsageMonitorProps> = ({ | ||
sessionFrgmt, | ||
col = 1, | ||
size = 'default', | ||
}) => { | ||
const { token } = theme.useToken(); | ||
const { styles } = useStyles(); | ||
|
||
const kernel_nodes = useFragment( | ||
graphql` | ||
fragment SessionUsageMonitorFragment on ComputeSessionNode { | ||
kernel_nodes { | ||
edges { | ||
node { | ||
live_stat | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
sessionFrgmt, | ||
); | ||
|
||
const liveStat = JSON.parse( | ||
_.get(kernel_nodes, 'kernel_nodes.edges[0].node.live_stat') ?? '{}', | ||
); | ||
const sortedLiveStat = useMemo( | ||
() => | ||
Object.keys(liveStat) | ||
.sort((a, b) => { | ||
const aUtil = a.includes('_util'); | ||
const bUtil = b.includes('_util'); | ||
const aMem = a.includes('_mem'); | ||
const bMem = b.includes('_mem'); | ||
|
||
if (aUtil && !bUtil) return -1; | ||
if (!aUtil && bUtil) return 1; | ||
if (aMem && !bMem) return -1; | ||
if (!aMem && bMem) return 1; | ||
|
||
return 0; | ||
}) | ||
.reduce((acc: { [key: string]: any }, key) => { | ||
acc[key] = liveStat[key]; | ||
return acc; | ||
}, {}), | ||
[liveStat], | ||
); | ||
|
||
return ( | ||
<> | ||
<Row className={styles.progress} gutter={token.marginXL}> | ||
{sortedLiveStat?.cpu_util ? ( | ||
<Col | ||
span={_.get(gridColSpanMap, col)} | ||
style={{ marginBottom: token.marginXS }} | ||
> | ||
<Flex direction="column" align="end"> | ||
<Flex | ||
align="start" | ||
gap={token.marginSM} | ||
style={{ width: '100%' }} | ||
> | ||
<ResourceTypeIcon | ||
type={resourceTypeMap.cpu} | ||
showTooltip={true} | ||
/> | ||
<Progress | ||
showInfo | ||
status="normal" | ||
percent={_.toNumber( | ||
toFixedFloorWithoutTrailingZeros( | ||
liveStat?.cpu_util?.pct || 0, | ||
1, | ||
), | ||
)} | ||
strokeLinecap="butt" | ||
size={size === 'default' ? { height: token.sizeMD } : size} | ||
strokeColor={token.colorPrimary} | ||
></Progress> | ||
</Flex> | ||
{size === 'small' ? ( | ||
<Typography.Text> | ||
{`${liveStat?.cpu_util?.pct} %`} | ||
</Typography.Text> | ||
) : null} | ||
</Flex> | ||
</Col> | ||
) : null} | ||
{sortedLiveStat?.mem ? ( | ||
<Col | ||
span={_.get(gridColSpanMap, col)} | ||
style={{ marginBottom: token.marginXS }} | ||
> | ||
<Flex direction="column" align="end"> | ||
<Flex | ||
align="start" | ||
gap={token.marginSM} | ||
style={{ width: '100%' }} | ||
> | ||
<ResourceTypeIcon | ||
type={resourceTypeMap.mem} | ||
showTooltip={true} | ||
/> | ||
<Progress | ||
showInfo | ||
status="normal" | ||
percent={_.toNumber( | ||
toFixedFloorWithoutTrailingZeros( | ||
sortedLiveStat?.mem?.pct || 0, | ||
1, | ||
), | ||
)} | ||
strokeLinecap="butt" | ||
size={{ height: 20 }} | ||
strokeColor={token.colorPrimary} | ||
></Progress> | ||
</Flex> | ||
<Typography.Text> | ||
{`(mem) ${ | ||
iSizeToSize(sortedLiveStat?.mem?.current, 'g')?.numberUnit | ||
}iB | ||
/ | ||
${ | ||
iSizeToSize(sortedLiveStat?.mem?.capacity, 'g')?.numberUnit | ||
}iB`} | ||
</Typography.Text> | ||
</Flex> | ||
</Col> | ||
) : null} | ||
{/* accelerator type is in the format of `[device_name]_[util|mem]` */} | ||
{_.map(sortedLiveStat, (value, key) => { | ||
const device = key.split('_')?.[0] as keyof typeof resourceTypeMap; | ||
|
||
return _.includes( | ||
_.keysIn(_.omit(resourceTypeMap, 'cpu', 'mem')), | ||
device, | ||
) ? ( | ||
<> | ||
{/* only show accelerator's util and mem */} | ||
{(_.includes(key, 'util') || _.includes(key, 'mem')) && ( | ||
<Col | ||
span={_.get(gridColSpanMap, col)} | ||
style={{ marginBottom: token.marginXS }} | ||
> | ||
<Flex direction="column" align="end"> | ||
<Flex | ||
align="start" | ||
gap={token.marginSM} | ||
style={{ width: '100%' }} | ||
> | ||
<ResourceTypeIcon | ||
type={resourceTypeMap[device]} | ||
showTooltip={true} | ||
/> | ||
<Progress | ||
showInfo | ||
status="normal" | ||
percent={_.toNumber( | ||
toFixedFloorWithoutTrailingZeros( | ||
sortedLiveStat?.[device]?.pct || 0, | ||
1, | ||
), | ||
)} | ||
strokeLinecap="butt" | ||
size={{ height: token.sizeMD }} | ||
strokeColor={token.colorPrimary} | ||
></Progress> | ||
</Flex> | ||
<Typography.Text> | ||
{_.includes(key, 'mem') && | ||
`${device} (mem) ${iSizeToSize(sortedLiveStat?.mem?.current, 'g')?.numberUnit}iB / ${iSizeToSize(sortedLiveStat?.mem?.capacity, 'g')?.numberUnit}iB`} | ||
</Typography.Text> | ||
</Flex> | ||
</Col> | ||
)} | ||
</> | ||
) : null; | ||
})} | ||
</Row> | ||
<Flex justify="end" style={{ width: '100%' }}> | ||
<Typography.Text> | ||
{`I/O Read: ${iSizeToSize(sortedLiveStat?.io_read?.current, 'g')?.numberUnit}B / Write: ${iSizeToSize(sortedLiveStat?.io_write?.current, 'g')?.numberUnit}B`} | ||
</Typography.Text> | ||
</Flex> | ||
</> | ||
); | ||
}; | ||
|
||
export default SessionUsageMonitor; |