본문 바로가기

정리글

mbed OS 에서 메모리 사용량 체크 mbed_stats.h

과제가 거의 완료되어 여유시간이 남았으니 회사에서 작업했던 내용을 조금씩 정리하고자 한다.

 

곧있으면 투입된 다른 프로젝트가 시작할터이니 그 전에 조금씩 해놔야지.

 

지난 과제에서는 Mbed OS 가 들어간 커스텀 보드를 사용했다.

 

근데 뭐만 넣었다 하면 맨날 리셋이 되더라...

 

거의 대부분의 원인은 달려있던 통신모듈이 강제로 리셋 때리는 거였다.

 

다른 Thread에서 Priority를 높게 잡아가면 통신모듈에서 돌아가는 Thread가 잘 안돌아가서 오류 뿜뿜 -> 리셋

 

아무튼, 처음에는 메모리 문제인가 해서 이것저것 빼고, 코드 전체 크기도 줄이고, Static array 생성한것도 줄여보고 다 했다.

 

거의 한달동안 이 짓을 해서 코드가 꽤 가볍게 돌아가긴 하더라. 불필요하게 많이 선언된 배열들을 다 줄여버렸다.

 

최종적으로 문제를 해결한것은 결국 통신모듈에도 디버그를 걸어 문제를 찾아냈다. ( 외부협력업체꺼라 보기가 힘들었다. )

 

이렇게 문제를 해결하던 도중에 사용한게 mbed_stats.h 다.

 

메모리가 부족한지 확실하게 알고싶었던 우리는 stack과 heap 크기를 출력하는 이 헤더가 있다는걸 알았다.

 

typedef struct {
uint32_t current_size; /**< Bytes currently allocated on the heap */
uint32_t max_size; /**< Maximum bytes allocated on the heap at one time since reset */
uint32_t total_size; /**< Cumulative sum of bytes allocated on the heap that have not been freed */
uint32_t reserved_size; /**< Current number of bytes reserved for the heap */
uint32_t alloc_cnt; /**< Current number of allocations that have not been freed since reset */
uint32_t alloc_fail_cnt; /**< Number of failed allocations since reset */
uint32_t overhead_size; /**< Number of bytes used to store heap statistics. This overhead takes up space on the heap, reducing the available heap space */
} mbed_stats_heap_t;
typedef struct {
uint32_t thread_id; /**< Identifier for the thread that owns the stack or 0 if representing accumulated statistics */
uint32_t max_size; /**< Maximum number of bytes used on the stack since the thread was started */
uint32_t reserved_size; /**< Current number of bytes reserved for the stack */
uint32_t stack_cnt; /**< The number of stacks represented in the accumulated statistics or 1 if representing a single stack */
} mbed_stats_stack_t;

내부에는 mbed os 에서 사용하는 stack과 heap을 확인할 수 있는 구조체가 존재한다.

 

void mbed_stats_stack_get(mbed_stats_stack_t *stats);

void mbed_stats_heap_get(mbed_stats_heap_t *stats);

 

각 함수를 이용해서 heap과 stack의 현재 상태를 가져올 수 있다.

 

근데 stack은 여러개가 존재하니

 

size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count);

 

후배가 이 함수를 써서 각 thread의 크기를 따로 출력했다.

 

결국 확인한건 메모리는 생각보단 여유로왔던 것이고( 실제로는 부족하긴 했다. 지레짐작으로 정리했던게 우연히 도움이 되었다.), 문제는 다른곳에 있었다는것이였다.

'정리글' 카테고리의 다른 글

JAVA Maven 프로젝트에서 JNA 사용  (0) 2020.09.16
IIO - Linux Industrial I/O subsystem  (0) 2020.09.02
LWM2M 했던 것들 정리  (0) 2020.07.14
TCP tunneling 을 빙자한 Raw Socket  (0) 2020.06.11
시리얼 통신  (0) 2019.07.03