|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Deomid "rojer" Ryabkov |
| 3 | + * All rights reserved |
| 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 | +#include "esp_esf_buf_monitor.h" |
| 19 | + |
| 20 | +#include <stdlib.h> |
| 21 | + |
| 22 | +#include "mgos.h" |
| 23 | + |
| 24 | +struct pbuf; |
| 25 | + |
| 26 | +struct esf_buf *g_pending_bufs[8] = {0}; |
| 27 | +uint32_t g_esf_buf_canary_ctr = 0; |
| 28 | +uint8_t g_esf_buf_canary_strikes = 0; |
| 29 | + |
| 30 | +extern struct esf_buf *__real_esf_buf_alloc(struct pbuf *p, uint32_t type, |
| 31 | + uint32_t a4); |
| 32 | +struct esf_buf *__wrap_esf_buf_alloc(struct pbuf *p, uint32_t type, |
| 33 | + uint32_t a4) { |
| 34 | + struct esf_buf *eb = __real_esf_buf_alloc(p, type, a4); |
| 35 | + if (eb != NULL) { |
| 36 | + for (int i = 0; i < 8; i++) { |
| 37 | + if (g_pending_bufs[i] == NULL) { |
| 38 | + g_pending_bufs[i] = eb; |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + return eb; |
| 44 | +} |
| 45 | + |
| 46 | +extern void __real_esf_buf_recycle(struct esf_buf *eb, uint32_t type); |
| 47 | +void __wrap_esf_buf_recycle(struct esf_buf *eb, uint32_t type) { |
| 48 | + for (int i = 0; i < 8; i++) { |
| 49 | + if (g_pending_bufs[i] == eb) { |
| 50 | + g_pending_bufs[i] = NULL; |
| 51 | + g_esf_buf_canary_ctr++; |
| 52 | + } |
| 53 | + } |
| 54 | + __real_esf_buf_recycle(eb, type); |
| 55 | +} |
| 56 | + |
| 57 | +static uint32_t s_esf_buf_last_canary_ctr = 0; |
| 58 | + |
| 59 | +static void esp_esf_buf_monitor_timer_cb(void *arg) { |
| 60 | + int num_pending = 0; |
| 61 | + for (int i = 0; i < 8; i++) { |
| 62 | + if (g_pending_bufs[i] != NULL) num_pending++; |
| 63 | + } |
| 64 | + LOG(LL_DEBUG, ("np %d ctr %lu %lu", num_pending, s_esf_buf_last_canary_ctr, |
| 65 | + g_esf_buf_canary_ctr)); |
| 66 | + if (num_pending == 0) { |
| 67 | + // All clear, no problem. |
| 68 | + return; |
| 69 | + } |
| 70 | + if (g_esf_buf_canary_ctr != s_esf_buf_last_canary_ctr) { |
| 71 | + // Things are moving along, all good. |
| 72 | + s_esf_buf_last_canary_ctr = g_esf_buf_canary_ctr; |
| 73 | + return; |
| 74 | + } |
| 75 | + LOG(LL_ERROR, ("TX is stuck!")); |
| 76 | + static bool cb_invoked = false; |
| 77 | + if (!cb_invoked) { |
| 78 | + esp_esf_buf_monitor_failure(); |
| 79 | + cb_invoked = true; |
| 80 | + } |
| 81 | + (void) arg; |
| 82 | +} |
| 83 | + |
| 84 | +void esp_esf_buf_monitor_failure(void) WEAK; |
| 85 | +void esp_esf_buf_monitor_failure(void) { |
| 86 | + mgos_system_restart_after(1000); |
| 87 | +} |
| 88 | + |
| 89 | +extern void __real_lmacProcessTXStartData(uint8_t id); |
| 90 | +IRAM void __wrap_lmacProcessTXStartData(uint8_t id) { |
| 91 | +#ifdef ESP_ESF_BUF_TRIGGER_BUG |
| 92 | + // Introducing a delay here triggers the condition reasonably quickly |
| 93 | + // under moderate traffic load (3 x curl; sleep 1 in parallel). |
| 94 | + if (id == 0) { |
| 95 | + ets_delay_us(500); |
| 96 | + } |
| 97 | +#endif |
| 98 | + __real_lmacProcessTXStartData(id); |
| 99 | +} |
| 100 | + |
| 101 | +void esp_esf_buf_monitor_init(void) { |
| 102 | + mgos_set_timer(ESP_ESF_BUF_MONITOR_INTERVAL_MS, MGOS_TIMER_REPEAT, |
| 103 | + esp_esf_buf_monitor_timer_cb, NULL); |
| 104 | +} |
0 commit comments