Guest User

Untitled

a guest
Apr 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use Sys::Mmap;
  4. use Benchmark ':hireswallclock';
  5. use Time::HiRes qw(gettimeofday tv_interval);
  6. use strict;
  7.  
  8. my @cpids;
  9.  
  10. my $forkstep = 500;
  11. my $killcnt = 200;
  12. my $mmapsize = 100;
  13.  
  14. #fork + kill tests
  15. print "No mmaped file\n\n";
  16. allforktests();
  17. print "\n";
  18.  
  19. #fork +kill tests with mmaped file
  20. print "Creating and mmaping ${mmapsize}M file\n\n";
  21.  
  22. if (!-s("/tmp/mmaptest") == $mmapsize*1048576) {
  23. system("/bin/dd if=/dev/zero of=/tmp/mmaptest bs=1048576 count=${mmapsize}");
  24. }
  25. open(my $fh, "+</tmp/mmaptest")
  26. || die "Could not open map file: $!";
  27. mmap(my $mmap, $mmapsize*1048576, PROT_READ|PROT_WRITE, MAP_SHARED, $fh)
  28. || die "Could not map file: $!";
  29. # read entire file into memory
  30. for (0 .. $mmapsize-1) { my $a = substr($mmap, $_*1048576, 1048576); }
  31.  
  32. allforktests();
  33. munmap($mmap);
  34. print "\n";
  35.  
  36. #modify mmaped file tests
  37. mmap($mmap, $mmapsize*1048576, PROT_READ|PROT_WRITE, MAP_SHARED, $fh)
  38. || die "Could not map file: $!";
  39. # read entire file into memory
  40. for (0 .. $mmapsize-1) { my $a = substr($mmap, $_*1048576, 1048576); }
  41.  
  42. print "Modifying random points in ${mmapsize}M mmaped file with 0 children\n\n";
  43. modifyfiletests();
  44. print "\n";
  45.  
  46. print "Modifying random points in ${mmapsize}M mmaped file with 4000 children\n\n";
  47. forkchildren(4000);
  48. modifyfiletests();
  49. killchildren();
  50.  
  51. munmap($mmap);
  52.  
  53. sub modifyfiletests {
  54.  
  55. for (my $i = 100; $i < 500; $i += 100) {
  56. print "Modify $i points\n";
  57. timethis(500, sub {
  58. for (1 .. $i) {
  59. my $pos = int rand($mmapsize*1048576-256);
  60. substr($mmap, $pos, 256) = chr(ord('a') + rand(26)) x 256;
  61. }
  62. });
  63. sleep(1);
  64. }
  65.  
  66. }
  67.  
  68. sub allforktests {
  69.  
  70. # test forking 1 process
  71. for (1 .. 6) {
  72. my $nchild = scalar(@cpids);
  73. print "Time to fork + immediately reap 1 child, $nchild other children\n";
  74. timefork();
  75. forkchildren($forkstep);
  76. }
  77.  
  78. killchildren();
  79. print "\n";
  80.  
  81. for (1 .. 5) {
  82. forkchildren($forkstep);
  83. my $nchild = scalar(@cpids);
  84. print "Time to kill + reap $killcnt child processes, $nchild other children\n";
  85. timekill($killcnt);
  86. }
  87.  
  88. killchildren();
  89. }
  90.  
  91. sub timefork {
  92.  
  93. timethis(500, sub {
  94. if (my $pid = fork()) {
  95. # in parent, wait for child
  96. waitpid($pid, 0);
  97.  
  98. } else {
  99. # in child, exit immediately
  100. exit(0);
  101. }
  102. });
  103.  
  104. }
  105.  
  106. sub timekill {
  107. my $nkill = shift;
  108.  
  109. timethis($nkill, sub {
  110. my $pid = splice(@cpids, rand(@cpids), 1);
  111. kill 3, $pid;
  112. waitpid($pid, 0);
  113. });
  114.  
  115. # refork
  116. forkchildren($nkill);
  117. }
  118.  
  119. sub forkchildren {
  120. my $nchild = shift;
  121.  
  122. for (1 .. $nchild) {
  123. if (my $pid = fork()) {
  124. push @cpids, $pid;
  125. } else {
  126. # in child, just slow wait for signal
  127. $SIG{QUIT} = sub { die; };
  128. eval { sleep(5) while 1; };
  129. exit(0);
  130. }
  131. }
  132. sleep(1);
  133.  
  134. }
  135.  
  136. sub killchildren {
  137.  
  138. my $nchild = scalar(@cpids);
  139. print "Killing $nchild children\n";
  140.  
  141. timethis(1, sub {
  142. while (my $pid = shift @cpids) {
  143. kill 3, $pid;
  144. waitpid($pid, 0);
  145. }
  146. });
  147. sleep(1);
  148. }
Add Comment
Please, Sign In to add comment