RecoSim  1.0
 All Classes Files Functions Variables Enumerations
random_bcet_wcet_sw.h
1 
13 #ifndef random_bcet_wcet_sw_MODULE_ALGORITHMS_FILE_H
14 #define random_bcet_wcet_sw_MODULE_ALGORITHMS_FILE_H
15 
16 #include "user_parameters.h"
17 
19 #include <time.h> /* time */
20 
21 My_Simulation_states current_time_state;
22 
23 /*****************************************************************************/
24 /************************** BEGIN USER SPACE ********************************/
25 
26 template<int Ni, int No>
27 void random_bcet_wcet_sw(User_algorithm_interface<Ni, No> &user_algo_interface) {
28  // Sw Default behavior
29  srand ((unsigned int)sc_time_stamp().to_double());
30  user_algo_interface.get_logfile() << sc_time_stamp() << "init srand" << endl;
31 
32 
33 
34  while(true) {
35 
36  /*******************************************************************************/
37  /**** MANDATORY PART (EXCEPT DISPLAY) ****/
38 
39 
40 
41 
42  /* WAIT FOR START ALGORITHM EVENT : Only for Dynamic tasks */
43  user_algo_interface.get_logfile() << sc_time_stamp() << ": " << user_algo_interface.get_name() << " Beginning of loop" << endl;
44  current_time_state = tasks_execution_time;
45  user_algo_interface.b_execution_requested();
46 
47 
48 
49  user_algo_interface.get_logfile() << sc_time_stamp() << ": " << user_algo_interface.get_name() << " Execution requested" << endl;
50 
51  /* TRACE : The algorithm is idle in order to have an accurate trace */
52  user_algo_interface.set_algorithm_idle();
53 
54  /* WAIT FOR NEXT PERIOD to begin to start algorithm execution. For non-periodic modules, function returns right away */
55  user_algo_interface.wait_until_next_period();
56 
57 
58 
59 
60 #ifdef GENERATE_LOG_FILE
61  user_algo_interface.get_logfile() << sc_time_stamp() << ": " << user_algo_interface.get_name() << " New period started" << endl;
62 #endif
63  user_algo_interface.set_algorithm_waiting();
64 
65  /**** BEGINNING OF DATA RECEPTION SEGMENT ****/
66 
67 
68 
69  user_algo_interface.b_all_data_received();
70 
71  user_algo_interface.set_algorithm_running();
72 
73  /**** END OF DATA RECEPTION SEGMENT ****/
74 
75  /**** BEGINNING OF USER ALGORITHM ****/
76 
77 
78  sc_time my_WCET=(user_algo_interface.get_WCET())*(CPU_FREQUENCIES_STEPS[0]/CPU_FREQUENCIES_STEPS[global_operating_point]);
79  sc_time my_BCET=(user_algo_interface.get_BCET())*(CPU_FREQUENCIES_STEPS[0]/CPU_FREQUENCIES_STEPS[global_operating_point]);
80 
81  int *data;
82  data = user_algo_interface.get_data_in_ptr(0);
83  if ((data[0]>>16)==0)
84  current_time_state = BCET;
85  if ((data[0]>>16)==1)
86  current_time_state = WCET;
87  if ((data[0]>>16)==2)
88  current_time_state = BCET;
89  if ((data[0]>>16)>2)
90  current_time_state = RANDOM;
91 
92  user_algo_interface.get_logfile() << sc_time_stamp() << ": " << user_algo_interface.get_name() << " DATA : " << (data[0]>>16) << endl;
93 
94 
95  double random = rand()%101;
96 
97  double random_result =(random/100.0)*(my_WCET.to_seconds()-my_BCET.to_seconds());
98 
99  sc_time random_execution_time = my_BCET;
100 
101 // if (user_algo_interface.is_algorithm_execution_mode(RANDOM_EXECUTION_TIME) == true)
102 
103  if (current_time_state == RANDOM)
104  random_execution_time = my_BCET + sc_time(random_result, SC_SEC);
105 
106  if (current_time_state == BCET)
107  random_execution_time = my_BCET;
108 
109  if (current_time_state == WCET)
110  random_execution_time = my_WCET;
111 
112 
113 
114  user_algo_interface.get_logfile() << sc_time_stamp() << ": " << user_algo_interface.get_name() << " Execution times : " << random_execution_time.to_seconds()*1000 << " ms : " << endl;
115 
116 
117  // Preemption points
118  for(int i = 0; i < user_algo_interface.get_nb_preemption_points(); i++) {
119  user_algo_interface.compute(random_execution_time/(user_algo_interface.get_nb_preemption_points() + 1));
120 
121  user_algo_interface.preemption_point();
122  user_algo_interface.set_algorithm_running();
123 
124  }
125 
126  // Wait for the last execution segment (after last preemption point)
127  user_algo_interface.compute(random_execution_time/(user_algo_interface.get_nb_preemption_points() + 1));
128 #ifdef GENERATE_LOG_FILE
129  user_algo_interface.get_logfile() << sc_time_stamp() << ": " << user_algo_interface.get_name() << " User algorithm ended" << endl;
130 #endif
131 
132 
133 
134 
135 
136 
137  /**** END OF USER ALGORITHM ****/
138 
139 
140  // Check that all data issued from previous algorithm execution has been processed and sent
141  user_algo_interface.b_all_data_sent();
142 
143  // When all data is sent, indicate that a new sequence starts now
144  // MUST BE DONE ONLY AFTER VERIFYING THAT ALL PREVIOUS DATA HAVE BEEN SENT
145  user_algo_interface.start_new_transaction_sequence();
146 
147  // Find a channel that is not transient and has updated data
148  int channel_id = 0;
149  while(user_algo_interface.is_channel_transient(channel_id) || !user_algo_interface.nb_data_received(channel_id)) channel_id++;
150 
151  // Copy data
152  int data_in_length = 0;
153  int data_out_length = 0;
154  for(int i = 0; i < No; i++) {
155  data_in_length = user_algo_interface.get_data_in_length(channel_id);
156  data_out_length = user_algo_interface.get_data_out_length(i);
157 
158  if(data_out_length <= data_in_length) {
159  memcpy(user_algo_interface.get_data_out_ptr(i), user_algo_interface.get_data_in_ptr(channel_id), data_out_length);
160  } else {
161  memcpy(user_algo_interface.get_data_out_ptr(i), user_algo_interface.get_data_in_ptr(channel_id), data_in_length);
162  for(int j = data_in_length; j < data_out_length; j+=4) user_algo_interface.get_data_out_ptr(i)[j/4] = 0;
163  }
164 
165  user_algo_interface.set_address_out(i, user_algo_interface.get_address_in(channel_id));
166  }
167 
168  // Release input sockets (we won't need their data anymore) for they are used by preceding modules again
169  user_algo_interface.release_all_input_sockets();
170 
171  // Notify RZ to continue processing
172  user_algo_interface.end_of_algorithm();
173 
174  // Send data to all sockets
175  user_algo_interface.nb_send_all_data();
176 
177 
178  // Wait for the event notifying a possible change in the user algorithm thread
179  user_algo_interface.wait_for_update_user_algorithm();
180 
181  user_algo_interface.get_logfile() << sc_time_stamp() << ": " << user_algo_interface.get_name() << " Update user algorithm event received" << endl;
182 
183  if(user_algo_interface.kill_user_algorithm()) {
184 #ifdef GENERATE_LOG_FILE
185  user_algo_interface.get_logfile() << user_algo_interface.get_name() << " User algorithm thread has to be killed!" << endl;
186 #endif
187  return;
188  }
189  }
190 
191 }
192 
193 /************************** END USER SPACE ********************************/
194 /***************************************************************************/
195 
196 #endif
virtual ofstream & get_logfile(void)=0
Get logfile.
virtual bool & kill_user_algorithm(void)=0
Get a reference to a boolean indicating whether the user algorithm should be killed or not (for insta...
virtual void start_new_transaction_sequence(int id)=0
Indicate that a new transaction sequence is beginning (i.e. sending new data) for a particular socket...
virtual int * get_data_in_ptr(int socket)=0
Get pointer to incoming data from socket 'socket'.
virtual void set_address_out(int socket, sc_dt::uint64 value)=0
Set address for outgoing transaction on socket 'socket'.
virtual bool nb_data_received(string name)=0
Check if the data from channel 'name' have been received yet. Non-blocking function.
virtual int get_nb_preemption_points(void)=0
Get the number of preemption points that should be inserted in the algorithm.
virtual void b_all_data_received(void)=0
Check if the data from all channels have been received yet. Blocking function: if the data is not ava...
virtual void preemption_point(void)=0
Function to call to emulate preemption.
virtual sc_time get_WCET(void) const =0
Get task worst case execution time (WCET), depending on current implementation.
virtual int get_data_out_length(int socket) const =0
Get output socket data length.
virtual void compute(sc_time duration)=0
Emulate an execution time of the algorithm.
virtual void release_all_input_sockets(void)=0
Release all input socket so that preceding modules might send some more data.
virtual void set_algorithm_waiting(void)=0
Function to call when the algorithm is waiting for data in order to have an accurate trace...
virtual int get_data_in_length(int socket) const =0
Get input socket data length.
virtual const char * get_name(void) const =0
Get task name.
virtual void b_execution_requested(void)=0
Wait until an algorithm execution has been requested/granted by the manager.
Definition: user_algorithm_interface.h:35
virtual bool is_channel_transient(string name)=0
Check if channel 'name' is transient or not.
virtual void end_of_algorithm(void)=0
Function to call at the end of algorithm.
virtual int * get_data_out_ptr(int socket)=0
Get pointer to outgoing data from socket 'socket'.
virtual void set_algorithm_running(void)=0
Function to call when the algorithm is running in order to have an accurate trace.
virtual void set_algorithm_idle(void)=0
Function to call when the algorithm is idle in order to have an accurate trace.
virtual void wait_until_next_period(void)=0
Wait until the next period is started. For non-periodic tasks, return immediately.
virtual sc_dt::uint64 get_address_in(int socket) const =0
Retrieve address from the incoming transaction on socket 'socket'.
virtual void b_all_data_sent(void)=0
Check if data has been sent on every socket. Blocking function: if data send is not finished yet...
virtual void nb_send_all_data(void)=0
Send data towards all channels. Non-blocking function.
virtual sc_time get_BCET(void) const =0
Get task best case execution time (BCET), depending on current implementation.