Subversion Repositories shark

Rev

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