Project

General

Profile

Bug #11164 ยป memory_test_min.py

tkalmus (Thomas Kalmus), 05/20/2015 03:05 PM

 
1
import re
2
import time
3
import os
4
import random
5
import sys
6
import gc
7

    
8
start_time=time.time()
9

    
10
# Monitor use of Resident and Virtual memory.
11
class Memory:	
12
	
13
	def __init__(self):
14
		self.shared_dirty = '.+?Shared_Dirty:\s+(\d+)'
15
		self.priv_dirty = '.+?Private_Dirty:\s+(\d+)'
16
		self.MEM_REGEXP = re.compile("{shared_dirty}{priv_dirty}".format(shared_dirty=self.shared_dirty, priv_dirty=self.priv_dirty), re.DOTALL)
17
		
18
	# get memory usage
19
	def get_memory_map(self, pids):
20
		memory_map = {}
21
		memory_map[ "pids_found" ] = {}
22
		memory_map[ "shared_dirty" ] = 0
23
		memory_map[ "priv_dirty" ] = 0
24
        
25
		for pid in pids:
26
			try:
27
				lines = None
28
				
29
				with open( "/proc/{pid}/smaps".format(pid=pid), "r" ) as infile:
30
					lines = infile.read()
31
			except:
32
				lines = None
33
				
34
			if lines:
35
				for shared_dirty, priv_dirty in re.findall( self.MEM_REGEXP, lines ):
36
					memory_map[ "pids_found" ][pid] = True
37
					memory_map[ "shared_dirty" ] += int( shared_dirty )
38
					memory_map[ "priv_dirty" ] += int( priv_dirty )		
39
				
40
		memory_map[ "pids_found" ] = memory_map[ "pids_found" ].keys()
41
		return memory_map
42
	
43
	# get the processes and get the value of the memory usage	
44
	def memory_usage( self):
45
		pids   = [ os.getpid() ]
46
		result = self.get_memory_map( pids)
47

    
48
		result[ "pids" ]   = pids
49

    
50
		return result
51
		
52
	# print the values of the private and shared memories
53
	def log( self, process_name='', log_tag=""):
54
		if process_name == "header":
55
			print " %-6s %5s %-12s %10s %10s" % ("proces", "pid", "log", "priv_dirty", "shared_dirty")
56
		else:
57
			global start_time
58
			Time = time.time() - start_time
59
			mem = self.memory_usage( )
60
			print " %-6s %5d %-12s %10d %10d" % (process_name, os.getpid(), log_tag, mem["priv_dirty"]/1000, mem["shared_dirty"]/1000)
61

    
62
# function to delay the processes a bit
63
def time_step( n):
64
	global start_time
65
	while (time.time() - start_time) < n:
66
		time.sleep( 0.01)
67
				
68
# 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
69
#
70
# case 0 (default) : we make a huge array of small objects by formatting a string
71
# case 1 : we make a huge array of small objects without formatting a string (we use the to_s function)
72
# case 2 : we make a smaller array of big objects										
73
def memory_object( size, option=2):
74
	count = size/20
75
	
76
	if option > 3 or option < 1:
77
		result = [ "%20.18f"% random.random() for i in xrange(count) ]
78
	
79
	elif option == 1:
80
		result = [ str( random.random() ) for i in xrange(count) ]
81
		
82
	elif option == 2:
83
		count = count/10
84
		result = [ ("%20.18f"% random.random())*30 for i in xrange(count) ]
85
		
86
	return result
87

    
88
##### main #####
89

    
90
print "python version {version}".format(version=sys.version)
91

    
92
memory = Memory()
93

    
94
gc.disable()
95

    
96
# print the column headers and first line
97
memory.log( "header")	# Print the headers of the columns
98
 
99
# Allocation of memory
100
big_memory = memory_object( 1000 * 1000 * 10)	# Allocate memory
101

    
102
memory.log( "Parent", "post alloc")
103

    
104
lab_time = time.time() - start_time
105
if lab_time < 3.9:
106
    lab_time = 0
107

    
108
# start the forking
109
pid = os.fork()		# fork the process
110
if pid == 0:
111
	Time = 4
112
	time_step( Time + lab_time)
113
	memory.log( "Child", "{time} initial".format(time=Time))
114

    
115
	# force GC when nothing happened
116
	gc.enable(); gc.collect(); gc.disable()
117

    
118
	Time = 10
119
	time_step( Time + lab_time)
120
	memory.log( "Child", "{time} empty GC".format(time=Time))
121

    
122
	time.sleep( 1)
123
	
124
	sys.exit(0)
125

    
126
Time = 4
127
time_step( Time + lab_time)
128
memory.log( "Parent", "{time} fork".format(time=Time))
129

    
130
# Wait for child process to finish
131
os.waitpid( pid, 0)