Skip to content

Commit 6e8ff9c

Browse files
committed
modmicropython: Move mem_info() and qstr_info() functions from unix port.
TODO: Merge useful functionality from modpyb too.
1 parent 17c5ce3 commit 6e8ff9c

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

py/modmicropython.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <stdint.h>
28+
#include <stdio.h>
2729
#include "mpconfig.h"
2830
#include "misc.h"
2931
#include "qstr.h"
3032
#include "obj.h"
3133
#include "builtin.h"
34+
#include "stackctrl.h"
35+
#include "gc.h"
3236

3337
// Various builtins specific to MicroPython runtime,
3438
// living in micropython module
@@ -37,18 +41,41 @@
3741
STATIC mp_obj_t mp_micropython_mem_total() {
3842
return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated());
3943
}
44+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total);
4045

4146
STATIC mp_obj_t mp_micropython_mem_current() {
4247
return MP_OBJ_NEW_SMALL_INT(m_get_current_bytes_allocated());
4348
}
49+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_current_obj, mp_micropython_mem_current);
4450

4551
STATIC mp_obj_t mp_micropython_mem_peak() {
4652
return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated());
4753
}
48-
49-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total);
50-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_current_obj, mp_micropython_mem_current);
5154
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_peak_obj, mp_micropython_mem_peak);
55+
56+
mp_obj_t mp_micropython_mem_info(mp_uint_t n_args, const mp_obj_t *args) {
57+
printf("mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
58+
m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
59+
printf("stack: " UINT_FMT "\n", mp_stack_usage());
60+
#if MICROPY_ENABLE_GC
61+
gc_dump_info();
62+
if (n_args == 1) {
63+
// arg given means dump gc allocation table
64+
gc_dump_alloc_table();
65+
}
66+
#endif
67+
return mp_const_none;
68+
}
69+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_mem_info_obj, 0, 1, mp_micropython_mem_info);
70+
71+
STATIC mp_obj_t qstr_info(void) {
72+
mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
73+
qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
74+
printf("qstr pool: n_pool=" UINT_FMT ", n_qstr=" UINT_FMT ", n_str_data_bytes=" UINT_FMT ", n_total_bytes=" UINT_FMT "\n",
75+
n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
76+
return mp_const_none;
77+
}
78+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_qstr_info_obj, qstr_info);
5279
#endif
5380

5481
#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
@@ -61,6 +88,8 @@ STATIC const mp_map_elem_t mp_module_micropython_globals_table[] = {
6188
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_total), (mp_obj_t)&mp_micropython_mem_total_obj },
6289
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_current), (mp_obj_t)&mp_micropython_mem_current_obj },
6390
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_peak), (mp_obj_t)&mp_micropython_mem_peak_obj },
91+
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_info), (mp_obj_t)&mp_micropython_mem_info_obj },
92+
{ MP_OBJ_NEW_QSTR(MP_QSTR_qstr_info), (mp_obj_t)&mp_micropython_qstr_info_obj },
6493
#endif
6594
#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
6695
{ MP_OBJ_NEW_QSTR(MP_QSTR_alloc_emergency_exception_buf), (mp_obj_t)&mp_alloc_emergency_exception_buf_obj },

py/qstrdefs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ Q(rect)
363363
Q(mem_total)
364364
Q(mem_current)
365365
Q(mem_peak)
366+
Q(mem_info)
367+
Q(qstr_info)
366368
#endif
367369

368370
#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)

unix/main.c

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -267,31 +267,6 @@ int usage(char **argv) {
267267
return 1;
268268
}
269269

270-
#if MICROPY_MEM_STATS
271-
STATIC mp_obj_t mem_info(mp_uint_t n_args, const mp_obj_t *args) {
272-
printf("mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
273-
m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
274-
printf("stack: " UINT_FMT "\n", mp_stack_usage());
275-
#if MICROPY_ENABLE_GC
276-
gc_dump_info();
277-
if (n_args == 1) {
278-
// arg given means dump gc allocation table
279-
gc_dump_alloc_table();
280-
}
281-
#endif
282-
return mp_const_none;
283-
}
284-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mem_info_obj, 0, 1, mem_info);
285-
#endif
286-
287-
STATIC mp_obj_t qstr_info(void) {
288-
mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
289-
qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
290-
printf("qstr pool: n_pool=" UINT_FMT ", n_qstr=" UINT_FMT ", n_str_data_bytes=" UINT_FMT ", n_total_bytes=" UINT_FMT "\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
291-
return mp_const_none;
292-
}
293-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(qstr_info_obj, qstr_info);
294-
295270
// Process options which set interpreter init options
296271
void pre_process_options(int argc, char **argv) {
297272
for (int a = 1; a < argc; a++) {
@@ -407,11 +382,6 @@ int main(int argc, char **argv) {
407382

408383
mp_obj_list_init(mp_sys_argv, 0);
409384

410-
#if MICROPY_MEM_STATS
411-
mp_store_name(qstr_from_str("mem_info"), (mp_obj_t*)&mem_info_obj);
412-
#endif
413-
mp_store_name(qstr_from_str("qstr_info"), (mp_obj_t*)&qstr_info_obj);
414-
415385
// Here is some example code to create a class and instance of that class.
416386
// First is the Python, then the C code.
417387
//
@@ -520,7 +490,8 @@ int main(int argc, char **argv) {
520490
}
521491

522492
if (mp_verbose_flag) {
523-
mem_info(0, NULL);
493+
extern mp_obj_t mp_micropython_mem_info(mp_uint_t n_args, const mp_obj_t *args);
494+
mp_micropython_mem_info(0, NULL);
524495
}
525496

526497
mp_deinit();

0 commit comments

Comments
 (0)