leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 2 // Copyright 2016-2018 ARM Ltd.
leothedragon 0:8f0bb79ddd48 3 //
leothedragon 0:8f0bb79ddd48 4 // SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 5 //
leothedragon 0:8f0bb79ddd48 6 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:8f0bb79ddd48 7 // you may not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 8 // You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 9 //
leothedragon 0:8f0bb79ddd48 10 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 11 //
leothedragon 0:8f0bb79ddd48 12 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 13 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:8f0bb79ddd48 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 15 // See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 16 // limitations under the License.
leothedragon 0:8f0bb79ddd48 17 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 #ifdef TARGET_LIKE_MBED
leothedragon 0:8f0bb79ddd48 20
leothedragon 0:8f0bb79ddd48 21 #ifdef MBED_HEAP_STATS_ENABLED
leothedragon 0:8f0bb79ddd48 22 // used by print_heap_stats only
leothedragon 0:8f0bb79ddd48 23 #include "mbed_stats.h"
leothedragon 0:8f0bb79ddd48 24 #define __STDC_FORMAT_MACROS
leothedragon 0:8f0bb79ddd48 25 #include <inttypes.h>
leothedragon 0:8f0bb79ddd48 26 #include "mbed-client/m2mbase.h"
leothedragon 0:8f0bb79ddd48 27 #endif
leothedragon 0:8f0bb79ddd48 28
leothedragon 0:8f0bb79ddd48 29 // fixup the compilation on AMRCC for PRIu32
leothedragon 0:8f0bb79ddd48 30 #define __STDC_FORMAT_MACROS
leothedragon 0:8f0bb79ddd48 31 #include <inttypes.h>
leothedragon 0:8f0bb79ddd48 32
leothedragon 0:8f0bb79ddd48 33 #include "memory_tests.h"
leothedragon 0:8f0bb79ddd48 34
leothedragon 0:8f0bb79ddd48 35 #include "mbed-client/m2mblockmessage.h"
leothedragon 0:8f0bb79ddd48 36 #include "mbed-client/m2mdevice.h"
leothedragon 0:8f0bb79ddd48 37 #include "mbed-client/m2mfirmware.h"
leothedragon 0:8f0bb79ddd48 38 #include "mbed-client/m2minterfacefactory.h"
leothedragon 0:8f0bb79ddd48 39 #include "mbed-client/m2mobject.h"
leothedragon 0:8f0bb79ddd48 40 #include "mbed-client/m2mserver.h"
leothedragon 0:8f0bb79ddd48 41 #include "mbed-client/m2msecurity.h"
leothedragon 0:8f0bb79ddd48 42 #include "source/include/m2mreporthandler.h"
leothedragon 0:8f0bb79ddd48 43
leothedragon 0:8f0bb79ddd48 44 #include "mbed.h"
leothedragon 0:8f0bb79ddd48 45 #include "mbed_stats.h"
leothedragon 0:8f0bb79ddd48 46
leothedragon 0:8f0bb79ddd48 47 #include <assert.h>
leothedragon 0:8f0bb79ddd48 48
leothedragon 0:8f0bb79ddd48 49 void heap_stats()
leothedragon 0:8f0bb79ddd48 50 {
leothedragon 0:8f0bb79ddd48 51 #ifdef MBED_HEAP_STATS_ENABLED
leothedragon 0:8f0bb79ddd48 52 mbed_stats_heap_t stats;
leothedragon 0:8f0bb79ddd48 53 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 54 printf("**** current_size: %" PRIu32 "\n", stats.current_size);
leothedragon 0:8f0bb79ddd48 55 printf("**** max_size : %" PRIu32 "\n", stats.max_size);
leothedragon 0:8f0bb79ddd48 56 #endif // MBED_HEAP_STATS_ENABLED
leothedragon 0:8f0bb79ddd48 57 }
leothedragon 0:8f0bb79ddd48 58
leothedragon 0:8f0bb79ddd48 59 void m2mobject_test_set(M2MObjectList& object_list)
leothedragon 0:8f0bb79ddd48 60 {
leothedragon 0:8f0bb79ddd48 61 #ifdef MBED_HEAP_STATS_ENABLED
leothedragon 0:8f0bb79ddd48 62 printf("*************************************\n");
leothedragon 0:8f0bb79ddd48 63
leothedragon 0:8f0bb79ddd48 64 mbed_stats_heap_t stats;
leothedragon 0:8f0bb79ddd48 65 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 66
leothedragon 0:8f0bb79ddd48 67 uint32_t initial = stats.current_size;
leothedragon 0:8f0bb79ddd48 68
leothedragon 0:8f0bb79ddd48 69 const int object_count = 1;
leothedragon 0:8f0bb79ddd48 70 const int object_id_range_start = 90;
leothedragon 0:8f0bb79ddd48 71 const int object_id_range_end = object_id_range_start + object_count;
leothedragon 0:8f0bb79ddd48 72
leothedragon 0:8f0bb79ddd48 73 const int object_instance_count = 5;
leothedragon 0:8f0bb79ddd48 74 const int resource_count = 5;
leothedragon 0:8f0bb79ddd48 75
leothedragon 0:8f0bb79ddd48 76 int total_object_count = 0;
leothedragon 0:8f0bb79ddd48 77 int total_object_instance_count = 0;
leothedragon 0:8f0bb79ddd48 78 int total_resource_count = 0;
leothedragon 0:8f0bb79ddd48 79
leothedragon 0:8f0bb79ddd48 80 for (int object_id = object_id_range_start; object_id < object_id_range_end; object_id++) {
leothedragon 0:8f0bb79ddd48 81
leothedragon 0:8f0bb79ddd48 82 char buff[64];
leothedragon 0:8f0bb79ddd48 83 sprintf(buff, "%d", object_id);
leothedragon 0:8f0bb79ddd48 84 M2MObject *obj = M2MInterfaceFactory::create_object(buff);
leothedragon 0:8f0bb79ddd48 85
leothedragon 0:8f0bb79ddd48 86 for (int object_instance_id = 0; object_instance_id < object_instance_count; object_instance_id++) {
leothedragon 0:8f0bb79ddd48 87
leothedragon 0:8f0bb79ddd48 88 M2MObjectInstance* obj_inst = obj->create_object_instance(object_instance_id);
leothedragon 0:8f0bb79ddd48 89
leothedragon 0:8f0bb79ddd48 90 assert(obj_inst != NULL);
leothedragon 0:8f0bb79ddd48 91 total_object_instance_count++;
leothedragon 0:8f0bb79ddd48 92 for (int resource_id = 0; resource_id < resource_count; resource_id++) {
leothedragon 0:8f0bb79ddd48 93 String res_name;
leothedragon 0:8f0bb79ddd48 94 String res_type;
leothedragon 0:8f0bb79ddd48 95
leothedragon 0:8f0bb79ddd48 96 res_name.append_int(resource_id);
leothedragon 0:8f0bb79ddd48 97 res_type.append_int(resource_id);
leothedragon 0:8f0bb79ddd48 98
leothedragon 0:8f0bb79ddd48 99 M2MResource* resource = obj_inst->create_dynamic_resource(res_name, res_type,
leothedragon 0:8f0bb79ddd48 100 M2MResourceInstance::INTEGER, true);
leothedragon 0:8f0bb79ddd48 101
leothedragon 0:8f0bb79ddd48 102 assert(resource != NULL);
leothedragon 0:8f0bb79ddd48 103
leothedragon 0:8f0bb79ddd48 104 resource->set_operation(M2MBase::GET_ALLOWED);
leothedragon 0:8f0bb79ddd48 105 resource->set_value(7);
leothedragon 0:8f0bb79ddd48 106
leothedragon 0:8f0bb79ddd48 107 total_resource_count++;
leothedragon 0:8f0bb79ddd48 108 }
leothedragon 0:8f0bb79ddd48 109 }
leothedragon 0:8f0bb79ddd48 110
leothedragon 0:8f0bb79ddd48 111 object_list.push_back(obj);
leothedragon 0:8f0bb79ddd48 112 total_object_count++;
leothedragon 0:8f0bb79ddd48 113 }
leothedragon 0:8f0bb79ddd48 114
leothedragon 0:8f0bb79ddd48 115 printf("objects : %d\n", total_object_count);
leothedragon 0:8f0bb79ddd48 116 printf("obj instances : %d\n", total_object_instance_count);
leothedragon 0:8f0bb79ddd48 117 printf("resources : %d\n", total_resource_count);
leothedragon 0:8f0bb79ddd48 118
leothedragon 0:8f0bb79ddd48 119 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 120 printf("heap used : %" PRIu32 "\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 121
leothedragon 0:8f0bb79ddd48 122 printf("*************************************\n");
leothedragon 0:8f0bb79ddd48 123 #endif // MBED_HEAP_STATS_ENABLED
leothedragon 0:8f0bb79ddd48 124 }
leothedragon 0:8f0bb79ddd48 125
leothedragon 0:8f0bb79ddd48 126 // Note: the mbed-os needs to be compiled with MBED_HEAP_STATS_ENABLED to get
leothedragon 0:8f0bb79ddd48 127 // functional heap stats, or the mbed_stats_heap_get() will return just zeroes.
leothedragon 0:8f0bb79ddd48 128 void m2mobject_stats()
leothedragon 0:8f0bb79ddd48 129 {
leothedragon 0:8f0bb79ddd48 130 #ifdef MBED_HEAP_STATS_ENABLED
leothedragon 0:8f0bb79ddd48 131 printf("\n*** M2M object sizes in bytes ***\n");
leothedragon 0:8f0bb79ddd48 132 printf("M2MBase: %" PRIu32 "\n", sizeof(M2MBase));
leothedragon 0:8f0bb79ddd48 133 printf("M2MObject: %" PRIu32 "\n", sizeof(M2MObject));
leothedragon 0:8f0bb79ddd48 134 printf("M2MObjectInstance: %" PRIu32 "\n", sizeof(M2MObjectInstance));
leothedragon 0:8f0bb79ddd48 135 printf("M2MResource: %" PRIu32 "\n", sizeof(M2MResource));
leothedragon 0:8f0bb79ddd48 136 printf("M2MResourceInstance: %" PRIu32 "\n", sizeof(M2MResourceInstance));
leothedragon 0:8f0bb79ddd48 137 printf("M2MDevice: %" PRIu32 "\n", sizeof(M2MDevice));
leothedragon 0:8f0bb79ddd48 138 printf("M2MFirmware: %" PRIu32 "\n", sizeof(M2MFirmware));
leothedragon 0:8f0bb79ddd48 139 printf("M2MServer: %" PRIu32 "\n", sizeof(M2MServer));
leothedragon 0:8f0bb79ddd48 140 printf("M2MSecurity: %" PRIu32 "\n", sizeof(M2MSecurity));
leothedragon 0:8f0bb79ddd48 141 printf("M2MBlockMessage: %" PRIu32 "\n", sizeof(M2MBlockMessage));
leothedragon 0:8f0bb79ddd48 142 printf("M2MReportHandler: %" PRIu32 "\n", sizeof(M2MReportHandler));
leothedragon 0:8f0bb79ddd48 143 printf("*************************************\n\n");
leothedragon 0:8f0bb79ddd48 144
leothedragon 0:8f0bb79ddd48 145 mbed_stats_heap_t stats;
leothedragon 0:8f0bb79ddd48 146 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 147
leothedragon 0:8f0bb79ddd48 148 printf("*** M2M heap stats in bytes***\n");
leothedragon 0:8f0bb79ddd48 149 uint32_t initial = stats.current_size;
leothedragon 0:8f0bb79ddd48 150
leothedragon 0:8f0bb79ddd48 151 // M2MDevice
leothedragon 0:8f0bb79ddd48 152 M2MDevice *device_object = M2MInterfaceFactory::create_device();
leothedragon 0:8f0bb79ddd48 153 assert(device_object);
leothedragon 0:8f0bb79ddd48 154 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 155 printf("M2MDevice heap size: %" PRIu32 "\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 156 M2MDevice::delete_instance();
leothedragon 0:8f0bb79ddd48 157 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 158 if (initial != stats.current_size) {
leothedragon 0:8f0bb79ddd48 159 printf("M2MDevice leaked: %" PRIu32 "bytes\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 160 }
leothedragon 0:8f0bb79ddd48 161
leothedragon 0:8f0bb79ddd48 162 // M2MServer
leothedragon 0:8f0bb79ddd48 163 initial = stats.current_size;
leothedragon 0:8f0bb79ddd48 164 M2MServer *server = M2MInterfaceFactory::create_server();
leothedragon 0:8f0bb79ddd48 165 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 166 printf("M2MServer heap size: %" PRIu32 "\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 167 delete server;
leothedragon 0:8f0bb79ddd48 168 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 169 if (initial != stats.current_size) {
leothedragon 0:8f0bb79ddd48 170 printf("M2MServer leaked: %" PRIu32 "bytes\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 171 }
leothedragon 0:8f0bb79ddd48 172
leothedragon 0:8f0bb79ddd48 173 // M2MSecurity
leothedragon 0:8f0bb79ddd48 174 initial = stats.current_size;
leothedragon 0:8f0bb79ddd48 175 M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
leothedragon 0:8f0bb79ddd48 176 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 177 printf("M2MSecurity heap size: %" PRIu32 "\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 178 M2MSecurity::delete_instance();
leothedragon 0:8f0bb79ddd48 179 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 180 if (initial != stats.current_size) {
leothedragon 0:8f0bb79ddd48 181 printf("M2MSecurity leaked: %" PRIu32 "bytes\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 182 }
leothedragon 0:8f0bb79ddd48 183
leothedragon 0:8f0bb79ddd48 184 // M2MFirmware
leothedragon 0:8f0bb79ddd48 185 initial = stats.current_size;
leothedragon 0:8f0bb79ddd48 186 M2MFirmware *firmware = M2MInterfaceFactory::create_firmware();
leothedragon 0:8f0bb79ddd48 187 assert(firmware);
leothedragon 0:8f0bb79ddd48 188 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 189 printf("M2MFirmware heap size: %" PRIu32 "\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 190 M2MFirmware::delete_instance();
leothedragon 0:8f0bb79ddd48 191 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 192 if (initial != stats.current_size) {
leothedragon 0:8f0bb79ddd48 193 printf("M2MFirmware leaked: %" PRIu32 "bytes\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 194 }
leothedragon 0:8f0bb79ddd48 195
leothedragon 0:8f0bb79ddd48 196 // Basic object creation
leothedragon 0:8f0bb79ddd48 197 initial = stats.current_size;
leothedragon 0:8f0bb79ddd48 198 uint32_t before_object = initial;
leothedragon 0:8f0bb79ddd48 199 M2MObject *obj = M2MInterfaceFactory::create_object("1");
leothedragon 0:8f0bb79ddd48 200 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 201 printf("M2MObject heap size: %" PRIu32 "\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 202 initial = stats.current_size;
leothedragon 0:8f0bb79ddd48 203
leothedragon 0:8f0bb79ddd48 204 M2MObjectInstance* obj_inst = obj->create_object_instance();
leothedragon 0:8f0bb79ddd48 205 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 206 printf("M2MObjectInstance heap size: %" PRIu32 "\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 207
leothedragon 0:8f0bb79ddd48 208 initial = stats.current_size;
leothedragon 0:8f0bb79ddd48 209 M2MResource* res = obj_inst->create_dynamic_resource("1", "1", M2MResourceInstance::STRING, false);
leothedragon 0:8f0bb79ddd48 210 assert(res);
leothedragon 0:8f0bb79ddd48 211 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 212 printf("M2MResource heap size: %" PRIu32 "\n", stats.current_size - initial);
leothedragon 0:8f0bb79ddd48 213
leothedragon 0:8f0bb79ddd48 214 delete obj;
leothedragon 0:8f0bb79ddd48 215 mbed_stats_heap_get(&stats);
leothedragon 0:8f0bb79ddd48 216 if (before_object != stats.current_size) {
leothedragon 0:8f0bb79ddd48 217 printf("Resource leaked: %" PRIu32 "bytes\n", stats.current_size - before_object);
leothedragon 0:8f0bb79ddd48 218 }
leothedragon 0:8f0bb79ddd48 219 printf("*************************************\n\n");
leothedragon 0:8f0bb79ddd48 220 #endif // MBED_HEAP_STATS_ENABLED
leothedragon 0:8f0bb79ddd48 221 }
leothedragon 0:8f0bb79ddd48 222 #endif // TARGET_LIKE_MBED