forked from ik2sb/PICS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_clock.py
executable file
·184 lines (135 loc) · 6 KB
/
sim_clock.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
import sys
import time
import os
import random
import logging
import sim_lib_common as clib
from threading import Condition
import sim_global_variables as simgval
Global_Wall_Clock_CV = Condition()
Global_Wall_Clock = 0
gQ_OUT = None
g_my_block_id = None
g_log_handler = None
def set_log (path):
logger = logging.getLogger('sim_clock')
log_file = path + '/[' + str(g_my_block_id) + ']-sim_clock.log'
hdlr = logging.FileHandler(log_file)
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
# for stdout...
ch = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.setLevel(logging.INFO)
return logger
def increase_Wall_Clock (tick):
global Global_Wall_Clock
global Global_Wall_Clock_CV
Global_Wall_Clock_CV.acquire()
Global_Wall_Clock += tick
Global_Wall_Clock_CV.notify()
Global_Wall_Clock_CV.release()
def sim_clock_evt_sim_start_processing (q_msg):
evt_code = q_msg[4]
if evt_code != simgval.gEVT_SIM_START:
g_log_handler.error("[SimClock] %s sim_clock_evt_sim_start_processing EVT_CODE (%s) Error! => %s" %(str(Global_Wall_Clock), simgval.get_sim_code_str(evt_code), q_msg))
clib.sim_exit()
g_log_handler.info("[SimClock] %s Simulation Start..." % (Global_Wall_Clock))
# send msg to sim_evt_handler ==> Wake up event for simulation event handler
q_evt = clib.make_queue_event (Global_Wall_Clock, g_my_block_id, simgval.gBLOCK_SIM_EVENT_HANDLER, simgval.gEVT_SET_CLOCK, None, None)
gQ_OUT.put(q_evt)
# update global wall clock
# increase_Wall_Clock (1)
def sim_clock_evt_sim_end_processing (q_msg):
evt_code = q_msg[4]
if evt_code != simgval.gEVT_SIM_END:
g_log_handler.error("[SimClock] %s sim_clock_evt_sim_end_processing EVT_CODE (%s) Error! => %s" %(str(Global_Wall_Clock), simgval.get_sim_code_str(evt_code), q_msg))
clib.sim_exit()
g_log_handler.info("[SimClock] %s EVT_RECV: %s" % (str(Global_Wall_Clock), simgval.get_sim_code_str(evt_code)))
# simulation complete event
sim_end_evt = clib.make_queue_event (Global_Wall_Clock, g_my_block_id, simgval.gBLOCK_SIM_EVENT_HANDLER, simgval.gEVT_SIM_END, None, None)
gQ_OUT.put(sim_end_evt)
def sim_clock_evt_set_clock_ack_processing (q_msg):
evt_clock = q_msg[1]
evt_code = q_msg[4]
evt_sub_code = q_msg[5]
evt_data = q_msg[6]
if evt_code != simgval.gEVT_SET_CLOCK_ACK:
g_log_handler.error("[SimClock] %s sim_clock_evt_sim_ack_processing EVT_CODE (%s) Error! => %s" %(str(Global_Wall_Clock), simgval.get_sim_code_str(evt_code), q_msg))
clib.sim_exit()
if Global_Wall_Clock == (evt_clock):
#time.sleep(1)
print "[SimClock] Simulation Clock OK: SIM_TIMER:%ds, SIM_EVENT_HANDLER:%ds" % (Global_Wall_Clock, evt_clock)
# increase wall clock
tick = evt_sub_code
increase_Wall_Clock (tick)
q_evt = clib.make_queue_event (Global_Wall_Clock, g_my_block_id, simgval.gBLOCK_SIM_EVENT_HANDLER, simgval.gEVT_SET_CLOCK, tick, None)
gQ_OUT.put(q_evt)
else:
g_log_handler.error("[SimClock] Simulation Clock Mismatch: SIM_TIMER:%ds, SIM_EVENT_HANDLER:%ds" \
% (Global_Wall_Clock, evt_clock))
clib.sim_exit()
def sim_clock_event_processing (q_msg):
ret_val = False
evt_code = q_msg[4]
if evt_code == simgval.gEVT_SIM_START:
# processing simulation start event
sim_clock_evt_sim_start_processing (q_msg)
elif evt_code == simgval.gEVT_SIM_END:
# processing simulation end event
sim_clock_evt_sim_end_processing (q_msg)
ret_val = True
elif evt_code == simgval.gEVT_SET_CLOCK_ACK:
# clock ack (from event handler) processing
sim_clock_evt_set_clock_ack_processing (q_msg)
else:
g_log_handler.error("[SimClock] %s EVT_CODE (%s) Error! => %s" % (str(Global_Wall_Clock), simgval.get_sim_code_str(evt_code), q_msg))
clib.sim_exit()
return ret_val
def th_sim_clock (my_block_id, conf_obj, q_in, q_evt_handler):
global g_my_block_id
global g_log_handler
global gQ_OUT
if my_block_id != simgval.gBLOCK_SIM_CLOCK:
print "[SimClock] SimClock Block ID (I:%d, G:%d) Error!!!" % (my_block_id, simgval.gBLOCK_SIM_CLOCK)
clib.sim_exit()
else:
g_my_block_id = my_block_id
gQ_OUT = q_evt_handler
time.sleep(random.random())
log = set_log (conf_obj.log_path)
log.info("[SimClock] %s Start Sim Clock Thread!..." % (Global_Wall_Clock))
g_log_handler = log
while True:
q_message = q_in.get()
q_in.task_done()
# ------------------------
# EVENT MESSAGE
# ------------------------
# EVENT_SRC = Event Message Sender
# EVENT_DST = Event Message Receiver
# EVENT_CODE = Event Code
# EVENT_SUB_CODE = Event Sub Code
# EVENT_DATA = Event DaTa
# ------------------------
evt_src = q_message[2]
evt_dst = q_message[3]
if evt_dst != my_block_id:
print "[SimClock] %s EVT_MSG ERROR! (Wrong EVT_DST) - " % (str(Global_Wall_Clock)), q_message
log.error(str(Global_Wall_Clock) + ' EVT_MSG ERROR! (Wrong EVT_DST) - %s' % str(q_message))
clib.sim_exit()
continue
if evt_src != simgval.gBLOCK_MAIN and evt_src != simgval.gBLOCK_SIM_EVENT_HANDLER:
print "[SimClock] %s EVT_MSG ERROR! (Wrong EVT_SRC) - " % (str(Global_Wall_Clock)), q_message
log.error(str(Global_Wall_Clock) + ' EVT_MSG ERROR! (Wrong EVT_SRC) - %s' % str(q_message))
clib.sim_exit()
continue
# EVENT_CODE PROCESSING
term_flag = sim_clock_event_processing (q_message)
if term_flag is True:
print "[SimClock] %s Last Sim Clock: %s" % (str(Global_Wall_Clock), str(Global_Wall_Clock))
break
print "[SimClock] %s Sim Clock Thread Terminated." % (str(Global_Wall_Clock))