1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/*
* 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/.
*/
/*!
* \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 <stdbool.h>
#include "dlt_common.h"
#include "dlt_user.h"
void exec(const char *cmd, char *buffer, size_t length);
void printMemoryUsage();
char *occupyMemory(int 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();
}
}
void exec(const char *cmd, char *buffer, size_t length)
{
FILE *pipe = NULL;
strncpy(buffer, "ERROR", length);
if ((pipe = popen(cmd, "r")) == NULL)
return;
while (fgets(buffer, length, pipe) != NULL);
if (pipe != NULL)
pclose(pipe);
}
void printMemoryUsage()
{
char result[128] = { 0 };
char command[128] = { 0 };
snprintf(command, sizeof(command), "pmap %d | grep total", getpid());
exec(command, result, sizeof(result));
printf("%s", result);
}
char *occupyMemory(int size)
{
char *buf = (char *)malloc(size * sizeof(char));
for (int i = 0; i < 1; i++)
buf[i] = 1;
return buf;
}
|