|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * \file ns_monitor.c |
| 20 | + * \brief Utility functions for nanostack maintenance |
| 21 | + * |
| 22 | + * This module tracks stack current heap usage and triggers GC if heap usage is too high. |
| 23 | + * GC is triggered by: |
| 24 | + * 1. Heap usage is above HEAP_USAGE_HIGH |
| 25 | + * 2. Heap usage is above HEAP_USAGE_CRITICAL |
| 26 | + * 3. If nsdynmemLIB memory allocation has failed since last check |
| 27 | + */ |
| 28 | + |
| 29 | +#include "nsconfig.h" |
| 30 | +#include "ns_types.h" |
| 31 | +#define HAVE_DEBUG |
| 32 | +#include "ns_trace.h" |
| 33 | +#include "nsdynmemLIB.h" |
| 34 | +#include "ipv6_stack/ipv6_routing_table.h" |
| 35 | +#include "NWK_INTERFACE/Include/protocol.h" |
| 36 | + |
| 37 | +#define TRACE_GROUP "mntr" |
| 38 | + |
| 39 | +typedef enum { |
| 40 | + NS_MONITOR_STATE_HEAP_GC_IDLE = 0, |
| 41 | + NS_MONITOR_STATE_HEAP_GC_HIGH, |
| 42 | + NS_MONITOR_STATE_GC_CRITICAL |
| 43 | +} ns_monitor_state_e; |
| 44 | + |
| 45 | +#define HEAP_HIGH_THRESHOLD (0.7) /* Heap usage HIGH threshold */ |
| 46 | +#define HEAP_CRITICAL_THRESHOLD (0.9) /* Heap usage CRITICAL threshold */ |
| 47 | + |
| 48 | +#define NS_MAINTENANCE_TIMER_INTERVAL 10 // Maintenance interval |
| 49 | + |
| 50 | +typedef struct ns_monitor__s { |
| 51 | + ns_mem_heap_size_t heap_high_watermark; |
| 52 | + ns_mem_heap_size_t heap_critical_watermark; |
| 53 | + uint32_t prev_heap_alloc_fail_cnt; |
| 54 | + ns_monitor_state_e ns_monitor_heap_gc_state; |
| 55 | + const mem_stat_t *mem_stats; |
| 56 | + uint16_t ns_maintenance_timer; |
| 57 | +} ns_monitor_t; |
| 58 | + |
| 59 | +static ns_monitor_t *ns_monitor_ptr = NULL; |
| 60 | + |
| 61 | +typedef void (ns_maintenance_gc_cb)(void); |
| 62 | + |
| 63 | +/* |
| 64 | + * Garbage collection functions. |
| 65 | + * Add more GC performing functions to the table |
| 66 | + * |
| 67 | + */ |
| 68 | +static ns_maintenance_gc_cb *ns_maintenance_gc_functions[] = { |
| 69 | + ipv6_destination_cache_forced_gc |
| 70 | +}; |
| 71 | + |
| 72 | +static void ns_monitor_heap_gc(bool full_gc) |
| 73 | +{ |
| 74 | + (void) full_gc; |
| 75 | + |
| 76 | + for (unsigned int i = 0; i < sizeof(ns_maintenance_gc_functions) / sizeof(ns_maintenance_gc_functions[0]); i++) { |
| 77 | + (ns_maintenance_gc_functions[i])(); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +static void ns_monitor_periodic_heap_health_check(void) |
| 82 | +{ |
| 83 | + if (ns_monitor_ptr->mem_stats->heap_sector_allocated_bytes > ns_monitor_ptr->heap_critical_watermark) { |
| 84 | + if (ns_monitor_ptr->ns_monitor_heap_gc_state != NS_MONITOR_STATE_GC_CRITICAL) { |
| 85 | + ns_mem_heap_size_t prev_heap_sector_allocated_bytes = ns_monitor_ptr->mem_stats->heap_sector_allocated_bytes; |
| 86 | + // Heap usage above CRITICAL |
| 87 | + ns_monitor_heap_gc(true); |
| 88 | + ns_monitor_ptr->ns_monitor_heap_gc_state = NS_MONITOR_STATE_GC_CRITICAL; |
| 89 | + tr_info("Stack GC critical: freed %lu bytes", (unsigned long)(prev_heap_sector_allocated_bytes - ns_monitor_ptr->mem_stats->heap_sector_allocated_bytes)); |
| 90 | + } |
| 91 | + } else if (ns_monitor_ptr->mem_stats->heap_sector_allocated_bytes > ns_monitor_ptr->heap_high_watermark) { |
| 92 | + if (ns_monitor_ptr->ns_monitor_heap_gc_state == NS_MONITOR_STATE_HEAP_GC_IDLE) { |
| 93 | + ns_mem_heap_size_t prev_heap_sector_allocated_bytes = ns_monitor_ptr->mem_stats->heap_sector_allocated_bytes; |
| 94 | + // Heap usage above HIGH |
| 95 | + ns_monitor_heap_gc(false); |
| 96 | + ns_monitor_ptr->ns_monitor_heap_gc_state = NS_MONITOR_STATE_HEAP_GC_HIGH; |
| 97 | + tr_info("Stack GC high: freed %lu bytes", (unsigned long)(prev_heap_sector_allocated_bytes - ns_monitor_ptr->mem_stats->heap_sector_allocated_bytes)); |
| 98 | + } |
| 99 | + } else if (ns_monitor_ptr->mem_stats->heap_sector_allocated_bytes <= ns_monitor_ptr->heap_high_watermark) { |
| 100 | + // Heap usage in normal range |
| 101 | + ns_monitor_ptr->ns_monitor_heap_gc_state = NS_MONITOR_STATE_HEAP_GC_IDLE; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void ns_monitor_timer(uint16_t seconds) |
| 106 | +{ |
| 107 | + if (ns_monitor_ptr) { |
| 108 | + ns_monitor_ptr->ns_maintenance_timer += seconds; |
| 109 | + |
| 110 | + if (ns_monitor_ptr->mem_stats->heap_alloc_fail_cnt > ns_monitor_ptr->prev_heap_alloc_fail_cnt) { |
| 111 | + // Heap allocation failure occurred since last check |
| 112 | + ns_monitor_ptr->prev_heap_alloc_fail_cnt = ns_monitor_ptr->mem_stats->heap_alloc_fail_cnt; |
| 113 | + if (ns_monitor_ptr->ns_monitor_heap_gc_state != NS_MONITOR_STATE_GC_CRITICAL) { |
| 114 | + ns_monitor_ptr->ns_monitor_heap_gc_state = NS_MONITOR_STATE_GC_CRITICAL; |
| 115 | + ns_monitor_heap_gc(true); |
| 116 | + ns_monitor_ptr->ns_maintenance_timer = 0; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (ns_monitor_ptr->ns_maintenance_timer >= NS_MAINTENANCE_TIMER_INTERVAL) { |
| 121 | + ns_monitor_ptr->ns_maintenance_timer -= NS_MAINTENANCE_TIMER_INTERVAL; |
| 122 | + tr_debug("ns_monitor_maintenance_timer(), used %lu/%lu", (unsigned long)ns_monitor_ptr->mem_stats->heap_sector_allocated_bytes, (unsigned long)ns_monitor_ptr->mem_stats->heap_sector_size); |
| 123 | + ns_monitor_periodic_heap_health_check(); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +int ns_monitor_init(void) |
| 129 | +{ |
| 130 | + if (ns_monitor_ptr || !ns_dyn_mem_get_mem_stat()) { |
| 131 | + // already initialized or memory statistics not available |
| 132 | + return -2; |
| 133 | + } |
| 134 | + |
| 135 | + ns_monitor_ptr = ns_dyn_mem_alloc(sizeof(ns_monitor_t)); |
| 136 | + |
| 137 | + if (ns_monitor_ptr) { |
| 138 | + ns_monitor_ptr->mem_stats = ns_dyn_mem_get_mem_stat(); |
| 139 | + ns_monitor_ptr->heap_high_watermark = ns_monitor_ptr->mem_stats->heap_sector_size * HEAP_HIGH_THRESHOLD; |
| 140 | + ns_monitor_ptr->heap_critical_watermark = ns_monitor_ptr->mem_stats->heap_sector_size * HEAP_CRITICAL_THRESHOLD; |
| 141 | + ns_monitor_ptr->ns_monitor_heap_gc_state = NS_MONITOR_STATE_HEAP_GC_IDLE; |
| 142 | + ns_monitor_ptr->ns_maintenance_timer = 0; |
| 143 | + ns_monitor_ptr->prev_heap_alloc_fail_cnt = 0; |
| 144 | + tr_debug("monitor high %lu, critical %lu", (unsigned long)ns_monitor_ptr->heap_high_watermark, (unsigned long)ns_monitor_ptr->heap_critical_watermark); |
| 145 | + return 0; |
| 146 | + } |
| 147 | + |
| 148 | + return -1; |
| 149 | +} |
| 150 | + |
| 151 | +int ns_monitor_clear(void) |
| 152 | +{ |
| 153 | + if (ns_monitor_ptr) { |
| 154 | + ns_dyn_mem_free(ns_monitor_ptr); |
| 155 | + ns_monitor_ptr = NULL; |
| 156 | + return 0; |
| 157 | + } |
| 158 | + |
| 159 | + return -1; |
| 160 | +} |
| 161 | + |
| 162 | +int ns_monitor_heap_gc_threshold_set(uint8_t percentage_high, uint8_t percentage_critical) |
| 163 | +{ |
| 164 | + if (ns_monitor_ptr && (percentage_critical <= 100) && (percentage_high < percentage_critical)) { |
| 165 | + ns_monitor_ptr->heap_high_watermark = ns_monitor_ptr->mem_stats->heap_sector_size * percentage_high / 100; |
| 166 | + ns_monitor_ptr->heap_critical_watermark = ns_monitor_ptr->mem_stats->heap_sector_size * percentage_critical / 100; |
| 167 | + return 0; |
| 168 | + } |
| 169 | + |
| 170 | + return -1; |
| 171 | +} |
0 commit comments