# CPUMonitor { #vscodenb.CPUMonitor }

```python
vscodenb.CPUMonitor(
    width=None,
    update_interval=0.5,
    show_summary=True,
    persist=False,
    color=False,
    summary=False,
    fold=16,
    group_by='node',
    label=None,
)
```

Live CPU monitoring with adaptive display.

Monitors per-core CPU usage and displays it in a grid layout.
Automatically detects SLURM nodes and Jupyter environment.

## Parameters {.doc-section .doc-section-parameters}

<code>[**width**]{.parameter-name} [:]{.parameter-annotation-sep} [[int](`int`)]{.parameter-annotation} [ = ]{.parameter-default-sep} [None]{.parameter-default}</code>

:   Display width in characters. If None, auto-detects terminal/notebook width.

<code>[**update_interval**]{.parameter-name} [:]{.parameter-annotation-sep} [[float](`float`)]{.parameter-annotation} [ = ]{.parameter-default-sep} [0.5]{.parameter-default}</code>

:   Time between updates in seconds

<code>[**show_summary**]{.parameter-name} [:]{.parameter-annotation-sep} [[bool](`bool`)]{.parameter-annotation} [ = ]{.parameter-default-sep} [True]{.parameter-default}</code>

:   Show summary statistics when monitoring ends

<code>[**persist**]{.parameter-name} [:]{.parameter-annotation-sep} [[bool](`bool`)]{.parameter-annotation} [ = ]{.parameter-default-sep} [False]{.parameter-default}</code>

:   If True, keep display visible after completion. If False (default), the display disappears when monitoring stops.

<code>[**color**]{.parameter-name} [:]{.parameter-annotation-sep} [[bool](`bool`)]{.parameter-annotation} [ = ]{.parameter-default-sep} [False]{.parameter-default}</code>

:   If True, use color coding (green < 50%, yellow 50-80%, red > 80%). If False (default), all bars are gray.

<code>[**summary**]{.parameter-name} [:]{.parameter-annotation-sep} [[bool](`bool`)]{.parameter-annotation} [ = ]{.parameter-default-sep} [False]{.parameter-default}</code>

:   If True, display results as an HTML table with mean CPU usage per core instead of progress bars. Memory usage (mean/max) is shown next to the node name. The table will be shown after completion regardless of persist setting.

<code>[**fold**]{.parameter-name} [:]{.parameter-annotation-sep} [[int](`int`)]{.parameter-annotation} [ = ]{.parameter-default-sep} [16]{.parameter-default}</code>

:   Maximum number of CPU bars per row before wrapping to a new line. Bars are automatically redistributed across rows to minimize empty space in the last row. Only applies to HTML display (Jupyter/VSCode).

<code>[**group_by**]{.parameter-name} [:]{.parameter-annotation-sep} [[str](`str`)]{.parameter-annotation} [ = ]{.parameter-default-sep} [\"node\"]{.parameter-default}</code>

:   How to group CPU bars: "node" groups by physical node, "task" groups by SLURM task. For non-SLURM environments, both modes behave the same.

<code>[**label**]{.parameter-name} [:]{.parameter-annotation-sep} [[str](`str`)]{.parameter-annotation} [ = ]{.parameter-default-sep} [None]{.parameter-default}</code>

:   Label to display at the top left of the widget. If None (default), no label is displayed.

## Examples {.doc-section .doc-section-examples}

```python
>>> with CPUMonitor():
...     result = computation()
```

```python
>>> with CPUMonitor(width=120, update_interval=1.0):
...     result = computation()
```

```python
>>> with CPUMonitor(persist=True):
...     result = computation()  # Display remains after completion
```

```python
>>> with CPUMonitor(color=True):
...     result = computation()  # Use color coding
```

```python
>>> with CPUMonitor(summary=True):
...     result = computation()  # Show table with CPU and memory stats
```

```python
>>> with CPUMonitor(label="Training Epoch 1"):
...     result = computation()  # Display with custom label
```

## Methods

| Name | Description |
| --- | --- |
| [start](#vscodenb.CPUMonitor.start) | Start monitoring. |
| [stop](#vscodenb.CPUMonitor.stop) | Stop monitoring. |

### start { #vscodenb.CPUMonitor.start }

```python
vscodenb.CPUMonitor.start()
```

Start monitoring.

### stop { #vscodenb.CPUMonitor.stop }

```python
vscodenb.CPUMonitor.stop(had_error=False)
```

Stop monitoring.