Skip to content

Commit a8e4c21

Browse files
committed
utop: Add initial implementation for ESP32.
Signed-off-by: Daniël van de Giessen <[email protected]>
1 parent 68e0dfc commit a8e4c21

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

micropython/utop/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# utop
2+
3+
Provides a top-like live overview of the running system.
4+
5+
On the `esp32` port this depends on the `esp32.idf_task_stats()` function, which
6+
can be enabled by adding the following lines to the board `sdkconfig`:
7+
8+
```ini
9+
CONFIG_FREERTOS_USE_TRACE_FACILITY=y
10+
CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID=y
11+
CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS=y
12+
```

micropython/utop/manifest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
metadata(
2+
version="0.1.0",
3+
description="Provides a top-like live overview of the running system.",
4+
)
5+
6+
module("utop.py")

micropython/utop/utop.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import time
2+
3+
try:
4+
import esp32
5+
import _thread
6+
except ImportError:
7+
esp32 = None
8+
9+
def top(update_interval_ms=1000, timeout_ms=None, thread_names={}):
10+
time_start = time.ticks_ms()
11+
previous_total_runtime = 0
12+
previous_task_runtimes = {}
13+
previous_line_count = 0
14+
esp32_task_state_names = ("running", "ready", "blocked", "suspended", "deleted", "invalid")
15+
16+
while timeout_ms is None or abs(time.ticks_diff(time.ticks_ms(), time_start)) < timeout_ms:
17+
if previous_line_count > 0:
18+
print("\x1b[{}A".format(previous_line_count), end="")
19+
line_count = 0
20+
21+
if esp32 is not None:
22+
if not hasattr(esp32, "idf_task_stats"):
23+
print(
24+
"INFO: esp32.idf_task_stats() is not available, cannot list active tasks.\x1b[K"
25+
)
26+
line_count += 1
27+
else:
28+
print(" CPU% CORE PRIORITY STATE STACKWATERMARK NAME\x1b[K")
29+
line_count += 1
30+
31+
total_runtime, tasks = esp32.idf_task_stats()
32+
current_thread_id = _thread.get_ident()
33+
tasks.sort(key=lambda t: t[0])
34+
for (
35+
task_id,
36+
task_name,
37+
task_state,
38+
task_priority,
39+
task_runtime,
40+
task_stackhighwatermark,
41+
task_coreid,
42+
) in tasks:
43+
task_runtime_percentage = "-"
44+
if previous_total_runtime != 0 and task_id in previous_task_runtimes:
45+
task_runtime_percentage = "{:.2f}%".format(
46+
100
47+
* ((task_runtime - previous_task_runtimes[task_id]) & (2**32 - 1))
48+
/ ((total_runtime - previous_total_runtime) & (2**32 - 1))
49+
)
50+
task_state_name = "unknown"
51+
if task_state >= 0 and task_state < len(esp32_task_state_names):
52+
task_state_name = esp32_task_state_names[task_state]
53+
54+
print(
55+
"{:>7} {:>4d} {:>8d} {:<9} {:<14d} {}{}\x1b[K".format(
56+
task_runtime_percentage,
57+
task_coreid,
58+
task_priority,
59+
task_state_name,
60+
task_stackhighwatermark,
61+
task_name if task_id not in thread_names else thread_names[task_id],
62+
" (*)" if task_id == current_thread_id else "",
63+
)
64+
)
65+
line_count += 1
66+
67+
previous_task_runtimes[task_id] = task_runtime
68+
previous_total_runtime = total_runtime
69+
else:
70+
print("INFO: Platform does not support listing active tasks.\x1b[K")
71+
line_count += 1
72+
73+
if previous_line_count > line_count:
74+
for _ in range(previous_line_count - line_count):
75+
print("\x1b[K")
76+
print("\x1b[{}A".format(previous_line_count - line_count), end="")
77+
78+
previous_line_count = line_count
79+
80+
try:
81+
time.sleep_ms(update_interval_ms)
82+
except KeyboardInterrupt:
83+
break

0 commit comments

Comments
 (0)