From 3d955be62938ed21d9a799a420d5aad7aa7c579d Mon Sep 17 00:00:00 2001 From: Michael Geramb Date: Sat, 11 Nov 2023 19:48:11 +0100 Subject: [PATCH] Add temp buffer statistic --- src/HAP.cpp | 16 ++++++++-------- src/HomeSpan.cpp | 11 ++++++----- src/HomeSpan.h | 2 +- src/Network.cpp | 2 +- src/Utils.cpp | 20 ++++++++++++++++++++ src/Utils.h | 21 +++++++++++++++++---- 6 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 8a2d3b7..4f4d645 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -96,7 +96,7 @@ void HAPClient::init(){ } if(!nvs_get_blob(hapNVS,"CONTROLLERS",NULL,&len)){ // if found long-term Controller Pairings data from NVS - TempBuffer tBuf(len/sizeof(Controller)); + TempBuffer tBuf(len/sizeof(Controller), "HAPClient::init"); nvs_get_blob(hapNVS,"CONTROLLERS",tBuf.get(),&len); // retrieve data for(int i=0;i httpBuf(messageSize+1); // leave room for null character added below + TempBuffer httpBuf(messageSize+1, "HAPClient::processRequest"); // leave room for null character added below if(cPair){ // expecting encrypted message LOG2("<<<< #### "); @@ -859,7 +859,7 @@ int HAPClient::getAccessoriesURL(){ LOG1(")...\n"); int nBytes = homeSpan.sprintfAttributes(NULL); // get size of HAP attributes JSON - TempBuffer jBuf(nBytes+1); + TempBuffer jBuf(nBytes+1,"HAPClient::getAccessoriesURL"); homeSpan.sprintfAttributes(jBuf.get()); // create JSON database (will need to re-cast to uint8_t* below) char *body; @@ -958,7 +958,7 @@ int HAPClient::postPairingsURL(){ case 5: { LOG1("List...\n"); - TempBuffer tBuf(listControllers(NULL)); + TempBuffer tBuf(listControllers(NULL), "HAPClient::postPairingsURL listControllers"); char *body; asprintf(&body,"HTTP/1.1 200 OK\r\nContent-Type: application/pairing+tlv8\r\nContent-Length: %d\r\n\r\n",tBuf.len()); // create Body with Content Length = size of TLV data @@ -1393,7 +1393,7 @@ void HAPClient::eventNotify(SpanBuf *pObj, int nObj, int ignoreClient){ void HAPClient::tlvRespond(){ - TempBuffer tBuf(tlv8.pack(NULL)); // create buffer to hold TLV data + TempBuffer tBuf(tlv8.pack(NULL), "HAPClient::tlvRespond"); // create buffer to hold TLV data tlv8.pack(tBuf.get()); // pack TLV records into buffer char *body; @@ -1433,7 +1433,7 @@ int HAPClient::receiveEncrypted(uint8_t *httpBuf, int messageSize){ return(0); } - TempBuffer tBuf(n+16); // expected number of total bytes = n bytes in encoded message + 16 bytes for appended authentication tag + TempBuffer tBuf(n+16, "HAPClient::receiveEncrypted"); // expected number of total bytes = n bytes in encoded message + 16 bytes for appended authentication tag if(client.read(tBuf.get(),tBuf.len())!=tBuf.len()){ LOG0("\n\n*** ERROR: Malformed encrypted message frame\n\n"); @@ -1469,7 +1469,7 @@ void HAPClient::sendEncrypted(char *body, uint8_t *dataBuf, int dataLen){ if(maxFrameSize>FRAME_SIZE) // cap maxFrameSize by FRAME_SIZE (HAP restriction) maxFrameSize=FRAME_SIZE; - TempBuffer tBuf(2+maxFrameSize+16); // 2-byte AAD + encrytped data + 16-byte authentication tag + TempBuffer tBuf(2+maxFrameSize+16, "HAPClient::sendEncrypted"); // 2-byte AAD + encrytped data + 16-byte authentication tag tBuf.get()[0]=bodyLen%256; // store number of bytes in first frame that encrypts the Body (AAD bytes) tBuf.get()[1]=bodyLen/256; @@ -1696,7 +1696,7 @@ void HAPClient::saveControllers(){ return; } - TempBuffer tBuf(controllerList.size()); // create temporary buffer to hold Controller data + TempBuffer tBuf(controllerList.size(), "HAPClient::saveControllers"); // create temporary buffer to hold Controller data std::copy(controllerList.begin(),controllerList.end(),tBuf.get()); // copy data from linked list to buffer nvs_set_blob(hapNVS,"CONTROLLERS",tBuf.get(),tBuf.len()); // update data diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 8576666..c510b59 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -580,7 +580,7 @@ void Span::processSerialCommand(const char *c){ case 'Z': { HAPClient::saveControllers(); break; - TempBuffer tBuf(HAPClient::listControllers(NULL)); + TempBuffer tBuf(HAPClient::listControllers(NULL), "Span::processSerialCommand Z"); HAPClient::listControllers(tBuf.get()); Serial.printf("SIZE = %d\n",tBuf.len()); HAPClient::hexPrintRow(tBuf.get(),tBuf.len()); @@ -628,7 +628,7 @@ void Span::processSerialCommand(const char *c){ case 'd': { - TempBuffer qBuf(sprintfAttributes(NULL)+1); + TempBuffer qBuf(sprintfAttributes(NULL)+1, "Span::processSerialCommand d"); sprintfAttributes(qBuf.get()); LOG0("\n*** Attributes Database: size=%d configuration=%d ***\n\n",qBuf.len()-1,hapConfig.configNumber); @@ -854,6 +854,7 @@ void Span::processSerialCommand(const char *c){ case 'm': { LOG0("Free Heap=%d bytes (low=%d)\n",heap_caps_get_free_size(MALLOC_CAP_DEFAULT),heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT)); + LOG0("Max used TempBuffer size=%d bytes from %s\n", TempBufferBase::getMaxUsedTempBufferSize(), TempBufferBase::getNameOfBufferWithLargestBufferSize()); } break; @@ -1023,7 +1024,7 @@ void Span::processSerialCommand(const char *c){ LOG0("\n*** Pairing Data used for Cloning another Device\n\n"); size_t olen; - TempBuffer tBuf(256); + TempBuffer tBuf(256, "Span::processSerialCommand P"); mbedtls_base64_encode((uint8_t *)tBuf.get(),256,&olen,(uint8_t *)&HAPClient::accessory,sizeof(struct Accessory)); LOG0("Accessory data: %s\n",tBuf.get()); for(const auto &cont : HAPClient::controllerList){ @@ -1037,7 +1038,7 @@ void Span::processSerialCommand(const char *c){ case 'C': { LOG0("\n*** Clone Pairing Data from another Device\n\n"); - TempBuffer tBuf(200); + TempBuffer tBuf(200, "Span::processSerialCommand C"); size_t olen; tBuf.get()[0]='\0'; @@ -1591,7 +1592,7 @@ int Span::sprintfAttributes(char **ids, int numIDs, int flags, char *cBuf){ boolean Span::updateDatabase(boolean updateMDNS){ uint8_t tHash[48]; - TempBuffer tBuf(sprintfAttributes(NULL,GET_META|GET_PERMS|GET_TYPE|GET_DESC)+1); + TempBuffer tBuf(sprintfAttributes(NULL,GET_META|GET_PERMS|GET_TYPE|GET_DESC)+1, "Span::updateDatabase"); sprintfAttributes(tBuf.get(),GET_META|GET_PERMS|GET_TYPE|GET_DESC); mbedtls_sha512_ret((uint8_t *)tBuf.get(),tBuf.len(),tHash,1); // create SHA-384 hash of JSON (can be any hash - just looking for a unique key) diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 2fb0f97..7522a56 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -730,7 +730,7 @@ class SpanCharacteristic{ size_t olen; mbedtls_base64_encode(NULL,0,&olen,data,len); // get length of string buffer needed (mbedtls includes the trailing null in this size) - TempBuffer tBuf(olen); // create temporary string buffer, with room for trailing null + TempBuffer tBuf(olen, "SpanCharacteristic::setData"); // create temporary string buffer, with room for trailing null mbedtls_base64_encode((uint8_t*)tBuf.get(),olen,&olen,data,len ); // encode data into string buf setString(tBuf.get()); // call setString to continue processing as if characteristic was a string } diff --git a/src/Network.cpp b/src/Network.cpp index 5d69ad2..004eba0 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -184,7 +184,7 @@ void Network::apConfigure(){ continue; } - TempBuffer httpBuf(messageSize+1); // leave room for null character added below + TempBuffer httpBuf(messageSize+1, "Network::apConfigure"); // leave room for null character added below int nBytes=client.read(httpBuf.get(),messageSize); // read all available bytes up to maximum allowed+1 diff --git a/src/Utils.cpp b/src/Utils.cpp index 7af158e..645caef 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -279,3 +279,23 @@ void PushButton::reset(){ ////////////////////////////////////// touch_value_t PushButton::threshold=0; + +////////////////////////////////////// + +int TempBufferBase::maxUsedTempBufferSize = 0; + +////////////////////////////////////// + +int TempBufferBase::getMaxUsedTempBufferSize() { + return maxUsedTempBufferSize; +} + +////////////////////////////////////// + +const char* TempBufferBase::nameOfBufferWithLargestBufferSize = ""; + +////////////////////////////////////// + +const char* TempBufferBase::getNameOfBufferWithLargestBufferSize() { + return nameOfBufferWithLargestBufferSize; +} \ No newline at end of file diff --git a/src/Utils.h b/src/Utils.h index 4272494..20ca8bf 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -41,19 +41,32 @@ String mask(char *c, int n); // simply utility that creates a String fr // Creates a temporary buffer that is freed after // going out of scope +class TempBufferBase +{ + protected: + static int maxUsedTempBufferSize; + static const char* nameOfBufferWithLargestBufferSize; + public: + static int getMaxUsedTempBufferSize(); + static const char* getNameOfBufferWithLargestBufferSize(); +}; + template -class TempBuffer { +class TempBuffer : TempBufferBase { private: - bufType *buf; int nBytes; int nElements; public: - - TempBuffer(int _nElements) : nElements(_nElements) { + TempBuffer(int _nElements, const char* nameOfBuffer) : nElements(_nElements) { nBytes=nElements*sizeof(bufType); + if (maxUsedTempBufferSize < nBytes) + { + maxUsedTempBufferSize = nBytes; + nameOfBufferWithLargestBufferSize = nameOfBuffer; + } buf=(bufType *)malloc(nBytes); if(buf==NULL){ Serial.print("\n\n*** FATAL ERROR: Requested allocation of ");