Subversion Repositories shark

Rev

Rev 235 | Rev 250 | 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
 
241 giacomo 162
      nrt_task_default_model(nrt);
163
      nrt_task_def_save_arrivals(nrt);
164
      nrt_task_def_arg(nrt,arg);
165
      nrt_task_def_ctrl_jet(nrt);
166
      nrt_task_def_level(nrt,local_scheduler_level);
221 giacomo 167
 
241 giacomo 168
      *thread = task_create("POSIXSTAR", thread_code, &nrt, NULL);
169
      if (*thread == NIL)
170
        return FSF_ERR_CREATE_THREAD;
221 giacomo 171
 
241 giacomo 172
      POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
234 giacomo 173
 
241 giacomo 174
      task_activate(*thread);
221 giacomo 175
 
176
    break;
177
    case FSF_SCHEDULER_EDF:
178
 
179
      break;
235 giacomo 180
 
221 giacomo 181
    case FSF_SCHEDULER_RM:
235 giacomo 182
 
221 giacomo 183
    default:
241 giacomo 184
      return FSF_ERR_INVALID_SERVER;
221 giacomo 185
      break;
186
  }
187
 
188
  return 0;
189
 
190
}
191
 
192
int fsf_get_server
193
  (fsf_server_id_t *server,
194
   pthread_t       thread)
195
{
196
  int local_scheduler_level, scheduler_id;
197
 
241 giacomo 198
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
199
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
221 giacomo 200
 
201
  switch (scheduler_id) {
202
    case FSF_SCHEDULER_POSIX:  
203
      return POSIXSTAR_getbudget(local_scheduler_level,thread);
204
    case FSF_SCHEDULER_EDF:
235 giacomo 205
      return EDFSTAR_getbudget(local_scheduler_level,thread);
221 giacomo 206
    case FSF_SCHEDULER_RM:
235 giacomo 207
      return RMSTAR_getbudget(local_scheduler_level,thread);
208
    default:
221 giacomo 209
      return -1;
210
  }
211
 
212
  return -1;
213
 
214
}
215
 
216
int fsf_cancel_contract
217
  (fsf_server_id_t *server)
218
{
219
 
220
  int local_scheduler_level, scheduler_id;
221
 
222
  #ifdef FSF_DEBUG
223
    kern_printf("(Remove server %d)",*server);
224
  #endif
225
 
226
  /* Check server id */
227
  if (*server < 0)
228
    return FSF_ERR_INVALID_SERVER;
229
 
241 giacomo 230
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,*server);
231
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,*server);
221 giacomo 232
 
233
  switch (scheduler_id) {
234
    case FSF_SCHEDULER_POSIX:
235
 
236
      /* Check if some thread use the server */
237
      if(POSIXSTAR_budget_has_thread(local_scheduler_level,*server))
238
        return FSF_ERR_SERVER_USED;
239
 
240
      break;
241
    case FSF_SCHEDULER_EDF:
235 giacomo 242
      /* Check if some thread use the server */
243
      if(EDFSTAR_budget_has_thread(local_scheduler_level,*server))
244
        return FSF_ERR_SERVER_USED;
245
      break;
246
 
221 giacomo 247
    case FSF_SCHEDULER_RM:
235 giacomo 248
      /* Check if some thread use the server */
249
      if(RMSTAR_budget_has_thread(local_scheduler_level,*server))
250
        return FSF_ERR_SERVER_USED;
251
 
221 giacomo 252
      break;
253
  }
254
 
241 giacomo 255
  SERVER_removebudget(fsf_server_level,*server);
221 giacomo 256
 
257
  level_free_descriptor(local_scheduler_level);
258
 
259
  remove_contract(*server);
260
 
261
  *server = -1;
262
 
263
  return 0;
264
 
265
}
266
 
267
int fsf_renegotiate_contract
268
  (const fsf_contract_parameters_t *new_contract,
269
   fsf_server_id_t server)
270
{
271
 
272
  #ifdef FSF_DEBUG
273
    kern_printf("(Renegotiate for server %d)",server);
274
  #endif
275
 
276
  if (!new_contract)
277
    return FSF_ERR_NOT_INITIALIZED;
278
 
279
  if (server < 0)
280
    return FSF_ERR_INVALID_SERVER;
281
 
241 giacomo 282
  return adjust_SERVER_budget_from_contract(new_contract,server);
221 giacomo 283
 
284
}
241 giacomo 285