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

Generated by: LCOV version 1.15-5-g462f71d