11 #define NUM_MALLOC (10000)
12 #define SIZE_MALLOC (10000)
32 gettimeofday (&tv
, &tz
);
33 return ((tv
.tv_usec
/ 1000000.0) + (1.0 * tv
.tv_sec
));
48 update (int print
, const char *n
, double s
, double e
, minmax
* data
)
57 data
->sumquad
+= d
* d
;
58 mean
= data
->sum
/ data
->n
;
59 sigma
= sqrt ((data
->sumquad
- ((data
->sum
* data
->sum
) / data
->n
)) /
69 mean
= data
->sum
/ data
->n
;
70 sigma
= sqrt ((data
->sumquad
- ((data
->sum
* data
->sum
) / data
->n
)) /
74 printf ("%s min=%.9f, max=%.9f, mean=%.9f, sigma=%.9f\n",
75 n
? n
: data
->name
, data
->min
, data
->max
, mean
, sigma
);
91 double s
, first
, last_h
;
93 minmax maldata
, raldata
, freedata
;
98 m
= (void **) malloc (NUM_MALLOC
* sizeof (void *));
99 for (i
= 0; i
< NUM_MALLOC
; i
++) {
102 first
= last_h
= getcurtime ();
103 for (i
= 0; i
< NUM_MALLOC
; i
++) {
104 t
= (size_t) (1 + drand48 () * SIZE_MALLOC
);
105 m
[i
] = calloc (t
, 1);
106 if (((unsigned long) m
[i
] & (sizeof(void *) * 2 - 1)) != 0) {
107 fprintf(stderr
,"Alignment error %p\n", m
[i
]);
110 memset (m
[i
], -1, t
);
113 for (j
= 0; j
< 1000; j
++) {
114 for (i
= 0; i
< NUM_MALLOC
; i
++) {
116 t
= (size_t) (1 + drand48 () * SIZE_MALLOC
);
118 m
[i
] = realloc (m
[i
], t
);
119 update (0, "realloc", s
, getcurtime (), &raldata
);
120 if (((unsigned long) m
[i
] & (sizeof(void *) * 2 - 1)) != 0) {
121 fprintf(stderr
,"Alignment error %p\n", m
[i
]);
124 memset (m
[i
], -1, t
);
130 update (0, "free ", s
, getcurtime (), &freedata
);
132 t
= (size_t) (1 + drand48 () * SIZE_MALLOC
);
135 update (0, "malloc ", s
, getcurtime (), &maldata
);
136 if (((unsigned long) m
[i
] & (sizeof(void *) * 2 - 1)) != 0) {
137 fprintf(stderr
,"Alignment error %p\n", m
[i
]);
140 memset (m
[i
], -1, t
);
143 if (mallinfo().uordblks
> max_size
) {
144 max_size
= mallinfo().uordblks
;
148 if ((s
- last_h
) > 10) {
150 printf ("Count = %d %f, max memory %d\n",
151 n
* NUM_MALLOC
, last_h
- first
, mallinfo().uordblks
);
152 update (1, NULL
, 0.0, getcurtime (), &maldata
);
153 update (1, NULL
, 0.0, getcurtime (), &raldata
);
154 update (1, NULL
, 0.0, getcurtime (), &freedata
);
157 for (i
= 0; i
< NUM_MALLOC
; i
++) {
161 printf ("Total used memory = %d, max memory %d\n",
162 mallinfo().uordblks
, max_size
);