-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_run_phase_ii.py
executable file
·554 lines (439 loc) · 19.5 KB
/
run_run_phase_ii.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
"""
Given a number of servers, run phase_ii using different parameters, and prepare
files to be used in phase_iii.
This program will:
- take number of servers as input, N_min, N_max, t time
- it will pick the best 5 attack parameters, create a folder for each.
- cut the number of states and adjust the difficulty.
- download the energy consumption to the data folder.
-----------------------------------------------------
Planning for run_run_phase_iii:
* todo!
"""
# python3 run_run_phase_ii.py -n 72 --nservers 4 --ncores 32 -r 164000000000
from src.time_required import time_required, seconds_2_time
INTERVAL = 2**23
def get_server_names():
"""Get the server names to retrieve their power usage later.
This method will also deduce the number of servers!
"""
import os
with open(os.environ["OAR_NODEFILE"], "r") as f:
server_names = f.readlines()
server_names = [name.strip() for name in set(server_names)]
return server_names
def save_power_consumption_data(t_start, t_end):
import os"""
Given a number of servers, run phase_ii using different parameters, and prepare
files to be used in phase_iii.
This program will:
- take number of servers as input, N_min, N_max, t time
- it will pick the best 5 attack parameters, create a folder for each.
- cut the number of states and adjust the difficulty.
- download the energy consumption to the data folder.
-----------------------------------------------------
Planning for run_run_phase_iii:
* todo!
"""
# python3 run_run_phase_ii.py -n 72 --nservers 4 --ncores 32 -r 164000000000
from src.time_required import time_required, seconds_2_time
INTERVAL = 2**23
def get_server_names():
"""Get the server names to retrieve their power usage later.
This method will also deduce the number of servers!
"""
import os
with open(os.environ["OAR_NODEFILE"], "r") as f:
server_names = f.readlines()
server_names = [name.strip() for name in set(server_names)]
return server_names
def save_power_consumption_data(t_start, t_end):
import os
job_id = os.environ["OAR_JOBID"]
# &start_time=2021-06-08T15:00&end_time=2021-06-08T17:00
# combine server names
url_to_download = f"https://api.grid5000.fr/stable/sites/grenoble/metrics?job_id={job_id}&metrics=bmc_node_power_watt&&start_time={t_start}&end_time={t_end}"
commad = f"curl '{url_to_download}' | jq -r '.[] | [.timestamp, .device_id, .metric_id, .value, .labels|tostring] | @csv' > data/phase_ii_bmc_watt{job_id}.csv"
# download & save the data into a file
os.system(commad)
# curl 'https://api.grid5000.fr/stable/sites/grenoble/metrics?job_id=2296184&metrics=bmc_node_power_watt' | jq -r '.[] | [.timestamp, .device_id, .metric_id, .value, .labels|tostring] | @csv' > bmc_watt.cs
def say_folder_is_done():
"""Assume we are inside the folder."""
with open("data/phase_ii_status", "w") as f:
f.write("DONE!")
def is_folder_done():
import os
return os.path.exists("data/phase_ii_status")
def init_folder(n,
nstates,
nsenders,
nreceivers,
difficulty):
"""Copy the long message attack template folder to modify the new foldre.
"""
import os
files = os.listdir()
# notice that we are ignoring src folder since not all c files are needed
# also we want Makefile for phase_ii not the general one!
ignored_dir = set([".gdbinit", "playground", "backup_data", ".gitignore",
".git", "src", "Makefile", "doc", "experiments"])
path = f"experiments/N{n}_nstates{int(nstates)}_nsenders{nsenders}_nreceivers{nreceivers}_diff{difficulty}"
# create the special folder for the experiments
if not os.path.exists(path):
os.mkdir(path)
# copy the data folder only once!
os.system(f"rsync -a data {path}")
# if by accident we ran phase_iii, we need to clean the source files.
os.system(f"rm -rf {os.path.join(path, 'src/')}")
for f in files:
if f in ignored_dir:
continue
# copy the necessary files
os.system(f"rsync -a {f} {path}")
print(f"done with rsync {path}")
# copy files needed from src/
src_path = os.path.join(path, "src/")
os.mkdir(src_path)
src_files = ["common.c", "dict.c", "phase_ii.c", "receiver.c",
"sender.c", "time_required.py"]
# copy only needed source files for phase_ii
for src in src_files:
os.system(f"cp src/{src} {os.path.join(path, 'src/')}")
os.system(f"rsync -a src/util/ {os.path.join(path, 'src/')}util")
os.chdir(path)
os.system("mv Makefile_phase_ii Makefile")
# truncate the states file
print(f"Going to truncate the states file to {nstates*32}")
# since our states file doesn't contain all states but rather
# a compressed file
nbytes_in_states_file = (nstates*32)//INTERVAL
os.system(f"truncate --size={nbytes_in_states_file} data/states")
return path
def attack_choices(n,
nservers,
ncores_per_server,
server_memory): # todo set parameters
"""
Return the best five attack parameters on a given and a given nservers.
"""
import os
from math import log2
# loop over available choices
# difficulty <= 8
choices = [] # (nstates, nsenders, nreceivers, time_needed)
# how many bytes in the file
nbytes = os.stat("data/states").st_size
# how many states when the file gets uncompressed
# n_available_states = (nbytes/32) * (INTERVAL)
total_memory_nstates = nbytes*INTERVAL
available_memory = server_memory*nservers
nmsgs = 1000 # 1k is enough so senders won't eat the memory
value_size = 4 # bytes of 1 entry in the dictionary
print(f"mem avail=2^{log2(available_memory)}, states_mem = 2^{log2(total_memory_nstates)}")
for nreceivers in range(nservers, # start
nservers*ncores_per_server - nservers,
nservers): # step size
nsenders = (ncores_per_server*nservers) - nreceivers
for diff in range(0, 9):
# compute l
# rule 1: don't pass n/2 limit
# rule 2: we can't use more states than memory allows us!
# rule 3: we can't fill memory with nstates less than available
# in phase_i!
# @todo this is wrong! correct your calculation we're being biased
# towards 0 difficulty!
# take into account senders' consumption of memory
available_memory = server_memory*nservers - (nsenders*nmsgs*nreceivers*(n/8))
nstates = min(2**(n/2),
total_memory_nstates/(32), # how many states in the file
(available_memory*2**diff)/(value_size) # how many states in total can be fitted in dictionay across all nodes
)
nstates = int(nstates)
t = time_required(n,
nstates,
nsenders,
nreceivers,
diff)
choices.append((t, nstates, nsenders, nreceivers, diff))
# sort choices according to the time in ascending order
# sorted(choices,
choices.sort(key=lambda tup: tup[0])
# return the best 10 parameters or the closest number if they are < 10
return choices[:min(20, len(choices))]
if __name__ == "__main__":
import os
import argparse
from math import log2, ceil
from datetime import datetime
# This the folder where all experiments will be done
os.system("mkdir -p experiments")
parser = argparse.ArgumentParser()
parser.add_argument("-n",
type=int,
help="Number of BITS to be attacked, multiple of 8")
parser.add_argument("--nservers",
type=int,
help="How many servers?")
# assume all servers are the same.
parser.add_argument("--ncores",
type=int,
help="How many cpu cores does a server have?")
parser.add_argument("-r",
"--ram",
type=int,
help="what is the available memory for\
the dictionary per server?")
args = parser.parse_args()
# find the best 5 attack parameters.
best_parameters = attack_choices(args.n,
args.nservers,
args.ncores,
args.ram)
print("summary of the parameters")
print("-------------------------")
total_expected_time = 4*sum(tup[0] for tup in best_parameters)
print(f"total exp time = {seconds_2_time(total_expected_time)}")
for p in best_parameters:
print(f"nsenders={p[2]}, nreceivers={p[3]},\
l={log2(p[1])} difficulty={p[4]}")
print(f"time={seconds_2_time(4*p[0])}")
print("----------------------------------------")
# for each one of them create a folder_name_parameters
# run each attack in its repsoecting folder for at most for at
# most 3x the time estimated time.
for atck in best_parameters:
print("=======================================")
print("Now attacking: ")
print(f"nsenders={p[2]}, nreceivers={p[3]},\
nstates={log2(p[1])} difficulty={p[4]}")
print(f"time={seconds_2_time(4*p[0])}")
# N will be in bytes
N = ceil(args.n/8)
init_folder(N,
atck[1], # nstates
atck[2], # nsenders
atck[3], # nreceivers
atck[4], # difficulty
)
if is_folder_done():
print(f"skipping {atck}")
os.chdir("../../")
continue
t_start = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
# run the attack, nstates is used from states file
# nsenders is computed on the fly.
N = args.n//8
print(t_start)
command = f"timeout {int(4*p[0])}s python run_phase_ii.py\
--nservers {args.nservers} --receivers {atck[3]} -N {N} --ram {args.ram} \
--interval 23 -d {atck[4]}"
print(command)
# sleep(10)
os.system(command)
t_end = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
print(t_end)
say_folder_is_done() # we should not visit the folder again
print("************************************************")
# collect the energy consumption.
save_power_consumption_data(t_start, t_end)
# return to the base folder
os.chdir("../../")
# repeat
print("we are done! many thanks to grid5000.fr")
# todo list
# write another script to treat the collected data
# write another script to run phase_iii based on the existing folder
job_id = os.environ["OAR_JOBID"]
# &start_time=2021-06-08T15:00&end_time=2021-06-08T17:00
# combine server names
url_to_download = f"https://api.grid5000.fr/stable/sites/grenoble/metrics?job_id={job_id}&metrics=bmc_node_power_watt&&start_time={t_start}&end_time={t_end}"
commad = f"curl '{url_to_download}' | jq -r '.[] | [.timestamp, .device_id, .metric_id, .value, .labels|tostring] | @csv' > data/phase_ii_bmc_watt{job_id}.csv"
# download & save the data into a file
os.system(commad)
# curl 'https://api.grid5000.fr/stable/sites/grenoble/metrics?job_id=2296184&metrics=bmc_node_power_watt' | jq -r '.[] | [.timestamp, .device_id, .metric_id, .value, .labels|tostring] | @csv' > bmc_watt.cs
def say_folder_is_done():
"""Assume we are inside the folder."""
with open("data/phase_ii_status", "w") as f:
f.write("DONE!")
def is_folder_done():
import os
return os.path.exists("data/phase_ii_status")
def init_folder(n,
nstates,
nsenders,
nreceivers,
difficulty):
"""Copy the long message attack template folder to modify the new foldre.
"""
import os
files = os.listdir()
# notice that we are ignoring src folder since not all c files are needed
# also we want Makefile for phase_ii not the general one!
ignored_dir = set([".gdbinit", "playground", "backup_data", ".gitignore",
".git", "src", "Makefile", "doc", "experiments"])
path = f"experiments/N{n}_nstates{int(nstates)}_nsenders{nsenders}_nreceivers{nreceivers}_diff{difficulty}"
# create the special folder for the experiments
if not os.path.exists(path):
os.mkdir(path)
# copy the data folder only once!
os.system(f"rsync -a data {path}")
# if by accident we ran phase_iii, we need to clean the source files.
os.system(f"rm -rf {os.path.join(path, 'src/')}")
for f in files:
if f in ignored_dir:
continue
# copy the necessary files
os.system(f"rsync -a {f} {path}")
print(f"done with rsync {path}")
# copy files needed from src/
src_path = os.path.join(path, "src/")
os.mkdir(src_path)
src_files = ["common.c", "dict.c", "phase_ii.c", "receiver.c",
"sender.c", "time_required.py"]
# copy only needed source files for phase_ii
for src in src_files:
os.system(f"cp src/{src} {os.path.join(path, 'src/')}")
os.system(f"rsync -a src/util/ {os.path.join(path, 'src/')}util")
os.chdir(path)
os.system("mv Makefile_phase_ii Makefile")
# truncate the states file
print(f"Going to truncate the states file to {nstates*32}")
# since our states file doesn't contain all states but rather
# a compressed file
nbytes_in_states_file = (nstates*32)//INTERVAL
os.system(f"truncate --size={nbytes_in_states_file} data/states")
return path
def attack_choices(n,
nservers,
ncores_per_server,
server_memory): # todo set parameters
"""
Return the best five attack parameters on a given and a given nservers.
"""
import os
from math import log2
# loop over available choices
# difficulty <= 8
choices = [] # (nstates, nsenders, nreceivers, time_needed)
# how many bytes in the file
nbytes = os.stat("data/states").st_size
# how many states when the file gets uncompressed
# n_available_states = (nbytes/32) * (INTERVAL)
total_memory_nstates = nbytes*INTERVAL
available_memory = server_memory*nservers
nmsgs = 1000 # 1k is enough so senders won't eat the memory
value_size = 4 # bytes of 1 entry in the dictionary
print(f"mem avail=2^{log2(available_memory)}, states_mem = 2^{log2(total_memory_nstates)}")
for nreceivers in range(nservers, # start
nservers*ncores_per_server - nservers,
nservers): # step size
nsenders = (ncores_per_server*nservers) - nreceivers
for diff in range(0, 9):
# compute l
# rule 1: don't pass n/2 limit
# rule 2: we can't use more states than memory allows us!
# rule 3: we can't fill memory with nstates less than available
# in phase_i!
# @todo this is wrong! correct your calculation we're being biased
# towards 0 difficulty!
# take into account senders' consumption of memory
available_memory = server_memory*nservers - (nsenders*nmsgs*nreceivers*(n/8))
nstates = min(2**(n/2),
total_memory_nstates/(32), # how many states in the file
(available_memory*2**diff)/(value_size) # how many states in total can be fitted in dictionay across all nodes
)
nstates = int(nstates)
t = time_required(n,
nstates,
nsenders,
nreceivers,
diff)
choices.append((t, nstates, nsenders, nreceivers, diff))
# sort choices according to the time in ascending order
# sorted(choices,
choices.sort(key=lambda tup: tup[0])
# return the best 10 parameters or the closest number if they are < 10
return choices[:min(20, len(choices))]
if __name__ == "__main__":
import os
import argparse
from math import log2, ceil
from datetime import datetime
# This the folder where all experiments will be done
os.system("mkdir -p experiments")
parser = argparse.ArgumentParser()
parser.add_argument("-n",
type=int,
help="Number of BITS to be attacked, multiple of 8")
parser.add_argument("--nservers",
type=int,
help="How many servers?")
# assume all servers are the same.
parser.add_argument("--ncores",
type=int,
help="How many cpu cores does a server have?")
parser.add_argument("-r",
"--ram",
type=int,
help="what is the available memory for\
the dictionary per server?")
args = parser.parse_args()
# find the best 5 attack parameters.
best_parameters = attack_choices(args.n,
args.nservers,
args.ncores,
args.ram)
print("summary of the parameters")
print("-------------------------")
total_expected_time = 4*sum(tup[0] for tup in best_parameters)
print(f"total exp time = {seconds_2_time(total_expected_time)}")
for p in best_parameters:
print(f"nsenders={p[2]}, nreceivers={p[3]},\
l={log2(p[1])} difficulty={p[4]}")
print(f"time={seconds_2_time(4*p[0])}")
print("----------------------------------------")
# for each one of them create a folder_name_parameters
# run each attack in its repsoecting folder for at most for at
# most 3x the time estimated time.
for atck in best_parameters:
print("=======================================")
print("Now attacking: ")
print(f"nsenders={p[2]}, nreceivers={p[3]},\
nstates={log2(p[1])} difficulty={p[4]}")
print(f"time={seconds_2_time(4*p[0])}")
# N will be in bytes
N = ceil(args.n/8)
init_folder(N,
atck[1], # nstates
atck[2], # nsenders
atck[3], # nreceivers
atck[4], # difficulty
)
if is_folder_done():
print(f"skipping {atck}")
os.chdir("../../")
continue
t_start = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
# run the attack, nstates is used from states file
# nsenders is computed on the fly.
N = args.n//8
print(t_start)
command = f"timeout {int(4*p[0])}s python run_phase_ii.py\
--nservers {args.nservers} --receivers {atck[3]} -N {N} --ram {args.ram} \
--interval 23 -d {atck[4]}"
print(command)
# sleep(10)
os.system(command)
t_end = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
print(t_end)
say_folder_is_done() # we should not visit the folder again
print("************************************************")
# collect the energy consumption.
save_power_consumption_data(t_start, t_end)
# return to the base folder
os.chdir("../../")
# repeat
print("we are done! many thanks to grid5000.fr")
# todo list
# write another script to treat the collected data
# write another script to run phase_iii based on the existing folder