Skip to content

Commit

Permalink
Review corrections
Browse files Browse the repository at this point in the history
-Rename ns_monitor_api.c/h to ns_conf.c/h
-Check GC function availability before calling it
-Add gc level parameter to gc function
  • Loading branch information
Arto Kinnunen committed Jul 30, 2019
1 parent 01e7d84 commit 64e6ff3
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 18 deletions.
14 changes: 7 additions & 7 deletions nanostack/ns_monitor_api.h → nanostack/ns_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
* limitations under the License.
*/

#ifndef _NS_MONITOR_API_H_
#define _NS_MONITOR_API_H_
#ifndef _NS_CONF_H_
#define _NS_CONF_H_

#ifdef __cplusplus
extern "C" {
#endif

/**
* \file ns_monitor_api.h
* \brief Nanostack monitoring API.
* \file ns_conf.h
* \brief Nanostack configuration API.
*/

/**
Expand All @@ -35,7 +35,7 @@ extern "C" {
* a GC will try to release memory that is used for caching. When CRITTICAL threshold is exceeded them GC will try to release
* memory more aggressiveliy.
*
* Nanostack memory monitoring can only work if memory statistics are enabled to nsdynmemLIB.
* Nanostack memory monitoring can only work if memory statistics are enabled in nsdynmemLIB.
*
* \param percentage_high Percentage of total heap when garbage collection is first time triggered
Expand All @@ -44,11 +44,11 @@ extern "C" {
* \return 0 in success, negative value in case of error.
*
*/
int ns_monitor_api_gc_threshold_set(uint8_t percentage_high, uint8_t percentage_critical);
int ns_conf_gc_threshold_set(uint8_t percentage_high, uint8_t percentage_critical);


#ifdef __cplusplus
}
#endif

#endif /* _NS_MONITOR_API_H_ */
#endif /* _NS_CONF_H_ */
10 changes: 6 additions & 4 deletions source/Core/ns_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef struct ns_monitor__s {

static ns_monitor_t *ns_monitor_ptr = NULL;

typedef void (ns_maintenance_gc_cb)(void);
typedef void (ns_maintenance_gc_cb)(bool full_gc);

/*
* Garbage collection functions.
Expand All @@ -74,7 +74,9 @@ static void ns_monitor_heap_gc(bool full_gc)
(void) full_gc;

for (unsigned int i = 0; i < sizeof(ns_maintenance_gc_functions) / sizeof(ns_maintenance_gc_functions[0]); i++) {
(ns_maintenance_gc_functions[i])();
if (ns_maintenance_gc_functions[i]) {
(ns_maintenance_gc_functions[i])(full_gc);
}
}
}

Expand Down Expand Up @@ -142,7 +144,7 @@ int ns_monitor_init(void)
ns_monitor_ptr->ns_monitor_heap_gc_state = NS_MONITOR_STATE_HEAP_GC_IDLE;
ns_monitor_ptr->ns_maintenance_timer = 0;
ns_monitor_ptr->prev_heap_alloc_fail_cnt = 0;
tr_debug("Monitor high:%lu, critical:%lu total:%lu", (unsigned long)ns_monitor_ptr->heap_high_watermark, (unsigned long)ns_monitor_ptr->heap_critical_watermark, (unsigned long)ns_monitor_ptr->mem_stats->heap_sector_size);
tr_debug("Monitor init high:%lu, critical:%lu total:%lu", (unsigned long)ns_monitor_ptr->heap_high_watermark, (unsigned long)ns_monitor_ptr->heap_critical_watermark, (unsigned long)ns_monitor_ptr->mem_stats->heap_sector_size);
return 0;
}

Expand All @@ -165,7 +167,7 @@ int ns_monitor_heap_gc_threshold_set(uint8_t percentage_high, uint8_t percentage
if (ns_monitor_ptr && (percentage_critical <= 100) && (percentage_high < percentage_critical)) {
ns_monitor_ptr->heap_high_watermark = ns_monitor_ptr->mem_stats->heap_sector_size * percentage_high / 100;
ns_monitor_ptr->heap_critical_watermark = ns_monitor_ptr->mem_stats->heap_sector_size * percentage_critical / 100;
tr_debug("Monitor high:%lu, critical:%lu total:%lu", (unsigned long)ns_monitor_ptr->heap_high_watermark, (unsigned long)ns_monitor_ptr->heap_critical_watermark, (unsigned long)ns_monitor_ptr->mem_stats->heap_sector_size);
tr_debug("Monitor set high:%lu, critical:%lu total:%lu", (unsigned long)ns_monitor_ptr->heap_high_watermark, (unsigned long)ns_monitor_ptr->heap_critical_watermark, (unsigned long)ns_monitor_ptr->mem_stats->heap_sector_size);
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "Core/include/ns_monitor.h"

int ns_monitor_api_gc_threshold_set(uint8_t percentage_high, uint8_t percentage_critical)
int ns_conf_gc_threshold_set(uint8_t percentage_high, uint8_t percentage_critical)
{
return ns_monitor_heap_gc_threshold_set(percentage_high, percentage_critical);
}
9 changes: 6 additions & 3 deletions source/ipv6_stack/ipv6_routing_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -1072,13 +1072,16 @@ void ipv6_destination_redirect(const uint8_t *dest_addr, const uint8_t *sender_a
}
#endif

void ipv6_destination_cache_forced_gc(void)
void ipv6_destination_cache_forced_gc(bool full_gc)
{
int gc_count = ns_list_count(&ipv6_destination_cache);

/* Minimize size of destination cache, keep absolutely minimum number of entries */
/* Minimize size of destination cache:
* - keep absolutely minimum number of entries if not full gc
* - clear all entries in case of full gc
**/
ns_list_foreach_reverse_safe(ipv6_destination_t, entry, &ipv6_destination_cache) {
if (entry->lifetime == 0 || gc_count > destination_cache_config.long_term_entries) {
if (entry->lifetime == 0 || gc_count > destination_cache_config.long_term_entries || full_gc) {
ns_list_remove(&ipv6_destination_cache, entry);
ipv6_destination_release(entry);
gc_count--;
Expand Down
2 changes: 1 addition & 1 deletion source/ipv6_stack/ipv6_routing_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void ipv6_destination_cache_timer(uint8_t ticks);
#ifdef HAVE_IPV6_ND
void ipv6_destination_redirect(const uint8_t *dest_addr, const uint8_t *sender_addr, const uint8_t *redirect_addr, int8_t interface_id, addrtype_t ll_type, const uint8_t *ll_address);
#endif
void ipv6_destination_cache_forced_gc(void);
void ipv6_destination_cache_forced_gc(bool full_gc);

/* Combined Routing Table (RFC 4191) and Prefix List (RFC 4861) */
/* On-link prefixes have the on_link flag set and next_hop is unset */
Expand Down
2 changes: 1 addition & 1 deletion sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ SRCS += \
source/Service_Libs/utils/ns_crc.c \
source/Service_Libs/utils/isqrt.c \
source/Service_Libs/utils/ns_file_system.c \
source/Service_Libs/utils/ns_monitor_api.c \
source/Service_Libs/utils/ns_conf.c \
source/Service_Libs/mdns/ns_mdns_api.c \
source/Service_Libs/mdns/ns_fnet_port.c \
source/Service_Libs/mdns/ns_fnet_events.c \
Expand Down
2 changes: 1 addition & 1 deletion test/nanostack/unittest/stub/ipv6_routing_table_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void ipv6_destination_redirect(const uint8_t *dest_addr, const uint8_t *sender_a
{
}

void ipv6_destination_cache_forced_gc(void)
void ipv6_destination_cache_forced_gc(bool full_gc)
{
ipv6_routing_table_stub_called = true;
}
Expand Down
5 changes: 5 additions & 0 deletions test/nanostack/unittest/stub/ns_monitor_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ void ns_monitor_init(void)
{
}

int ns_monitor_clear(void)
{
return 0;
}

void ns_monitor_timer(uint16_t seconds)
{
}
Expand Down

0 comments on commit 64e6ff3

Please sign in to comment.