forked from jssmith/ray-scheduler-prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_trace.py
173 lines (156 loc) · 5.34 KB
/
build_trace.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import sys
import simplejson as json
from collections import defaultdict
ROOT_TASK_ID = "0"
def parse_json_dir(log_dir):
event_logs = []
for filename in os.listdir(log_dir):
if not os.path.isfile(os.path.join(log_dir, filename)):
continue
if 'worker' not in filename:
continue
if filename.endswith('c++.log'):
continue
event_log = []
with open(os.path.join(log_dir, filename)) as f:
lines = f.readlines()
for line in lines:
event = json.loads(line.strip())
event_log.append(event)
event_logs.append(event_log)
return event_logs
# Returns two dependency mappings. The first has key object id, value task uuid
# that produced it. The second has key task, value list of object ids that it
# depends on.
def build_dependencies(event_logs):
task_dependencies = {}
for event_log in event_logs:
for event in event_log:
if event['event'] != 'SCHEDULE':
continue
task_dependencies[event['taskId']] = event['dependsOn']
return task_dependencies
def build_tasks(task_dependencies, event_log, task_roots):
tasks = []
phases = []
is_driver = False
task_id = None
phase = 0
depends_on = []
submits = []
creates = []
cur_time = 0
for event in event_log:
event_type = event['event']
if event_type == 'SCHEDULE':
schedule = {'taskId': event['taskId']}
if cur_time == 0:
# This is the very first task scheduled, by the driver.
offset = 0
else:
offset = event['time'] - cur_time
schedule['timeOffset'] = offset
submits.append(schedule)
elif event_type == 'DRIVER_BEGIN':
assert(task_id == None)
cur_time = event['time']
is_driver = True
print "Program began at ", cur_time
elif event_type == 'BEGIN':
phases = []
task_id = event['taskId']
phase = 0
depends_on = task_dependencies[task_id]
submits = []
creates = []
cur_time = event['time']
elif event_type == 'PHASE_END':
phases.append({
'phaseId': phase,
'dependsOn': depends_on,
'submits': submits,
'duration': event['time'] - cur_time,
'creates': creates,
})
elif event_type == 'PHASE_BEGIN':
phase += 1
depends_on = event['dependsOn']
submits = []
creates = []
cur_time = event['time']
elif event_type == 'END':
phases.append({
'phaseId': phase,
'dependsOn': depends_on,
'submits': submits,
'duration': event['time'] - cur_time,
'creates': creates,
})
tasks.append({
'taskId': task_id,
'phases': phases,
'results': event['results'],
})
elif event_type == 'PUT':
object_id = event['objectId']
creates.append({
'objectId': event['objectId'],
'size': event['size'],
'timeOffset': event['time'] - cur_time,
})
elif event_type == 'DRIVER_END':
if not is_driver:
continue
phases.append({
'phaseId': phase,
'dependsOn': depends_on,
'submits': submits,
'duration': event['time'] - cur_time,
'creates': creates,
})
break
else:
print "Found unexpected event type {0}".format(event_type)
sys.exit(-1)
# The task ID should not be set if this the driver program.
if (task_id is None) and (event_log):
task_id = ROOT_TASK_ID
tasks.append({
'taskId': task_id,
'phases': phases,
'results': [],
})
task_roots.append(task_id)
return tasks
def dump_tasks(task_roots, tasks, trace_filename):
# There should only be one driver program.
if len(task_roots) == 0:
print "Error: No task roots found."
return
if len(task_roots) > 1:
print "Error: More than one task root."
return
# Write out the trace.
with open(trace_filename, 'w') as f:
f.write(json.dumps({
'rootTask': task_roots[0],
'tasks': tasks,
}, sort_keys=True, indent=4, separators=(',', ': ')))
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print 'First argument must be directory with logs'
sys.exit(1)
log_dir = sys.argv[1]
trace_filename = 'trace.json'
if len(sys.argv) >= 3:
trace_filename = sys.argv[2]
print 'Dumping trace built from {0} directory to {1}'.format(log_dir,
trace_filename)
event_logs = parse_json_dir(log_dir)
task_dependencies = build_dependencies(event_logs)
task_roots, tasks = [], []
for event_log in event_logs:
tasks += build_tasks(task_dependencies, event_log, task_roots)
dump_tasks(task_roots, tasks, trace_filename)