blob: 7d1b6bf38b33651d31d2d2efa68bd455ab906175 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020027#include <stdio.h>
Damien George51dfcb42015-01-01 20:27:54 +000028
Damien George89deec02015-01-09 20:12:54 +000029#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/builtin.h"
31#include "py/stackctrl.h"
32#include "py/gc.h"
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020033
34// Various builtins specific to MicroPython runtime,
35// living in micropython module
36
Damien George89deec02015-01-09 20:12:54 +000037#if MICROPY_PY_MICROPYTHON_MEM_INFO
38
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020039#if MICROPY_MEM_STATS
Damien Georgeabc19592015-01-12 22:34:38 +000040STATIC mp_obj_t mp_micropython_mem_total(void) {
Damien Georgebb4c6f32014-07-31 10:49:14 +010041 return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020042}
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020043STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total);
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020044
Damien Georgeabc19592015-01-12 22:34:38 +000045STATIC mp_obj_t mp_micropython_mem_current(void) {
Damien Georgebb4c6f32014-07-31 10:49:14 +010046 return MP_OBJ_NEW_SMALL_INT(m_get_current_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020047}
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020048STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_current_obj, mp_micropython_mem_current);
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020049
Damien Georgeabc19592015-01-12 22:34:38 +000050STATIC mp_obj_t mp_micropython_mem_peak(void) {
Damien Georgebb4c6f32014-07-31 10:49:14 +010051 return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020052}
Damien George0c36da02014-03-08 15:24:39 +000053STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_peak_obj, mp_micropython_mem_peak);
Damien George89deec02015-01-09 20:12:54 +000054#endif
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020055
56mp_obj_t mp_micropython_mem_info(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000057 (void)args;
Damien George89deec02015-01-09 20:12:54 +000058#if MICROPY_MEM_STATS
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020059 printf("mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
60 m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
Damien George89deec02015-01-09 20:12:54 +000061#endif
62#if MICROPY_STACK_CHECK
Damien George5d48f232015-01-09 20:37:49 +000063 printf("stack: " UINT_FMT " out of " INT_FMT "\n", mp_stack_usage(), MP_STATE_VM(stack_limit));
Damien George89deec02015-01-09 20:12:54 +000064#else
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020065 printf("stack: " UINT_FMT "\n", mp_stack_usage());
Damien George89deec02015-01-09 20:12:54 +000066#endif
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020067#if MICROPY_ENABLE_GC
68 gc_dump_info();
69 if (n_args == 1) {
70 // arg given means dump gc allocation table
71 gc_dump_alloc_table();
72 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +000073#else
74 (void)n_args;
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020075#endif
76 return mp_const_none;
77}
78STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_mem_info_obj, 0, 1, mp_micropython_mem_info);
79
80STATIC mp_obj_t qstr_info(void) {
81 mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
82 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
83 printf("qstr pool: n_pool=" UINT_FMT ", n_qstr=" UINT_FMT ", n_str_data_bytes=" UINT_FMT ", n_total_bytes=" UINT_FMT "\n",
84 n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
85 return mp_const_none;
86}
87STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_qstr_info_obj, qstr_info);
Damien George89deec02015-01-09 20:12:54 +000088
89#endif // MICROPY_PY_MICROPYTHON_MEM_INFO
Damien George91d457a2014-01-20 10:30:24 +000090
Dave Hylands5b7fd202014-07-01 23:46:53 -070091#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
92STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf);
93#endif
94
Damien George0c36da02014-03-08 15:24:39 +000095STATIC const mp_map_elem_t mp_module_micropython_globals_table[] = {
96 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_micropython) },
Damien George89deec02015-01-09 20:12:54 +000097#if MICROPY_PY_MICROPYTHON_MEM_INFO
Damien George91d457a2014-01-20 10:30:24 +000098#if MICROPY_MEM_STATS
Damien George0c36da02014-03-08 15:24:39 +000099 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_total), (mp_obj_t)&mp_micropython_mem_total_obj },
100 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_current), (mp_obj_t)&mp_micropython_mem_current_obj },
101 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_peak), (mp_obj_t)&mp_micropython_mem_peak_obj },
Damien George89deec02015-01-09 20:12:54 +0000102#endif
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +0200103 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_info), (mp_obj_t)&mp_micropython_mem_info_obj },
104 { MP_OBJ_NEW_QSTR(MP_QSTR_qstr_info), (mp_obj_t)&mp_micropython_qstr_info_obj },
Damien George91d457a2014-01-20 10:30:24 +0000105#endif
Dave Hylands5b7fd202014-07-01 23:46:53 -0700106#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
107 { MP_OBJ_NEW_QSTR(MP_QSTR_alloc_emergency_exception_buf), (mp_obj_t)&mp_alloc_emergency_exception_buf_obj },
108#endif
Damien George0c36da02014-03-08 15:24:39 +0000109};
110
Damien George3b603f22014-11-29 14:39:27 +0000111STATIC MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table);
Damien George0c36da02014-03-08 15:24:39 +0000112
113const mp_obj_module_t mp_module_micropython = {
114 .base = { &mp_type_module },
115 .name = MP_QSTR_micropython,
Damien George8b0535e2014-04-05 21:53:54 +0100116 .globals = (mp_obj_dict_t*)&mp_module_micropython_globals,
Damien George0c36da02014-03-08 15:24:39 +0000117};