SignalApp.py 4.08 KB
Newer Older
German Leon's avatar
German Leon committed
1
2
3
4
5
6
import time
from classes.Logging import Logging
from threading  import Thread
from random import uniform

import common_parameters as cp  # All common parameters will bet at common_parameters module
German Leon's avatar
German Leon committed
7
import os,signal
German Leon's avatar
German Leon committed
8
9
10
11
12
13
14
15
import sys

"""
Signal the app to stop so GDB can execute the script to flip a value
"""


class SignalApp(Thread):
German Leon's avatar
German Leon committed
16
17
    def __init__(self,file_connection, max_wait_time, log_path, unique_id, signals_to_send, init_sleep, syncro,waitfinish):
        global crashsystem,hang
German Leon's avatar
German Leon committed
18
19
        hang=False
        super(SignalApp, self).__init__()
German Leon's avatar
German Leon committed
20
        self.__file_connection = file_connection
German Leon's avatar
German Leon committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
        os.system("rm -f {}".format(log_path))
        self.__log = Logging(log_file=log_path, unique_id=unique_id)

        # Most of the benchmarks we cannot wait until the end of the processing
        # Considering most of 90% of the time
        #self.__init_wait_time = uniform(init_sleep, max_wait_time * cp.MAX_SIGNAL_BEFORE_ENDING)
        self.__init_wait_time = uniform(0, max_wait_time * cp.MAX_SIGNAL_BEFORE_ENDING)
        self.__signals_to_send = signals_to_send
        self.__time_to_sleep = (max_wait_time - self.__init_wait_time) / self.__signals_to_send
       # self.__time_to_sleep = (max_wait_time) / self.__signals_to_send
        self._syncro=syncro
        self._waitfinish=waitfinish
    def run(self):
        # Send a series of signal to make sure gdb will flip a value in one of the interrupt signals
German Leon's avatar
German Leon committed
35
36
        #log_string = "Sending a signal using command: {} after {}s and each {}s.".format(self.__signal_cmd, self.__init_wait_time,self.__time_to_sleep)
        log_string = "Sending a signal  each {}s of {} times.".format(self.__time_to_sleep,self.__signals_to_send )
German Leon's avatar
German Leon committed
37
        if cp.DEBUG:
German Leon's avatar
German Leon committed
38
            self.__log.info(log_string)
German Leon's avatar
German Leon committed
39
        crashsystem=False
German Leon's avatar
German Leon committed
40
41
42
43
        try:
           (self._syncro).wait()
        #except threading.BrokenBarrierError:
        except:	
German Leon's avatar
German Leon committed
44
          (self._syncro).abort()
German Leon's avatar
German Leon committed
45
46
47
          self.__log.info("Breakpoint inicial fuera de tiempo")
          #(self._waitfinish).wait()
          (self._syncro).reset()   
German Leon's avatar
German Leon committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
          crashsystem=True
          return 
          #self.__log.info("Timeout syncron of breakpoint\n")
          
        pidf=open(self.__file_connection,"r")
        pid=int(pidf.read())
        pidf.close()
        #os.remove(self.__file_connection)
        
        #os.system(self.__signal_cmd)  
        #pidf = (os.popen(self.__signal_cmd))
             
        #print("Comando {} y resultado {} de tipo {}".format(self.__signal_cmd,pid, type (pid)) ) 
        #pid=int(pid.split('\n')[0])
        #pidf.close()
        print(" resultado pid {} de tipo {}".format(pid, type (pid)) )  
German Leon's avatar
German Leon committed
64
65
66
        # Time random
        #print ("INIT:"+str(self.__init_wait_time)+"sleep"+str())
        time.sleep(self.__init_wait_time)	   
German Leon's avatar
German Leon committed
67
        crash=False
German Leon's avatar
German Leon committed
68
        for signals in range(0, self.__signals_to_send):
German Leon's avatar
German Leon committed
69
70
71
            #os.system("{} > /dev/null 2>/dev/null".format(self.__signal_cmd))
            try:
              os.kill(pid,signal.SIGINT)
German Leon's avatar
German Leon committed
72
              self.__log.info("sending signal {}".format(signals))
German Leon's avatar
German Leon committed
73
            except:
German Leon's avatar
German Leon committed
74
75
76
77
78
79
80
              self.__log.info("Process is dead. Crash?")  
              os.kill(pid,signal.SIGKILL)
              (self._waitfinish).abort()
              (self._waitfinish).reset()
              (self._syncro).abort()
              (self._syncro).reset()  
              crash=True
German Leon's avatar
German Leon committed
81
              break
German Leon's avatar
German Leon committed
82
            
German Leon's avatar
German Leon committed
83
84
85
86
            try:
               (self._syncro).wait()
            except:	
               (self._syncro).abort()
German Leon's avatar
German Leon committed
87
               #break
German Leon's avatar
German Leon committed
88
               #print("Breakpoint fuera de tiempo")
German Leon's avatar
German Leon committed
89
            (self._syncro).reset()   
German Leon's avatar
German Leon committed
90
            time.sleep(self.__time_to_sleep)
German Leon's avatar
German Leon committed
91
        #(self._syncro).reset() 
German Leon's avatar
German Leon committed
92
93
94
95
96
97
98
99
100
        if not crash:
         try:
           (self._waitfinish).wait()
          #except  threading.BrokenBarrierError:
         except:	
           (self._waitfinish).abort()
           self.__log.info("Hang timeout execution")
           hang=True
           self.__log.info("Timeout execution programa")  
German Leon's avatar
German Leon committed
101
102
103
104
105
106
        (self._waitfinish).reset()

    def ishang (self):
        return hang
    def get_int_wait_time(self):
        return self.__init_wait_time