LCOV - code coverage report
Current view: top level - py - modmicropython.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.23.0-134-g7fe8f030e.info Lines: 46 46 100.0 %
Date: 2024-07-24 10:19:04 Functions: 12 12 100.0 %
Branches: 8 8 100.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython 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                 :            : 
      27                 :            : #include <stdio.h>
      28                 :            : 
      29                 :            : #include "py/builtin.h"
      30                 :            : #include "py/stackctrl.h"
      31                 :            : #include "py/runtime.h"
      32                 :            : #include "py/gc.h"
      33                 :            : #include "py/mphal.h"
      34                 :            : 
      35                 :            : #if MICROPY_PY_MICROPYTHON
      36                 :            : 
      37                 :            : // Various builtins specific to MicroPython runtime,
      38                 :            : // living in micropython module
      39                 :            : 
      40                 :            : #if MICROPY_ENABLE_COMPILER
      41                 :         26 : static mp_obj_t mp_micropython_opt_level(size_t n_args, const mp_obj_t *args) {
      42         [ +  + ]:         26 :     if (n_args == 0) {
      43                 :          8 :         return MP_OBJ_NEW_SMALL_INT(MP_STATE_VM(mp_optimise_value));
      44                 :            :     } else {
      45                 :         18 :         MP_STATE_VM(mp_optimise_value) = mp_obj_get_int(args[0]);
      46                 :         18 :         return mp_const_none;
      47                 :            :     }
      48                 :            : }
      49                 :            : static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_opt_level_obj, 0, 1, mp_micropython_opt_level);
      50                 :            : #endif
      51                 :            : 
      52                 :            : #if MICROPY_PY_MICROPYTHON_MEM_INFO
      53                 :            : 
      54                 :            : #if MICROPY_MEM_STATS
      55                 :         16 : static mp_obj_t mp_micropython_mem_total(void) {
      56                 :         16 :     return MP_OBJ_NEW_SMALL_INT(m_get_total_bytes_allocated());
      57                 :            : }
      58                 :            : static MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total);
      59                 :            : 
      60                 :          8 : static mp_obj_t mp_micropython_mem_current(void) {
      61                 :          8 :     return MP_OBJ_NEW_SMALL_INT(m_get_current_bytes_allocated());
      62                 :            : }
      63                 :            : static MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_current_obj, mp_micropython_mem_current);
      64                 :            : 
      65                 :          8 : static mp_obj_t mp_micropython_mem_peak(void) {
      66                 :          8 :     return MP_OBJ_NEW_SMALL_INT(m_get_peak_bytes_allocated());
      67                 :            : }
      68                 :            : static MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_peak_obj, mp_micropython_mem_peak);
      69                 :            : #endif
      70                 :            : 
      71                 :         18 : mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args) {
      72                 :         18 :     (void)args;
      73                 :            :     #if MICROPY_MEM_STATS
      74                 :         18 :     mp_printf(&mp_plat_print, "mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
      75                 :         18 :         (mp_uint_t)m_get_total_bytes_allocated(), (mp_uint_t)m_get_current_bytes_allocated(), (mp_uint_t)m_get_peak_bytes_allocated());
      76                 :            :     #endif
      77                 :            :     #if MICROPY_STACK_CHECK
      78                 :         18 :     mp_printf(&mp_plat_print, "stack: " UINT_FMT " out of " UINT_FMT "\n",
      79                 :         18 :         mp_stack_usage(), (mp_uint_t)MP_STATE_THREAD(stack_limit));
      80                 :            :     #else
      81                 :            :     mp_printf(&mp_plat_print, "stack: " UINT_FMT "\n", mp_stack_usage());
      82                 :            :     #endif
      83                 :            :     #if MICROPY_ENABLE_GC
      84                 :         18 :     gc_dump_info(&mp_plat_print);
      85         [ +  + ]:         18 :     if (n_args == 1) {
      86                 :            :         // arg given means dump gc allocation table
      87                 :          4 :         gc_dump_alloc_table(&mp_plat_print);
      88                 :            :     }
      89                 :            :     #else
      90                 :            :     (void)n_args;
      91                 :            :     #endif
      92                 :         18 :     return mp_const_none;
      93                 :            : }
      94                 :            : static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_mem_info_obj, 0, 1, mp_micropython_mem_info);
      95                 :            : 
      96                 :          8 : static mp_obj_t mp_micropython_qstr_info(size_t n_args, const mp_obj_t *args) {
      97                 :          8 :     (void)args;
      98                 :          8 :     size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
      99                 :          8 :     qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
     100                 :          8 :     mp_printf(&mp_plat_print, "qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n",
     101                 :            :         n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
     102         [ +  + ]:          8 :     if (n_args == 1) {
     103                 :            :         // arg given means dump qstr data
     104                 :          4 :         qstr_dump_data();
     105                 :            :     }
     106                 :          8 :     return mp_const_none;
     107                 :            : }
     108                 :            : static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_micropython_qstr_info_obj, 0, 1, mp_micropython_qstr_info);
     109                 :            : 
     110                 :            : #endif // MICROPY_PY_MICROPYTHON_MEM_INFO
     111                 :            : 
     112                 :            : #if MICROPY_PY_MICROPYTHON_STACK_USE
     113                 :          4 : static mp_obj_t mp_micropython_stack_use(void) {
     114                 :          4 :     return MP_OBJ_NEW_SMALL_INT(mp_stack_usage());
     115                 :            : }
     116                 :            : static MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_stack_use_obj, mp_micropython_stack_use);
     117                 :            : #endif
     118                 :            : 
     119                 :            : #if MICROPY_ENABLE_PYSTACK
     120                 :            : static mp_obj_t mp_micropython_pystack_use(void) {
     121                 :            :     return MP_OBJ_NEW_SMALL_INT(mp_pystack_usage());
     122                 :            : }
     123                 :            : static MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_pystack_use_obj, mp_micropython_pystack_use);
     124                 :            : #endif
     125                 :            : 
     126                 :            : #if MICROPY_ENABLE_GC
     127                 :        251 : static mp_obj_t mp_micropython_heap_lock(void) {
     128                 :        251 :     gc_lock();
     129                 :        251 :     return mp_const_none;
     130                 :            : }
     131                 :            : static MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_lock_obj, mp_micropython_heap_lock);
     132                 :            : 
     133                 :        251 : static mp_obj_t mp_micropython_heap_unlock(void) {
     134                 :        251 :     gc_unlock();
     135                 :        251 :     return MP_OBJ_NEW_SMALL_INT(MP_STATE_THREAD(gc_lock_depth));
     136                 :            : }
     137                 :            : static MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_unlock_obj, mp_micropython_heap_unlock);
     138                 :            : 
     139                 :            : #if MICROPY_PY_MICROPYTHON_HEAP_LOCKED
     140                 :          8 : static mp_obj_t mp_micropython_heap_locked(void) {
     141                 :          8 :     return MP_OBJ_NEW_SMALL_INT(MP_STATE_THREAD(gc_lock_depth));
     142                 :            : }
     143                 :            : static MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_heap_locked_obj, mp_micropython_heap_locked);
     144                 :            : #endif
     145                 :            : #endif
     146                 :            : 
     147                 :            : #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
     148                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_emergency_exception_buf);
     149                 :            : #endif
     150                 :            : 
     151                 :            : #if MICROPY_KBD_EXCEPTION
     152                 :          4 : static mp_obj_t mp_micropython_kbd_intr(mp_obj_t int_chr_in) {
     153                 :          4 :     mp_hal_set_interrupt_char(mp_obj_get_int(int_chr_in));
     154                 :          4 :     return mp_const_none;
     155                 :            : }
     156                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(mp_micropython_kbd_intr_obj, mp_micropython_kbd_intr);
     157                 :            : #endif
     158                 :            : 
     159                 :            : #if MICROPY_ENABLE_SCHEDULER
     160                 :      11581 : static mp_obj_t mp_micropython_schedule(mp_obj_t function, mp_obj_t arg) {
     161         [ +  + ]:      11581 :     if (!mp_sched_schedule(function, arg)) {
     162                 :         10 :         mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("schedule queue full"));
     163                 :            :     }
     164                 :      11571 :     return mp_const_none;
     165                 :            : }
     166                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(mp_micropython_schedule_obj, mp_micropython_schedule);
     167                 :            : #endif
     168                 :            : 
     169                 :            : static const mp_rom_map_elem_t mp_module_micropython_globals_table[] = {
     170                 :            :     { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_micropython) },
     171                 :            :     { MP_ROM_QSTR(MP_QSTR_const), MP_ROM_PTR(&mp_identity_obj) },
     172                 :            :     #if MICROPY_ENABLE_COMPILER
     173                 :            :     { MP_ROM_QSTR(MP_QSTR_opt_level), MP_ROM_PTR(&mp_micropython_opt_level_obj) },
     174                 :            :     #endif
     175                 :            :     #if MICROPY_PY_MICROPYTHON_MEM_INFO
     176                 :            :     #if MICROPY_MEM_STATS
     177                 :            :     { MP_ROM_QSTR(MP_QSTR_mem_total), MP_ROM_PTR(&mp_micropython_mem_total_obj) },
     178                 :            :     { MP_ROM_QSTR(MP_QSTR_mem_current), MP_ROM_PTR(&mp_micropython_mem_current_obj) },
     179                 :            :     { MP_ROM_QSTR(MP_QSTR_mem_peak), MP_ROM_PTR(&mp_micropython_mem_peak_obj) },
     180                 :            :     #endif
     181                 :            :     { MP_ROM_QSTR(MP_QSTR_mem_info), MP_ROM_PTR(&mp_micropython_mem_info_obj) },
     182                 :            :     { MP_ROM_QSTR(MP_QSTR_qstr_info), MP_ROM_PTR(&mp_micropython_qstr_info_obj) },
     183                 :            :     #endif
     184                 :            :     #if MICROPY_PY_MICROPYTHON_STACK_USE
     185                 :            :     { MP_ROM_QSTR(MP_QSTR_stack_use), MP_ROM_PTR(&mp_micropython_stack_use_obj) },
     186                 :            :     #endif
     187                 :            :     #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0)
     188                 :            :     { MP_ROM_QSTR(MP_QSTR_alloc_emergency_exception_buf), MP_ROM_PTR(&mp_alloc_emergency_exception_buf_obj) },
     189                 :            :     #endif
     190                 :            :     #if MICROPY_ENABLE_PYSTACK
     191                 :            :     { MP_ROM_QSTR(MP_QSTR_pystack_use), MP_ROM_PTR(&mp_micropython_pystack_use_obj) },
     192                 :            :     #endif
     193                 :            :     #if MICROPY_ENABLE_GC
     194                 :            :     { MP_ROM_QSTR(MP_QSTR_heap_lock), MP_ROM_PTR(&mp_micropython_heap_lock_obj) },
     195                 :            :     { MP_ROM_QSTR(MP_QSTR_heap_unlock), MP_ROM_PTR(&mp_micropython_heap_unlock_obj) },
     196                 :            :     #if MICROPY_PY_MICROPYTHON_HEAP_LOCKED
     197                 :            :     { MP_ROM_QSTR(MP_QSTR_heap_locked), MP_ROM_PTR(&mp_micropython_heap_locked_obj) },
     198                 :            :     #endif
     199                 :            :     #endif
     200                 :            :     #if MICROPY_KBD_EXCEPTION
     201                 :            :     { MP_ROM_QSTR(MP_QSTR_kbd_intr), MP_ROM_PTR(&mp_micropython_kbd_intr_obj) },
     202                 :            :     #endif
     203                 :            :     #if MICROPY_ENABLE_SCHEDULER
     204                 :            :     { MP_ROM_QSTR(MP_QSTR_schedule), MP_ROM_PTR(&mp_micropython_schedule_obj) },
     205                 :            :     #endif
     206                 :            : };
     207                 :            : 
     208                 :            : static MP_DEFINE_CONST_DICT(mp_module_micropython_globals, mp_module_micropython_globals_table);
     209                 :            : 
     210                 :            : const mp_obj_module_t mp_module_micropython = {
     211                 :            :     .base = { &mp_type_module },
     212                 :            :     .globals = (mp_obj_dict_t *)&mp_module_micropython_globals,
     213                 :            : };
     214                 :            : 
     215                 :            : MP_REGISTER_MODULE(MP_QSTR_micropython, mp_module_micropython);
     216                 :            : 
     217                 :            : #endif // MICROPY_PY_MICROPYTHON

Generated by: LCOV version 1.15-5-g462f71d