Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/vmalloc/tsafemalloc.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1999-2012 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "vmtest.h"
21
#include <pthread.h>
22
23
#ifndef N_THREAD
24
#define N_THREAD 32
25
#endif
26
#if N_THREAD > 32
27
#undef N_THREAD
28
#define N_THREAD 32
29
#endif
30
31
#define N_ALLOC 5000
32
33
typedef struct _thread_s
34
{ void* list[N_ALLOC];
35
size_t size[N_ALLOC];
36
} Tdata_t;
37
38
Tdata_t Tdata[N_THREAD];
39
40
void* allocate(void* arg)
41
{
42
int k, p;
43
size_t size, total;
44
int thread = (int)((long)arg);
45
Tdata_t *tdata = &Tdata[thread];
46
47
total = 0;
48
for(k = 0; k < N_ALLOC; ++k)
49
{ size = random() % 1024 + 1;
50
if(!(tdata->list[k] = malloc(size)) )
51
terror("Thread %d: failed to malloc(%d), total=%d", thread, size, total);
52
else
53
{ tdata->size[k] = size;
54
total += size;
55
memset(tdata->list[k], 1, size);
56
}
57
58
if(k > 10 )
59
{ p = random() % k;
60
if(tdata->list[p])
61
{ if(random()%4 == 0 )
62
{ size = 2*tdata->size[p];
63
if(!(tdata->list[p] = realloc(tdata->list[p], size)) )
64
terror("Thread %d: failed to realloc(%d), total=%d",
65
thread, size, total);
66
else
67
{ tdata->size[p] = size;
68
total += size/2;
69
memset(tdata->list[p], 255, size);
70
}
71
}
72
else
73
{ free(tdata->list[p]);
74
tdata->list[p] = 0;
75
tdata->size[p] = 0;
76
}
77
}
78
}
79
80
if(k > 0 && k%(N_ALLOC/4) == 0)
81
tinfo("Thread %d: k=%d", thread, k);
82
}
83
84
return (void*)0;
85
}
86
87
tmain()
88
{
89
int i, rv;
90
void *status;
91
pthread_t th[N_THREAD];
92
Vmstat_t vmst;
93
94
topts();
95
taso(ASO_THREAD);
96
97
if(argc > 1) /* set max number of regions to avoid collisions */
98
setregmax(atoi(argv[1]));
99
100
for(i = 0; i < N_THREAD; ++i)
101
{ if((rv = pthread_create(&th[i], NULL, allocate, (void*)((long)i))) != 0 )
102
terror("Failed to create thread %d", i);
103
tinfo("Thread %d was created", i);
104
}
105
106
for(i = 0; i < N_THREAD; ++i)
107
{ if((rv = pthread_join(th[i], &status)) != 0 )
108
terror("Failed waiting for thread %d", i);
109
tinfo("Thread %d return error %ld", i, (long)status);
110
}
111
112
vmstat((Vmalloc_t*)0, &vmst);
113
tinfo("#regions=%d #open-regions=%d #busy-regions=%d #probes=%d",
114
vmst.n_region, vmst.n_open, vmst.n_lock, vmst.n_probe);
115
tinfo("n_busy=%d n_free=%d s_busy=%d s_free=%d n_seg=%d extent=%d",
116
vmst.n_busy, vmst.n_free, vmst.s_busy, vmst.s_free, vmst.n_seg, vmst.extent );
117
118
texit(0);
119
}
120
121