Advertisement
petruchito

Ruby memory sharing test

May 22nd, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.73 KB | None | 0 0
  1. require 'pp'
  2. $start_time=Time.new
  3.  
  4. # Monitor use of Resident and Virtual memory.
  5. class Memory
  6.  
  7.   shared_dirty = '.+?Shared_Dirty:\s+(\d+)'
  8.   priv_dirty = '.+?Private_Dirty:\s+(\d+)'
  9.   MEM_REGEXP = /#{shared_dirty}#{priv_dirty}/m
  10.  
  11.   # get memory usage
  12.   def self.get_memory_map( pids)
  13.     memory_map = {}
  14.     memory_map[ :pids_found] = {}
  15.     memory_map[ :shared_dirty] = 0
  16.     memory_map[ :priv_dirty] = 0
  17.  
  18.     pids.each do |pid|
  19.       begin
  20.         lines = nil
  21.         lines = File.read( "/proc/#{pid}/smaps")
  22.       rescue
  23.         lines = nil
  24.       end
  25.       if lines
  26.         lines.scan(MEM_REGEXP) do |shared_dirty, priv_dirty|
  27.           memory_map[ :pids_found][pid] = true
  28.           memory_map[ :shared_dirty] += shared_dirty.to_i
  29.           memory_map[ :priv_dirty] += priv_dirty.to_i
  30.         end
  31.       end
  32.     end
  33.     memory_map[ :pids_found] = memory_map[ :pids_found].keys
  34.     return memory_map
  35.   end
  36.  
  37.   # get the processes and get the value of the memory usage
  38.   def self.memory_usage( )
  39.     pids   = [ $$]
  40.     result = self.get_memory_map( pids)
  41.  
  42.     result[ :pids]   = pids
  43.     return result
  44.   end
  45.  
  46.   # print the values of the private and shared memories
  47.   def self.log( process_name='', log_tag="")
  48.     if process_name == "header"
  49.       puts " %-6s %5s %-12s %10s %10s\n" % ["proces", "pid", "log", "priv_dirty", "shared_dirty"]
  50.     else
  51.       time = Time.new - $start_time
  52.       mem = Memory.memory_usage( )
  53.       puts " %-6s %5d %-12s %10d %10d\n" % [process_name, $$, log_tag, mem[:priv_dirty]/1000, mem[:shared_dirty]/1000]
  54.     end
  55.   end
  56. end
  57.  
  58. # function to delay the processes a bit
  59. def time_step( n)
  60.   while Time.new - $start_time < n
  61.     sleep( 0.01)
  62.   end
  63. end
  64.  
  65. # create an object of specified size. The option argument can be changed from 0 to 2 to visualize the behavior of the GC in various cases
  66. #
  67. # case 0 (default) : we make a huge array of small objects by formatting a string
  68. # case 1 : we make a huge array of small objects without formatting a string (we use the to_s function)
  69. # case 2 : we make a smaller array of big objects
  70. def memory_object( size, option=ARGV[0].to_i)
  71.   result = []
  72.   count = size/20
  73.  
  74.   if option > 3 or option < 1
  75.     count.times do
  76.       result << "%20.18f" % rand
  77.     end
  78.   elsif option == 1
  79.     count.times do
  80.       result << rand.to_s
  81.     end
  82.   elsif option == 2
  83.     count = count/10
  84.     count.times do
  85.       result << ("%20.18f" % rand)*30
  86.     end
  87.   end
  88.  
  89.   return result
  90. end
  91.  
  92. ##### main #####
  93.  
  94. puts "ruby version #{RUBY_VERSION}"
  95.  
  96. GC.disable
  97.  
  98. # print the column headers and first line
  99. Memory.log( "header")
  100.  
  101. # Allocation of memory
  102. big_memory = memory_object( 1000 * 1000 * 10)
  103.  
  104. Memory.log( "Parent", "post alloc")
  105.  
  106. lab_time = Time.new - $start_time
  107. if lab_time < 3.9
  108.   lab_time = 0
  109. end
  110.  
  111. #GC.enable; (full_mark: true, immediate_sweep: false); GC.disable
  112. GC.enable; 3.times {GC.start}; GC.disable
  113. pp GC.stat
  114. # start the forking
  115. pid = fork do
  116.   time = 4
  117.   time_step( time + lab_time)
  118.   Memory.log( "Child", "#{time} initial")
  119.  
  120.  
  121.   memory_object( 1000 * 1000 * 10)
  122.   #big_memory = memory_object( 1000 * 1000 * 10)
  123.   #GC.enable; GC.start; GC.disable
  124.   #big_memory2 = memory_object( 1000 * 1000 * 10)
  125.  
  126.   # force GC when nothing happened
  127.   # GC.enable; GC.start; GC.disable
  128.   #100.times do
  129.   GC.enable; GC.start; GC.disable
  130.   #GC.enable; GC.start(full_mark: false, immediate_sweep: false); GC.disable
  131.   pp GC.stat
  132.   #pp GC.stat[:count]
  133.   #pp GC.stat[:old_objects]
  134.   time = 8
  135.   time_step( time + lab_time)
  136.   Memory.log( "Child", "#{time} empty GC")
  137.  
  138.   sleep( 1)
  139.   STDOUT.flush
  140.   # end
  141.   exit!
  142. end
  143.  
  144. time = 4
  145. time_step( time + lab_time)
  146. Memory.log( "Parent", "#{time} fork")
  147.  
  148. # wait for the child to finish
  149. Process.wait( pid)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement