Subversion Repositories shark

Rev

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

Rev Author Line No. Line
221 giacomo 1
//=====================================================================
2
//       FFFFFFIII   RRRRR      SSTTTTTTT
3
//      FF         IIR   RR    SS
4
//     FF           IR        SS
5
//    FFFFFF         RRRR    SSSSST      
6
//   FF       FI       RRR  SS
7
//  FF         II     RRR  SS
8
// FF           IIIIIR    RS 
9
//       
10
// Basic FSF(FIRST Scheduling Framework) contract management
11
// S.Ha.R.K. Implementation
12
//=====================================================================
13
 
14
#include "fsf_contract.h"
241 giacomo 15
#include "fsf_server.h"
221 giacomo 16
 
17
#include <pthread.h>
18
#include <stdlib.h>
19
 
224 giacomo 20
//#define FSF_DEBUG
221 giacomo 21
 
241 giacomo 22
int fsf_server_level;
221 giacomo 23
 
241 giacomo 24
int FSF_register_module(int server_level)
224 giacomo 25
{
26
 
27
  printk("FSF Module\n");
28
 
241 giacomo 29
  fsf_server_level = server_level;
224 giacomo 30
 
31
  return 0;
32
 
33
}
34
 
221 giacomo 35
/* Convert the contract specification to
36
 * budget parameters
37
 */
241 giacomo 38
int set_SERVER_budget_from_contract
221 giacomo 39
  (const fsf_contract_parameters_t *contract,
40
   int *budget)
41
{
42
 
43
  int local_scheduler_level = 0;
44
 
45
   switch (contract->local_scheduler_id) {
46
     case FSF_SCHEDULER_POSIX:
241 giacomo 47
       local_scheduler_level = POSIXSTAR_register_level(fsf_server_level,5000,32);
221 giacomo 48
       break;
49
     case FSF_SCHEDULER_EDF:
241 giacomo 50
       local_scheduler_level = EDFSTAR_register_level(fsf_server_level);
221 giacomo 51
       break;
52
     case FSF_SCHEDULER_RM:
241 giacomo 53
       local_scheduler_level = RMSTAR_register_level(fsf_server_level);
221 giacomo 54
       break;
55
   }    
56
 
241 giacomo 57
  *budget = SERVER_setbudget(fsf_server_level,
221 giacomo 58
                              TIMESPEC2USEC(&(contract->budget_min)),
59
                              TIMESPEC2USEC(&(contract->period_max)),
60
                              local_scheduler_level,contract->local_scheduler_id);
61
 
62
  return 0;
63
 
64
}
65
 
241 giacomo 66
int adjust_SERVER_budget_from_contract
221 giacomo 67
  (const fsf_contract_parameters_t *contract,
68
   int budget)
69
{
70
 
241 giacomo 71
  SERVER_adjust_budget(fsf_server_level,
72
                       TIMESPEC2USEC(&(contract->budget_min)),
73
                       TIMESPEC2USEC(&(contract->period_max)),
74
                       budget);
221 giacomo 75
 
76
  return 0;
77
 
78
}
79
 
80
/* Admission Test function */
81
int add_contract(const fsf_contract_parameters_t *contract)
82
{
83
 
84
  return 0;
85
 
86
}
87
 
88
int link_contract_to_server(const fsf_contract_parameters_t *contract,
89
                            fsf_server_id_t server)
90
{
91
 
92
  return 0;
93
 
94
}
95
 
96
int remove_contract(fsf_server_id_t server)
97
{
98
 
99
  return 0;
100
 
101
}
102
 
103
 
104
int fsf_negotiate_contract
105
  (const fsf_contract_parameters_t *contract,
106
   fsf_server_id_t                 *server)
107
{
108
 
109
  /* Check if contract is initialized */
110
  if (!contract) return FSF_ERR_NOT_INITIALIZED;
111
 
112
  /* Admission Test */
113
  if (FSF_ADMISSION_TEST_IS_ENABLED)
114
    if (add_contract(contract))
115
      return FSF_ERR_CONTRACT_REJECTED;
116
 
241 giacomo 117
  /* SERVER => BUDGET */    
118
  set_SERVER_budget_from_contract(contract,server);
221 giacomo 119
 
120
  #ifdef FSF_DEBUG
121
    kern_printf("(New Server %d)",*server);
122
  #endif
123
 
124
  if (*server >= 0)
125
    link_contract_to_server(contract,*server);
126
  else
127
    return FSF_ERR_CREATE_SERVER;
128
 
129
  return 0;
130
 
131
}
132
 
241 giacomo 133
int fsf_create_thread
134
  (fsf_server_id_t    server,
135
   pthread_t         *thread,
136
   pthread_attr_t    *attr,
137
   fsf_thread_code_t  thread_code,
138
   void              *arg,
139
   void              *local_scheduler_arg)
221 giacomo 140
{
141
 
241 giacomo 142
  NRT_TASK_MODEL nrt;
221 giacomo 143
  int local_scheduler_level,scheduler_id;
144
 
145
  /* Move thread from the posix module to local scheduler */
146
 
147
  #ifdef FSF_DEBUG 
241 giacomo 148
    kern_printf("(FSF:Insert thread = %d to Server = %d)",thread,server);
221 giacomo 149
  #endif
150
 
151
  /* Check if server and thread exsist */
241 giacomo 152
  if (server == NIL)
153
    return FSF_ERR_INVALID_SERVER;
221 giacomo 154
 
241 giacomo 155
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server);
156
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server);
221 giacomo 157
 
158
  /* Check if thread is already bind */
159
  switch (scheduler_id) {
160
    case FSF_SCHEDULER_POSIX:
161
 
250 giacomo 162
      if (local_scheduler_arg == NULL) {
163
        nrt_task_default_model(nrt);
164
        nrt_task_def_save_arrivals(nrt);
165
        nrt_task_def_arg(nrt,arg);
166
        nrt_task_def_ctrl_jet(nrt);
167
        nrt_task_def_level(nrt,local_scheduler_level);
221 giacomo 168
 
250 giacomo 169
        *thread = task_create("POSIXSTAR", thread_code, &nrt, NULL);
170
      }
241 giacomo 171
      if (*thread == NIL)
172
        return FSF_ERR_CREATE_THREAD;
221 giacomo 173
 
241 giacomo 174
      POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
234 giacomo 175
 
241 giacomo 176
      task_activate(*thread);
221 giacomo 177
 
178
    break;
179
    case FSF_SCHEDULER_EDF:
180
 
250 giacomo 181
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
182
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
183
 
184
      *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg, NULL);
185
      if (*thread == NIL)
186
        return FSF_ERR_CREATE_THREAD;
187
 
188
      EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
189
 
190
      task_activate(*thread);
191
 
221 giacomo 192
      break;
235 giacomo 193
 
221 giacomo 194
    case FSF_SCHEDULER_RM:
235 giacomo 195
 
221 giacomo 196
    default:
241 giacomo 197
      return FSF_ERR_INVALID_SERVER;
221 giacomo 198
      break;
199
  }
200
 
201
  return 0;
202
 
203
}
204
 
205
int fsf_get_server
206
  (fsf_server_id_t *server,
207
   pthread_t       thread)
208
{
209
  int local_scheduler_level, scheduler_id;
210
 
241 giacomo 211
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
212
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
221 giacomo 213
 
214
  switch (scheduler_id) {
215
    case FSF_SCHEDULER_POSIX:  
216
      return POSIXSTAR_getbudget(local_scheduler_level,thread);
217
    case FSF_SCHEDULER_EDF:
235 giacomo 218
      return EDFSTAR_getbudget(local_scheduler_level,thread);
221 giacomo 219
    case FSF_SCHEDULER_RM:
235 giacomo 220
      return RMSTAR_getbudget(local_scheduler_level,thread);
221
    default:
221 giacomo 222
      return -1;
223
  }
224
 
225
  return -1;
226
 
227
}
228
 
229
int fsf_cancel_contract
230
  (fsf_server_id_t *server)
231
{
232
 
233
  int local_scheduler_level, scheduler_id;
234
 
235
  #ifdef FSF_DEBUG
236
    kern_printf("(Remove server %d)",*server);
237
  #endif
238
 
239
  /* Check server id */
240
  if (*server < 0)
241
    return FSF_ERR_INVALID_SERVER;
242
 
241 giacomo 243
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,*server);
244
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,*server);
221 giacomo 245
 
246
  switch (scheduler_id) {
247
    case FSF_SCHEDULER_POSIX:
248
 
249
      /* Check if some thread use the server */
250
      if(POSIXSTAR_budget_has_thread(local_scheduler_level,*server))
251
        return FSF_ERR_SERVER_USED;
252
 
253
      break;
254
    case FSF_SCHEDULER_EDF:
235 giacomo 255
      /* Check if some thread use the server */
256
      if(EDFSTAR_budget_has_thread(local_scheduler_level,*server))
257
        return FSF_ERR_SERVER_USED;
258
      break;
259
 
221 giacomo 260
    case FSF_SCHEDULER_RM:
235 giacomo 261
      /* Check if some thread use the server */
262
      if(RMSTAR_budget_has_thread(local_scheduler_level,*server))
263
        return FSF_ERR_SERVER_USED;
264
 
221 giacomo 265
      break;
266
  }
267
 
241 giacomo 268
  SERVER_removebudget(fsf_server_level,*server);
221 giacomo 269
 
270
  level_free_descriptor(local_scheduler_level);
271
 
272
  remove_contract(*server);
273
 
274
  *server = -1;
275
 
276
  return 0;
277
 
278
}
279
 
280
int fsf_renegotiate_contract
281
  (const fsf_contract_parameters_t *new_contract,
282
   fsf_server_id_t server)
283
{
284
 
285
  #ifdef FSF_DEBUG
286
    kern_printf("(Renegotiate for server %d)",server);
287
  #endif
288
 
289
  if (!new_contract)
290
    return FSF_ERR_NOT_INITIALIZED;
291
 
292
  if (server < 0)
293
    return FSF_ERR_INVALID_SERVER;
294
 
241 giacomo 295
  return adjust_SERVER_budget_from_contract(new_contract,server);
221 giacomo 296
 
297
}
241 giacomo 298