Subversion Repositories shark

Rev

Rev 1252 | Details | Compare with Previous | Last modification | View Log | RSS feed

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