Subversion Repositories shark

Rev

Rev 273 | Rev 334 | 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
 
142
  int local_scheduler_level,scheduler_id;
143
 
144
  #ifdef FSF_DEBUG
267 giacomo 145
    kern_printf("(FSF:Insert thread = %d to Server = %d)",*thread,server);
221 giacomo 146
  #endif
147
 
148
  /* Check if server and thread exsist */
241 giacomo 149
  if (server == NIL)
150
    return FSF_ERR_INVALID_SERVER;
221 giacomo 151
 
241 giacomo 152
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,server);
153
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,server);
221 giacomo 154
 
155
  /* Check if thread is already bind */
156
  switch (scheduler_id) {
157
    case FSF_SCHEDULER_POSIX:
158
 
298 giacomo 159
      nrt_task_def_arg(*(NRT_TASK_MODEL *)(local_scheduler_arg),arg);
160
      nrt_task_def_level(*(NRT_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
221 giacomo 161
 
298 giacomo 162
      *thread = task_create("POSIXSTAR", thread_code, local_scheduler_arg, NULL);
267 giacomo 163
      if (*thread == NIL) {
164
        #ifdef FSF_DEBUG
165
          kern_printf("(FSF:Error creating thread)");
166
        #endif
241 giacomo 167
        return FSF_ERR_CREATE_THREAD;
267 giacomo 168
      }
221 giacomo 169
 
241 giacomo 170
      POSIXSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
234 giacomo 171
 
241 giacomo 172
      task_activate(*thread);
221 giacomo 173
 
174
    break;
175
    case FSF_SCHEDULER_EDF:
176
 
250 giacomo 177
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
178
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
179
 
180
      *thread = task_create("EDFSTAR", thread_code, local_scheduler_arg, NULL);
181
      if (*thread == NIL)
182
        return FSF_ERR_CREATE_THREAD;
183
 
184
      EDFSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
185
 
186
      task_activate(*thread);
187
 
221 giacomo 188
      break;
235 giacomo 189
 
221 giacomo 190
    case FSF_SCHEDULER_RM:
235 giacomo 191
 
273 giacomo 192
      hard_task_def_arg(*(HARD_TASK_MODEL *)(local_scheduler_arg),arg);
193
      hard_task_def_level(*(HARD_TASK_MODEL *)(local_scheduler_arg),local_scheduler_level);
194
 
195
      *thread = task_create("RMSTAR", thread_code, local_scheduler_arg, NULL);
196
      if (*thread == NIL)
197
        return FSF_ERR_CREATE_THREAD;
198
 
199
      RMSTAR_setbudget(local_scheduler_level, *thread, (int)(server));
200
 
201
      task_activate(*thread);
202
 
203
      break;
221 giacomo 204
    default:
241 giacomo 205
      return FSF_ERR_INVALID_SERVER;
221 giacomo 206
      break;
207
  }
208
 
209
  return 0;
210
 
211
}
212
 
213
int fsf_get_server
214
  (fsf_server_id_t *server,
215
   pthread_t       thread)
216
{
217
  int local_scheduler_level, scheduler_id;
218
 
241 giacomo 219
  local_scheduler_level = SERVER_get_local_scheduler_level_from_pid(fsf_server_level,thread);
220
  scheduler_id = SERVER_get_local_scheduler_id_from_pid(fsf_server_level, thread);
221 giacomo 221
 
222
  switch (scheduler_id) {
223
    case FSF_SCHEDULER_POSIX:
224
      return POSIXSTAR_getbudget(local_scheduler_level,thread);
225
    case FSF_SCHEDULER_EDF:
235 giacomo 226
      return EDFSTAR_getbudget(local_scheduler_level,thread);
221 giacomo 227
    case FSF_SCHEDULER_RM:
235 giacomo 228
      return RMSTAR_getbudget(local_scheduler_level,thread);
229
    default:
221 giacomo 230
      return -1;
231
  }
232
 
233
  return -1;
234
 
235
}
236
 
237
int fsf_cancel_contract
238
  (fsf_server_id_t *server)
239
{
240
 
241
  int local_scheduler_level, scheduler_id;
242
 
243
  #ifdef FSF_DEBUG
244
    kern_printf("(Remove server %d)",*server);
245
  #endif
246
 
247
  /* Check server id */
248
  if (*server < 0)
249
    return FSF_ERR_INVALID_SERVER;
250
 
241 giacomo 251
  local_scheduler_level = SERVER_get_local_scheduler_level_from_budget(fsf_server_level,*server);
252
  scheduler_id = SERVER_get_local_scheduler_id_from_budget(fsf_server_level,*server);
221 giacomo 253
 
254
  switch (scheduler_id) {
255
    case FSF_SCHEDULER_POSIX:
256
 
257
      /* Check if some thread use the server */
258
      if(POSIXSTAR_budget_has_thread(local_scheduler_level,*server))
259
        return FSF_ERR_SERVER_USED;
260
 
261
      break;
262
    case FSF_SCHEDULER_EDF:
235 giacomo 263
      /* Check if some thread use the server */
264
      if(EDFSTAR_budget_has_thread(local_scheduler_level,*server))
265
        return FSF_ERR_SERVER_USED;
266
      break;
267
 
221 giacomo 268
    case FSF_SCHEDULER_RM:
235 giacomo 269
      /* Check if some thread use the server */
270
      if(RMSTAR_budget_has_thread(local_scheduler_level,*server))
271
        return FSF_ERR_SERVER_USED;
272
 
221 giacomo 273
      break;
274
  }
275
 
241 giacomo 276
  SERVER_removebudget(fsf_server_level,*server);
221 giacomo 277
 
278
  level_free_descriptor(local_scheduler_level);
279
 
280
  remove_contract(*server);
281
 
282
  *server = -1;
283
 
284
  return 0;
285
 
286
}
287
 
288
int fsf_renegotiate_contract
289
  (const fsf_contract_parameters_t *new_contract,
290
   fsf_server_id_t server)
291
{
292
 
293
  #ifdef FSF_DEBUG
294
    kern_printf("(Renegotiate for server %d)",server);
295
  #endif
296
 
297
  if (!new_contract)
298
    return FSF_ERR_NOT_INITIALIZED;
299
 
300
  if (server < 0)
301
    return FSF_ERR_INVALID_SERVER;
302
 
241 giacomo 303
  return adjust_SERVER_budget_from_contract(new_contract,server);
221 giacomo 304
 
305
}
241 giacomo 306