[6ccf55]: testcases / kernel / mem / lib / mem.c Maximize Restore History

Download this file

mem.c    718 lines (648 with data), 20.4 kB

  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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "test.h"
#include "usctest.h"
#include "../include/mem.h"
#include "config.h"
#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
&& HAVE_MPOL_CONSTANTS
#include <numaif.h>
#endif
void oom(int testcase, int mempolicy, int lite)
{
pid_t pid;
int status;
#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
&& HAVE_MPOL_CONSTANTS
unsigned long nmask = 2;
#endif
switch(pid = fork()) {
case -1:
tst_brkm(TBROK|TERRNO, cleanup, "fork");
case 0:
#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
&& HAVE_MPOL_CONSTANTS
if (mempolicy)
if (set_mempolicy(MPOL_BIND, &nmask, MAXNODES) == -1)
tst_brkm(TBROK|TERRNO, cleanup,
"set_mempolicy");
#endif
test_alloc(testcase, lite);
exit(0);
default:
break;
}
tst_resm(TINFO, "expected victim is %d.", pid);
if (waitpid(-1, &status, 0) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "waitpid");
if (testcase == OVERCOMMIT) {
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
tst_resm(TFAIL, "the victim unexpectedly failed: %d",
status);
} else {
if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL)
tst_resm(TFAIL, "the victim unexpectedly failed: %d",
status);
}
}
void write_memcg(void)
{
int fd;
char buf[BUFSIZ], mem[BUFSIZ];
fd = open(MEMCG_PATH_NEW "/memory.limit_in_bytes", O_WRONLY);
if (fd == -1)
tst_brkm(TBROK|TERRNO, cleanup, "open %s", buf);
sprintf(mem, "%ld", TESTMEM);
if (write(fd, mem, strlen(mem)) != strlen(mem))
tst_brkm(TBROK|TERRNO, cleanup, "write %s", buf);
close(fd);
fd = open(MEMCG_PATH_NEW "/tasks", O_WRONLY);
if (fd == -1)
tst_brkm(TBROK|TERRNO, cleanup, "open %s", buf);
snprintf(buf, BUFSIZ, "%d", getpid());
if (write(fd, buf, strlen(buf)) != strlen(buf))
tst_brkm(TBROK|TERRNO, cleanup, "write %s", buf);
close(fd);
}
void write_cpusets(void)
{
char cpus[BUFSIZ] = "";
char buf[BUFSIZ] = "";
int fd;
gather_cpus(cpus);
tst_resm(TINFO, "CPU list for 2nd node is %s.", cpus);
/* try either '/dev/cpuset/mems' or '/dev/cpuset/cpuset.mems'
* please see Documentation/cgroups/cpusets.txt of kernel src for detail */
fd = open(CPATH_NEW "/mems", O_WRONLY);
if (fd == -1) {
if (errno == ENOENT) {
fd = open(CPATH_NEW "/cpuset.mems", O_WRONLY);
if (fd == -1)
tst_brkm(TBROK|TERRNO, cleanup, "open %s", buf);
} else
tst_brkm(TBROK|TERRNO, cleanup, "open %s", buf);
}
if (write(fd, "1", 1) != 1)
tst_brkm(TBROK|TERRNO, cleanup, "write %s", buf);
close(fd);
/* try either '/dev/cpuset/cpus' or '/dev/cpuset/cpuset.cpus'
* please see Documentation/cgroups/cpusets.txt of kernel src for detail */
fd = open(CPATH_NEW "/cpus", O_WRONLY);
if (fd == -1) {
if (errno == ENOENT) {
fd = open(CPATH_NEW "/cpuset.cpus", O_WRONLY);
if (fd == -1)
tst_brkm(TBROK|TERRNO, cleanup, "open %s", buf);
} else
tst_brkm(TBROK|TERRNO, cleanup, "open %s", buf);
}
if (write(fd, cpus, strlen(cpus)) != strlen(cpus))
tst_brkm(TBROK|TERRNO, cleanup, "write %s", buf);
close(fd);
fd = open(CPATH_NEW "/tasks", O_WRONLY);
if (fd == -1)
tst_brkm(TBROK|TERRNO, cleanup, "open %s", buf);
snprintf(buf, BUFSIZ, "%d", getpid());
if (write(fd, buf, strlen(buf)) != strlen(buf))
tst_brkm(TBROK|TERRNO, cleanup, "write %s", buf);
close(fd);
}
void testoom(int mempolicy, int lite, int numa)
{
if (numa && !mempolicy)
write_cpusets();
tst_resm(TINFO, "start normal OOM testing.");
oom(NORMAL, mempolicy, lite);
tst_resm(TINFO, "start OOM testing for mlocked pages.");
oom(MLOCK, mempolicy, lite);
if (access(PATH_KSM, F_OK) == -1)
tst_brkm(TCONF, NULL, "KSM configuration is not enabled");
tst_resm(TINFO, "start OOM testing for KSM pages.");
oom(KSM, mempolicy, lite);
}
long count_numa(void)
{
int nnodes = 0;
while(path_exist(PATH_SYS_SYSTEM "/node/node%d", nnodes))
nnodes++;
return nnodes;
}
int path_exist(const char *path, ...)
{
va_list ap;
char pathbuf[PATH_MAX];
va_start(ap, path);
vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
va_end(ap);
return access(pathbuf, F_OK) == 0;
}
void gather_cpus(char *cpus)
{
int ncpus = 0;
int i;
char buf[BUFSIZ];
while(path_exist(PATH_SYS_SYSTEM "/cpu/cpu%d", ncpus))
ncpus++;
for (i = 0; i < ncpus; i++)
if (path_exist(PATH_SYS_SYSTEM "/node/node1/cpu%d", i)) {
sprintf(buf, "%d,", i);
strcat(cpus, buf);
}
/* Remove the trailing comma. */
cpus[strlen(cpus) - 1] = '\0';
}
int alloc_mem(long int length, int testcase)
{
void *s;
tst_resm(TINFO, "allocating %ld bytes.", length);
s = mmap(NULL, length, PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
if (s == MAP_FAILED) {
if (testcase == OVERCOMMIT && errno == ENOMEM)
return 1;
else
tst_brkm(TBROK|TERRNO, cleanup, "mmap");
}
if (testcase == MLOCK && mlock(s, length) == -1)
tst_brkm(TINFO|TERRNO, cleanup, "mlock");
#ifdef HAVE_MADV_MERGEABLE
if (testcase == KSM
&& madvise(s, length, MADV_MERGEABLE) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "madvise");
#endif
memset(s, '\a', length);
return 0;
}
void test_alloc(int testcase, int lite)
{
if (lite)
alloc_mem(TESTMEM + MB, testcase);
else
while(1)
if (alloc_mem(LENGTH, testcase))
return;
}
void umount_mem(char *path, char *path_new)
{
FILE *fp;
int fd;
char s_new[BUFSIZ], s[BUFSIZ], value[BUFSIZ];
/* Move all processes in task to its parent node. */
sprintf(s, "%s/tasks", path);
fd = open(s, O_WRONLY);
if (fd == -1)
tst_resm(TWARN|TERRNO, "open %s", s);
snprintf(s_new, BUFSIZ, "%s/tasks", path_new);
fp = fopen(s_new, "r");
if (fp == NULL)
tst_resm(TWARN|TERRNO, "fopen %s", s_new);
if ((fd != -1) && (fp != NULL)) {
while (fgets(value, BUFSIZ, fp) != NULL)
if (write(fd, value, strlen(value) - 1)
!= strlen(value) - 1)
tst_resm(TWARN|TERRNO, "write %s", s);
}
if (fd != -1)
close(fd);
if (fp != NULL)
fclose(fp);
if (rmdir(path_new) == -1)
tst_resm(TWARN|TERRNO, "rmdir %s", path_new);
if (umount(path) == -1)
tst_resm(TWARN|TERRNO, "umount %s", path);
if (rmdir(path) == -1)
tst_resm(TWARN|TERRNO, "rmdir %s", path);
}
void mount_mem(char *name, char *fs, char *options, char *path, char *path_new)
{
if (mkdir(path, 0777) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "mkdir %s", path);
if (mount(name, path, fs, 0, options) == -1) {
if (errno == ENODEV) {
if (rmdir(path) == -1)
tst_resm(TWARN|TERRNO, "rmdir %s failed",
path);
tst_brkm(TCONF, NULL,
"file system %s is not configured in kernel", fs);
}
tst_brkm(TBROK|TERRNO, cleanup, "mount %s", path);
}
if (mkdir(path_new, 0777) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "mkdir %s", path_new);
}
void ksm_usage(void)
{
printf(" -n Number of processes\n");
printf(" -s Memory allocation size in MB\n");
printf(" -u Memory allocation unit in MB\n");
}
void check(char *path, long int value)
{
FILE *fp;
char buf[BUFSIZ];
snprintf(buf, BUFSIZ, "%s%s", PATH_KSM, path);
fp = fopen(buf, "r");
if (fp == NULL)
tst_brkm(TBROK|TERRNO, tst_exit, "fopen");
if (fgets(buf, BUFSIZ, fp) == NULL)
tst_brkm(TBROK|TERRNO, tst_exit, "fgets");
fclose(fp);
tst_resm(TINFO, "%s is %ld.", path, atol(buf));
if (atol(buf) != value)
tst_resm(TFAIL, "%s is not %ld.", path, value);
}
void verify(char value, int proc, int start, int end, int start2, int end2)
{
int i, j;
void *s = NULL;
s = malloc((end - start) * (end2 - start2));
if (s == NULL)
tst_brkm(TBROK|TERRNO, tst_exit, "malloc");
tst_resm(TINFO, "child %d verifies memory content.", proc);
memset(s, value, (end - start) * (end2 - start2));
if (memcmp(memory[proc][start], s, (end - start) * (end2 - start2))
!= 0)
for (j = start; j < end; j++)
for (i = start2; i < end2; i++)
if (memory[proc][j][i] != value)
tst_resm(TFAIL, "child %d has %c at "
"%d,%d,%d.",
proc, memory[proc][j][i], proc,
j, i);
free(s);
}
void group_check(int run, int pages_shared, int pages_sharing,
int pages_volatile, int pages_unshared,
int sleep_millisecs, int pages_to_scan)
{
int fd;
char buf[BUFSIZ];
int old_num, new_num;
/* 1 seconds for ksm to scan pages. */
while (sleep(1) == 1)
continue;
fd = open("/sys/kernel/mm/ksm/full_scans", O_RDONLY);
if (fd == -1)
tst_brkm(TBROK|TERRNO, cleanup, "open");
/* wait 3 increments of full_scans */
if (read(fd, buf, BUFSIZ) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "read");
old_num = new_num = atoi(buf);
if (lseek(fd, 0, SEEK_SET) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "lseek");
while (new_num < old_num * 3) {
sleep(1);
if (read(fd, buf, BUFSIZ) < 0)
tst_brkm(TBROK|TERRNO, cleanup, "read");
new_num = atoi(buf);
if (lseek(fd, 0, SEEK_SET) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "lseek");
}
close(fd);
tst_resm(TINFO, "check!");
check("run", run);
check("pages_shared", pages_shared);
check("pages_sharing", pages_sharing);
check("pages_volatile", pages_volatile);
check("pages_unshared", pages_unshared);
check("sleep_millisecs", sleep_millisecs);
check("pages_to_scan", pages_to_scan);
}
void create_same_memory(int size, int num, int unit)
{
char buf[BUFSIZ], buf2[BUFSIZ];
int i, j, k;
int status, fd;
int *child;
long ps, pages;
ps = sysconf(_SC_PAGE_SIZE);
pages = 1024 * 1024 / ps;
child = malloc(num);
if (child == NULL)
tst_brkm(TBROK|TERRNO, cleanup, "malloc");
memory = malloc(num * sizeof(**memory));
if (memory == NULL)
tst_brkm(TBROK|TERRNO, cleanup, "malloc");
/* Don't call cleanup in those children. Instead, do a cleanup from the
parent after fetched children's status.*/
switch (child[0] = fork()) {
case -1:
tst_brkm(TBROK|TERRNO, cleanup, "fork");
case 0:
tst_resm(TINFO, "child 0 stops.");
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child 0 continues...");
tst_resm(TINFO, "child 0 allocates %d MB filled with 'c'.",
size);
memory[0] = malloc(size / unit * sizeof(*memory));
if (memory[0] == NULL)
tst_brkm(TBROK|TERRNO, tst_exit, "malloc");
for (j = 0; j * unit < size; j++) {
memory[0][j] = mmap(NULL, unit * MB,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
if (memory[0][j] == MAP_FAILED)
tst_brkm(TBROK|TERRNO, tst_exit, "mmap");
#ifdef HAVE_MADV_MERGEABLE
if (madvise(memory[0][j], unit * MB, MADV_MERGEABLE)
== -1)
tst_brkm(TBROK|TERRNO, tst_exit, "madvise");
#endif
for (i = 0; i < unit * MB; i++)
memory[0][j][i] = 'c';
}
tst_resm(TINFO, "child 0 stops.");
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child 0 continues...");
verify('c', 0, 0, size / unit, 0, unit * MB);
tst_resm(TINFO, "child 0 changes memory content to 'd'.");
for (j = 0; j < size / unit; j++) {
for (i = 0; i < unit * MB; i++)
memory[0][j][i] = 'd';
}
/* Unmerge. */
tst_resm(TINFO, "child 0 stops.");
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child 0 continues...");
verify('d', 0, 0, size / unit, 0, unit * MB);
/* Stop. */
tst_resm(TINFO, "child 0 stops.");
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child 0 continues...");
exit(0);
}
switch (child[1] = fork()) {
case -1:
tst_brkm(TBROK|TERRNO, cleanup, "fork");
case 0:
tst_resm(TINFO, "child 1 stops.");
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child 1 continues...");
tst_resm(TINFO, "child 1 allocates %d MB filled with 'a'.",
size);
memory[1] = malloc(size / unit * sizeof(*memory));
if (memory[1] == NULL)
tst_brkm(TBROK|TERRNO, tst_exit, "malloc");
for (j = 0; j < size / unit; j++) {
memory[1][j] = mmap(NULL, unit * MB,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
if (memory[1][j] == MAP_FAILED)
tst_brkm(TBROK|TERRNO, tst_exit, "mmap");
#ifdef HAVE_MADV_MERGEABLE
if (madvise(memory[1][j], unit * MB, MADV_MERGEABLE)
== -1)
tst_brkm(TBROK|TERRNO, tst_exit, "madvise");
#endif
for (i = 0; i < unit * MB; i++)
memory[1][j][i] = 'a';
}
tst_resm(TINFO, "child 1 stops.");
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child 1 continues...");
verify('a', 1, 0, size / unit, 0, unit * MB);
tst_resm(TINFO, "child 1 changes memory content to 'b'.");
for (j = 0; j < size / unit; j++) {
for (i = 0; i < unit * MB; i++)
memory[1][j][i] = 'b';
}
tst_resm(TINFO, "child 1 stops.");
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child 1 continues...");
verify('b', 1, 0, size / unit, 0, unit * MB);
tst_resm(TINFO, "child 1 changes memory content to 'd'");
for (j = 0; j < size / unit; j++) {
for (i = 0; i < unit * MB; i++)
memory[1][j][i] = 'd';
}
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child 1 continues...");
verify('d', 1, 0, size / unit, 0, unit * MB);
tst_resm(TINFO, "child 1 changes one page to 'e'.");
memory[1][size / unit - 1][unit * MB - 1] = 'e';
/* Unmerge. */
tst_resm(TINFO, "child 1 stops.");
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child 1 continues...");
verify('e', 1, size / unit - 1, size / unit,
unit * MB - 1, unit * MB);
verify('d', 1, 0, size / unit - 1, 0, unit * MB - 1);
/* Stop. */
tst_resm(TINFO, "child 1 stops.");
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child 1 continues...");
exit(0);
}
for (k = 2; k < num; k++) {
switch (child[k] = fork()) {
case -1:
tst_brkm(TBROK|TERRNO, cleanup, "fork");
case 0:
tst_resm(TINFO, "child %d stops.", k);
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child %d continues...", k);
tst_resm(TINFO, "child %d allocates %d "
"MB filled with 'a'.", k, size);
memory[k] = malloc(size / unit * sizeof(*memory));
if (memory[k] == NULL)
tst_brkm(TBROK|TERRNO, tst_exit, "malloc");
for (j = 0; j < size / unit; j++) {
memory[k][j] = mmap(NULL, unit * MB,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS
|MAP_PRIVATE, -1, 0);
if (memory[k][j] == MAP_FAILED)
tst_brkm(TBROK|TERRNO, cleanup,
"mmap");
#ifdef HAVE_MADV_MERGEABLE
if (madvise(memory[k][j], unit * MB,
MADV_MERGEABLE) == -1)
tst_brkm(TBROK|TERRNO, cleanup,
"madvise");
#endif
for (i = 0; i < unit * MB; i++)
memory[k][j][i] = 'a';
}
tst_resm(TINFO, "child %d stops.", k);
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child %d continues...", k);
tst_resm(TINFO, "child %d changes memory content to "
"'d'", k);
for (j = 0; j < size / unit; j++) {
for (i = 0; i < unit * MB; i++)
memory[k][j][i] = 'd';
}
/* Unmerge. */
tst_resm(TINFO, "child %d stops.", k);
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child %d continues...", k);
/* Stop. */
tst_resm(TINFO, "child %d stops.", k);
if (raise(SIGSTOP) == -1)
tst_brkm(TBROK|TERRNO, tst_exit, "kill");
tst_resm(TINFO, "child %d continues...", k);
exit(0);
}
}
tst_resm(TINFO, "KSM merging...");
snprintf(buf, BUFSIZ, "%s%s", PATH_KSM, "run");
fd = open(buf, O_WRONLY);
if (fd == -1)
tst_brkm(TBROK|TERRNO, cleanup, "open");
if (write(fd, "1", 1) != 1)
tst_brkm(TBROK|TERRNO, cleanup, "write");
close(fd);
snprintf(buf, BUFSIZ, "%s%s", PATH_KSM, "pages_to_scan");
snprintf(buf2, BUFSIZ, "%ld", size * pages * num);
fd = open(buf, O_WRONLY);
if (fd == -1)
tst_brkm(TBROK|TERRNO, cleanup, "open");
if (write(fd, buf2, strlen(buf2)) != strlen(buf2))
tst_brkm(TBROK|TERRNO, cleanup, "write");
close(fd);
snprintf(buf, BUFSIZ, "%s%s", PATH_KSM, "sleep_millisecs");
fd = open(buf, O_WRONLY);
if (fd == -1)
tst_brkm(TBROK|TERRNO, cleanup, "open");
if (write(fd, "0", 1) != 1)
tst_brkm(TBROK|TERRNO, cleanup, "write");
close(fd);
tst_resm(TINFO, "wait for all children to stop.");
for (k = 0; k < num; k++) {
if (waitpid(child[k], &status, WUNTRACED) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "waitpid");
if (!WIFSTOPPED(status))
tst_brkm(TBROK, cleanup, "child %d was not stopped.",
k);
}
tst_resm(TINFO, "resume all children.");
for (k = 0; k < num; k++) {
if (kill(child[k], SIGCONT) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "kill child[%d]", k);
}
group_check(1, 2, size * num * pages - 2, 0, 0, 0, size * pages * num);
tst_resm(TINFO, "wait for child 1 to stop.");
if (waitpid(child[1], &status, WUNTRACED) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "waitpid");
if (!WIFSTOPPED(status))
tst_brkm(TBROK, cleanup, "child 1 was not stopped.");
/* Child 1 changes all pages to 'b'. */
tst_resm(TINFO, "resume child 1.");
if (kill(child[1], SIGCONT) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "kill");
group_check(1, 3, size * num * pages - 3, 0, 0, 0, size * pages * num);
tst_resm(TINFO, "wait for child 1 to stop.");
if (waitpid(child[1], &status, WUNTRACED) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "waitpid");
if (!WIFSTOPPED(status))
tst_brkm(TBROK, cleanup, "child 1 was not stopped.");
/* All children change pages to 'd'. */
tst_resm(TINFO, "resume all children.");
for (k = 0; k < num; k++) {
if (kill(child[k], SIGCONT) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "kill child[%d]", k);
}
group_check(1, 1, size * num * pages - 1, 0, 0, 0, size * pages * num);
tst_resm(TINFO, "wait for all children to stop.");
for (k = 0; k < num; k++) {
if (waitpid(child[k], &status, WUNTRACED) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "waitpid");
if (!WIFSTOPPED(status))
tst_brkm(TBROK, cleanup, "child %d was not stopped.",
k);
}
/* Child 1 changes pages to 'e'. */
tst_resm(TINFO, "resume child 1.");
if (kill(child[1], SIGCONT) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "kill");
group_check(1, 1, size * num * pages - 2, 0, 1, 0, size * pages * num);
tst_resm(TINFO, "wait for child 1 to stop.");
if (waitpid(child[1], &status, WUNTRACED) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "waitpid");
if (!WIFSTOPPED(status))
tst_brkm(TBROK, cleanup, "child 1 was not stopped.");
tst_resm(TINFO, "resume all children.");
for (k = 0; k < num; k++) {
if (kill(child[k], SIGCONT) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "kill child[%d]", k);
}
tst_resm(TINFO, "KSM unmerging...");
snprintf(buf, BUFSIZ, "%s%s", PATH_KSM, "run");
fd = open(buf, O_WRONLY);
if (fd == -1)
tst_brkm(TBROK|TERRNO, cleanup, "open");
if (write(fd, "2", 1) != 1)
tst_brkm(TBROK|TERRNO, cleanup, "write");
group_check(2, 0, 0, 0, 0, 0, size * pages * num);
tst_resm(TINFO, "wait for all children to stop.");
for (k = 0; k < num; k++) {
if (waitpid(child[k], &status, WUNTRACED) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "waitpid");
if (!WIFSTOPPED(status))
tst_brkm(TBROK, cleanup, "child %d was not stopped.",
k);
}
tst_resm(TINFO, "resume all children.");
for (k = 0; k < num; k++) {
if (kill(child[k], SIGCONT) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "kill child[%d]", k);
}
tst_resm(TINFO, "stop KSM.");
if (lseek(fd, 0, SEEK_SET) == -1)
tst_brkm(TBROK|TERRNO, cleanup, "lseek");
if (write(fd, "0", 1) != 1)
tst_brkm(TBROK|TERRNO, cleanup, "write");
close(fd);
group_check(0, 0, 0, 0, 0, 0, size * pages * num);
while (waitpid(-1, &status, WUNTRACED | WCONTINUED) > 0)
if (WEXITSTATUS(status) != 0)
tst_resm(TFAIL, "child exit status is %d",
WEXITSTATUS(status));
}
void check_ksm_options(int *size, int *num, int *unit)
{
if (opt_size) {
*size = atoi(opt_sizestr);
if (*size < 1)
tst_brkm(TBROK, cleanup,
"size cannot be less than 1.");
}
if (opt_unit) {
*unit = atoi(opt_unitstr);
if (*unit > *size)
tst_brkm(TBROK, cleanup,
"unit cannot be greater than size.");
if (*size % *unit != 0)
tst_brkm(TBROK, cleanup,
"the remainder of division of size by unit is "
"not zero.");
}
if (opt_num) {
*num = atoi(opt_numstr);
if (*num < 3)
tst_brkm(TBROK, cleanup,
"process number cannot be less 3.");
}
}