-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimplement_manifest.py
197 lines (170 loc) · 6.72 KB
/
implement_manifest.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import os
import sys
import yaml
import random
import math
import re
MAX_UB = 65535
def make_sub(sub_old, sub_new, file_path):
with open(file_path, "r") as sources:
lines = sources.readlines()
with open(file_path, "w") as sources:
for line in lines:
sources.write(
re.sub(sub_old,
sub_new,
line)
)
def implement_manifest_v2(manifest, sketch_dir):
"""
Not supporting multiple sketches of different types
Not supporting LB UB
"""
assert(len(manifest['sketches']) == 1)
sk = manifest['sketches'][0]
rows = sk['rows']
cols = sk['cols']
# levels = sk['levels']
file_path = os.path.join(sketch_dir, 'sandbox.c')
sub_new = '#define NUM_COLS {}'.format(cols)
sub_old = '#define NUM_COLS [0-9]*'
make_sub(sub_old, sub_new, file_path)
sub_new = '#define NUM_ROWS {}'.format(rows)
sub_old = '#define NUM_ROWS [0-9]*'
make_sub(sub_old, sub_new, file_path)
def implement_manifest_v1(manifest):
total_thr = manifest['total_thr'] # aggregate thr for device over all flows
sandbox_lines = [
'#include "sandbox.h"\n\n'
]
p4actions = []
sk_num = 0
for sk in manifest['sketches']:
# sk.thr is the portion of all packets relevant to sk
# different sketches can be updated on the same packet so
# sum of sk.thr over all sketches can exceed total_thr
rows = sk['rows']
cols = sk['cols']
prob_update = sk['thr'] * sk['frac'] / total_thr
this_range = math.ceil(prob_update * MAX_UB)
LB = random.randint(0, MAX_UB - this_range)
UB = LB + this_range
# Sandbox
sandbox_lines.append('#define NUM_COLS_{} {}\n'.format(sk_num, cols))
sandbox_lines.append('#define NUM_ROWS_{} {}\n\n'.format(sk_num, rows))
sandbox_lines.append(
'__declspec(emem export scope(global)) '
'int32_t sketch_{}[NUM_ROWS_{}][NUM_COLS_{}];\n\n'
.format(sk_num, sk_num, sk_num))
for r in range(rows):
sandbox_lines.append("HASH_FUNC{}({}, NUM_COLS_{})\n"
.format(r, sk_num, sk_num))
sandbox_lines.append("UPDATE_ROW({}, {}, {}, {})\n"
.format(sk_num, r, LB, UB))
sandbox_lines.append('\n')
# P4 file
p4actions += ["cms_update_{}_{}();".format(sk_num, r)
for r in range(rows)]
sk_num += 1
with open('cm-sketch/sandbox.c', 'w') as f:
for line in sandbox_lines:
f.write(line)
sketch_block = (["primitive_action {}\n".format(x) for x in p4actions] +
["action sketch_action() {\n"] +
[" {}\n".format(x) for x in p4actions] +
["}\n"])
p4 = open('cm-sketch/main.p4', 'r')
lines = p4.readlines()
p4.close()
new_lines = []
sketching = False
for line in lines:
if("// sketch block end" in line):
sketching = False
if(not sketching):
new_lines.append(line)
if("// sketch block start" in line):
sketching = True
new_lines.extend(sketch_block)
new_p4 = open('cm-sketch/main.p4', 'w')
for line in new_lines:
new_p4.write(line)
new_p4.close()
def implement_manifest_v3(manifest, sketch_dir):
total_thr = manifest['total_thr'] # aggregate thr for device over all flows
sandbox_lines = [
'#include "sandbox.h"\n\n',
'#define LB 0\n',
'#define UB 65535\n\n'
]
sketch_util_lines = []
sk_num = 0
for sk in manifest['sketches']:
# sk.thr is the portion of all packets relevant to sk
# different sketches can be updated on the same packet so
# sum of sk.thr over all sketches can exceed total_thr
rows = sk['rows']
cols = sk['cols']
prob_update = sk['thr'] * sk['frac'] / total_thr
this_range = math.ceil(prob_update * MAX_UB)
LB = random.randint(0, MAX_UB - this_range)
UB = LB + this_range
# Sandbox
sandbox_lines.append('#define NUM_COLS_{} {}\n'.format(sk_num, cols))
sandbox_lines.append('#define NUM_ROWS_{} {}\n\n'.format(sk_num, rows))
if(sketch_dir == 'univmon'):
sandbox_lines.append('#define NUM_LEVELS_{} {}\n\n'.format(sk_num, sk['levels']))
sandbox_lines.append(
'__declspec(emem export scope(global)) '
'int32_t sketch_{}[NUM_LEVELS_{}][NUM_ROWS_{}][NUM_COLS_{}];\n\n'
.format(sk_num, sk_num, sk_num, sk_num))
sandbox_lines.append("HASH_LEVEL({})\n".format(sk_num))
sandbox_lines.append("GET_LEVEL({})\n".format(sk_num))
else:
sandbox_lines.append(
'__declspec(emem export scope(global)) '
'int32_t sketch_{}[NUM_ROWS_{}][NUM_COLS_{}];\n\n'
.format(sk_num, sk_num, sk_num))
for r in range(rows):
sandbox_lines.append("HASH_FUNC{}(index, {}, NUM_COLS_{})\n"
.format(r, sk_num, sk_num))
if(sketch_dir == 'count-sketch' or sketch_dir == 'univmon'):
sandbox_lines.append("HASH_FUNC{}(filter, {}, 2)\n"
.format(r, sk_num))
sandbox_lines.append("UPDATE_ROW({}, {})\n"
.format(sk_num, r))
sandbox_lines.append('\n')
# Sketch update util
if(sketch_dir == 'univmon'):
sketch_util_lines.append(
" uint32_t level_{} = get_level_{}(srcAddr, dstAddr);\n"
.format(sk_num, sk_num)
)
sketch_util_lines += [
" row_update_{}_{}(srcAddr, dstAddr, level_{});\n"
.format(sk_num, r, sk_num)
for r in range(rows)]
else:
sketch_util_lines += [
" row_update_{}_{}(srcAddr, dstAddr);\n".format(sk_num, r)
for r in range(rows)]
sk_num += 1
with open(os.path.join(sketch_dir, 'sketch.h'), 'w') as f:
for line in sandbox_lines:
f.write(line)
f.write("__forceinline\n")
f.write("void sketch_update_util(uint32_t srcAddr, uint32_t dstAddr) {\n")
for line in sketch_util_lines:
f.write(line)
f.write("}\n")
if(__name__ == '__main__'):
manifest_file = sys.argv[1]
with open(manifest_file) as f:
manifest = yaml.safe_load(f)
if(len(sys.argv) > 2):
manifest_id = int(sys.argv[2]) - 1
assert(len(sys.argv) > 3)
sketch_dir = sys.argv[3]
implement_manifest_v3(manifest[manifest_id], sketch_dir)
else:
implement_manifest_v1(manifest)