Subversion Repositories shark

Rev

Rev 1266 | Rev 1538 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1254 giacomo 1
/* FSF Loader
2
 *
3
 * Load and run a specific set of tasks/contracts
4
 *
5
 * This is the system indipendent part
6
 *
7
 * Giacomo Guidi <giacomo@gandalf.sssup.it>
8
 * Michael Timarchi <trimarchi@gandalf.sssup.it>
9
 *
10
 */
11
 
12
#include "fsf_contract.h" //Framework main header
13
#include "calibrate.h"
14
#include "func.h" //Generic function definitions
1255 giacomo 15
#include "lconst.h"
1254 giacomo 16
 
17
/* Activate task output debug */
18
#define TASK_OUTPUT
19
 
1255 giacomo 20
int cal_cycles = CALIBRATION_RESULT; //Calibration const, it converts usec to cycles
1254 giacomo 21
struct timespec zero_time; //Zero time of the simulation                                        
22
extern struct loader_task loader_task_list[]; //Loader task array
23
extern int total_loader_task; //Loader task number
24
 
25
/* OS: Oneshot Task:
26
   begin
27
    - execution
28
   end
29
*/
30
void *oneshot_task(void *arg)
31
{
32
  long long i,exec_cycles = 0;
33
  struct loader_task *l = (struct loader_task *)(arg);
1262 giacomo 34
  #ifdef TASK_OUTPUT
35
    #ifdef OS_SHARK
36
      char tmp[20];
37
    #endif
38
  #endif
1254 giacomo 39
 
1264 giacomo 40
  start_oneshot_task();
41
 
42
  /* to avoid problem if the task start inside the create function */
1254 giacomo 43
  if (l->act_current == 0) l->act_current = 1;                                                                                                                            
44
  #ifdef TASK_OUTPUT
1262 giacomo 45
    #ifdef OS_SHARK
46
      sprintf(tmp,"[ONESHOT]");
47
      printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
48
    #endif
1254 giacomo 49
  #endif
50
 
51
  exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles;
52
 
53
  /* Execution delay */
54
  for (i=0;i<exec_cycles;i++)
55
    __asm__ __volatile__ ("xorl %%eax,%%eax\n\t"
56
                          "cpuid\n\t"
57
                          :::"eax","ebx","ecx","edx");
58
 
1264 giacomo 59
  end_oneshot_task();
1266 giacomo 60
 
61
  #ifdef TASK_OUTPUT
62
    #ifdef OS_SHARK
63
      sprintf(tmp,"[--END--]");
64
      printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
65
    #endif
66
  #endif
1264 giacomo 67
 
1254 giacomo 68
  return NULL;
69
 
70
}
71
 
72
/* CT: Cyclical Task:
73
   begin
74
     while (1) {
75
       - execution
76
       - end_cycle
77
     }
78
   end (never end)
79
*/
80
void *periodic_task(void *arg)
81
{
1304 giacomo 82
  long long i,exec_cycles = 0,block_cycles = 0;
1254 giacomo 83
  int act = 0;
84
  struct loader_task *l = (struct loader_task *)(arg);
85
 
1262 giacomo 86
  #ifdef TASK_OUTPUT
87
    #ifdef OS_SHARK
88
      char tmp[20];
89
    #endif
90
  #endif
91
 
1264 giacomo 92
  start_periodic_task();
93
 
1254 giacomo 94
  if (l->act_current == 0) l->act_current = 1;
95
 
96
  while(1) {
97
 
1264 giacomo 98
    start_job_periodic_task();
99
 
1254 giacomo 100
    #ifdef TASK_OUTPUT
1262 giacomo 101
      #ifdef OS_SHARK
102
        sprintf(tmp,"C[%06d]",act);
103
        printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
104
      #endif
1254 giacomo 105
    #endif
106
 
107
    exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles;
1304 giacomo 108
    block_cycles = (long long)(TIMESPEC2USEC(&l->block[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles;  
109
 
1254 giacomo 110
    /* Execution delay */  
111
    for (i=0;i<exec_cycles;i++)
112
    __asm__ __volatile__ ("xorl %%eax,%%eax\n\t"
113
                          "cpuid\n\t"
114
                          :::"eax","ebx","ecx","edx");
1304 giacomo 115
    if (l->muxstatus == 2) {
116
 
117
      #ifdef TASK_OUTPUT
118
        #ifdef OS_SHARK
119
          sprintf(tmp,"C[LOCK%02d]",l->resource);
120
          printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, RED, tmp);
121
        #endif
122
      #endif
123
 
124
      generic_lock_mutex(l->resource);
125
      for (i=0;i<block_cycles;i++)
126
      __asm__ __volatile__ ("xorl %%eax,%%eax\n\t"
127
                            "cpuid\n\t"
128
                            :::"eax","ebx","ecx","edx");
129
      generic_unlock_mutex(l->resource);
130
 
131
      #ifdef TASK_OUTPUT
132
        #ifdef OS_SHARK
133
          sprintf(tmp,"C[FREE%02d]",l->resource);
134
          printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
135
        #endif
136
      #endif
137
 
138
    }    
139
 
1264 giacomo 140
    end_job_periodic_task();
141
 
1254 giacomo 142
    generic_task_endcycle();
143
 
144
    act++;
145
 
146
  }  
1264 giacomo 147
 
148
  end_periodic_task();
1266 giacomo 149
 
150
  #ifdef TASK_OUTPUT
151
    #ifdef OS_SHARK
152
      sprintf(tmp,"[--END--]");
153
      printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
154
    #endif
155
  #endif
1254 giacomo 156
 
157
  return NULL;
158
 
159
}
160
 
161
/* BT: Background Task:
162
   begin
163
     while (1) {
164
       - execution
165
     }
166
   end (never end)
167
*/
168
void *back_task(void *arg)
169
{
1304 giacomo 170
  long long i,exec_cycles = 0,block_cycles = 0;
1254 giacomo 171
  int act = 0;
172
  struct loader_task *l = (struct loader_task *)(arg);
1262 giacomo 173
  #ifdef TASK_OUTPUT
174
    #ifdef OS_SHARK
175
      char tmp[20];
176
    #endif
177
  #endif
1254 giacomo 178
 
1264 giacomo 179
  start_back_task();
180
 
1254 giacomo 181
  if (l->act_current == 0) l->act_current = 1;
182
 
183
  while(1) {
184
 
1264 giacomo 185
    start_job_back_task();
186
 
1254 giacomo 187
    #ifdef TASK_OUTPUT
1262 giacomo 188
      #ifdef OS_SHARK
189
        sprintf(tmp,"B[%06d]",act);
190
        printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
191
      #endif
1254 giacomo 192
    #endif
193
 
194
    exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles;
1304 giacomo 195
    block_cycles = (long long)(TIMESPEC2USEC(&l->block[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles;  
196
 
1254 giacomo 197
    /* Execution delay */
198
    for (i=0;i<exec_cycles;i++)
199
    __asm__ __volatile__ ("xorl %%eax,%%eax\n\t"
200
                          "cpuid\n\t"
201
                          :::"eax","ebx","ecx","edx");
1304 giacomo 202
    if (l->muxstatus == 2) {
203
 
204
      #ifdef TASK_OUTPUT
205
        #ifdef OS_SHARK
206
          sprintf(tmp,"B[LOCK%02d]",l->resource);
207
          printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, RED, tmp);
208
        #endif
209
      #endif
210
 
211
      generic_lock_mutex(l->resource);
212
      for (i=0;i<block_cycles;i++)
213
      __asm__ __volatile__ ("xorl %%eax,%%eax\n\t"
214
                            "cpuid\n\t"
215
                            :::"eax","ebx","ecx","edx");
216
      generic_unlock_mutex(l->resource);
217
 
218
      #ifdef TASK_OUTPUT
219
        #ifdef OS_SHARK
220
          sprintf(tmp,"C[FREE%02d]",l->resource);
221
          printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
222
        #endif
223
      #endif
224
 
225
    }    
226
 
1264 giacomo 227
    end_job_back_task();
228
 
1254 giacomo 229
    act++;
230
 
1264 giacomo 231
  }
232
 
233
  end_back_task();
1266 giacomo 234
 
235
  #ifdef TASK_OUTPUT
236
    #ifdef OS_SHARK
237
      sprintf(tmp,"[--END--]");
238
      printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
239
    #endif
240
  #endif
241
 
1254 giacomo 242
  return NULL;
243
 
244
}
245
 
246
/* Task create */
247
/* this function create the task struct in memory */
248
void loader_task_create()
249
{
250
 
251
  struct loader_task *current = loader_task_list;
252
  int i=0, k=0;
253
 
254
  while (k <total_loader_task) {
255
    k++;
256
 
257
    for (i=0; i < current->number; i++) {
258
 
259
      pthread_t j;
260
      int err = 0;
261
 
262
      switch(current->task_type)  {
263
      case PAR_TASK_OS:
264
        err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL,
265
              oneshot_task,(void *)current,generic_get_task_model(current));
266
        break;
267
      case PAR_TASK_BT:
268
        err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL,
269
              back_task,(void *)current,generic_get_task_model(current));
270
        break;
271
      case PAR_TASK_CT:
272
        err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL,
273
              periodic_task,(void *)current,generic_get_task_model(current));
274
        break;
275
      }
276
      if (err) {
277
        printf("Error fsf task creating\n");
278
        generic_end_simulation();
279
      }
280
 
281
    }
282
 
283
    current = &loader_task_list[k];
284
 
285
  }
286
 
287
  printf("Created %d loader tasks\n",k);
288
 
289
 
290
}
291
 
292
/* Main Function */
293
int start_environment()
294
{
295
 
296
  extern struct timespec total_time;
297
 
298
  /* Calibrate the exec time */  
299
  generic_calibrate_cycle();
300
 
301
  /* Create the servers usign defined contracts */
302
  generic_fsfinit();
303
 
304
  /* Create the tasks */
305
  loader_task_create();
306
 
307
  /* Start the simulation */
308
  generic_start_simulation();
309
 
310
  /* Set the simulation end time */
311
  generic_set_simulation_time(&total_time);
312
 
313
  return 0;
314
 
315
}