Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers memory_tests.cpp Source File

memory_tests.cpp

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2018 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #ifdef TARGET_LIKE_MBED
00020 
00021 #ifdef MBED_HEAP_STATS_ENABLED
00022 // used by print_heap_stats only
00023 #include "mbed_stats.h"
00024 #define __STDC_FORMAT_MACROS
00025 #include <inttypes.h>
00026 #include "mbed-client/m2mbase.h"
00027 #endif
00028 
00029 // fixup the compilation on AMRCC for PRIu32
00030 #define __STDC_FORMAT_MACROS
00031 #include <inttypes.h>
00032 
00033 #include "memory_tests.h"
00034 
00035 #include "mbed-client/m2mblockmessage.h"
00036 #include "mbed-client/m2mdevice.h"
00037 #include "mbed-client/m2mfirmware.h"
00038 #include "mbed-client/m2minterfacefactory.h"
00039 #include "mbed-client/m2mobject.h"
00040 #include "mbed-client/m2mserver.h"
00041 #include "mbed-client/m2msecurity.h"
00042 #include "source/include/m2mreporthandler.h"
00043 
00044 #include "mbed.h"
00045 #include "mbed_stats.h"
00046 
00047 #include <assert.h>
00048 
00049 void heap_stats()
00050 {
00051 #ifdef MBED_HEAP_STATS_ENABLED
00052     mbed_stats_heap_t stats;
00053     mbed_stats_heap_get(&stats);
00054     printf("**** current_size: %" PRIu32 "\n", stats.current_size);
00055     printf("**** max_size    : %" PRIu32 "\n", stats.max_size);
00056 #endif // MBED_HEAP_STATS_ENABLED
00057 }
00058 
00059 void m2mobject_test_set(M2MObjectList& object_list)
00060 {
00061 #ifdef MBED_HEAP_STATS_ENABLED
00062     printf("*************************************\n");
00063 
00064     mbed_stats_heap_t stats;
00065     mbed_stats_heap_get(&stats);
00066 
00067     uint32_t initial = stats.current_size;
00068 
00069     const int object_count = 1;
00070     const int object_id_range_start = 90;
00071     const int object_id_range_end = object_id_range_start + object_count;
00072 
00073     const int object_instance_count = 5;
00074     const int resource_count = 5;
00075 
00076     int total_object_count = 0;
00077     int total_object_instance_count = 0;
00078     int total_resource_count = 0;
00079 
00080     for (int object_id = object_id_range_start; object_id < object_id_range_end; object_id++) {
00081 
00082         char buff[64];
00083         sprintf(buff, "%d", object_id);
00084         M2MObject *obj = M2MInterfaceFactory::create_object(buff);
00085 
00086         for (int object_instance_id = 0; object_instance_id < object_instance_count; object_instance_id++) {
00087 
00088             M2MObjectInstance* obj_inst = obj->create_object_instance(object_instance_id);
00089 
00090             assert(obj_inst != NULL);
00091             total_object_instance_count++;
00092             for (int resource_id = 0; resource_id < resource_count; resource_id++) {
00093                 String res_name;
00094                 String res_type;
00095 
00096                 res_name.append_int(resource_id);
00097                 res_type.append_int(resource_id);
00098 
00099                 M2MResource* resource = obj_inst->create_dynamic_resource(res_name, res_type,
00100                                                                         M2MResourceInstance::INTEGER, true);
00101 
00102                 assert(resource != NULL);
00103 
00104                 resource->set_operation(M2MBase::GET_ALLOWED);
00105                 resource->set_value(7);
00106 
00107                 total_resource_count++;
00108             }
00109         }
00110 
00111         object_list.push_back(obj);
00112         total_object_count++;
00113     }
00114 
00115     printf("objects       : %d\n", total_object_count);
00116     printf("obj instances : %d\n", total_object_instance_count);
00117     printf("resources     : %d\n", total_resource_count);
00118 
00119     mbed_stats_heap_get(&stats);
00120     printf("heap used     : %" PRIu32 "\n", stats.current_size - initial);
00121 
00122     printf("*************************************\n");
00123 #endif // MBED_HEAP_STATS_ENABLED
00124 }
00125 
00126 // Note: the mbed-os needs to be compiled with MBED_HEAP_STATS_ENABLED to get
00127 // functional heap stats, or the mbed_stats_heap_get() will return just zeroes.
00128 void m2mobject_stats()
00129 {
00130 #ifdef MBED_HEAP_STATS_ENABLED
00131     printf("\n*** M2M object sizes in bytes ***\n");
00132     printf("M2MBase: %" PRIu32 "\n", sizeof(M2MBase));
00133     printf("M2MObject: %" PRIu32 "\n", sizeof(M2MObject));
00134     printf("M2MObjectInstance: %" PRIu32 "\n", sizeof(M2MObjectInstance));
00135     printf("M2MResource: %" PRIu32 "\n", sizeof(M2MResource));
00136     printf("M2MResourceInstance: %" PRIu32 "\n", sizeof(M2MResourceInstance));
00137     printf("M2MDevice: %" PRIu32 "\n", sizeof(M2MDevice));
00138     printf("M2MFirmware: %" PRIu32 "\n", sizeof(M2MFirmware));
00139     printf("M2MServer: %" PRIu32 "\n", sizeof(M2MServer));
00140     printf("M2MSecurity: %" PRIu32 "\n", sizeof(M2MSecurity));
00141     printf("M2MBlockMessage: %" PRIu32 "\n", sizeof(M2MBlockMessage));
00142     printf("M2MReportHandler: %" PRIu32 "\n", sizeof(M2MReportHandler));
00143     printf("*************************************\n\n");
00144 
00145     mbed_stats_heap_t stats;
00146     mbed_stats_heap_get(&stats);
00147 
00148     printf("*** M2M heap stats in bytes***\n");
00149     uint32_t initial = stats.current_size;
00150 
00151     // M2MDevice
00152     M2MDevice *device_object = M2MInterfaceFactory::create_device();
00153     assert(device_object);
00154     mbed_stats_heap_get(&stats);
00155     printf("M2MDevice heap size: %" PRIu32 "\n", stats.current_size - initial);
00156     M2MDevice::delete_instance();
00157     mbed_stats_heap_get(&stats);
00158     if (initial != stats.current_size) {
00159         printf("M2MDevice leaked: %" PRIu32 "bytes\n", stats.current_size - initial);
00160     }
00161 
00162     // M2MServer
00163     initial = stats.current_size;
00164     M2MServer *server = M2MInterfaceFactory::create_server();
00165     mbed_stats_heap_get(&stats);
00166     printf("M2MServer heap size: %" PRIu32 "\n", stats.current_size - initial);
00167     delete server;
00168     mbed_stats_heap_get(&stats);
00169     if (initial != stats.current_size) {
00170         printf("M2MServer leaked: %" PRIu32 "bytes\n", stats.current_size - initial);
00171     }
00172 
00173     // M2MSecurity
00174     initial = stats.current_size;
00175     M2MSecurity *security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
00176     mbed_stats_heap_get(&stats);
00177     printf("M2MSecurity heap size: %" PRIu32 "\n", stats.current_size - initial);
00178     M2MSecurity::delete_instance();
00179     mbed_stats_heap_get(&stats);
00180     if (initial != stats.current_size) {
00181         printf("M2MSecurity leaked: %" PRIu32 "bytes\n", stats.current_size - initial);
00182     }
00183 
00184     // M2MFirmware
00185     initial = stats.current_size;
00186     M2MFirmware *firmware = M2MInterfaceFactory::create_firmware();
00187     assert(firmware);
00188     mbed_stats_heap_get(&stats);
00189     printf("M2MFirmware heap size: %" PRIu32 "\n", stats.current_size - initial);
00190     M2MFirmware::delete_instance();
00191     mbed_stats_heap_get(&stats);
00192     if (initial != stats.current_size) {
00193         printf("M2MFirmware leaked: %" PRIu32 "bytes\n", stats.current_size - initial);
00194     }
00195 
00196     // Basic object creation
00197     initial = stats.current_size;
00198     uint32_t before_object = initial;
00199     M2MObject *obj = M2MInterfaceFactory::create_object("1");
00200     mbed_stats_heap_get(&stats);
00201     printf("M2MObject heap size: %" PRIu32 "\n", stats.current_size - initial);
00202     initial = stats.current_size;
00203 
00204     M2MObjectInstance* obj_inst = obj->create_object_instance();
00205     mbed_stats_heap_get(&stats);
00206     printf("M2MObjectInstance heap size: %" PRIu32 "\n", stats.current_size - initial);
00207 
00208     initial = stats.current_size;
00209     M2MResource* res = obj_inst->create_dynamic_resource("1", "1", M2MResourceInstance::STRING, false);
00210     assert(res);
00211     mbed_stats_heap_get(&stats);
00212     printf("M2MResource heap size: %" PRIu32 "\n", stats.current_size - initial);
00213 
00214     delete obj;
00215     mbed_stats_heap_get(&stats);
00216     if (before_object != stats.current_size) {
00217         printf("Resource leaked: %" PRIu32 "bytes\n", stats.current_size - before_object);
00218     }
00219     printf("*************************************\n\n");
00220 #endif // MBED_HEAP_STATS_ENABLED
00221 }
00222 #endif // TARGET_LIKE_MBED