Skip to content

Commit 89deec0

Browse files
committed
py: Add MICROPY_PY_MICROPYTHON_MEM_INFO to enable mem-info funcs.
This allows to enable mem-info functions in micropython module, even if MICROPY_MEM_STATS is not enabled. In this case, you get mem_info and qstr_info but not mem_{total,current,peak}.
1 parent 4a5895c commit 89deec0

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

py/modmicropython.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626

2727
#include <stdio.h>
2828

29+
#include "py/mpstate.h"
2930
#include "py/builtin.h"
3031
#include "py/stackctrl.h"
3132
#include "py/gc.h"
3233

3334
// Various builtins specific to MicroPython runtime,
3435
// living in micropython module
3536

37+
#if MICROPY_PY_MICROPYTHON_MEM_INFO
38+
3639
#if MICROPY_MEM_STATS
3740
STATIC mp_obj_t mp_micropython_mem_total() {
3841
return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated());
@@ -48,11 +51,18 @@ STATIC mp_obj_t mp_micropython_mem_peak() {
4851
return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated());
4952
}
5053
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_peak_obj, mp_micropython_mem_peak);
54+
#endif
5155

5256
mp_obj_t mp_micropython_mem_info(mp_uint_t n_args, const mp_obj_t *args) {
57+
#if MICROPY_MEM_STATS
5358
printf("mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
5459
m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
60+
#endif
61+
#if MICROPY_STACK_CHECK
62+
printf("stack: " UINT_FMT " out of " INT_FMT "\n", mp_stack_usage(), MP_STATE_VM(stack_limit) - mp_stack_usage());
63+
#else
5564
printf("stack: " UINT_FMT "\n", mp_stack_usage());
65+
#endif
5666
#if MICROPY_ENABLE_GC
5767
gc_dump_info();
5868
if (n_args == 1) {
@@ -72,18 +82,21 @@ STATIC mp_obj_t qstr_info(void) {
7282
return mp_const_none;
7383
}
7484
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_qstr_info_obj, qstr_info);
75-
#endif
85+
86+
#endif // MICROPY_PY_MICROPYTHON_MEM_INFO
7687

7788
#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
7889
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf);
7990
#endif
8091

8192
STATIC const mp_map_elem_t mp_module_micropython_globals_table[] = {
8293
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_micropython) },
94+
#if MICROPY_PY_MICROPYTHON_MEM_INFO
8395
#if MICROPY_MEM_STATS
8496
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_total), (mp_obj_t)&mp_micropython_mem_total_obj },
8597
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_current), (mp_obj_t)&mp_micropython_mem_current_obj },
8698
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_peak), (mp_obj_t)&mp_micropython_mem_peak_obj },
99+
#endif
87100
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_info), (mp_obj_t)&mp_micropython_mem_info_obj },
88101
{ MP_OBJ_NEW_QSTR(MP_QSTR_qstr_info), (mp_obj_t)&mp_micropython_qstr_info_obj },
89102
#endif

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ typedef double mp_float_t;
369369
#define MICROPY_PY___FILE__ (1)
370370
#endif
371371

372+
// Whether to provide mem-info related functions in micropython module
373+
#ifndef MICROPY_PY_MICROPYTHON_MEM_INFO
374+
#define MICROPY_PY_MICROPYTHON_MEM_INFO (0)
375+
#endif
376+
372377
// Whether to provide "array" module. Note that large chunk of the
373378
// underlying code is shared with "bytearray" builtin type, so to
374379
// get real savings, it should be disabled too.

py/qstrdefs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,12 @@ Q(polar)
363363
Q(rect)
364364
#endif
365365

366+
#if MICROPY_PY_MICROPYTHON_MEM_INFO
366367
#if MICROPY_MEM_STATS
367368
Q(mem_total)
368369
Q(mem_current)
369370
Q(mem_peak)
371+
#endif
370372
Q(mem_info)
371373
Q(qstr_info)
372374
#endif

stmhal/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
5858
#define MICROPY_PY_BUILTINS_FROZENSET (1)
5959
#define MICROPY_PY_BUILTINS_EXECFILE (1)
60+
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
6061
#define MICROPY_PY_SYS_EXIT (1)
6162
#define MICROPY_PY_SYS_STDFILES (1)
6263
#define MICROPY_PY_CMATH (1)

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
5858
#define MICROPY_PY_BUILTINS_FROZENSET (1)
5959
#define MICROPY_PY_BUILTINS_COMPILE (1)
60+
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
6061
#define MICROPY_PY_SYS_EXIT (1)
6162
#define MICROPY_PY_SYS_PLATFORM "linux"
6263
#define MICROPY_PY_SYS_MAXSIZE (1)

0 commit comments

Comments
 (0)