Subversion Repositories shark

Rev

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