forked from zzfan/ndnflow
/
paper_kit.py
548 lines (444 loc) · 17.9 KB
/
paper_kit.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import matplotlib#need to manually install py-matplotlib
matplotlib.use('Agg')
matplotlib.rcParams["font.size"] = 21
matplotlib.rcParams["xtick.labelsize"] = 18
matplotlib.rcParams["lines.linewidth"] = 3.0
matplotlib.rcParams["pdf.fonttype"] = 42
import matplotlib.pyplot as plt
from settings import log, OUTPUT_DIR
import settings
import sys
import platform
import time
import md5
import os, os.path
import signal, sys, time
import string
import smtplib
import inspect
import logging
import threading
import shlex, subprocess
import signal
import random
from smtplib import SMTP
#************** Global Settings ****************************************
IS_MT = True #Multi Threads Run
IS_REFRESH = True
#IS_REFRESH = False
MAX_THREADN = 24
#OUT_DIR = OUTPUT_DIR
PAPER = "paperkit"
DEBUG = False
LOG_LEVEL = logging.DEBUG
LOG_LEVEL = logging.INFO
"""the platform of the system"""
HOSTOS = platform.system()
if HOSTOS.startswith("Darwin"):#mac os x system, probably your local machine, debug mode
DEBUG = True
MAX_THREADN = 2 #for local notebook, we set a small threads number
#LOG_LEVEL = logging.DEBUG #for local development, we need debug mode
#OUTPUT_DIR += "-debug" #for local, output to a debug directory
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
MAX_THREADN = 1
#************** Global Settings ****************************************
'''log = logging.getLogger("paperKit") #root logger
format = logging.Formatter('%(levelname)8s:%(module)10s:%(funcName)20s:%(lineno)3d: %(message)s')
fh = logging.FileHandler("paperkit.log", mode="w")
fh.setFormatter(format)
sh = logging.StreamHandler() #console
sh.setFormatter(format)
log.addHandler(sh)
log.addHandler(fh)
log.setLevel(LOG_LEVEL)
'''
"""tag:
>: a new task begins, this new task may contain serveral subtasks
<: a task finishes, means that all the subtasks finishes
+: underlying is called and begin
-: underlying end
!+: data cache reading begin
!-: data cache reading end
$: data cache writing
"""
class Manager:
""" Super Class of all the manager class, name as an example, CaseTemplate, Dot, Line, Figure and Paper"""
def __init__(self, Id, **kwargs): #kwargs["outType"]
self.Id = Id
self.isMT = IS_MT
self.t0 = time.time() #the time instance is created
def notify(self, way="email", **msg): #way="email"|"print"
'''notify users about running result, currently there are two ways: email, print
'''
self.t1 = time.time()
data = PAPER+" ends "+str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(self.t1)))+ \
" TotalN=" + str(CaseTemplate.TotalN) +" SuccessN="+str(CaseTemplate.SuccessN)+ \
" ExistingN="+str(CaseTemplate.ExistingN) +" FailN="+str(CaseTemplate.FailN)
data = msg.get("data", data)
log.info(data)
if way == "print":
return
TO = msg.get("to", ["shock.jiang@gmail.com"])
FROM = "06jxk@163.com"
SMTP_HOST = "smtp.163.com"
user= "06jxk"
passwords="jiangxiaoke"
mailb = ["paper ends", data]
mailh = ["From: "+FROM, "To: shock.jiang@gmail.com", "Subject: " +data]
mailmsg = "\r\n\r\n".join(["\r\n".join(mailh), "\r\n".join(mailb)])
send = SMTP(SMTP_HOST)
send.login(user, passwords)
rst = send.sendmail(FROM, TO, mailmsg)
if rst != {}:
self.log.warn("send mail error: "+str(rst))
else:
self.log.info("sending mail finished")
send.close()
class DataCacheTemplate():
"""cache data to local file and if needed, read data from cache
for CaseTemplate, we may get the data from cache, which is embeded in "run" mechanism;
however, for FigureTemplate, we just write the data to file if only needed
"""
def __init__(self, Id, headers=None, datass=None, **kwargs): #headers = ["rowN", "latency", "hop"]
self.Id = Id
self.cacheout = os.path.join(OUTPUT_DIR, "cache")
if not os.path.exists(self.cacheout):
os.makedirs(self.cacheout)
self.cacheout = os.path.join(self.cacheout, self.Id+"-datacache.txt")
self.headers = headers
self.datass = datass #[[], [], [], ...], ever sub[] is one column in the file, or belong to a same line in the figure
if self.datass == None:
self.datass = []
self.is_refresh = kwargs.get("refresh", IS_REFRESH)
def to_refresh(self):
""" should re-run the task(most cases) or not (cache data is there and to_refresh=False)
easy True, hard False
"""
return self.is_refresh or (not os.path.exists(self.cacheout))
def read(self):
"""read data from cache
"""
log.info("!+ %s cache reading begins")
fin = open(self.cacheout)
if self.datass == None:
self.datass = []
if self.datass == []:
pass
else:
assert isinstance(self.datass[0], list), "self.datass[0] is not a list"
assert len(self.datass[0]) == 0, "self.datass[0] is not empty"
self.datass = []
for line in fin.readlines():
line = line.strip()
if line.startswith("#headers:"):
cols = line[len("#headers:"):].strip().split("|")
if self.headers == None:
for head in cols:
if header != "":
self.headers.append(col)
elif line.startswith("#command:") or line.startswith("#note:"):
pass
elif line != "":
cols = line.split()
li = [float(cols[i]) for i in range(len(cols))]
self.datass.append(li)
log.info("!- %s cache reading ends")
def write(self):
"""write data to cache
layout: horizontal, every sublist will be placed in one row
"""
if self.datass == None:
log.critical("datass is None")
return
fout = open(self.cacheout, "w") #write the data to result file
if self.headers != None:
line = ""
for header in self.headers:
line += "|" + header
line.strip()
line = "#headers: " + line+"\n"
fout.write(line)
#line = "#command: " + case.cmd + "\n"
#fout.write(line)
#assert self.datass != None, "self.datass == None"
assert isinstance(self.datass, list), "self.datass is not a list, %s" %(self.datass)
#assert isinstance(self.datass[0], list), "self.datass[0] is not a list, %s" %(self.datass[0])
for li in self.datass:
for val in li:
fout.write("%s\t" %(val))
fout.write("\n")
fout.flush()
fout.close()
log.info("$ " + self.Id+" cache writing")
class CaseTemplate(Manager, threading.Thread, DataCacheTemplate):
""" run program/simulation case, trace file is printed to self.trace, console msg is printed to self.output
and self.cacheout is stored the statstical information
self.data
"""
TotalN = 0
LiveN = 0 #LiveN < MAX_THREADN
SuccessN = 0
ExistingN = 0
FailN = 0 #TotalN = SuccessN + ExistingN + FailN
def __init__(self, Id, **kwargs):
threading.Thread.__init__(self)
Manager.__init__(self, Id)
DataCacheTemplate.__init__(self, Id, headers=["index", "value"], datass=None, kwargs=kwargs)
self.setDaemon(True)
self.result = None
self.datas = []
def start(self):
CaseTemplate.LiveN += 1
#log.debug("%s start" %(self.Id))
threading.Thread.start(self)
def run(self):
""" run the case, after running, the statstical result is held in self.data as list
"""
#CaseTemplate.LiveN += 1
log.info("> " +self.Id+" begins TotalN/LiveN/SuccessN/ExistingN/FailN=%d/%d/%d/%d/%d" \
%(CaseTemplate.TotalN, CaseTemplate.LiveN, CaseTemplate.SuccessN, CaseTemplate.ExistingN, CaseTemplate.FailN))
if not self.to_refresh():
CaseTemplate.ExistingN += 1
self.read()
self.datas = self.datass[0]
self.result = True
pass
else:
rst = self.underlying()
if rst == True:
CaseTemplate.SuccessN += 1
self.result = True
self.get_data()
if not self.is_refresh:
self.write()
else:
log.error(self.Id+" return error" )
if os.path.exists(self.cacheout):
os.remove(self.cacheout)
CaseTemplate.FailN += 1
self.result = False
CaseTemplate.LiveN -= 1
log.info("< " +self.Id+" ends TotalN/LiveN/SuccessN/ExistingN/FailN=%d/%d/%d/%d/%d" \
%(CaseTemplate.TotalN, CaseTemplate.LiveN, CaseTemplate.SuccessN, CaseTemplate.ExistingN, CaseTemplate.FailN))
#waiting for override
def underlying(self):
log.info("+ %s begin" %(self.Id))
log.info("- %s begin" %(self.Id))
return True
#waiting for override
def get_data(self):
"""extract data from output of underlying and all the data should be stored in datass.
"""
pass
class DotTemplate():
def __init__(self, x, y):
self.x = x
self.y = y
class LineTemplate(Manager):
def __init__(self, dots=None, xs=None, ys=None, plt={}, **kwargs):
"""plt: label(the key)
"""
#for dot in dots:
if dots != None:
dotN = len(dots)
self.xs = [dots[i].x for i in range(dotN)]
self.ys = [dots[i].y for i in range(dotN)]
else:
dotN = len(xs)
self.xs = xs
self.ys = ys
self.plt = plt
class FigureTemplate(Manager, DataCacheTemplate):
""" information of Figure
such as title, xlabel, ylabel, etc
"""
def __init__(self, Id, lines=None, canvas={}, datass=None, **kwargs):
Manager.__init__(self, Id, outType=".png")
DataCacheTemplate.__init__(self, Id, headers=None, datass=None)
if lines != None:
self.lines = lines
else:
assert datass != None, "lines == None and datass == None"
assert type(datass) == list, "datass is not list, datass=%s" %(datass)
assert type(datass[0]) == list, "datass[0] is not the list, datass=%s" %(datass)
self.lines = []
xs = datass[0]
for i in range(1, len(datass)):
datas = datass[i]
line = LineTempalte(dots=None, xs=xs, ys=datas)
self.lines.append(line)
self.canvas = canvas
self.kwargs = kwargs
self.pngout = os.path.join(settings.OUT_FIG_DIR, self.Id+".png")
self.pdfout = os.path.join(settings.OUT_FIG_DIR, self.Id+".pdf")
def line(self):
log.debug(self.Id+" begin to draw ")
plt.clf()
cans = []
for line in self.lines:
log.debug("line.xs="+str(line.xs))
log.debug("line.ys="+str(line.ys))
log.debug("plt atts="+str(line.plt))
can = plt.plot(line.xs, line.ys, line.plt.pop("style", "-"), **line.plt)
#can = plt.scatter(x=line.xs, y=line.ys)
cans.append(can)
plt.grid(True)
plt.xlabel(self.canvas.pop("xlabel", " "))
plt.ylabel(self.canvas.pop("ylabel", " "))
plt.xlim(xmax=self.canvas.pop("xmax", None))
self.extend(plt)#extend line
plt.legend(**self.canvas)
#loc='lower/upper left'
if HOSTOS.startswith("Darwin"):
pass
plt.savefig(self.pngout)
plt.savefig(self.pdfout)
log.debug(self.Id+" fig save to "+self.pngout)
plt.close()
log.info(self.Id+" ends")
def scatter(self):
log.debug(self.Id+" begin to draw ")
plt.clf()
cans = []
for line in self.lines:
log.debug("line.xs="+str(line.xs))
log.debug("line.ys="+str(line.ys))
log.debug("plt atts="+str(line.plt))
#can = plt.plot(line.xs, line.ys, line.plt.pop("style", "-"), **line.plt)
can = plt.scatter(x=line.xs, y=line.ys, s=self.kwargs.get("c", 1), **line.plt)
cans.append(can)
plt.grid(True)
plt.xlabel(self.canvas.pop("xlabel", "X"))
plt.ylabel(self.canvas.pop("ylabel", "X"))
plt.xlim(xmax=self.canvas.pop("xmax", None))
self.extend(plt)#extend line
plt.legend(**self.canvas)
#loc='lower/upper left'
if HOSTOS.startswith("Darwin"):
pass
plt.savefig(self.pngout)
plt.savefig(self.pdfout)
log.debug(self.Id+" fig save to "+self.pngout)
plt.close()
log.info(self.Id+" ends")
def extend(self, plt): #extend line
log.warn("wait for override")
pass
def bar(self):
log.debug(self.Id+" begin to draw ")
plt.clf()
self.bars = []
Width = self.canvas.pop("width", 0.2)
for i in range(len(self.lines)):
line = self.lines[i]
#for line in self.lines:
print "line.xs=",line.xs
print "line.ys=", line.ys
xs = [x+i*Width for x in line.xs]
bar = plt.bar(left=xs, height=line.ys, width=Width, bottom=0, align="center", **line.plt)
self.bars.append(bar)
#plt.legend( (p1[0], p2[0]), ('Men', 'Women') )
plt.legend((self.bars[i][0] for i in range(len(self.lines))), (self.lines[i].plt["label"] for i in range(len(self.lines))))
#for bar in self.bars:
xs = [Width * len(self.lines)/2 + j for j in line.xs]
plt.xticks(xs, line.xs)
plt.grid(True)
plt.xlabel(self.canvas.pop("xlabel", " "))
plt.ylabel(self.canvas.pop("ylabel", " "))
plt.legend(**self.canvas)
plt.title(self.canvas.pop("title", " "))
#plt.xticklabels([])
self.extend(plt)
plt.legend(**self.canvas)
plt.savefig(self.pngout)
plt.savefig(self.pdfout)
log.debug(self.Id+" fig save to "+self.pngout)
plt.close()
log.info(self.Id+" finishes")
class GodTemplate(Manager, threading.Thread):
""" GodTemplate to control all the processes of the program, when to run cases, how to assign data and draw figs
"""
IsError = False
def start(self):
threading.Thread.start(self)
#signal.pause()
def run(self):
cases = self.cases
log.info("> "+ self.Id + " run begins")
CaseTemplate.TotalN = len(cases)
keys = cases.keys()
if len(keys)<= MAX_THREADN:
for Id, case in cases.items():
case.start()
for Id, case in cases.items():
if case.isAlive():
case.join()
else:
aliveThds = []
for j in range(MAX_THREADN):
key = keys[j]
case = cases[key]
aliveThds.append(case)
case.start()
next = MAX_THREADN
while next < len(keys):
endN = 0
endThds = []
for case in aliveThds:
if case.isAlive() == False:
endN += 1
endThds.append(case)
if case.result == False:
God.IsError = True
break
if GodTemplate.IsError:
break
for case in endThds:
aliveThds.remove(case)
for i in range(endN):
if next >= len(keys):
break
key = keys[next]
case = cases[key]
aliveThds.append(case)
case.start()
next += 1
#time.sleep(1)
for case in aliveThds:
if case.isAlive():
case.join()
#pass
#time.sleep(1)
else:
if case.result == False:
GodTemplate.IsError = True
break
#aliveThds.remove(case)
#endThds.append(case)
log.info("< "+ self.Id + " run ends with IsError="+str(GodTemplate.IsError) +\
" TotalN="+str(len(cases))+" SuccessN="+str(CaseTemplate.SuccessN)+ " ExsitingN="+str(CaseTemplate.ExistingN) +" FailN="+str(CaseTemplate.FailN))
signal.alarm(1)
def __init__(self, paper):
""" GodTemplate will run all the cases needed
"""
Manager.__init__(self, Id="GOD")
threading.Thread.__init__(self)
self.setDaemon(True)
self.cases = {}
def setup(self):
"""set up all the data needed, which should call run
"""
pass
def world(self, **kwargs):
"""create a world, which contains multiple days/figures
"""
pass
def monday(self, **kwargs):
"""create one thing/figure
"""
def tuesday(self, **kwargs):
"""create another/figure
"""