Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Commit

Permalink
panel content for #4 and #7
Browse files Browse the repository at this point in the history
  • Loading branch information
echarles committed Feb 14, 2022
1 parent 9a7f312 commit f8831be
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 81 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ jlab-watch:
--ServerApp.jpserver_extensions="{'jupyterlab_resource_usage': True}" \
--port 8234)

kill:
lsof -i TCP:8234 | grep LISTEN | awk '{print $2}' | xargs kill -9

watch:
($(CONDA_ACTIVATE) ${ENV_NAME}; \
yarn watch )
Expand Down
17 changes: 15 additions & 2 deletions notebooks/cpu.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "0ccb3a21-64c5-4ee2-a4fc-59c23afb0944",
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Input \u001b[0;32mIn [1]\u001b[0m, in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 6\u001b[0m x\u001b[38;5;241m*\u001b[39mx\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# processes = cpu_count()\u001b[39;00m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;66;03m# print('utilizing %d cores\\n' % processes)\u001b[39;00m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# pool = Pool(processes)\u001b[39;00m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# pool.map(f, range(processes))\u001b[39;00m\n\u001b[0;32m---> 13\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Input \u001b[0;32mIn [1]\u001b[0m, in \u001b[0;36mf\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mf\u001b[39m(x):\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m 6\u001b[0m x\u001b[38;5;241m*\u001b[39mx\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"# from multiprocessing import Pool\n",
"# from multiprocessing import cpu_count\n",
Expand Down
153 changes: 74 additions & 79 deletions src/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import useInterval from './useInterval';
type Usage = {
timestamp: Date | null;
kernelId: string;
hostname: string;
kernel_cpu: number;
kernel_memory: number;
host_cpu_percent: number;
Expand All @@ -26,43 +27,18 @@ type Usage = {
};
};

const UNKONWN_USAGE: Usage = {
timestamp: null,
kernelId: '',
kernel_cpu: -1,
kernel_memory: -1,
host_cpu_percent: -1,
host_virtual_memory: {
active: -1,
available: -1,
free: -1,
inactive: -1,
percent: -1,
total: -1,
used: -1,
wired: -1
}
};

const POLL_INTERVAL_SEC = 5;

const POLL_MAX_INTERVAL_SEC = 300;

type KernelPoll = {
poll: Poll<void, any, 'stand-by'>;
usage: Usage;
path: string;
usage: Usage | undefined;
};

const kernelPools = new Map<string, KernelPoll>();

const getUsage = (kernelId: string) => {
const kernelPoll = kernelPools.get(kernelId);
if (kernelPoll) {
return kernelPoll.usage;
}
return UNKONWN_USAGE;
};

const KernelUsage = (props: {
widgetAdded: ISignal<INotebookTracker, NotebookPanel | null>;
currentNotebookChanged: ISignal<INotebookTracker, NotebookPanel | null>;
Expand Down Expand Up @@ -98,7 +74,7 @@ const KernelUsage = (props: {
});
};

const doPoll = (kernelId: string) => {
const doPoll = (kernelId: string, path: string) => {
let kernelPoll = kernelPools.get(kernelId);
if (!kernelPoll) {
const poll = new Poll<void, any, 'stand-by'>({
Expand All @@ -114,7 +90,8 @@ const KernelUsage = (props: {
});
kernelPoll = {
poll,
usage: UNKONWN_USAGE
path,
usage: undefined
};
kernelPools.set(kernelId, kernelPoll);
}
Expand All @@ -132,65 +109,83 @@ const KernelUsage = (props: {
>
) => {
const kernelId = args.newValue?.id;
setKernelId(kernelId);
doPoll(kernelId as string);
if (kernelId) {
setKernelId(kernelId);
const path = panel?.sessionContext.session?.model.path;
doPoll(kernelId as string, path as string);
}
}
);
if (panel?.sessionContext.session?.id !== kernelId) {
if (panel?.sessionContext.session?.kernel?.id) {
const kernelId = panel?.sessionContext.session?.kernel?.id;
setKernelId(kernelId);
doPoll(kernelId as string);
if (kernelId) {
setKernelId(kernelId);
const path = panel?.sessionContext.session?.model.path;
doPoll(kernelId as string, path);
}
}
}
}
);
return (
<>
<h3 className="jp-kernelusage-separator">Kernel Usage</h3>
<div className="jp-kernelusage-separator">Kernel ID: {kernelId}</div>
<div className="jp-kernelusage-separator">
Timestamp: {kernelId && getUsage(kernelId).timestamp?.toLocaleString()}
</div>
<div className="jp-kernelusage-separator">
CPU: {kernelId && getUsage(kernelId).kernel_cpu}
</div>
<div className="jp-kernelusage-separator">
Memory: {kernelId && getUsage(kernelId).kernel_memory}
</div>
<hr></hr>
<h4 className="jp-kernelusage-separator">Host CPU</h4>
<div className="jp-kernelusage-separator">
Percentage {kernelId && getUsage(kernelId).host_cpu_percent}
</div>
<h4 className="jp-kernelusage-separator">Host Virtual Memory</h4>
<div className="jp-kernelusage-separator">
Active: {kernelId && getUsage(kernelId).host_virtual_memory.active}
</div>
<div className="jp-kernelusage-separator">
Available:{' '}
{kernelId && getUsage(kernelId).host_virtual_memory.available}
</div>
<div className="jp-kernelusage-separator">
Free: {kernelId && getUsage(kernelId).host_virtual_memory.free}
</div>
<div className="jp-kernelusage-separator">
Inactive: {kernelId && getUsage(kernelId).host_virtual_memory.inactive}
</div>
<div className="jp-kernelusage-separator">
Percent: {kernelId && getUsage(kernelId).host_virtual_memory.percent}
</div>
<div className="jp-kernelusage-separator">
Total: {kernelId && getUsage(kernelId).host_virtual_memory.total}
</div>
<div className="jp-kernelusage-separator">
Used: {kernelId && getUsage(kernelId).host_virtual_memory.used}
</div>
<div className="jp-kernelusage-separator">
Wired: {kernelId && getUsage(kernelId).host_virtual_memory.wired}
</div>
</>
);

if (kernelId) {
const kernelPoll = kernelPools.get(kernelId);
if (kernelPoll) {
return (
<>
<h3 className="jp-kernelusage-separator">Kernel Usage</h3>
<div className="jp-kernelusage-separator">
Kernel Host: {kernelPoll.usage?.hostname}
</div>
<div className="jp-kernelusage-separator">
Notebook: {kernelPoll.path}
</div>
<div className="jp-kernelusage-separator">Kernel ID: {kernelId}</div>
<div className="jp-kernelusage-separator">
Timestamp: {kernelPoll.usage?.timestamp?.toLocaleString()}
</div>
<div className="jp-kernelusage-separator">
CPU: {kernelPoll.usage?.kernel_cpu}
</div>
<div className="jp-kernelusage-separator">
Memory: {kernelPoll.usage?.kernel_memory}
</div>
<hr></hr>
<h4 className="jp-kernelusage-separator">Host CPU</h4>
<div className="jp-kernelusage-separator">
Percentage {kernelPoll.usage?.host_cpu_percent}
</div>
<h4 className="jp-kernelusage-separator">Host Virtual Memory</h4>
<div className="jp-kernelusage-separator">
Active: {kernelPoll.usage?.host_virtual_memory.active}
</div>
<div className="jp-kernelusage-separator">
Available: {kernelPoll.usage?.host_virtual_memory.available}
</div>
<div className="jp-kernelusage-separator">
Free: {kernelPoll.usage?.host_virtual_memory.free}
</div>
<div className="jp-kernelusage-separator">
Inactive: {kernelPoll.usage?.host_virtual_memory.inactive}
</div>
<div className="jp-kernelusage-separator">
Percent: {kernelPoll.usage?.host_virtual_memory.percent}
</div>
<div className="jp-kernelusage-separator">
Total: {kernelPoll.usage?.host_virtual_memory.total}
</div>
<div className="jp-kernelusage-separator">
Used: {kernelPoll.usage?.host_virtual_memory.used}
</div>
<div className="jp-kernelusage-separator">
Wired: {kernelPoll.usage?.host_virtual_memory.wired}
</div>
</>
);
}
}
return <h3>Kernel usage is not available</h3>;
};

export class KernelUsageWidget extends ReactWidget {
Expand Down

0 comments on commit f8831be

Please sign in to comment.