Subversion Repositories shark

Rev

Rev 1255 | 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
 
40
  if (l->act_current == 0) l->act_current = 1;                                                                                                                            
41
  #ifdef TASK_OUTPUT
1262 giacomo 42
    #ifdef OS_SHARK
43
      sprintf(tmp,"[ONESHOT]");
44
      printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
45
    #endif
1254 giacomo 46
  #endif
47
 
48
  exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles;
49
 
50
  /* Execution delay */
51
  for (i=0;i<exec_cycles;i++)
52
    __asm__ __volatile__ ("xorl %%eax,%%eax\n\t"
53
                          "cpuid\n\t"
54
                          :::"eax","ebx","ecx","edx");
55
 
56
  return NULL;
57
 
58
}
59
 
60
/* CT: Cyclical Task:
61
   begin
62
     while (1) {
63
       - execution
64
       - end_cycle
65
     }
66
   end (never end)
67
*/
68
void *periodic_task(void *arg)
69
{
70
  long long i,exec_cycles = 0;
71
  int act = 0;
72
  struct loader_task *l = (struct loader_task *)(arg);
73
 
1262 giacomo 74
  #ifdef TASK_OUTPUT
75
    #ifdef OS_SHARK
76
      char tmp[20];
77
    #endif
78
  #endif
79
 
1254 giacomo 80
  if (l->act_current == 0) l->act_current = 1;
81
 
82
  while(1) {
83
 
84
    #ifdef TASK_OUTPUT
1262 giacomo 85
      #ifdef OS_SHARK
86
        sprintf(tmp,"C[%06d]",act);
87
        printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
88
      #endif
1254 giacomo 89
    #endif
90
 
91
    exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles;
92
 
93
    /* Execution delay */  
94
    for (i=0;i<exec_cycles;i++)
95
    __asm__ __volatile__ ("xorl %%eax,%%eax\n\t"
96
                          "cpuid\n\t"
97
                          :::"eax","ebx","ecx","edx");
98
 
99
    generic_task_endcycle();
100
 
101
    act++;
102
 
103
  }  
104
 
105
  return NULL;
106
 
107
}
108
 
109
/* BT: Background Task:
110
   begin
111
     while (1) {
112
       - execution
113
     }
114
   end (never end)
115
*/
116
void *back_task(void *arg)
117
{
118
  long long i,exec_cycles = 0;
119
  int act = 0;
120
  struct loader_task *l = (struct loader_task *)(arg);
1262 giacomo 121
  #ifdef TASK_OUTPUT
122
    #ifdef OS_SHARK
123
      char tmp[20];
124
    #endif
125
  #endif
1254 giacomo 126
 
127
  if (l->act_current == 0) l->act_current = 1;
128
 
129
  while(1) {
130
 
131
    #ifdef TASK_OUTPUT
1262 giacomo 132
      #ifdef OS_SHARK
133
        sprintf(tmp,"B[%06d]",act);
134
        printf_xy((get_current_exec_task() % 5) * 9 + 34,get_current_exec_task()  / 5 + 5, GREEN, tmp);
135
      #endif
1254 giacomo 136
    #endif
137
 
138
    exec_cycles = (long long)(TIMESPEC2USEC(&l->exec[l->act_current-1])) * CALIBRATION_DELTA / cal_cycles;
139
 
140
    /* Execution delay */
141
    for (i=0;i<exec_cycles;i++)
142
    __asm__ __volatile__ ("xorl %%eax,%%eax\n\t"
143
                          "cpuid\n\t"
144
                          :::"eax","ebx","ecx","edx");
145
 
146
    act++;
147
 
148
  }                                                                                                                
149
  return NULL;
150
 
151
}
152
 
153
/* Task create */
154
/* this function create the task struct in memory */
155
void loader_task_create()
156
{
157
 
158
  struct loader_task *current = loader_task_list;
159
  int i=0, k=0;
160
 
161
  while (k <total_loader_task) {
162
    k++;
163
 
164
    for (i=0; i < current->number; i++) {
165
 
166
      pthread_t j;
167
      int err = 0;
168
 
169
      switch(current->task_type)  {
170
      case PAR_TASK_OS:
171
        err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL,
172
              oneshot_task,(void *)current,generic_get_task_model(current));
173
        break;
174
      case PAR_TASK_BT:
175
        err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL,
176
              back_task,(void *)current,generic_get_task_model(current));
177
        break;
178
      case PAR_TASK_CT:
179
        err = generic_create_thread(generic_get_server_from_contract(current->contract),&j,NULL,
180
              periodic_task,(void *)current,generic_get_task_model(current));
181
        break;
182
      }
183
      if (err) {
184
        printf("Error fsf task creating\n");
185
        generic_end_simulation();
186
      }
187
 
188
    }
189
 
190
    current = &loader_task_list[k];
191
 
192
  }
193
 
194
  printf("Created %d loader tasks\n",k);
195
 
196
 
197
}
198
 
199
/* Main Function */
200
int start_environment()
201
{
202
 
203
  extern struct timespec total_time;
204
 
205
  /* Calibrate the exec time */  
206
  generic_calibrate_cycle();
207
 
208
  /* Create the servers usign defined contracts */
209
  generic_fsfinit();
210
 
211
  /* Create the tasks */
212
  loader_task_create();
213
 
214
  /* Start the simulation */
215
  generic_start_simulation();
216
 
217
  /* Set the simulation end time */
218
  generic_set_simulation_time(&total_time);
219
 
220
  return 0;
221
 
222
}