diff --git a/notebooks/cpu.ipynb b/notebooks/cpu.ipynb index c677ee3..432517e 100644 --- a/notebooks/cpu.ipynb +++ b/notebooks/cpu.ipynb @@ -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\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))" ] }, { diff --git a/notebooks/memory.ipynb b/notebooks/memory.ipynb index 3e1946e..a895d6a 100644 --- a/notebooks/memory.ipynb +++ b/notebooks/memory.ipynb @@ -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" ] }, { diff --git a/src/format.ts b/src/format.ts index 3e3e37f..cbc92f6 100644 --- a/src/format.ts +++ b/src/format.ts @@ -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) { @@ -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 || diff --git a/src/widget.tsx b/src/widget.tsx index c2861ed..85d937e 100644 --- a/src/widget.tsx +++ b/src/widget.tsx @@ -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; @@ -150,7 +150,7 @@ const KernelUsage = (props: { CPU: {kernelPoll.usage?.kernel_cpu.toFixed(1)}
- Memory: {convertToLargestUnit(kernelPoll.usage?.kernel_memory)} + Memory: {formatForDisplay(kernelPoll.usage?.kernel_memory)}

Host CPU

@@ -160,21 +160,21 @@ const KernelUsage = (props: {

Host Virtual Memory

Active:{' '} - {convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.active)} + {formatForDisplay(kernelPoll.usage?.host_virtual_memory.active)}
Available:{' '} - {convertToLargestUnit( + {formatForDisplay( kernelPoll.usage?.host_virtual_memory.available )}
Free:{' '} - {convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.free)} + {formatForDisplay(kernelPoll.usage?.host_virtual_memory.free)}
Inactive:{' '} - {convertToLargestUnit( + {formatForDisplay( kernelPoll.usage?.host_virtual_memory.inactive )}
@@ -183,15 +183,15 @@ const KernelUsage = (props: {
Total:{' '} - {convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.total)} + {formatForDisplay(kernelPoll.usage?.host_virtual_memory.total)}
Used:{' '} - {convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.used)} + {formatForDisplay(kernelPoll.usage?.host_virtual_memory.used)}
Wired:{' '} - {convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.wired)} + {formatForDisplay(kernelPoll.usage?.host_virtual_memory.wired)}
);