-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
92 lines (65 loc) · 2.35 KB
/
util.py
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
#!/usr/bin/env python3
from collections import defaultdict
import psutil
import time
import sys
import csv
import os
def monitor_memory(subproc):
pid = subproc.pid
proc = psutil.Process(pid)
### Only for MATLAB
if sys.platform.startswith("win"):
while not proc.children():
pass
proc = proc.children()[0]
###
elif sys.platform.startswith("linux"):
with open('/proc/' + str(pid) + '/oom_score_adj', 'w') as score:
score.write('1000')
initial_memory = proc.memory_info().rss
max_memory = 0
print("Monitoring " + proc.name() + "...")
print("Initial memory usage:", initial_memory)
try:
while subproc.poll() is None:
current_memory = proc.memory_info().rss
if max_memory < current_memory:
max_memory = current_memory
time.sleep(0.050) # 20Hz sampling rate
except:
print("Exception:", sys.exc_info()[0])
print("Process exited with code: ", subproc.returncode)
memory_usage = max_memory - initial_memory
print("Max memory usage:", max_memory)
print("Delta memory usage:", memory_usage)
return max_memory, memory_usage
def get_matrix_list(directory, extension):
matrix_list = list()
file_list = list()
# Iterate over all the entries
for file in os.listdir(directory):
if file.endswith(extension):
matrix_list.append(file)
# Store full path
file_list.append(os.path.join(directory, file))
return file_list, matrix_list
def get_file(directory, filename):
out = ""
# Iterate over all the entries
for file in os.listdir(directory):
if file.endswith(filename):
# Store full path
out = (os.path.join(directory, file))
return out
def extract_columns(directory):
columns = defaultdict(list)
with open(directory, 'r') as f:
reader = csv.DictReader(f) # read rows into a dictionary format
for row in reader: # read a row as {column1: value1, column2: value2,...}
for (k, v) in row.items(): # go over each column name and value
if k != 'name':
columns[k].append(float(v)) # append the value into the appropriate list based on column name k
else:
columns[k].append(v)
return columns