automotive-dlt
dlt-test-init-free.c
Go to the documentation of this file.
1 /*
2 * @licence app begin@
3 * SPDX license identifier: MPL-2.0
4 *
5 * Copyright (C) 2011-2015, BMW AG
6 *
7 * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8 *
9 * This Source Code Form is subject to the terms of the
10 * Mozilla Public License (MPL), v. 2.0.
11 * If a copy of the MPL was not distributed with this file,
12 * You can obtain one at http://mozilla.org/MPL/2.0/.
13 *
14 * For further information see http://www.genivi.org/.
15 * @licence end@
16 */
17 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdbool.h>
31 
32 #include "dlt_common.h"
33 #include "dlt_user.h"
34 
35 void exec(const char* cmd, char *buffer, size_t length);
36 void printMemoryUsage();
37 char* occupyMemory(uint size);
38 void do_example_test();
39 void do_dlt_test();
40 
42 
43 int main(int argc, char **argv)
44 {
45  if (argc > 1)
46  num_repetitions = strtol(argv[1], 0, 10);
47  else
48  num_repetitions = 1000;
49 
50  printf("Will do %d repetitions.\n", num_repetitions);
51 
52  //do_example_test();
53  do_dlt_test();
54 
55  printf("Done.\n");
56 
57  return 0;
58 }
59 
60 // Should increase and then decrease memory amount.
62 {
63  const int immediatelyFree = 0;
64 
65  int numBufs = 1024;
66  int sizePerBuf = 1024 * 1024; // 1MB
67 
68  char** bufs = (char**) malloc(numBufs * sizeof(char*));
69 
70  for (int i = 0; i < numBufs; i++)
71  {
72  bufs[i] = occupyMemory(sizePerBuf);
73 
74  printf("after alloc: ");
76 
77  if (immediatelyFree)
78  {
79  free(bufs[i]);
80 
81  printf("after free: ");
83  }
84  }
85 
86  printf("deleting memory:\n");
87 
88  if (!immediatelyFree)
89  for (int i = 0; i < numBufs; i++)
90  //for (int i = numBufs - 1; i >= 0; i--) // other way round works, too
91  {
92  free(bufs[i]);
93 
94  printf("after free: ");
96  }
97 
98  free(bufs);
99 }
100 
101 // Should give stable amount of memory across all iterations.
103 {
104  for (int i = 0; i < num_repetitions; i++)
105  {
106  dlt_init();
107  dlt_free();
108 
109  printf("Iteration %d) - currently used memory amount: ", i);
111  }
112 }
113 
114 void exec(const char* cmd, char *buffer, size_t length)
115 {
116  FILE* pipe = NULL;
117  strncpy(buffer, "ERROR", length);
118 
119  if ( (pipe = popen(cmd, "r")) == NULL )
120  return;
121 
122  while (fgets(buffer, length, pipe) != NULL);
123 
124  if(pipe != NULL)
125  pclose(pipe);
126 }
127 
129 {
130  char result[128] = { 0 };
131  char command[128] = { 0 };
132 
133  snprintf(command, sizeof(command), "pmap %d | grep total", getpid());
134 
135  exec(command, result, sizeof(result));
136 
137  printf("%s", result);
138 }
139 
140 char* occupyMemory(uint size)
141 {
142  char* buf = (char*) malloc(size * sizeof(char));
143  for (int i = 0; i < 1; i++)
144  {
145  buf[i] = 1;
146  }
147 
148  return buf;
149 }
int main(int argc, char **argv)
void printMemoryUsage()
void exec(const char *cmd, char *buffer, size_t length)
DltReturnValue dlt_free(void)
Definition: dlt_user.c:676
int num_repetitions
void do_dlt_test()
unsigned char buffer[BUFFER_SIZE]
Buffer for dlt file transfer. The size is defined by BUFFER_SIZE.
DltReturnValue dlt_init(void)
Definition: dlt_user.c:188
char * occupyMemory(uint size)
void do_example_test()
#define NULL
Definition: dlt_common.h:232