-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalloc_mem.c
368 lines (303 loc) · 11.2 KB
/
alloc_mem.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
alloc_mem: C program to allocate a specified amount of memory
or shared memory and then sleep. Memory can
be allocated in any block size (megs).
Notes: Failure to allocate a memory can be caused by few different things
- your out of memory and/or swap. (Virtual memory)
- maxdsize, maxdsize_64
- shmmni, shmmax, shmmnu
- ulimit
@(#) Revision: 1.11
@(#) $Date:2017/01
Rev history
2000/05 1.00 Jonathan Senkerik Orig version for HP-UX
2016/01 1.10 Jonathan Senkerik Ported to Linux
2017/01 1.11 Jonathan Senkerik Reformatted to look cleaner
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <signal.h>
#include <errno.h>
void Handle_Sig (int);
int main (argc, argv)
int argc;
char *argv[255];
{
int IterationCounter;
int isUsingBlocks, isReleaseOnExit, isLockingMem, isUsingSHM, isVerbose, isHelpFlag;
int Size, BlockSize, *Result, Result2;
int shmidCount;
int *shmid;
long Count;
char *shmptr;
/*
init variables
*/
isUsingBlocks = 0; /* Use Blocks flag */
isReleaseOnExit = 0; /* Exit & release allocated memory on exit flag */
isLockingMem = 0; /* Lock memory flag */
isUsingSHM = 0; /* Use shared memory */
isVerbose = 0; /* Verbose flag */
isHelpFlag = 0; /* Help flag */
Size = 0; /* Total size to allocate */
BlockSize = 0; /* Block size to allocate */
shmidCount = 0;
for (IterationCounter = 1; IterationCounter < argc; IterationCounter++) /* Go through command line args */
{
if (!strcmp (argv[IterationCounter], "-b"))
isUsingBlocks = 1;
if (!strcmp (argv[IterationCounter], "-r"))
isReleaseOnExit = 1;
if (!strcmp (argv[IterationCounter], "-l"))
isLockingMem = 1;
if (!strcmp (argv[IterationCounter], "-s"))
isUsingSHM = 1;
if (!strcmp (argv[IterationCounter], "-v"))
isVerbose = 1;
if (!strcmp (argv[IterationCounter], "-?"))
isHelpFlag = 1;
if (Size != 0)
{
BlockSize = atol (argv[IterationCounter]);
isUsingBlocks = 1;
}
if (Size == 0)
Size = atol (argv[IterationCounter]);
}
if (Size <= 0)
isHelpFlag = 1;
if (BlockSize <= 0)
{
if (isUsingSHM == 1)
BlockSize = 10;
else
BlockSize = 1;
}
if (BlockSize > Size)
isHelpFlag = 1;
if (isHelpFlag == 1)
{
printf ("usage: %s [-b] [-r] [-s] [-l] [-v] [size] [bsize]\n -b allocate in blocks (default std=1meg, shm=10meg)\n", argv[0]);
printf (" -r Release memory after it has been allocated\n");
printf (" -s Allocate using shared memory\n");
printf (" -l Lock pages in memory (need root or MLOCK privs)\n");
printf (" -v Verbose\n");
printf (" size = Allocate specified in megs (default 10 megs)\n");
printf (" bsize = Specify block size (requires -b option)\n");
printf ("\n\n Example : ./alloc_mem -b 200 10 (Allocate 200 megs memory in 10 meg chunks)\n");
printf (" ./alloc_mem -b -s 500 (Allocate 500 megs shared memory in 10 meg chunks)\n");
printf (" ./alloc_mem -r -l 2000 100 (Allocate 2 gig memory in 100 meg chunks)\n");
printf ("\n\n Notes: Failure to allocate a memory can be caused by few different things\n");
printf (" - your out of memory and/or swap. (Virtual memory)\n");
printf (" - maxdsize, maxdsize_64\n");
printf (" - shmmni, shmmax, shmmnu\n");
printf (" - ulimit\n");
exit (1);
}
if (isVerbose == 1)
{
printf ("\n Version = 1.11\n");
if (isUsingBlocks == 1)
printf (" Allocate memory in blocks = True\n");
else
printf (" Allocate memory in blocks = False\n");
if (isReleaseOnExit == 1)
printf (" Release after allocated = True\n");
else
printf (" Release after allocated = False\n");
if (isUsingSHM == 1)
printf (" Use shared memory = True\n");
else
printf (" Use shared memory = False\n");
if (isLockingMem == 1)
printf (" Lock pages in memory = True\n");
else
printf (" Lock pages in memory = False\n");
printf (" Total memory to allocate = %d meg\n", Size);
if (isUsingBlocks == 1)
printf (" Allocated in block size = %d meg\n", BlockSize);
}
/*
Are we creating a shared memory segment???
*/
if (isUsingSHM == 1)
{
if (isLockingMem == 1) /* lockpages if isLockingMem is set */
{
Result2 = mlockall (MCL_CURRENT | MCL_FUTURE);
if (Result2 == 0)
printf ("Setting locked pages success.\n");
else
perror ("Setting locked pages failed ");
}
if (isUsingBlocks == 1)
{ /* in blocks */
shmid = malloc (Size / BlockSize * sizeof (int));
for (IterationCounter = BlockSize; IterationCounter <= Size; IterationCounter = IterationCounter + BlockSize)
{
*(shmid + shmidCount) = shmget (IPC_PRIVATE, BlockSize * 1048576, 0600);
if (*(shmid + shmidCount) == -1)
{
perror ("Allocated shared memory failed ");
Done (isReleaseOnExit, isLockingMem, isUsingSHM, isVerbose, shmid, shmidCount);
}
else
{
shmptr = shmat (*(shmid + shmidCount), (char *) 0, 0);
/*
If we don't write to each (4k) page, then the kernel
will not create the page...
*/
for (Count = 0; Count < (BlockSize * 1048576); Count = Count + 4096)
{
*(shmptr + Count) = 0;
}
printf ("Create shared segment, %i megs success. (shmid = %i)", IterationCounter, *(shmid + shmidCount));
if (isLockingMem == 1)
{
Result2 = shmctl (*(shmid + shmidCount), SHM_LOCK, 0);
if (Result2 == 0)
printf (" -locked success.\n");
else
printf (" -locked failed!!!\n");
}
else
printf ("\n");
shmdt (shmptr);
}
shmidCount++;
}
}
else /* else create shared mem seg in one big chunk */
{
shmid = malloc (sizeof (int));
*(shmid + shmidCount) = shmget (IPC_PRIVATE, Size * 1048576, 0600);
if (*(shmid + shmidCount) == -1)
{
perror ("Allocated shared memory failed ");
exit (1);
}
else
{
shmptr = shmat ((*(shmid + shmidCount)), (char *) 0, 0);
/*
If we don't write to each (4k) page, then the kernel
will not create the page...
*/
for (Count = 0; Count < (Size * 1048576); Count = Count + 4096)
{
*(shmptr + Count) = 0;
}
printf ("Create shared segment, %i megs success. (shmid = %i)", Size, *(shmid + shmidCount));
}
if (isLockingMem == 1)
{
Result2 = shmctl (*(shmid + shmidCount), SHM_LOCK, 0);
if (Result2 == 0)
printf (" -locked success.\n");
else
printf (" -locked failed!!!\n");
}
else
printf ("\n");
shmdt (shmptr);
shmidCount = 1; /* here we're only allocating 1 block */
}
Done (isReleaseOnExit, isLockingMem, isUsingSHM, isVerbose, shmid, shmidCount);
}
/*
We must be allocating memory normally (data area).
Using calloc (instead of malloc) to force the kernel
to create the page.
*/
if (isLockingMem == 1) /* lock pages if isLockingMem is set */
{
Result2 = mlockall (MCL_CURRENT | MCL_FUTURE);
if (Result2 == 0)
printf ("Setting locked pages success.\n");
else
perror ("Setting locked pages failed ");
}
if (isUsingBlocks == 1) /* create in blocks */
{
for (IterationCounter = BlockSize; IterationCounter <= Size; IterationCounter = IterationCounter + BlockSize)
{
Result = calloc (262144 * BlockSize, sizeof (int));
if (Result == 0)
{
printf ("Malloc %i meg failed. Requested %i megs.\n", IterationCounter, Size);
perror ("Error ");
IterationCounter = Size;
}
else
{
printf ("Malloc %i meg success. ", IterationCounter);
printf ("Address 0x%x\n", Result);
}
}
Done (isReleaseOnExit, isLockingMem, isUsingSHM, isVerbose, NULL, 0);
}
else
{ /* 1 big chunk */
Size = Size * 1024 * 256;
Result = calloc (Size, sizeof (int));
if (Result == 0)
{
printf ("Malloc %i megs failed.\n", Size / 1024 / 256);
perror ("Error ");
exit (1);
}
else
printf ("Malloc %i megs success.\n", Size / 1024 / 256);
Done (isReleaseOnExit, isLockingMem, isUsingSHM, isVerbose, NULL, 0);
}
}
/*
We are all done... Do we wait or exit? (isReleaseOnExit)
*/
Done (isReleaseOnExit, isLockingMem, isUsingSHM, isVerbose, shmid, shmidCount)
int isReleaseOnExit, isLockingMem, isUsingSHM, isVerbose, *shmid, shmidCount;
{
int IterationCounter;
if (isReleaseOnExit == 1)
{
if (isUsingSHM == 1) /* Remove shared memory segments */
{
for (IterationCounter = 0; IterationCounter < shmidCount; IterationCounter++)
{
if (isVerbose == 1 && *(shmid + IterationCounter) != -1)
printf (" Removing shared memory segment %d \n", *(shmid + IterationCounter));
shmctl (*(shmid + IterationCounter), IPC_RMID, 0);
}
}
exit (0);
}
else
{
printf (" Waiting for BREAK signal ( [CTRL]-C )...\n");
signal (SIGINT, Handle_Sig); /* Wait for BREAK to exit */
pause ();
if (isVerbose == 1)
printf (" OK, received BREAK signal...");
printf ("\n");
if (isUsingSHM == 1) /* Remove shared memory segments */
{
for (IterationCounter = 0; IterationCounter < shmidCount; IterationCounter++)
{
if (isVerbose == 1 && *(shmid + IterationCounter) != -1)
printf (" Removing shared memory segment %d \n", *(shmid + IterationCounter));
shmctl (*(shmid + IterationCounter), IPC_RMID, 0);
}
}
exit (0);
}
}
void Handle_Sig (Junk)
int Junk;
{
// Do nothing...
}