diff options
author | Sven Hassler <sven_hassler@mentor.com> | 2015-09-10 13:11:34 +0200 |
---|---|---|
committer | Alexander Wenzel <Alexander.AW.Wenzel@bmw.de> | 2015-10-07 10:40:49 +0200 |
commit | d781908d0006d3a8b681d95540558d3a231d9c79 (patch) | |
tree | 58d6e569563bde74ba122798a3c7f34578ebaa80 | |
parent | 1236195e9b93aeb6bfa625956fa027f96003756d (diff) | |
download | DLT-daemon-d781908d0006d3a8b681d95540558d3a231d9c79.tar.gz |
Added programme to test repeated calls of dlt_init and dlt_free
Signed-off-by: Alexander Wenzel <Alexander.AW.Wenzel@bmw.de>
-rw-r--r-- | src/tests/CMakeLists.txt | 7 | ||||
-rw-r--r-- | src/tests/dlt-test-init-free.c | 152 |
2 files changed, 158 insertions, 1 deletions
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index d4ac21c..7f9dc52 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -60,7 +60,12 @@ add_executable(dlt-test-fork-handler ${dlt_test_fork_handler_SRCS}) target_link_libraries(dlt-test-fork-handler dlt) set_target_properties(dlt-test-fork-handler PROPERTIES LINKER_LANGUAGE C) -install(TARGETS dlt-test-multi-process dlt-test-multi-process-client dlt-test-user dlt-test-client dlt-test-stress-user dlt-test-stress-client dlt-test-stress dlt-test-filetransfer dlt-test-fork-handler +set(dlt_test_init_free_SRCS dlt-test-init-free.c) +add_executable(dlt-test-init-free ${dlt_test_init_free_SRCS}) +target_link_libraries(dlt-test-init-free dlt) +set_target_properties(dlt-test-init-free PROPERTIES LINKER_LANGUAGE C) + +install(TARGETS dlt-test-multi-process dlt-test-multi-process-client dlt-test-user dlt-test-client dlt-test-stress-user dlt-test-stress-client dlt-test-stress dlt-test-filetransfer dlt-test-fork-handler dlt-test-init-free RUNTIME DESTINATION bin COMPONENT base) diff --git a/src/tests/dlt-test-init-free.c b/src/tests/dlt-test-init-free.c new file mode 100644 index 0000000..d299938 --- /dev/null +++ b/src/tests/dlt-test-init-free.c @@ -0,0 +1,152 @@ +/* +* @licence app begin@ +* SPDX license identifier: MPL-2.0 +* +* Copyright (C) 2011-2015, BMW AG +* +* This file is part of GENIVI Project DLT - Diagnostic Log and Trace. +* +* This Source Code Form is subject to the terms of the +* Mozilla Public License (MPL), v. 2.0. +* If a copy of the MPL was not distributed with this file, +* You can obtain one at http://mozilla.org/MPL/2.0/. +* +* For further information see http://www.genivi.org/. +* @licence end@ +*/ + +/*! +* \author Sven Hassler <sven_hassler@mentor.com> +* +* \copyright Copyright © 2011-2015 BMW AG. \n +* License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/. +* +* \file dlt-test-init-free.c +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "dlt_common.h" +#include "dlt_user.h" + +char* exec(const char* cmd); +void printMemoryUsage(); +char* occupyMemory(uint size); +void do_example_test(); +void do_dlt_test(); + +int num_repetitions; + +int main(int argc, char **argv) +{ + if (argc > 1) + num_repetitions = strtol(argv[1], 0, 10); + else + num_repetitions = 1000; + + printf("Will do %d repetitions.\n", num_repetitions); + + //do_example_test(); + do_dlt_test(); + + printf("Done.\n"); + + return 0; +} + +// Should increase and then decrease memory amount. +void do_example_test() +{ + const int immediatelyFree = 0; + + int numBufs = 1024; + int sizePerBuf = 1024 * 1024; // 1MB + + char** bufs = (char**) malloc(numBufs * sizeof(char*)); + + for (int i = 0; i < numBufs; i++) + { + bufs[i] = occupyMemory(sizePerBuf); + + printf("after alloc: "); + printMemoryUsage(); + + if (immediatelyFree) + { + free(bufs[i]); + + printf("after free: "); + printMemoryUsage(); + } + } + + printf("deleting memory:\n"); + + if (!immediatelyFree) + for (int i = 0; i < numBufs; i++) + //for (int i = numBufs - 1; i >= 0; i--) // other way round works, too + { + free(bufs[i]); + + printf("after free: "); + printMemoryUsage(); + } + + free(bufs); +} + +// Should give stable amount of memory across all iterations. +void do_dlt_test() +{ + for (int i = 0; i < num_repetitions; i++) + { + dlt_init(); + dlt_free(); + + printf("Iteration %d) - currently used memory amount: ", i); + printMemoryUsage(); + } +} + +char* exec(const char* cmd) +{ + FILE* pipe = popen(cmd, "r"); + if (!pipe) + return "ERROR"; + + char* buffer = (char*)malloc(128); + while (!feof(pipe)) + { + fgets(buffer, 128, pipe); + } + pclose(pipe); + + return buffer; +} + +void printMemoryUsage() +{ + char command[128] = "pmap "; + + char buf[128]; + snprintf(buf, sizeof(command), "%d", getpid()); + strcat(command, buf); + strcat(command, " | grep total"); + + char* result = exec(command); + + printf("%s", result); +} + +char* occupyMemory(uint size) +{ + char* buf = (char*) malloc(size * sizeof(char)); + for (int i = 0; i < 1; i++) + { + buf[i] = 1; + } + + return buf; +} |