Subversion Repositories shark

Rev

Rev 417 | 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;  
417 giacomo 315
        TIME             Tmin;
316
        TIME             Tmax;
661 giacomo 317
        TIME             deadline;
318
        bool             d_equals_t;
404 trimarchi 319
} server_elem;
320
 
321
int recalculate_contract(bandwidth_t U);
322
 
323
 
221 giacomo 324
// The following type references a function that may become 
325
// a thread's code
326
 
327
typedef void * (*fsf_thread_code_t) (void *);
328
 
329
// Negotiate contract functions: The following functions are used to
330
// create servers for a contract parameters specification and also to
331
// assign one or more threads to a server (Note: the current
332
// implementation only supports one thread per server; this limitation
333
// will be removed in the next phase of the project)
334
 
335
// The first time that any of these operations is called, it creates
336
// all the internal management structures that are necessary for the
337
// FIRST Scheduling Framework to operate properly.
338
 
339
int
340
fsf_negotiate_contract
341
  (const fsf_contract_parameters_t *contract,
342
   fsf_server_id_t      *server);
343
 
344
//Description: The operation negotiates a contract for a new
345
//server. If the on-line admission test is enabled it determines
346
//whether the contract can be admitted or not based on the current
347
//contracts established in the system. Then it creates the server and
348
//recalculates all necessary parameters for the contracts already
349
//present in the system. This is a potentially blocking operation; it
350
//returns when the system has either rejected the contract, or
351
//admitted it and made it effective. It returns zero and places the
352
//server identification number in the location pointed to by the
353
//server input parameter if accepted, or an error if rejected.  No
354
//thread is bound to the newly created server, which will be idle
355
//until a thread is bound to it. This operation can only be executed
356
//by threads that are already bound to an active server and therefore
357
//are being scheduled by the fsf scheduler.
358
 
359
int
241 giacomo 360
fsf_create_thread
361
  (fsf_server_id_t    server,
362
   pthread_t         *thread,
363
   pthread_attr_t    *attr,
364
   fsf_thread_code_t  thread_code,
365
   void              *arg,
366
   void              *local_scheduler_arg);
221 giacomo 367
 
241 giacomo 368
//Description: This operation creates a new thread inside a specific
369
//server. The local_scheduler_arg parameter is used to pass specific
370
//parameters to local scheduler. These parameters are application
371
//depented.
221 giacomo 372
 
373
int
374
fsf_get_server
375
  (fsf_server_id_t *server,
376
   pthread_t       thread);
377
 
378
//Description: This operation returns the server associated with a
379
//thread. It returns an error if the thread does not exist, it is not 
380
//under the control of the scheduling framework, or is not bound.
381
 
382
int
383
fsf_cancel_contract (fsf_server_id_t *server);
384
 
385
//Description: The operation eliminates the specified server and
386
//recalculates all necessary parameters for the contracts remaining in 
387
//the system. This is a potentially blocking operation; it returns when 
388
//the system has made the changes effective.
389
 
390
int
391
fsf_renegotiate_contract
392
  (const fsf_contract_parameters_t *new_contract,
393
   fsf_server_id_t server);
394
 
395
//Description: The operation renegotiates a contract for an existing
396
//server. 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 and an error is returned. If it can
400
//be admitted, it recalculates all necessary parameters for the
401
//contracts already present in the system anr returns zero. This is a
402
//potentially blocking operation; it returns when the system has
403
//either rejected the new contract, or admitted it and made it
404
//effective.
405
 
406
int
407
fsf_request_contract_renegotiation
408
  (const fsf_contract_parameters_t *new_contract,
409
   fsf_server_id_t                  server,
410
   int                              sig_notify,
411
   union sigval                     sig_value);
412
 
413
//Description: The operation enqueues a renegotiate operation for an
414
//existing server, and returns immediately. The renegotiate operation
415
//is performed asynchronously, as soon as it is practical; meanwhile
416
//the system operation will continue normally. When the renegotiation
417
//is made, if the on-line admission test is enabled it determines
418
//whether the contract can be admitted or not based on the current
419
//contracts established in the system. If it cannot be admitted, the
420
//old contract remains in effect. If it can be admitted, it
421
//recalculates all necessary parameters for the contracts already
422
//present in the system. When the operation is completed, notification
423
//is made to the caller, if requested, via a signal. The status of the
424
//operation (in progress, admitted, rejected) can be checked with the
425
//get_renegotiation_status operation.  The argument sig_notify can be
426
//NULL_SIGNAL (no notification), or any posix signal; and in this case
427
//sig_value is to be sent with the signal.
428
 
429
typedef enum {FSF_IN_PROGRESS,
430
              FSF_REJECTED,
431
              FSF_ADMITTED} fsf_renegotiation_status_t;
432
 
433
int
434
fsf_get_renegotiation_status
435
  (fsf_server_id_t server,
436
   fsf_renegotiation_status_t *renegotiation_status);
437
 
438
//Description: The operation reports on the status of the last
439
//renegotiation operation enqueued for the specified server. It is 
440
//callable even after notification of the completion of such operation,
441
//if requested.
442
 
443
int
444
fsf_request_change_quality_and_importance
445
  (fsf_server_id_t server,
446
   int new_importance,
447
   int new_quality);
448
 
449
//Description: The operation enqueues a request to change the quality and 
450
//importance parameters of the specified server, and returns immediately.
451
//The change operation is performed as soon as it is practical;
452
//meanwhile the system operation will continue normally.
453
 
454
 
455
////////////////////////////////////////////////////////////
456
//                  SCHEDULING BOUNDED WORKLOADS
457
////////////////////////////////////////////////////////////
458
 
459
int
460
fsf_schedule_next_timed_job
461
  (const struct timespec *at_absolute_time,
462
   struct timespec       *next_budget,
463
   struct timespec       *next_period,
464
   bool                  *was_deadline_missed,
465
   bool                  *was_budget_overran);
466
 
467
//Description: This operation is invoked by threads associated with
468
//bounded workload servers to indicate that a job has been completed
469
//(and that the scheduler may reassign the unused capacity of the
470
//current job to other servers), and also when the first job require
471
//to be scheduled. The system will activate the job at the specified 
472
//absolute time, and will then use the scheduling rules to determine 
473
//when the job can run, at which time the call returns. Upon return, 
474
//the system reports the current period and budget for the current
475
//job, whether the deadline of the previous job was missed or not, 
476
//and whether the budget of the previous job was overrun or not.
477
 
478
 
479
int
480
fsf_schedule_next_event_triggered_job
481
  (fsf_synch_object_handle_t *synch_handle,
482
   struct timespec           *next_budget,
483
   struct timespec           *next_period,
484
   bool                      *was_deadline_missed,
485
   bool                      *was_budget_overran);
486
 
487
//Description: This operation is invoked by threads associated with
488
//bounded workload servers to indicate that a job has been completed
489
//(and that the scheduler may reassign the unused capacity of the
490
//current job to other servers), and also when the first job require
491
//to be scheduled. If the specified synchronization object has events 
492
//queued, one of them is dequeued; otherwise the server will wait upon 
493
//the specified synchronization object until it is signalled. Then, the 
494
//system will use the scheduling rules to determine when the job can run 
495
//and the call will return at that time. Upon return, the system reports
496
//the current period and budget for the current job, whether the deadline
497
//of the previous job was missed or not, and whether the budget of the
498
//previous job was overrun or not.
499
 
500
 
501
//////////////////////////////////////////////////////////////
502
//           OBTAINING INFORMATION FROM THE SCHEDULER
503
//////////////////////////////////////////////////////////////
504
 
505
int
506
fsf_get_available_capacity (fsf_server_id_t server, float *capacity);
507
 
508
//Description: This operation returns the current spare capacity (in 
509
//percentage of processor or network utilization), currently assigned 
510
//to the importance level of the specified server.
511
 
512
int
513
fsf_get_total_quality (fsf_server_id_t server, int *total_quality);
514
 
515
//Description: This operation returns the sum of the quality parameters 
516
//for all servers in the system of importance level equal to that of 
517
//the specified server.
518
 
519
int
520
fsf_is_admission_test_enabled();
521
 
522
//Description: Returns true if the system is configured with the 
523
//on-line admission test enabled, or false otherwise.
524
 
525
 
526
#endif // _FSF_CONTRACT_H_