aboutsummaryrefslogtreecommitdiff
path: root/lib/python/Tools/Profile.py
blob: 1c44bc84578e611999db4ab5e88fd019e5bf51eb (plain)
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
import time
from Directories import resolveFilename, SCOPE_SYSETC

PERCENTAGE_START = 50
PERCENTAGE_END = 100

profile_start = time.time()

profile_data = {}
total_time = 1

try:
	profile_old = open(resolveFilename(SCOPE_SYSETC, "profile"), "r").readlines()

	t = None
	for line in profile_old:
		(t, id) = line[:-1].split('\t')
		t = float(t)
		total_time = t
		profile_data[id] = t
except:
	print "no profile data available"

profile_file = open(resolveFilename(SCOPE_SYSETC, "profile"), "w")

def profile(id):
	now = time.time() - profile_start
	if profile_file:
		profile_file.write("%.2f\t%s\n" % (now, id))
	if id in profile_data:
		t = profile_data[id]
		perc = t * (PERCENTAGE_END - PERCENTAGE_START) / total_time + PERCENTAGE_START
		try:
			open("/proc/progress", "w").write("%d \n" % perc)
		except IOError:
			pass

def profile_final():
	global profile_file
	if profile_file is not None:
		profile_file.close()
		profile_file = None