test.py 7.55 KB
Newer Older
German Leon's avatar
German Leon committed
1
#!/usr/bin/python
German Leon's avatar
German Leon committed
2
3
4
5
6
7
import sys, os, subprocess, random, time, datetime
import pexpect, re
import os.path
from os import path
import fnmatch
import serial
German Leon's avatar
German Leon committed
8
import watchdog
German Leon's avatar
German Leon committed
9
10
11
import config
from config import *

German Leon's avatar
German Leon committed
12
13
14
if inject:
  from config_inject import *

German Leon's avatar
German Leon committed
15
global w
German Leon's avatar
German Leon committed
16
17
18
def configurate_serie():
	#os.system("sudo stop ttyS0")
	Puerto=0
German Leon's avatar
German Leon committed
19
	Puerto="/dev/ttyS0"
German Leon's avatar
German Leon committed
20
21
22
23
24
25
26
27
28
29
30
	try:
	   serie = serial.Serial(Puerto, 115200)
	   serie.timeout=1;
   
	except serial.SerialException:
	   #-- Error al abrir el puerto serie
	   sys.stderr.write("Error al abrir puerto (%s)\n" % str(Puerto))
	return serie

#mantener vivos los watdog softwre y hardware
def KeepWatchdog():
German Leon's avatar
German Leon committed
31
32
33
34
35
36
   #cab=os.environ.get("MYHOME")
   #if (cab==None):
   #     cab=os.environ.get("HOME")
   w.keepalive()     
   #os.system(cab+"/bin/refresh_watchdog > watchsoft.test")
   #os.system("touch watchsoft.test")
German Leon's avatar
German Leon committed
37

German Leon's avatar
German Leon committed
38
39
40
41
42
43
44
# Comprime los ficheros con extension .tar
def gzipFiles():
  listOfFiles = os.listdir('results')
  for entry in listOfFiles:
      if fnmatch.fnmatch(entry, "*.tar") or fnmatch.fnmatch(entry, "*.log"):
           os.system("gzip -f results/" + entry)

German Leon's avatar
German Leon committed
45
def writeOutput(serie, output, elapsedTime, outputFileName, fileName):
German Leon's avatar
German Leon committed
46
47
      if verbose:
        print("\n***Elapsed time: %s ms. ***\n" % (elapsedTime))
German Leon's avatar
German Leon committed
48
49
      serie.write("\n***Elapsed time: %s ms. ***\n" % (elapsedTime))

German Leon's avatar
German Leon committed
50
#      if verbose:
German Leon's avatar
German Leon committed
51
#        print("%s " % (output) ) 
German Leon's avatar
German Leon committed
52
#      serie.write("%s " % (output) )
German Leon's avatar
German Leon committed
53
54
55
56
57
58
59
60
61
62
63
64

      output += "\n\n***Elapsed time: " + `elapsedTime` + " ms. ***\n\n"
      outputFile = open(outputFileName, "w")
      outputFile.write(output)
      outputFile.flush()
      outputFile.close()
#      os.system("tar -rvf results/" + fileName + ".tar " + outputFileName)
#      os.system("rm " + outputFileName)

def runBenchmark(fiIni, fileName, tIni):
  
  serie=configurate_serie()
German Leon's avatar
German Leon committed
65
  w=watchdog.watchdog() 
German Leon's avatar
German Leon committed
66
67
68
69
70
71
72
  numTimeouts = 0

  if (totalFiNumber == -1): # bucle infinito
      forever = True
  else:
      forever = False

German Leon's avatar
German Leon committed
73
74
  if log:
    stdLogFile = open("results/"+fileName+"_"+tIni+".log", "w")
German Leon's avatar
German Leon committed
75
76
77
78
79
80

  fiNumber = fiIni - 1
  while forever or ( (fiNumber+1) < totalFiNumber+1):

    fiNumber = fiNumber + 1

German Leon's avatar
German Leon committed
81
82
#    KeepWatchdog()
    w.keepalive() 
German Leon's avatar
German Leon committed
83

German Leon's avatar
German Leon committed
84
85
86
87
88
89
90
91
#    if (fiNumber > 100):
#        tout = 90
#    else:
#        tout = timeout

    if inject:
      generateFiPointFromGroupedLines()

German Leon's avatar
German Leon committed
92
93
94
95
96
97
98
99
100
101
102
    try:

      x = datetime.datetime.now()
      outputFileName =  "results/"+fileName + x.strftime("_%H%M%S.err")
      ti = x.strftime("%m/%d/%Y %H:%M:%S")

      # Measure execution time
      startTime = time.time()

#      os.system("sudo /home/ubuntu/bin/gpufreq-info")
#      os.system("cpufreq-info | grep 'current CPU' | cut -d's' -f2 | cut -d'(' -f1")
German Leon's avatar
German Leon committed
103
104
#      print("./"+outputExeFile+" "+inputParameters)
      p = pexpect.spawn("./"+outputExeFile+" "+inputParameters, timeout=timeout)
German Leon's avatar
German Leon committed
105
106
107
108
109
110
111
112
113
114

      p.expect(pexpect.EOF)

      # Process elspased time
      elapsedTime = 1000*(time.time() - startTime)

      p.close()

      if (p.exitstatus == 0):
        if verbose:
German Leon's avatar
German Leon committed
115
          print("(%d) %s; PASS in %.2f ms." % (fiNumber, ti, elapsedTime) )
German Leon's avatar
German Leon committed
116
          print(p.before)
German Leon's avatar
German Leon committed
117
118
119
          sys.stdout.flush()
        if log:
          stdLogFile.write("(%d) %s; PASS in %.2f ms.\n" % (fiNumber, ti, elapsedTime) )
German Leon's avatar
German Leon committed
120
          stdLogFile.write("%s\n" % (p.before) )
German Leon's avatar
German Leon committed
121
122
123
124
125
126
127
128
129
130
          stdLogFile.flush()
#        serie.write("(%d) %s; PASS in %.2f ms.\n\r" % (fiNumber, ti, elapsedTime) )
#        serie.write("%s\n" % (p.before) )
#        serie.flush()
      elif (p.exitstatus == 4): 
        if verbose:
          print("(%d) %s; CRASH in %.2f ms." % (fiNumber, ti, elapsedTime) )
          sys.stdout.flush()
        if log:
          stdLogFile.write("(%d) %s; CRASH in %.2f ms.; %s\n" % (fiNumber, ti, elapsedTime, outputFileName) )
German Leon's avatar
German Leon committed
131
          stdLogFile.write("%s\n" % (p.before) )
German Leon's avatar
German Leon committed
132
133
134
135
136
          stdLogFile.flush()
#        serie.write("(%d) %s; CRASH in %.2f ms.; %s\n\r" % (fiNumber, ti, elapsedTime, outputFileName) )
#        serie.flush()
        if log:
          writeOutput(serie, p.before, elapsedTime, outputFileName, fileName+"_"+tIni)
German Leon's avatar
German Leon committed
137
138
      elif (p.exitstatus == 1): 
        if verbose:
German Leon's avatar
German Leon committed
139
          print("(%d) %s; SDC in %.2f ms." % (fiNumber, ti, elapsedTime) )
German Leon's avatar
German Leon committed
140
          print(p.before)
German Leon's avatar
German Leon committed
141
142
143
          sys.stdout.flush()
        if log:
          stdLogFile.write("(%d) %s; SDC in %.2f ms.; %s\n" % (fiNumber, ti, elapsedTime, outputFileName) )
German Leon's avatar
German Leon committed
144
          stdLogFile.write("%s\n" % (p.before) )
German Leon's avatar
German Leon committed
145
146
147
148
149
          stdLogFile.flush()
#        serie.write("(%d) %s; SDC in %.2f ms.; %s\n\r" % (fiNumber, ti, elapsedTime, outputFileName) )
#        serie.flush()
        if log:
          writeOutput(serie, p.before, elapsedTime, outputFileName, fileName+"_"+tIni)
German Leon's avatar
German Leon committed
150
151
152
      else:
        if verbose:
          print("(%d) %s; Exit status: %d in %.2f ms." % (fiNumber, ti, p.exitstatus, elapsedTime) )
German Leon's avatar
German Leon committed
153
154
155
          sys.stdout.flush()
        if log:
          stdLogFile.write("(%d) %s; Exit status: %d in %.2f ms.\n" % (fiNumber, ti, p.exitstatus, elapsedTime) )
German Leon's avatar
German Leon committed
156
          stdLogFile.write("%s\n" % (p.before) )
German Leon's avatar
German Leon committed
157
158
159
160
161
          stdLogFile.flush()
#        serie.write("(%d) %s; Exit status: %d in %.2f ms.\n\r" % (fiNumber, ti, p.exitstatus, elapsedTime) )
#        serie.flush()
        if log:
          writeOutput(serie, p.before, elapsedTime, outputFileName, fileName+"_"+tIni)
German Leon's avatar
German Leon committed
162
163
164
165
166
167
168
169
170
171
172


      numTimeouts = 0

    except KeyboardInterrupt:
      print("KeyboardInterrupt")
      sys.stdout.flush()
      lastFile = open("lastFiNumber", "w")
      lastFile.write("%d" % fiNumber)
      lastFile.flush()
      lastFile.close()
German Leon's avatar
German Leon committed
173
174
175
      if log:
        stdLogFile.flush()
        stdLogFile.close()
German Leon's avatar
German Leon committed
176
177
178
179
180
181
182
183
184
185
186
      sys.exit(2)

    except pexpect.EOF, e:
      print("Fin de archivo")

    except pexpect.TIMEOUT, e:

      terminated = p.terminate(force=True)
      elapsedTime = time.time() - startTime

      if verbose:
German Leon's avatar
German Leon committed
187
188
189
190
191
192
193
194
195
196
        print("(%d) %s; Timeout after %.2f ms." % (fiNumber, ti, elapsedTime) )
        sys.stdout.flush()
      if log:
        stdLogFile.write("(%d) %s; Timeout after %.2f ms.\n" % (fiNumber, ti, elapsedTime) )
        stdLogFile.write("%s\n" % (p.before) )
        stdLogFile.flush() 
      serie.write("(%d) %s; Timeout after %.2f ms.\n\r" % (fiNumber, ti, elapsedTime) )
      serie.flush()

#      sys.stdout.flush()
German Leon's avatar
German Leon committed
197
198
199
200
201
202
203
204
205
206
207
208
#      stdLogFile.close()

      numTimeouts += 1
      # Si se dan 3 timeouts seguidos, suponemoe que no se recuperara y rearrancamos la Jetson

      lastFile = open("lastFiNumber", "w")

      if (numTimeouts == 3): 
        fiNumber = fiNumber - 2 # contamos los tres ultimos timeouts consecutivos como un solo error no recuperable
        lastFile.write("%d" % fiNumber)
        lastFile.flush() 
        lastFile.close()
German Leon's avatar
German Leon committed
209
        time.sleep(1)
German Leon's avatar
German Leon committed
210
211
212
213
214
215
216
217
218
219
220
221
222
        sys.exit(3)
      else:
        lastFile.write("%d" % fiNumber)
        lastFile.flush() 
        lastFile.close()

    finally:

      lastFile = open("lastFiNumber", "w")
      lastFile.write("%d" % fiNumber)
      lastFile.flush() 
      lastFile.close()

German Leon's avatar
German Leon committed
223
#      time.sleep(1)
German Leon's avatar
German Leon committed
224
225

  # end while
German Leon's avatar
German Leon committed
226
227
  if log:
    stdLogFile.close()
German Leon's avatar
German Leon committed
228
229
230
231

#end runBenchmark

def main(argv):
German Leon's avatar
German Leon committed
232
  
German Leon's avatar
German Leon committed
233
234
235
  fileName = sys.argv[1]
  fiIni = int( sys.argv[2] )
  tIni = sys.argv[3]
German Leon's avatar
German Leon committed
236
#  w=watchdog.watchdog()
German Leon's avatar
German Leon committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#  if ( os.path.exists('lastFiNumber') ):
#    lastFile = open("lastFiNumber", "r")
#    fiIni = int( lastFile.read() ) + 1
#    lastFile.close()
#  else:
#    fiIni = 1

#  gzipFiles()  

  if inject:
    groupProfileLines()

#  x = datetime.datetime.now()
#  fileName =  bmName + x.strftime("_%H%M%S")

German Leon's avatar
German Leon committed
252
253
254
  runBenchmark(fiIni, fileName, tIni)


German Leon's avatar
German Leon committed
255
256
257
258
#  os.system("rm lastFiNumber")
#  os.system("mv /home/ubuntu/startup_script.sh /home/ubuntu/startup_script_noactivo.sh")


German Leon's avatar
German Leon committed
259
260
261
262
##############################################################################
#main()
if __name__ == "__main__":
  main(sys.argv)