Subversion Repositories shark

Rev

Rev 241 | Rev 404 | 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 <time.h>
15
#include <sys/boolean.h>
16
#include <sys/types.h>
17
 
18
#include "fsf_configuration_parameters.h"
19
#include "fsf_opaque_types.h"
20
 
224 giacomo 21
#include "edfstar.h"
235 giacomo 22
#include "rmstar.h"
224 giacomo 23
#include "posixstar.h"
334 giacomo 24
#include "mpegstar.h"
224 giacomo 25
 
221 giacomo 26
#ifndef _FSF_CONTRACT_H_
27
#define _FSF_CONTRACT_H_
28
 
224 giacomo 29
/* S.Ha.R.K. Init */
241 giacomo 30
int FSF_register_module(int server_level);
224 giacomo 31
 
221 giacomo 32
//////////////////////////////////////////////////////////////////
33
//                     BASIC TYPES AND CONSTANTS
34
//////////////////////////////////////////////////////////////////
35
 
36
typedef enum {FSF_BOUNDED, FSF_INDETERMINATE} fsf_workload_t;
37
 
38
typedef enum {FSF_CONTINUOUS, FSF_DISCRETE} fsf_granularity_t;
39
 
40
typedef struct {
41
  struct timespec    budget;    // Execution time
42
  struct timespec    period;    // Period
43
} fsf_utilization_value_t;
44
 
45
typedef struct {
46
  int                         size; // = 0
47
  fsf_utilization_value_t     unit[FSF_MAX_N_UTILIZATION_VALUES];
48
} fsf_utilization_set_t;
49
 
50
typedef unsigned long         fsf_preemption_level_t; // range 1..2**32-1
51
 
52
typedef struct {
53
  struct timespec          wcet;      // Execution time
54
  fsf_preemption_level_t   plevel;    // Preemption_Level, range 1..2**32-1
55
} fsf_critical_section_data_t;
56
 
57
typedef struct {
58
  int                          size; // = 0
59
  fsf_critical_section_data_t  section[FSF_MAX_N_CRITICAL_SECTIONS];
60
} fsf_critical_sections_t;
61
 
62
typedef int fsf_scheduler_id_t;
63
 
64
#define FSF_SCHEDULER_POSIX             0
65
#define FSF_SCHEDULER_EDF               1
66
#define FSF_SCHEDULER_RM                2
334 giacomo 67
#define FSF_SCHEDULER_MPEG              3
221 giacomo 68
 
69
// Constants for assigning default values
70
#define FSF_DEFAULT_WORKLOAD            FSF_INDETERMINATE
71
#define FSF_DEFAULT_GRANULARITY         FSF_CONTINUOUS
72
#define FSF_DEFAULT_QUALITY             0
73
#define FSF_DEFAULT_IMPORTANCE          1
74
#define FSF_DEFAULT_D_EQUALS_T          false
75
#define FSF_DEFAULT_DEADLINE            {0,0}
76
#define FSF_DEFAULT_SCHEDULER           FSF_SCHEDULER_POSIX
77
 
78
// Constants for omitting the assignment of values to specific arguments
79
// in calls to initialization functions
80
#define FSF_NULL_CRITICAL_SECTIONS      (fsf_critical_sections_t *)NULL
81
#define FSF_NULL_UTILIZATION_SET        (fsf_utilization_set_t *)NULL
82
#define FSF_NULL_DEADLINE               (struct timespec *)NULL
83
#define FSF_NULL_SIGNAL                 0
84
 
85
 
86
// Error codes
87
#define FSF_ERR_NOT_INITIALIZED                 2003001
88
#define FSF_ERR_TOO_MANY_TASKS                  2003002
89
#define FSF_ERR_ALREADY_INITIALIZED             2003003
90
#define FSF_ERR_BAD_ARGUMENT                    2003004
91
#define FSF_ERR_INVALID_SYNCH_OBJECT_HANDLE     2003005
92
#define FSF_ERR_NO_RENEGOTIATION_REQUESTED      2003006
93
#define FSF_ERR_CONTRACT_REJECTED               2003007
94
#define FSF_ERR_TOO_MANY_SERVERS                2003008
241 giacomo 95
#define FSF_ERR_CREATE_THREAD                   2003009
96
#define FSF_ERR_SERVER_USED                     2003010
97
#define FSF_ERR_INVALID_SERVER                  2003011
98
#define FSF_ERR_CREATE_SERVER                   2003012
221 giacomo 99
 
100
//////////////////////////////////////////////////////////////
101
//                       CONTRACT PARAMETERS
102
//////////////////////////////////////////////////////////////
103
 
104
// Contract parameters type; it is an opaque type
105
typedef FSF_CONTRACT_PARAMETERS_T_OPAQUE fsf_contract_parameters_t;
106
 
107
int
108
fsf_initialize_contract(fsf_contract_parameters_t *contract);
109
 
110
//Description: The operation receives a pointer to a contract parameters
111
//object and initializes it, setting it to the default values.
112
//  budget_min                  => {0,0};                              
113
//  period_max                  => {0,0};                              
114
//  budget_max                  => {0,0};                              
115
//  period_min                  => {0,0};                              
116
//  workload                    => DEFAULT_WORKLOAD;                   
117
 
118
//  d_equals_t                  => DEFAULT_D_EQUALS_T; (false or true)
119
//  deadline                    => DEFAULT_DEADLINE;                     
120
//  budget_overrun_sig_notify   => 0;                  (signal number)
121
//  budget_overrun_sig_value    => {0, NULL};
122
//  deadline_miss_sig_notify    => 0;                  (signal number)
123
//  deadline_miss_sig_value     => {0, NULL};
124
//                                                         
125
//  granularity                 => DEFAULT_GRANULARITY;               
126
//  utilization_set;            => size = 0                         
127
//  quality                     => DEFAULT_QUALITY;     (range 0..100)
128
//  importance                  => DEFAULT_IMPORTANCE;    (range 1..5)
129
//                                                         
130
//  preemption_level            => 0;               (range 1..2**32-1)
131
//  critical_sections;          => size = 0                         
132
 
133
int
134
fsf_set_contract_basic_parameters
135
  (fsf_contract_parameters_t *contract,
136
   const struct timespec  *budget_min,
137
   const struct timespec  *period_max,
138
   const struct timespec  *budget_max,
139
   const struct timespec  *period_min,
140
   fsf_workload_t          workload);
141
 
142
//Description: The operation updates the specified contract parameters
143
//object by setting its budget, period, and workload to the specified
144
//input parameters. (Note: the workload is a basic parameter because
145
//bounded tasks are triggered by the scheduler (see the Timed Schedule
146
//Next Job operation, later), while indeterminate tasks are not;
147
//therefore, their programming model is quite different).
148
 
149
int
150
fsf_get_contract_basic_parameters
151
  (const fsf_contract_parameters_t *contract,
152
   struct timespec  *budget_min,
153
   struct timespec  *period_max,
154
   struct timespec  *budget_max,
155
   struct timespec  *period_min,
156
   fsf_workload_t   *workload);
157
 
158
//Description: This operation obtains from the specified contract parameters
159
//object its budget, period, and workload, and copies them to the places
160
//pointed to by the corresponding input parameters.
161
 
162
int
163
fsf_set_contract_timing_requirements
164
  (fsf_contract_parameters_t *contract,
165
   bool                   d_equals_t,
166
   const struct timespec *deadline,
167
   int                    budget_overrun_sig_notify,
168
   union sigval           budget_overrun_sig_value,
169
   int                    deadline_miss_sig_notify,
170
   union sigval           deadline_miss_sig_value);
171
 
172
//Description: The operation updates the specified contract parameters
173
//object. d_equals_t is used as a boolean, deadline must be
174
//NULL_DEADLINE if d_equals_t is true, budget_overrun_sig_notify or
175
//deadline_miss_sig_notify may be NULL_SIGNAL (no notification) or any
176
//posix signal. budget_overrun_sig_value and deadline_miss_sig_value
177
//are the values to be delivered with the signal.
178
 
179
int
180
fsf_get_contract_timing_requirements
181
  (const fsf_contract_parameters_t *contract,
182
   bool                            *d_equals_t,
183
   struct timespec                 *deadline,
184
   int                             *budget_overrun_sig_notify,
185
   union sigval                    *budget_overrun_sig_value,
186
   int                             *deadline_miss_sig_notify,
187
   union sigval                    *deadline_miss_sig_value);
188
 
189
//Description: The operation obtains the corresponding input
190
//parameters from the specified contract parameters object. If
191
//d_equals_t is true, the deadline will not be updated.
192
 
193
int  
194
fsf_set_contract_reclamation_parameters
195
  (fsf_contract_parameters_t *contract,
196
   fsf_granularity_t            granularity,
197
   const fsf_utilization_set_t *utilization_set,
198
   int                          quality,
199
   int                          importance);
200
 
201
//Description: The operation updates the specified contract parameters
202
//object by setting its granularity, utilization set, quality, and
203
//importance to the specified input parameters.
204
 
205
int  
206
fsf_get_contract_reclamation_parameters
207
  (const fsf_contract_parameters_t *contract,
208
   fsf_granularity_t               *granularity,
209
   fsf_utilization_set_t           *utilization_set,
210
   int                             *quality,
211
   int                             *importance);
212
 
213
//Description: The operation obtains from the specified contract parameters
214
//object its granularity, utilization set, quality, and importance. Then
215
//copies them to the places pointed to by the specified input parameters.
216
//Only the utilization_values of the utilization_set that are in use, are
217
//copied (according to its size field). 
218
 
219
int
220
fsf_set_contract_synchronization_parameters
221
  (fsf_contract_parameters_t     *contract,
222
   fsf_preemption_level_t         preemption_level,
223
   const fsf_critical_sections_t *critical_sections);
224
 
225
//Description: The operation updates the specified contract parameters
226
//object by setting its preemption level and critical sections to the
227
//specified input parameters.
228
 
229
int
230
fsf_get_contract_synchronization_parameters
231
  (const fsf_contract_parameters_t *contract,
232
   fsf_preemption_level_t          *preemption_level,
233
   fsf_critical_sections_t         *critical_sections);
234
 
235
//Description: The operation obtains from the specified contract
236
//parameters object its preemption level and critical sections, and
237
//copies them to the places pointed to by the specified input
238
//parameters.  Only those critical_section_data records that are in use
239
//in the critical_sections structure are copied (according to its size
240
//field).
241
 
242
int
243
fsf_set_local_scheduler_parameter
244
  (fsf_contract_parameters_t *contract,
245
   fsf_scheduler_id_t local_scheduler_id);
246
 
241 giacomo 247
//Description: Set the local scheduler
248
 
221 giacomo 249
int
250
fsf_get_local_scheduler_parameter
251
  (const fsf_contract_parameters_t *contract,
252
   fsf_scheduler_id_t *local_scheduler_id);
253
 
241 giacomo 254
//Description: Get the local scheduler
255
 
221 giacomo 256
//////////////////////////////////////////////////////////////
257
//                 SYNCHRONIZATION OBJECTS
258
//////////////////////////////////////////////////////////////
259
 
260
//An abstract synchronization object is defined by the application.
261
//This object can be used by an application to wait for an event to
262
//arrive by invoking the Event Triggered Schedule Next Job operation.
263
//It can also be used to signal the event either causing a waiting
264
//server to wake up, or the event to be queued if no server is waiting
265
//for it.  It is defined by the following opaque type and has the
266
//following operations:
267
 
268
typedef FSF_SYNCH_OBJECT_HANDLE_T_OPAQUE fsf_synch_object_handle_t;
269
 
270
int
271
fsf_create_synchobject(fsf_synch_object_handle_t *synch_handle);
272
 
273
//Description: This operation creates and initializes a
274
//synchronization object variable managed by the scheduler, and
275
//returns a handle to it in the variable pointed to by synch_handle.
276
 
277
int
278
fsf_signal_synchobject(fsf_synch_object_handle_t *synch_handle);
279
 
280
//Description: If one or more servers are waiting upon the specified
281
//synchronization object one of them is awakened; if not, the event is
282
//queued at the synchronization object.
283
 
284
int
285
fsf_destroy_synchobject(fsf_synch_object_handle_t *synch_handle);
286
 
287
//This operation destroys the synchronization object (created by a
288
//previous call to fsf_create_synchobject) that is referenced by the
289
//synch_handle variable. After calling this operation, the
290
//synch_handle variable can not be used until it is initialized again
291
//by a call to fsf_create_synchobject.
292
 
293
 
294
///////////////////////////////////////////////////////////////
295
//                 CONTRACT NEGOCIATION OPERATIONS
296
///////////////////////////////////////////////////////////////
297
 
298
// Server Id type, that identifies a server created to manage a 
299
// given contract
300
 
301
typedef int      fsf_server_id_t;
302
 
303
// The following type references a function that may become 
304
// a thread's code
305
 
306
typedef void * (*fsf_thread_code_t) (void *);
307
 
308
// Negotiate contract functions: The following functions are used to
309
// create servers for a contract parameters specification and also to
310
// assign one or more threads to a server (Note: the current
311
// implementation only supports one thread per server; this limitation
312
// will be removed in the next phase of the project)
313
 
314
// The first time that any of these operations is called, it creates
315
// all the internal management structures that are necessary for the
316
// FIRST Scheduling Framework to operate properly.
317
 
318
int
319
fsf_negotiate_contract
320
  (const fsf_contract_parameters_t *contract,
321
   fsf_server_id_t      *server);
322
 
323
//Description: The operation negotiates a contract for a new
324
//server. If the on-line admission test is enabled it determines
325
//whether the contract can be admitted or not based on the current
326
//contracts established in the system. Then it creates the server and
327
//recalculates all necessary parameters for the contracts already
328
//present in the system. This is a potentially blocking operation; it
329
//returns when the system has either rejected the contract, or
330
//admitted it and made it effective. It returns zero and places the
331
//server identification number in the location pointed to by the
332
//server input parameter if accepted, or an error if rejected.  No
333
//thread is bound to the newly created server, which will be idle
334
//until a thread is bound to it. This operation can only be executed
335
//by threads that are already bound to an active server and therefore
336
//are being scheduled by the fsf scheduler.
337
 
338
int
241 giacomo 339
fsf_create_thread
340
  (fsf_server_id_t    server,
341
   pthread_t         *thread,
342
   pthread_attr_t    *attr,
343
   fsf_thread_code_t  thread_code,
344
   void              *arg,
345
   void              *local_scheduler_arg);
221 giacomo 346
 
241 giacomo 347
//Description: This operation creates a new thread inside a specific
348
//server. The local_scheduler_arg parameter is used to pass specific
349
//parameters to local scheduler. These parameters are application
350
//depented.
221 giacomo 351
 
352
int
353
fsf_get_server
354
  (fsf_server_id_t *server,
355
   pthread_t       thread);
356
 
357
//Description: This operation returns the server associated with a
358
//thread. It returns an error if the thread does not exist, it is not 
359
//under the control of the scheduling framework, or is not bound.
360
 
361
int
362
fsf_cancel_contract (fsf_server_id_t *server);
363
 
364
//Description: The operation eliminates the specified server and
365
//recalculates all necessary parameters for the contracts remaining in 
366
//the system. This is a potentially blocking operation; it returns when 
367
//the system has made the changes effective.
368
 
369
int
370
fsf_renegotiate_contract
371
  (const fsf_contract_parameters_t *new_contract,
372
   fsf_server_id_t server);
373
 
374
//Description: The operation renegotiates a contract for an existing
375
//server. If the on-line admission test is enabled it determines
376
//whether the contract can be admitted or not based on the current
377
//contracts established in the system. If it cannot be admitted, the
378
//old contract remains in effect and an error is returned. If it can
379
//be admitted, it recalculates all necessary parameters for the
380
//contracts already present in the system anr returns zero. This is a
381
//potentially blocking operation; it returns when the system has
382
//either rejected the new contract, or admitted it and made it
383
//effective.
384
 
385
int
386
fsf_request_contract_renegotiation
387
  (const fsf_contract_parameters_t *new_contract,
388
   fsf_server_id_t                  server,
389
   int                              sig_notify,
390
   union sigval                     sig_value);
391
 
392
//Description: The operation enqueues a renegotiate operation for an
393
//existing server, and returns immediately. The renegotiate operation
394
//is performed asynchronously, as soon as it is practical; meanwhile
395
//the system operation will continue normally. When the renegotiation
396
//is made, if the on-line admission test is enabled it determines
397
//whether the contract can be admitted or not based on the current
398
//contracts established in the system. If it cannot be admitted, the
399
//old contract remains in effect. If it can be admitted, it
400
//recalculates all necessary parameters for the contracts already
401
//present in the system. When the operation is completed, notification
402
//is made to the caller, if requested, via a signal. The status of the
403
//operation (in progress, admitted, rejected) can be checked with the
404
//get_renegotiation_status operation.  The argument sig_notify can be
405
//NULL_SIGNAL (no notification), or any posix signal; and in this case
406
//sig_value is to be sent with the signal.
407
 
408
typedef enum {FSF_IN_PROGRESS,
409
              FSF_REJECTED,
410
              FSF_ADMITTED} fsf_renegotiation_status_t;
411
 
412
int
413
fsf_get_renegotiation_status
414
  (fsf_server_id_t server,
415
   fsf_renegotiation_status_t *renegotiation_status);
416
 
417
//Description: The operation reports on the status of the last
418
//renegotiation operation enqueued for the specified server. It is 
419
//callable even after notification of the completion of such operation,
420
//if requested.
421
 
422
int
423
fsf_request_change_quality_and_importance
424
  (fsf_server_id_t server,
425
   int new_importance,
426
   int new_quality);
427
 
428
//Description: The operation enqueues a request to change the quality and 
429
//importance parameters of the specified server, and returns immediately.
430
//The change operation is performed as soon as it is practical;
431
//meanwhile the system operation will continue normally.
432
 
433
 
434
////////////////////////////////////////////////////////////
435
//                  SCHEDULING BOUNDED WORKLOADS
436
////////////////////////////////////////////////////////////
437
 
438
int
439
fsf_schedule_next_timed_job
440
  (const struct timespec *at_absolute_time,
441
   struct timespec       *next_budget,
442
   struct timespec       *next_period,
443
   bool                  *was_deadline_missed,
444
   bool                  *was_budget_overran);
445
 
446
//Description: This operation is invoked by threads associated with
447
//bounded workload servers to indicate that a job has been completed
448
//(and that the scheduler may reassign the unused capacity of the
449
//current job to other servers), and also when the first job require
450
//to be scheduled. The system will activate the job at the specified 
451
//absolute time, and will then use the scheduling rules to determine 
452
//when the job can run, at which time the call returns. Upon return, 
453
//the system reports the current period and budget for the current
454
//job, whether the deadline of the previous job was missed or not, 
455
//and whether the budget of the previous job was overrun or not.
456
 
457
 
458
int
459
fsf_schedule_next_event_triggered_job
460
  (fsf_synch_object_handle_t *synch_handle,
461
   struct timespec           *next_budget,
462
   struct timespec           *next_period,
463
   bool                      *was_deadline_missed,
464
   bool                      *was_budget_overran);
465
 
466
//Description: This operation is invoked by threads associated with
467
//bounded workload servers to indicate that a job has been completed
468
//(and that the scheduler may reassign the unused capacity of the
469
//current job to other servers), and also when the first job require
470
//to be scheduled. If the specified synchronization object has events 
471
//queued, one of them is dequeued; otherwise the server will wait upon 
472
//the specified synchronization object until it is signalled. Then, the 
473
//system will use the scheduling rules to determine when the job can run 
474
//and the call will return at that time. Upon return, the system reports
475
//the current period and budget for the current job, whether the deadline
476
//of the previous job was missed or not, and whether the budget of the
477
//previous job was overrun or not.
478
 
479
 
480
//////////////////////////////////////////////////////////////
481
//           OBTAINING INFORMATION FROM THE SCHEDULER
482
//////////////////////////////////////////////////////////////
483
 
484
int
485
fsf_get_available_capacity (fsf_server_id_t server, float *capacity);
486
 
487
//Description: This operation returns the current spare capacity (in 
488
//percentage of processor or network utilization), currently assigned 
489
//to the importance level of the specified server.
490
 
491
int
492
fsf_get_total_quality (fsf_server_id_t server, int *total_quality);
493
 
494
//Description: This operation returns the sum of the quality parameters 
495
//for all servers in the system of importance level equal to that of 
496
//the specified server.
497
 
498
int
499
fsf_is_admission_test_enabled();
500
 
501
//Description: Returns true if the system is configured with the 
502
//on-line admission test enabled, or false otherwise.
503
 
504
 
505
#endif // _FSF_CONTRACT_H_