Skip to content

Commit e7d1816

Browse files
committed
better format for the memory
1 parent fc3c993 commit e7d1816

File tree

4 files changed

+26
-38
lines changed

4 files changed

+26
-38
lines changed

notebooks/cpu.ipynb

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,24 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"id": "0ccb3a21-64c5-4ee2-a4fc-59c23afb0944",
77
"metadata": {},
8-
"outputs": [
9-
{
10-
"ename": "KeyboardInterrupt",
11-
"evalue": "",
12-
"output_type": "error",
13-
"traceback": [
14-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
15-
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
16-
"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",
17-
"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",
18-
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
19-
]
20-
}
21-
],
8+
"outputs": [],
229
"source": [
23-
"# from multiprocessing import Pool\n",
24-
"# from multiprocessing import cpu_count\n",
25-
"\n",
2610
"def f(x):\n",
2711
" while True:\n",
2812
" x*x\n",
2913
"\n",
14+
"f(1)\n",
15+
"\n",
16+
"# from multiprocessing import Pool\n",
17+
"# from multiprocessing import cpu_count\n",
18+
"\n",
3019
"# processes = cpu_count()\n",
3120
"# print('utilizing %d cores\\n' % processes)\n",
3221
"# pool = Pool(processes)\n",
33-
"# pool.map(f, range(processes))\n",
34-
"\n",
35-
"f(1)"
22+
"# pool.map(f, range(processes))"
3623
]
3724
},
3825
{

notebooks/memory.ipynb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 3,
5+
"execution_count": 2,
66
"id": "0ccb3a21-64c5-4ee2-a4fc-59c23afb0944",
77
"metadata": {},
88
"outputs": [],
99
"source": [
10-
"# b = bytearray(999999999)\n",
1110
"import time\n",
1211
"GB = 1024 * 1024 * 1024\n",
1312
"mem_to_eat = 10\n",
14-
"eat = \"a\" * GB * mem_to_eat\n",
15-
"# while True:\n",
16-
"# time.sleep(1)"
13+
"eat = \"a\" * GB * mem_to_eat"
1714
]
1815
},
1916
{

src/format.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ const MEMORY_UNIT_LIMITS: {
1313
PB: 1125899906842624
1414
};
1515

16+
export function formatForDisplay(numBytes: number | undefined): string {
17+
const lu = convertToLargestUnit(numBytes);
18+
return lu[0].toFixed(2) + ' ' + lu[1];
19+
}
20+
1621
/**
1722
* Given a number of bytes, convert to the most human-readable
1823
* format, (GB, TB, etc).
1924
* Taken from https://github.com/jupyter-server/jupyter-resource-usage/blob/e6ec53fa69fdb6de8e878974bcff006310658408/packages/labextension/src/memoryUsage.tsx#L272
2025
*/
21-
export function convertToLargestUnit(
26+
function convertToLargestUnit(
2227
numBytes: number | undefined
2328
): [number, MemoryUnit] {
2429
if (!numBytes) {
@@ -34,8 +39,7 @@ export function convertToLargestUnit(
3439
} else if (
3540
MEMORY_UNIT_LIMITS.MB === numBytes ||
3641
numBytes < MEMORY_UNIT_LIMITS.GB
37-
// eslint-disable-next-line prettier/prettier
38-
) {
42+
) {
3943
return [numBytes / MEMORY_UNIT_LIMITS.MB, 'MB'];
4044
} else if (
4145
MEMORY_UNIT_LIMITS.GB === numBytes ||

src/widget.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Kernel } from '@jupyterlab/services';
77
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
88
import { requestAPI } from './handler';
99
import useInterval from './useInterval';
10-
import { convertToLargestUnit } from './format';
10+
import { formatForDisplay } from './format';
1111

1212
type Usage = {
1313
timestamp: Date | null;
@@ -150,7 +150,7 @@ const KernelUsage = (props: {
150150
CPU: {kernelPoll.usage?.kernel_cpu.toFixed(1)}
151151
</div>
152152
<div className="jp-kernelusage-separator">
153-
Memory: {convertToLargestUnit(kernelPoll.usage?.kernel_memory)}
153+
Memory: {formatForDisplay(kernelPoll.usage?.kernel_memory)}
154154
</div>
155155
<hr></hr>
156156
<h4 className="jp-kernelusage-separator">Host CPU</h4>
@@ -160,21 +160,21 @@ const KernelUsage = (props: {
160160
<h4 className="jp-kernelusage-separator">Host Virtual Memory</h4>
161161
<div className="jp-kernelusage-separator">
162162
Active:{' '}
163-
{convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.active)}
163+
{formatForDisplay(kernelPoll.usage?.host_virtual_memory.active)}
164164
</div>
165165
<div className="jp-kernelusage-separator">
166166
Available:{' '}
167-
{convertToLargestUnit(
167+
{formatForDisplay(
168168
kernelPoll.usage?.host_virtual_memory.available
169169
)}
170170
</div>
171171
<div className="jp-kernelusage-separator">
172172
Free:{' '}
173-
{convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.free)}
173+
{formatForDisplay(kernelPoll.usage?.host_virtual_memory.free)}
174174
</div>
175175
<div className="jp-kernelusage-separator">
176176
Inactive:{' '}
177-
{convertToLargestUnit(
177+
{formatForDisplay(
178178
kernelPoll.usage?.host_virtual_memory.inactive
179179
)}
180180
</div>
@@ -183,15 +183,15 @@ const KernelUsage = (props: {
183183
</div>
184184
<div className="jp-kernelusage-separator">
185185
Total:{' '}
186-
{convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.total)}
186+
{formatForDisplay(kernelPoll.usage?.host_virtual_memory.total)}
187187
</div>
188188
<div className="jp-kernelusage-separator">
189189
Used:{' '}
190-
{convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.used)}
190+
{formatForDisplay(kernelPoll.usage?.host_virtual_memory.used)}
191191
</div>
192192
<div className="jp-kernelusage-separator">
193193
Wired:{' '}
194-
{convertToLargestUnit(kernelPoll.usage?.host_virtual_memory.wired)}
194+
{formatForDisplay(kernelPoll.usage?.host_virtual_memory.wired)}
195195
</div>
196196
</>
197197
);

0 commit comments

Comments
 (0)