blob: a74e6aa3cb109680605e1c37320017d7f336c103 [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"
Damien George6e74d242017-02-16 18:05:06 +110032#include "py/runtime.h"
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/gc.h"
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020034
35// Various builtins specific to MicroPython runtime,
36// living in micropython module
37
Damien George7dc23452016-10-07 15:59:50 +110038STATIC mp_obj_t mp_micropython_opt_level(size_t n_args, const mp_obj_t *args) {
39 if (n_args == 0) {
40 return MP_OBJ_NEW_SMALL_INT(MP_STATE_VM(mp_optimise_value));
41 } else {
42 MP_STATE_VM(mp_optimise_value) = mp_obj_get_int(args[0]);
43 return mp_const_none;
44 }
45}
46STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_opt_level_obj, 0, 1, mp_micropython_opt_level);
47
Damien George89deec02015-01-09 20:12:54 +000048#if MICROPY_PY_MICROPYTHON_MEM_INFO
49
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020050#if MICROPY_MEM_STATS
Damien Georgeabc19592015-01-12 22:34:38 +000051STATIC mp_obj_t mp_micropython_mem_total(void) {
Damien Georgebb4c6f32014-07-31 10:49:14 +010052 return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020053}
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020054STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total);
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020055
Damien Georgeabc19592015-01-12 22:34:38 +000056STATIC mp_obj_t mp_micropython_mem_current(void) {
Damien Georgebb4c6f32014-07-31 10:49:14 +010057 return MP_OBJ_NEW_SMALL_INT(m_get_current_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020058}
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020059STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_current_obj, mp_micropython_mem_current);
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020060
Damien Georgeabc19592015-01-12 22:34:38 +000061STATIC mp_obj_t mp_micropython_mem_peak(void) {
Damien Georgebb4c6f32014-07-31 10:49:14 +010062 return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated());
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +020063}
Damien George0c36da02014-03-08 15:24:39 +000064STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_peak_obj, mp_micropython_mem_peak);
Damien George89deec02015-01-09 20:12:54 +000065#endif
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020066
Damien George4b72b3a2016-01-03 14:21:40 +000067mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000068 (void)args;
Damien George89deec02015-01-09 20:12:54 +000069#if MICROPY_MEM_STATS
Damien Georgee72cda92015-04-11 12:15:47 +010070 mp_printf(&mp_plat_print, "mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
Damien Georgee5039c62015-02-15 13:17:11 +000071 (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 George89deec02015-01-09 20:12:54 +000072#endif
73#if MICROPY_STACK_CHECK
Damien George330165a2016-04-22 22:44:56 +000074 mp_printf(&mp_plat_print, "stack: " UINT_FMT " out of " INT_FMT "\n", mp_stack_usage(), MP_STATE_THREAD(stack_limit));
Damien George89deec02015-01-09 20:12:54 +000075#else
Damien Georgee72cda92015-04-11 12:15:47 +010076 mp_printf(&mp_plat_print, "stack: " UINT_FMT "\n", mp_stack_usage());
Damien George89deec02015-01-09 20:12:54 +000077#endif
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020078#if MICROPY_ENABLE_GC
79 gc_dump_info();
80 if (n_args == 1) {
81 // arg given means dump gc allocation table
82 gc_dump_alloc_table();
83 }
Damien Georgeff8dd3f2015-01-20 12:47:20 +000084#else
85 (void)n_args;
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020086#endif
87 return mp_const_none;
88}
89STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_mem_info_obj, 0, 1, mp_micropython_mem_info);
90
Damien George4b72b3a2016-01-03 14:21:40 +000091STATIC mp_obj_t mp_micropython_qstr_info(size_t n_args, const mp_obj_t *args) {
Damien Georgeea0461d2015-02-10 11:02:28 +000092 (void)args;
Damien George25784852015-12-17 12:41:40 +000093 size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020094 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
Damien George25784852015-12-17 12:41:40 +000095 mp_printf(&mp_plat_print, "qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n",
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +020096 n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
Damien Georgeea0461d2015-02-10 11:02:28 +000097 if (n_args == 1) {
98 // arg given means dump qstr data
99 qstr_dump_data();
100 }
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +0200101 return mp_const_none;
102}
Damien Georgeea0461d2015-02-10 11:02:28 +0000103STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_qstr_info_obj, 0, 1, mp_micropython_qstr_info);
Damien George89deec02015-01-09 20:12:54 +0000104
Damien Georgedf4ce932016-01-22 16:16:38 +0000105#if MICROPY_STACK_CHECK
106STATIC mp_obj_t mp_micropython_stack_use(void) {
107 return MP_OBJ_NEW_SMALL_INT(mp_stack_usage());
108}
109STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_stack_use_obj, mp_micropython_stack_use);
110#endif
111
Damien George89deec02015-01-09 20:12:54 +0000112#endif // MICROPY_PY_MICROPYTHON_MEM_INFO
Damien George91d457a2014-01-20 10:30:24 +0000113
Damien Georgedf4ce932016-01-22 16:16:38 +0000114#if MICROPY_ENABLE_GC
115STATIC mp_obj_t mp_micropython_heap_lock(void) {
116 gc_lock();
117 return mp_const_none;
118}
119STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_lock_obj, mp_micropython_heap_lock);
120
121STATIC mp_obj_t mp_micropython_heap_unlock(void) {
122 gc_unlock();
123 return mp_const_none;
124}
125STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_unlock_obj, mp_micropython_heap_unlock);
126#endif
127
Dave Hylands5b7fd202014-07-01 23:46:53 -0700128#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
129STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf);
130#endif
131
Damien George6e74d242017-02-16 18:05:06 +1100132#if MICROPY_ENABLE_SCHEDULER
133STATIC mp_obj_t mp_micropython_schedule(mp_obj_t function, mp_obj_t arg) {
134 if (!mp_sched_schedule(function, arg)) {
135 mp_raise_msg(&mp_type_RuntimeError, "schedule stack full");
136 }
137 return mp_const_none;
138}
139STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_micropython_schedule_obj, mp_micropython_schedule);
140#endif
141
Damien Georgecbf76742015-11-27 13:38:15 +0000142STATIC const mp_rom_map_elem_t mp_module_micropython_globals_table[] = {
143 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_micropython) },
Damien George791b65f2016-09-27 13:34:21 +1000144 { MP_ROM_QSTR(MP_QSTR_const), MP_ROM_PTR(&mp_identity_obj) },
Damien George7dc23452016-10-07 15:59:50 +1100145 { MP_ROM_QSTR(MP_QSTR_opt_level), MP_ROM_PTR(&mp_micropython_opt_level_obj) },
Damien George89deec02015-01-09 20:12:54 +0000146#if MICROPY_PY_MICROPYTHON_MEM_INFO
Damien George91d457a2014-01-20 10:30:24 +0000147#if MICROPY_MEM_STATS
Damien Georgecbf76742015-11-27 13:38:15 +0000148 { MP_ROM_QSTR(MP_QSTR_mem_total), MP_ROM_PTR(&mp_micropython_mem_total_obj) },
149 { MP_ROM_QSTR(MP_QSTR_mem_current), MP_ROM_PTR(&mp_micropython_mem_current_obj) },
150 { MP_ROM_QSTR(MP_QSTR_mem_peak), MP_ROM_PTR(&mp_micropython_mem_peak_obj) },
Damien George89deec02015-01-09 20:12:54 +0000151#endif
Damien Georgecbf76742015-11-27 13:38:15 +0000152 { MP_ROM_QSTR(MP_QSTR_mem_info), MP_ROM_PTR(&mp_micropython_mem_info_obj) },
153 { MP_ROM_QSTR(MP_QSTR_qstr_info), MP_ROM_PTR(&mp_micropython_qstr_info_obj) },
Damien Georgedf4ce932016-01-22 16:16:38 +0000154 #if MICROPY_STACK_CHECK
155 { MP_ROM_QSTR(MP_QSTR_stack_use), MP_ROM_PTR(&mp_micropython_stack_use_obj) },
156 #endif
Damien George91d457a2014-01-20 10:30:24 +0000157#endif
Dave Hylands5b7fd202014-07-01 23:46:53 -0700158#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
Damien Georgecbf76742015-11-27 13:38:15 +0000159 { MP_ROM_QSTR(MP_QSTR_alloc_emergency_exception_buf), MP_ROM_PTR(&mp_alloc_emergency_exception_buf_obj) },
Dave Hylands5b7fd202014-07-01 23:46:53 -0700160#endif
Damien Georgedf4ce932016-01-22 16:16:38 +0000161 #if MICROPY_ENABLE_GC
162 { MP_ROM_QSTR(MP_QSTR_heap_lock), MP_ROM_PTR(&mp_micropython_heap_lock_obj) },
163 { MP_ROM_QSTR(MP_QSTR_heap_unlock), MP_ROM_PTR(&mp_micropython_heap_unlock_obj) },
164 #endif
Damien George6e74d242017-02-16 18:05:06 +1100165 #if MICROPY_ENABLE_SCHEDULER
166 { MP_ROM_QSTR(MP_QSTR_schedule), MP_ROM_PTR(&mp_micropython_schedule_obj) },
167 #endif
Damien George0c36da02014-03-08 15:24:39 +0000168};
169
Damien George3b603f22014-11-29 14:39:27 +0000170STATIC MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table);
Damien George0c36da02014-03-08 15:24:39 +0000171
172const mp_obj_module_t mp_module_micropython = {
173 .base = { &mp_type_module },
Damien George8b0535e2014-04-05 21:53:54 +0100174 .globals = (mp_obj_dict_t*)&mp_module_micropython_globals,
Damien George0c36da02014-03-08 15:24:39 +0000175};