Skip to content

Commit

Permalink
improve mem logging
Browse files Browse the repository at this point in the history
  • Loading branch information
kglowacki committed Oct 9, 2017
1 parent e710eb4 commit e1a325f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 91 deletions.
19 changes: 19 additions & 0 deletions bin/read_serial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/python
import serial
import sys
import time

ser = serial.Serial(None,115200,timeout=None,rtscts=False,xonxoff=True)
ser.port=sys.argv[1]
ser.dtr=False
ser.rts=False #otherwise it will wait for download after reboot
ser.open()

try:
while True:
line = ser.readline()
print line,
#to work with 'tee'
sys.stdout.flush()
finally:
ser.close()
2 changes: 1 addition & 1 deletion components/oap-aws/awsiot.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static esp_err_t awsiot_rest_post(oap_measurement_t* meas, oap_sensor_config_t *
cJSON_AddItemToObject(reported, "status", status);

cJSON_AddNumberToObject(results, "uid", rand()); //what about 0?
cJSON_AddNumberToObject(status, "heap", xPortGetFreeHeapSize());
cJSON_AddNumberToObject(status, "heap", avg_free_heap_size());
cJSON_AddNumberToObject(status, "heap_min", xPortGetMinimumEverFreeHeapSize());


Expand Down
78 changes: 0 additions & 78 deletions components/oap-aws/awsiot_rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ static int download_callback(request_t *req, char *data, int len) {
esp_err_t awsiot_update_shadow(awsiot_config_t* awsiot_config, char* body) {
char uri[100];
sprintf(uri, "https://%s:%d", awsiot_config->endpoint, awsiot_config->port);
// char host_header[100];
// sprintf(host_header, "Host: %s", awsiot_config->endpoint);

request_t* req = req_new(uri);
if (!req) {
Expand Down Expand Up @@ -106,79 +104,3 @@ esp_err_t awsiot_update_shadow(awsiot_config_t* awsiot_config, char* body) {
return ESP_OK;
}
}


/*
#define RESPONSE_BUF_SIZE 1024
esp_err_t awsiot_update_shadow_old(awsiot_config_t awsiot_config, char* body) {
//heap_log* hl = heap_log_take(NULL, "start");
sslclient_context ssl_client = {.0};
ssl_init(&ssl_client);
int ret = ESP_OK;
//heap_log_take(hl, "after ssl_init");
ESP_LOGD(TAG, "connecting to %s:%d", awsiot_config.endpoint,awsiot_config.port);
if ((ssl_client.socket = open_socket(awsiot_config.endpoint,awsiot_config.port,1,0)) < 0) {
return ssl_client.socket;
} else {
ESP_LOGD(TAG, "connected");
}
//heap_log_take(hl, "connected");
char* rootCA = str_make((void*)verisign_root_ca_pem_start, verisign_root_ca_pem_end-verisign_root_ca_pem_start);
if (start_ssl_client(&ssl_client, (unsigned char*)rootCA, (unsigned char*)awsiot_config.cert, (unsigned char*)awsiot_config.pkey, awsiot_config.endpoint) == ESP_OK) {
free(rootCA);
//heap_log_take(hl, "start_ssl_client");
char* request = malloc(strlen(body) + 250);
sprintf(request, "POST /things/%s/shadow HTTP/1.1\n"
"Host: %s\n"
"Content-Type: application/json\n"
"Connection: close\n"
"Content-Length: %d\n"
"\r\n%s", awsiot_config.thingName, awsiot_config.endpoint, strlen(body), body);
ESP_LOGD(TAG, "%s (%d bytes)", request, strlen(request));
//heap_log_take(hl, "built request");
send_ssl_data(&ssl_client, (uint8_t *)request, strlen(request));
//heap_log_take(hl, "send_ssl_data");
free(request);
//heap_log_take(hl, "free request");
int len;
//TODO parse at least status code (would be nice to get json body) too
unsigned char* buf = malloc(RESPONSE_BUF_SIZE);
do {
len = get_ssl_receive(&ssl_client, buf, RESPONSE_BUF_SIZE);
if (len == MBEDTLS_ERR_SSL_WANT_READ || len == MBEDTLS_ERR_SSL_WANT_WRITE) {
continue;
} else if (len == -0x4C) {
ESP_LOGD(TAG, "timeout");
break;
} else if (len <= 0) {
ret = len;
break;
}
for (int i=0; i < len ; i++) {
putchar(buf[i]);
}
} while (1);
free(buf);
} else {
free(rootCA);
ret = ESP_FAIL;
}
//heap_log_take(hl, "request done");
stop_ssl_socket(&ssl_client);
//heap_log_take(hl, "stop_ssl_socket");
//heap_log_free(hl);
ESP_LOGI(TAG, "ssl request done %d", ret);
return ret;
}*/
1 change: 1 addition & 0 deletions components/oap-common/include/oap_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void heap_log_print(heap_log* log, heap_log* prev);
heap_log* heap_log_take(heap_log* log, const char* msg);
void heap_log_free(heap_log* log);

size_t avg_free_heap_size();

void log_task_stack(const char* task);
void log_heap_size(const char* msg);
Expand Down
34 changes: 27 additions & 7 deletions components/oap-common/oap_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,40 @@
void log_task_stack(const char* task) {
//uxTaskGetStackHighWaterMark is marked as UNTESTED
#if !CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION
ESP_LOGD(TAG, "stack min (%d) - task '%s'", uxTaskGetStackHighWaterMark( NULL ), task);
ESP_LOGD(TAG, "stack min %d (task %s)", uxTaskGetStackHighWaterMark( NULL ), task);
#endif
}


static size_t last_free_heap = 0;
/**
* current free heap is not very useful because it changes dynamically with multiple tasks running in parallel.
* to detect any leaks, we use a time window and choose the max value as real 'free heap'
*/
#define SAMPLES 10
static size_t heap_samples[SAMPLES] = {.0};
uint8_t sample_idx = 0;

size_t avg_free_heap_size() {
size_t max = 0;
for (int i = 0; i < SAMPLES; i++) {
if (heap_samples[i] > max) max = heap_samples[i];
}
return max;
}

void log_heap_size(const char* msg) {
size_t free_heap = xPortGetFreeHeapSize();
if (last_free_heap == 0) last_free_heap = free_heap;
ESP_LOGD(TAG, "heap min (%d) free (%d) change (%d) - '%s'",
if (heap_samples[sample_idx%SAMPLES] == 0) heap_samples[sample_idx%SAMPLES] = free_heap;



ESP_LOGD(TAG, "heap min %d avg %d free %d change %d (%s)",
xPortGetMinimumEverFreeHeapSize(),
free_heap, free_heap-last_free_heap, msg);
last_free_heap = free_heap;
avg_free_heap_size(),
free_heap,
free_heap-heap_samples[sample_idx%SAMPLES],
msg);
heap_samples[sample_idx%SAMPLES] = free_heap;
sample_idx++;
}

static void* dummy;
Expand Down
10 changes: 5 additions & 5 deletions components/oap-thingspk/thing_speak.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ static char* prepare_thingspeak_payload(oap_measurement_t* meas) {
meas->env->humidity);
}

if (meas->env_int) {
sprintf(payload, "%s&field7=%.2f&field8=%.2f", payload,
meas->env_int->temp,
meas->env_int->humidity);
}
//memory metrics
sprintf(payload, "%s&field7=%d&field8=%d", payload,
avg_free_heap_size(),
xPortGetMinimumEverFreeHeapSize());

return payload;
}

Expand Down

0 comments on commit e1a325f

Please sign in to comment.