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

Commit

Permalink
better format for the memory
Browse files Browse the repository at this point in the history
  • Loading branch information
echarles committed Feb 15, 2022
1 parent fc3c993 commit e7d1816
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 38 deletions.
29 changes: 8 additions & 21 deletions notebooks/cpu.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,24 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "0ccb3a21-64c5-4ee2-a4fc-59c23afb0944",
"metadata": {},
"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: "
]
}
],
"outputs": [],
"source": [
"# from multiprocessing import Pool\n",
"# from multiprocessing import cpu_count\n",
"\n",
"def f(x):\n",
" while True:\n",
" x*x\n",
"\n",
"f(1)\n",
"\n",
"# from multiprocessing import Pool\n",
"# from multiprocessing import cpu_count\n",
"\n",
"# processes = cpu_count()\n",
"# print('utilizing %d cores\\n' % processes)\n",
"# pool = Pool(processes)\n",
"# pool.map(f, range(processes))\n",
"\n",
"f(1)"
"# pool.map(f, range(processes))"
]
},
{
Expand Down
7 changes: 2 additions & 5 deletions notebooks/memory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"id": "0ccb3a21-64c5-4ee2-a4fc-59c23afb0944",
"metadata": {},
"outputs": [],
"source": [
"# b = bytearray(999999999)\n",
"import time\n",
"GB = 1024 * 1024 * 1024\n",
"mem_to_eat = 10\n",
"eat = \"a\" * GB * mem_to_eat\n",
"# while True:\n",
"# time.sleep(1)"
"eat = \"a\" * GB * mem_to_eat"
]
},
{
Expand Down
10 changes: 7 additions & 3 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ const MEMORY_UNIT_LIMITS: {
PB: 1125899906842624
};

export function formatForDisplay(numBytes: number | undefined): string {
const lu = convertToLargestUnit(numBytes);
return lu[0].toFixed(2) + ' ' + lu[1];
}

/**
* Given a number of bytes, convert to the most human-readable
* format, (GB, TB, etc).
* Taken from https://github.com/jupyter-server/jupyter-resource-usage/blob/e6ec53fa69fdb6de8e878974bcff006310658408/packages/labextension/src/memoryUsage.tsx#L272
*/
export function convertToLargestUnit(
function convertToLargestUnit(
numBytes: number | undefined
): [number, MemoryUnit] {
if (!numBytes) {
Expand All @@ -34,8 +39,7 @@ export function convertToLargestUnit(
} else if (
MEMORY_UNIT_LIMITS.MB === numBytes ||
numBytes < MEMORY_UNIT_LIMITS.GB
// eslint-disable-next-line prettier/prettier
) {
) {
return [numBytes / MEMORY_UNIT_LIMITS.MB, 'MB'];
} else if (
MEMORY_UNIT_LIMITS.GB === numBytes ||
Expand Down
18 changes: 9 additions & 9 deletions src/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Kernel } from '@jupyterlab/services';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
import { requestAPI } from './handler';
import useInterval from './useInterval';
import { convertToLargestUnit } from './format';
import { formatForDisplay } from './format';

type Usage = {
timestamp: Date | null;
Expand Down Expand Up @@ -150,7 +150,7 @@ const KernelUsage = (props: {
CPU: {kernelPoll.usage?.kernel_cpu.toFixed(1)}
</div>
<div className="jp-kernelusage-separator">
Memory: {convertToLargestUnit(kernelPoll.usage?.kernel_memory)}
Memory: {formatForDisplay(kernelPoll.usage?.kernel_memory)}
</div>
<hr></hr>
<h4 className="jp-kernelusage-separator">Host CPU</h4>
Expand All @@ -160,21 +160,21 @@ const KernelUsage = (props: {
<h4 className="jp-kernelusage-separator">Host Virtual Memory</h4>
<div className="jp-kernelusage-separator">
Active:{' '}
{convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.active)}
{formatForDisplay(kernelPoll.usage?.host_virtual_memory.active)}
</div>
<div className="jp-kernelusage-separator">
Available:{' '}
{convertToLargestUnit(
{formatForDisplay(
kernelPoll.usage?.host_virtual_memory.available
)}
</div>
<div className="jp-kernelusage-separator">
Free:{' '}
{convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.free)}
{formatForDisplay(kernelPoll.usage?.host_virtual_memory.free)}
</div>
<div className="jp-kernelusage-separator">
Inactive:{' '}
{convertToLargestUnit(
{formatForDisplay(
kernelPoll.usage?.host_virtual_memory.inactive
)}
</div>
Expand All @@ -183,15 +183,15 @@ const KernelUsage = (props: {
</div>
<div className="jp-kernelusage-separator">
Total:{' '}
{convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.total)}
{formatForDisplay(kernelPoll.usage?.host_virtual_memory.total)}
</div>
<div className="jp-kernelusage-separator">
Used:{' '}
{convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.used)}
{formatForDisplay(kernelPoll.usage?.host_virtual_memory.used)}
</div>
<div className="jp-kernelusage-separator">
Wired:{' '}
{convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.wired)}
{formatForDisplay(kernelPoll.usage?.host_virtual_memory.wired)}
</div>
</>
);
Expand Down

0 comments on commit e7d1816

Please sign in to comment.