Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 27 | #include <stdio.h> |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 28 | |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 29 | #include "py/mpstate.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 30 | #include "py/builtin.h" |
| 31 | #include "py/stackctrl.h" |
| 32 | #include "py/gc.h" |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 33 | |
| 34 | // Various builtins specific to MicroPython runtime, |
| 35 | // living in micropython module |
| 36 | |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 37 | #if MICROPY_PY_MICROPYTHON_MEM_INFO |
| 38 | |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 39 | #if MICROPY_MEM_STATS |
Damien George | abc1959 | 2015-01-12 22:34:38 +0000 | [diff] [blame] | 40 | STATIC mp_obj_t mp_micropython_mem_total(void) { |
Damien George | bb4c6f3 | 2014-07-31 10:49:14 +0100 | [diff] [blame] | 41 | return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated()); |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 42 | } |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 43 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total); |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 44 | |
Damien George | abc1959 | 2015-01-12 22:34:38 +0000 | [diff] [blame] | 45 | STATIC mp_obj_t mp_micropython_mem_current(void) { |
Damien George | bb4c6f3 | 2014-07-31 10:49:14 +0100 | [diff] [blame] | 46 | return MP_OBJ_NEW_SMALL_INT(m_get_current_bytes_allocated()); |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 47 | } |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 48 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_current_obj, mp_micropython_mem_current); |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 49 | |
Damien George | abc1959 | 2015-01-12 22:34:38 +0000 | [diff] [blame] | 50 | STATIC mp_obj_t mp_micropython_mem_peak(void) { |
Damien George | bb4c6f3 | 2014-07-31 10:49:14 +0100 | [diff] [blame] | 51 | return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated()); |
Paul Sokolovsky | 440cc3f | 2014-01-20 01:53:15 +0200 | [diff] [blame] | 52 | } |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 53 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_peak_obj, mp_micropython_mem_peak); |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 54 | #endif |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 55 | |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 56 | mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 57 | (void)args; |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 58 | #if MICROPY_MEM_STATS |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame] | 59 | mp_printf(&mp_plat_print, "mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n", |
Damien George | e5039c6 | 2015-02-15 13:17:11 +0000 | [diff] [blame] | 60 | (mp_uint_t)m_get_total_bytes_allocated(), (mp_uint_t)m_get_current_bytes_allocated(), (mp_uint_t)m_get_peak_bytes_allocated()); |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 61 | #endif |
| 62 | #if MICROPY_STACK_CHECK |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 63 | mp_printf(&mp_plat_print, "stack: " UINT_FMT " out of " INT_FMT "\n", mp_stack_usage(), MP_STATE_THREAD(stack_limit)); |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 64 | #else |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame] | 65 | mp_printf(&mp_plat_print, "stack: " UINT_FMT "\n", mp_stack_usage()); |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 66 | #endif |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 67 | #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 George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 73 | #else |
| 74 | (void)n_args; |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 75 | #endif |
| 76 | return mp_const_none; |
| 77 | } |
| 78 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_mem_info_obj, 0, 1, mp_micropython_mem_info); |
| 79 | |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 80 | STATIC mp_obj_t mp_micropython_qstr_info(size_t n_args, const mp_obj_t *args) { |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 81 | (void)args; |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 82 | size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes; |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 83 | qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes); |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 84 | mp_printf(&mp_plat_print, "qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n", |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 85 | n_pool, n_qstr, n_str_data_bytes, n_total_bytes); |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 86 | if (n_args == 1) { |
| 87 | // arg given means dump qstr data |
| 88 | qstr_dump_data(); |
| 89 | } |
Paul Sokolovsky | 6e8ff9c | 2014-12-01 20:41:56 +0200 | [diff] [blame] | 90 | return mp_const_none; |
| 91 | } |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 92 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_qstr_info_obj, 0, 1, mp_micropython_qstr_info); |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 93 | |
Damien George | df4ce93 | 2016-01-22 16:16:38 +0000 | [diff] [blame] | 94 | #if MICROPY_STACK_CHECK |
| 95 | STATIC mp_obj_t mp_micropython_stack_use(void) { |
| 96 | return MP_OBJ_NEW_SMALL_INT(mp_stack_usage()); |
| 97 | } |
| 98 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_stack_use_obj, mp_micropython_stack_use); |
| 99 | #endif |
| 100 | |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 101 | #endif // MICROPY_PY_MICROPYTHON_MEM_INFO |
Damien George | 91d457a | 2014-01-20 10:30:24 +0000 | [diff] [blame] | 102 | |
Damien George | df4ce93 | 2016-01-22 16:16:38 +0000 | [diff] [blame] | 103 | #if MICROPY_ENABLE_GC |
| 104 | STATIC mp_obj_t mp_micropython_heap_lock(void) { |
| 105 | gc_lock(); |
| 106 | return mp_const_none; |
| 107 | } |
| 108 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_lock_obj, mp_micropython_heap_lock); |
| 109 | |
| 110 | STATIC mp_obj_t mp_micropython_heap_unlock(void) { |
| 111 | gc_unlock(); |
| 112 | return mp_const_none; |
| 113 | } |
| 114 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_unlock_obj, mp_micropython_heap_unlock); |
| 115 | #endif |
| 116 | |
Dave Hylands | 5b7fd20 | 2014-07-01 23:46:53 -0700 | [diff] [blame] | 117 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0) |
| 118 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf); |
| 119 | #endif |
| 120 | |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 121 | STATIC const mp_rom_map_elem_t mp_module_micropython_globals_table[] = { |
| 122 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_micropython) }, |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 123 | #if MICROPY_PY_MICROPYTHON_MEM_INFO |
Damien George | 91d457a | 2014-01-20 10:30:24 +0000 | [diff] [blame] | 124 | #if MICROPY_MEM_STATS |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 125 | { MP_ROM_QSTR(MP_QSTR_mem_total), MP_ROM_PTR(&mp_micropython_mem_total_obj) }, |
| 126 | { MP_ROM_QSTR(MP_QSTR_mem_current), MP_ROM_PTR(&mp_micropython_mem_current_obj) }, |
| 127 | { MP_ROM_QSTR(MP_QSTR_mem_peak), MP_ROM_PTR(&mp_micropython_mem_peak_obj) }, |
Damien George | 89deec0 | 2015-01-09 20:12:54 +0000 | [diff] [blame] | 128 | #endif |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 129 | { MP_ROM_QSTR(MP_QSTR_mem_info), MP_ROM_PTR(&mp_micropython_mem_info_obj) }, |
| 130 | { MP_ROM_QSTR(MP_QSTR_qstr_info), MP_ROM_PTR(&mp_micropython_qstr_info_obj) }, |
Damien George | df4ce93 | 2016-01-22 16:16:38 +0000 | [diff] [blame] | 131 | #if MICROPY_STACK_CHECK |
| 132 | { MP_ROM_QSTR(MP_QSTR_stack_use), MP_ROM_PTR(&mp_micropython_stack_use_obj) }, |
| 133 | #endif |
Damien George | 91d457a | 2014-01-20 10:30:24 +0000 | [diff] [blame] | 134 | #endif |
Dave Hylands | 5b7fd20 | 2014-07-01 23:46:53 -0700 | [diff] [blame] | 135 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0) |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 136 | { MP_ROM_QSTR(MP_QSTR_alloc_emergency_exception_buf), MP_ROM_PTR(&mp_alloc_emergency_exception_buf_obj) }, |
Dave Hylands | 5b7fd20 | 2014-07-01 23:46:53 -0700 | [diff] [blame] | 137 | #endif |
Damien George | df4ce93 | 2016-01-22 16:16:38 +0000 | [diff] [blame] | 138 | #if MICROPY_ENABLE_GC |
| 139 | { MP_ROM_QSTR(MP_QSTR_heap_lock), MP_ROM_PTR(&mp_micropython_heap_lock_obj) }, |
| 140 | { MP_ROM_QSTR(MP_QSTR_heap_unlock), MP_ROM_PTR(&mp_micropython_heap_unlock_obj) }, |
| 141 | #endif |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Damien George | 3b603f2 | 2014-11-29 14:39:27 +0000 | [diff] [blame] | 144 | STATIC MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table); |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 145 | |
| 146 | const mp_obj_module_t mp_module_micropython = { |
| 147 | .base = { &mp_type_module }, |
| 148 | .name = MP_QSTR_micropython, |
Damien George | 8b0535e | 2014-04-05 21:53:54 +0100 | [diff] [blame] | 149 | .globals = (mp_obj_dict_t*)&mp_module_micropython_globals, |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 150 | }; |