blob: e6adc355b1f7b981251d475005af2d53f09badcc [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 George89deec02015-01-09 20:12:54 +000057#if MICROPY_MEM_STATS
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020058 printf("mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
59 m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
Damien George89deec02015-01-09 20:12:54 +000060#endif
61#if MICROPY_STACK_CHECK
Damien George5d48f232015-01-09 20:37:49 +000062 printf("stack: " UINT_FMT " out of " INT_FMT "\n", mp_stack_usage(), MP_STATE_VM(stack_limit));
Damien George89deec02015-01-09 20:12:54 +000063#else
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020064 printf("stack: " UINT_FMT "\n", mp_stack_usage());
Damien George89deec02015-01-09 20:12:54 +000065#endif
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020066#if MICROPY_ENABLE_GC
67 gc_dump_info();
68 if (n_args == 1) {
69 // arg given means dump gc allocation table
70 gc_dump_alloc_table();
71 }
72#endif
73 return mp_const_none;
74}
75STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_mem_info_obj, 0, 1, mp_micropython_mem_info);
76
77STATIC mp_obj_t qstr_info(void) {
78 mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
79 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
80 printf("qstr pool: n_pool=" UINT_FMT ", n_qstr=" UINT_FMT ", n_str_data_bytes=" UINT_FMT ", n_total_bytes=" UINT_FMT "\n",
81 n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
82 return mp_const_none;
83}
84STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_qstr_info_obj, qstr_info);
Damien George89deec02015-01-09 20:12:54 +000085
86#endif // MICROPY_PY_MICROPYTHON_MEM_INFO
Damien George91d457a2014-01-20 10:30:24 +000087
Dave Hylands5b7fd202014-07-01 23:46:53 -070088#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
89STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf);
90#endif
91
Damien George0c36da02014-03-08 15:24:39 +000092STATIC const mp_map_elem_t mp_module_micropython_globals_table[] = {
93 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_micropython) },
Damien George89deec02015-01-09 20:12:54 +000094#if MICROPY_PY_MICROPYTHON_MEM_INFO
Damien George91d457a2014-01-20 10:30:24 +000095#if MICROPY_MEM_STATS
Damien George0c36da02014-03-08 15:24:39 +000096 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_total), (mp_obj_t)&mp_micropython_mem_total_obj },
97 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_current), (mp_obj_t)&mp_micropython_mem_current_obj },
98 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_peak), (mp_obj_t)&mp_micropython_mem_peak_obj },
Damien George89deec02015-01-09 20:12:54 +000099#endif
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +0200100 { MP_OBJ_NEW_QSTR(MP_QSTR_mem_info), (mp_obj_t)&mp_micropython_mem_info_obj },
101 { MP_OBJ_NEW_QSTR(MP_QSTR_qstr_info), (mp_obj_t)&mp_micropython_qstr_info_obj },
Damien George91d457a2014-01-20 10:30:24 +0000102#endif
Dave Hylands5b7fd202014-07-01 23:46:53 -0700103#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
104 { MP_OBJ_NEW_QSTR(MP_QSTR_alloc_emergency_exception_buf), (mp_obj_t)&mp_alloc_emergency_exception_buf_obj },
105#endif
Damien George0c36da02014-03-08 15:24:39 +0000106};
107
Damien George3b603f22014-11-29 14:39:27 +0000108STATIC MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table);
Damien George0c36da02014-03-08 15:24:39 +0000109
110const mp_obj_module_t mp_module_micropython = {
111 .base = { &mp_type_module },
112 .name = MP_QSTR_micropython,
Damien George8b0535e2014-04-05 21:53:54 +0100113 .globals = (mp_obj_dict_t*)&mp_module_micropython_globals,
Damien George0c36da02014-03-08 15:24:39 +0000114};