]> rtime.felk.cvut.cz Git - frescor/frsh-include.git/blob - frsh_core.h
e5076820e1af20ac9f5fa0169de10cbd006c6f4f
[frescor/frsh-include.git] / frsh_core.h
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2009 FRESCOR consortium partners:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politécnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org for a link to partners' websites
17 //
18 //           FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 //   FSF API web pages: http://marte.unican.es/fsf/docs
32 //                      http://shark.sssup.it/contrib/first/docs/
33 //
34 //   This file is part of FRSH (FRescor ScHeduler)
35 //
36 //  FRSH is free software; you can redistribute it and/or modify it
37 //  under terms of the GNU General Public License as published by the
38 //  Free Software Foundation; either version 2, or (at your option) any
39 //  later version.  FRSH is distributed in the hope that it will be
40 //  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
41 //  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 //  General Public License for more details. You should have received a
43 //  copy of the GNU General Public License along with FRSH; see file
44 //  COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
45 //  Cambridge, MA 02139, USA.
46 //
47 //  As a special exception, including FRSH header files in a file,
48 //  instantiating FRSH generics or templates, or linking other files
49 //  with FRSH objects to produce an executable application, does not
50 //  by itself cause the resulting executable application to be covered
51 //  by the GNU General Public License. This exception does not
52 //  however invalidate any other reasons why the executable file might be
53 //  covered by the GNU Public License.
54 // -----------------------------------------------------------------------
55
56 //==============================================
57 //  ******** *******    ********  **      **
58 //  **///// /**////**  **//////  /**     /**
59 //  **      /**   /** /**        /**     /**
60 //  ******* /*******  /********* /**********
61 //  **////  /**///**  ////////** /**//////**
62 //  **      /**  //**        /** /**     /**
63 //  **      /**   //** ********  /**     /**
64 //  //       //     // ////////   //      //
65 //
66 // FRSH(FRescor ScHeduler), pronounced "fresh"
67 //==============================================
68
69 #ifndef _FRSH_CORE_H_
70 #define _FRSH_CORE_H_
71
72
73 /**
74  * @file frsh_core.h
75  **/
76
77
78 #include <time.h>
79 #include <sys/types.h>
80
81 #include "frsh_core_types.h"
82 #include "frsh_spare_capacity.h"
83
84
85 FRSH_CPP_BEGIN_DECLS
86
87 /**
88  * @defgroup core Core module
89  *
90  * This module includes the basic functions and services that are
91  * provided by any FRSH implementation. This module includes basic type
92  * definitions, and functions to
93  *
94  * - create a contract and initialize it
95  * - set/get the basic parameters of a contract
96  * - negotiate a service contract, obtaining a vres id
97  * - create and bind threads to vres
98  * - create/destroy a synchronization object
99  * - manage bounded workloads
100  *
101  **/
102
103
104 //////////////////////////////////////////////////////////////////////
105 //           INITIALIZATION SERVICES
106 //////////////////////////////////////////////////////////////////////
107
108 /**
109  * @defgroup initialization Initialization services
110  * @ingroup core
111  *
112  * These functions need to be called before doing any FRSH operation
113  * (including contract initialization).
114  *
115  * @{
116  **/
117
118
119 /**
120  * frsh_init()
121  *
122  * We cannot call any frsh functions before frsh_init. After calling
123  * frsh_init, the main will be executing in the background. Then, it
124  * can do the negotiations and create new threads if needed.  The
125  * second time this function is called it fails.
126  *
127  * @return 0 if no error. \n
128  *  FRSH_ERR_SYSTEM_ALREADY_INITIALIZED : if the function has already
129  *                                        been called before\n
130  *  .
131  *  It may also return any of the errors that may be returned by the
132  *  underlying operating system primitives required to perform the
133  *  FRSH system start up
134  *
135  **/
136 int frsh_init();
137
138 /*@}*/
139
140
141 /////////////////////////////////////////////////////////////
142 //                       CONTRACT PARAMETERS
143 /////////////////////////////////////////////////////////////
144 /**
145  * @defgroup contract Contract Creation and Initialization.
146  * @ingroup core
147  *
148  * These functions are used to create and initialize a contract, and
149  * set its parameters.
150  *
151  * @{
152  **/
153
154
155 /**
156  * frsh_contract_init()
157  *
158  * The operation receives a pointer to a contract object
159  * and initializes it, setting it to the default values.
160  *
161  * The default values are:
162  *
163  * - resource_id               => 0
164  * - ressource_type            => FRSH_CRT_PROCESSOR
165  * - contract_label               => ""
166  * - budget_min                => {0,0};
167  * - period_max                => {0,0};
168  * - budget_max                => {0,0};
169  * - period_min                => {0,0};
170  * - workload                  => FRSH_WT_INDETERMINATE
171  * - d_equals_t                => true
172  * - contract_type             => FRSH_CT_REGULAR;
173  * - deadline                  => {0,0};
174  * - budget_overrun_signal => 0;  (signal number)
175  * - budget_overrun_siginfo  => {0, NULL};
176  * - deadline_miss_signal  => 0;  (signal number)
177  * - deadline_miss_siginfo   => {0, NULL};
178  *
179  * - granularity               => DEFAULT_GRANULARITY;
180  * - utilization_set;          => size = 0
181  * - quality                   => DEFAULT_QUALITY;    (range 0..100)
182  * - importance                => DEFAULT_IMPORTANCE; (range 1..5)
183  * - preemption_level          => 0; (range 1..2**32-1)
184  * - critical_sections;        => size = 0
185  *
186  * - sched_policy              => DEFAULT_SCHED_POLICY (FRSH_NONE)
187  *
188  * @param     contract the pointer to the contract variable.
189  *
190  * @return 0 if no error \n
191  *     FRSH_ERR_BAD_ARGUMENT :  contract is NULL
192  *
193  **/
194 int frsh_contract_init(frsh_contract_t *contract);
195
196
197 /**
198  * frsh_contract_destroy()
199  *
200  * The operation deallocates all memory which might be allocated for
201  * the contract.
202  *
203  * @param contract the pointer to the contract variable.
204  **/
205 void frsh_contract_destroy(frsh_contract_t *contract);
206
207
208 /**
209  * frsh_contract_set_basic_params()
210  *
211  * The operation updates the specified contract parameters object by
212  * setting its budget, period, workload and type to the specified
213  * input parameters. (Note: the workload is a basic parameter because
214  * bounded tasks are triggered by the scheduler (see the
215  * frsh_timed_wait() and frsh_synchobj_wait* operations), while
216  * indeterminate tasks are not; therefore, their programming model is
217  * quite different).
218  *
219  * @param contract     the pointer to the contract object
220  * @param[in] budget_min   the minimum budget for the contract
221  * @param[in] period_max   the maximum period for the contract
222  * @param[in] workload     the kind of workload (can be FRSH_WT_BOUNDED,
223  *                            FRSH_WT_INDETERMINATE or FRSH_OVERHEAD)
224  * @param[in] contract_type can be FRSH_CT_REGULAR,
225  *                                 FRSH_CT_BACKGROUND, FRSH_CT_DUMMY.
226  *
227  * @return 0 if no error \n
228  *    FRSH_ERR_BAD_ARGUMENT :  if any of the pointers is NULL
229  *    or if only one of the time values is 0, and also if the
230  *    workload or the contract type values are unknown in the
231  *    enumerations.
232  **/
233 int frsh_contract_set_basic_params
234   (frsh_contract_t *contract,
235    const frsh_rel_time_t      *budget_min,
236    const frsh_rel_time_t      *period_max,
237    const frsh_workload_t      workload,
238    const frsh_contract_type_t contract_type);
239
240 /**
241  * frsh_contract_get_basic_params()
242  *
243  * This operation obtains from the specified contract object its
244  * budget, period, and workload, and copies them to the places pointed
245  * to by the corresponding output parameters.
246  *
247  * @param[in] contract   the pointer to the contract object
248  * @param[out] budget_min pointer to preallocated space
249  * @param[out] period_max pointer to preallocated space
250  * @param[out] workload pointer to preallocated space
251  * @param[out] contract_type pointer to preallocated space
252  *
253  * @return   0 if no error \n
254  *       FRSH_ERR_BAD_ARGUMENT :  if one of the contract or
255  *                                    pointers is NULL.
256  *
257  **/
258 int frsh_contract_get_basic_params
259   (const frsh_contract_t *contract,
260    frsh_rel_time_t  *budget_min,
261    frsh_rel_time_t  *period_max,
262    frsh_workload_t   *workload,
263    frsh_contract_type_t *contract_type);
264
265 /**
266  * frsh_contract_set_resource_and_label()
267  *
268  * Specify resource_id and type, and the contract label. Otherwise
269  * default values will apply. If the contract label is too long it is truncated
270  *
271  * @return   0 if no error \n
272  *       FRSH_ERR_BAD_ARGUMENT :  if the contract pointer is NULL.
273  **/
274 int frsh_contract_set_resource_and_label
275   (frsh_contract_t *contract,
276    const frsh_resource_type_t resource_type,
277    const frsh_resource_id_t resource_id,
278    const char *contract_label);
279
280
281 /**
282  * frsh_contract_get_resource_and_label()
283  *
284  * Obtain the resource_id and type, and the contract label.
285  *
286  * @return   0 if no error \n
287  *       FRSH_ERR_BAD_ARGUMENT :  if the contract or the contract_label
288  *                                 pointer is NULL.
289  **/
290 int frsh_contract_get_resource_and_label
291   (const frsh_contract_t *contract,
292    frsh_resource_type_t *resource_type,
293    frsh_resource_id_t *resource_id,
294    char *contract_label);
295
296
297 /**
298  * frsh_contract_set_timing_reqs()
299  *
300  * The operation updates the specified contract object, specifying
301  * additional time-related requirements.
302  *
303  * @param  contract The pointer to the contract object
304  *
305  * @param [in] d_equals_t It is a boolean value, set to true (1) if
306  *                        we want to specify a deadline different from
307  *                        the period  for the contract.
308  * @param [in] deadline  If the previous parameter is set to true,
309  *                       this parameter is ignored (the contract value
310  *                       will be NULL_DEADLINE internally). Otherwise, it
311  *                       contains the desired deadline value.
312  * @param [in] budget_overrun_signal contains the number of the signal
313  *                that must be raised if the budget of the vres is overrun.
314  *                If the value of this parameter is FRSH_NULL_SIGNAL, no signal will
315  *                be raised.
316  * @param [in] budget_overrun_siginfo contains the value that will be
317  *               passed to the signal "catcher" when the signal is raised.
318  *               This parameters is not used if the budget_overrun_signal
319  *               parameter is set to FRSH_NULL_SIGNAL.
320  * @param [in] deadline_miss_signal contains the number of the
321  *               signal that must be raised if the deadline of the
322  *               vres is missed. If the value of this parameter is
323  *               FRSH_NULL_SIGNAL, no signal is raised.
324  * @param [in] deadline_miss_siginfo contains the value that will be
325  *                passed to the signal "catcher" when the signal is
326  *                raised.  This parameter is not used if the
327  *                deadline_signal parameter is set to NULL_SIGNAL
328  *
329  * @return  0 if successful\n
330  *     FRSH_ERR_BAD_ARGUMENT :  if contract is NULL \b or \n
331  *      (d_equals_t is true and  deadline is not FRSH_NULL_DEADLINE) \b or \n
332  *      (budget_overrun_signal is not a valid signal)  \b or \n
333  *      (deadline_miss_signal is not a valid signal) \b or \n
334  *      (d_equals_t is false but (deadline is FRSH_NULL_DEADLINE or its value
335  *                                is grater than the contract's maximum period))
336  *
337  **/
338 int frsh_contract_set_timing_reqs
339   (frsh_contract_t *contract,
340    const bool                   d_equals_t,
341    const frsh_rel_time_t        *deadline,
342    const frsh_signal_t          budget_overrun_signal,
343    const frsh_signal_info_t     budget_overrun_siginfo,
344    const frsh_signal_t          deadline_miss_signal,
345    const frsh_signal_info_t     deadline_miss_siginfo);
346
347 /**
348  * frsh_contract_get_timing_reqs()
349  *
350  * The operation obtains the corresponding input parameters from the
351  * specified contract object. If d_equals_t is true, the deadline will
352  * be set to FRSH_NULL_DEADLINE.
353  *
354  * @return 0 if no error \n
355  *   FRSH_ERR_BAD_ARGUMENT :  if contract is NULL
356  *
357  **/
358 int frsh_contract_get_timing_reqs
359   (const frsh_contract_t *contract,
360    bool                    *d_equals_t,
361    frsh_rel_time_t         *deadline,
362    frsh_signal_t           *budget_overrun_signal,
363    frsh_signal_info_t      *budget_overrun_siginfo,
364    frsh_signal_t           *deadline_miss_signal,
365    frsh_signal_info_t      *deadline_miss_siginfo);
366
367
368 /*@}*/
369
370 //////////////////////////////////////////////////////////////////
371 //                 SYNCHRONIZATION OBJECTS
372 //////////////////////////////////////////////////////////////////
373 /**
374  * @defgroup synch  Synchronization objects
375  * @ingroup core
376  *
377  * Synchronisation objects provide an alternative to timers for
378  * bounded-workload vres to signal the end of their current job and
379  * return their remaining budget to FRSH.
380  *
381  * Instead of asking to be reactivated based on an absolute time, they
382  * queue themselves in a synchronisation object and will wait there
383  * until another vres (bounded-workload or indeterminate-workload)
384  * wakes them up with a signal call the earliest at the beginning of
385  * the next period.
386  *
387  * Indeterminate-workload vres cannot queue themselves here because
388  * they don't have any budget to return.  However they can signal on
389  * the objects to activate a waiting workload vres.
390  *
391  * For classical signal/wait synchronisation paradigms the application
392  * must use whatever mechanism the underlying OS provides.
393  *
394  * In the future we may add a broadcast operation that would signal a
395  * group of synchronization objects. We have not included a broadcast
396  * service in this version because it can be easily created by the
397  * user by signalling individual synchronization objects inside a
398  * loop.
399  *
400  * Notice that for synchronization objects there is no naming service
401  * like in shared objects because tasks that use synchronization are
402  * not developed independently, as they are closely coupled.
403  *
404  * @{
405  **/
406
407
408 /**
409  * frsh_synchobj_create()
410  *
411  * This operation creates and initializes a synchronization object
412  * variable managed by the scheduler, and returns a handle to it in
413  * the variable pointed to by synch_handle.
414  *
415  * @param[out]  synch_handle pointer to the variable that will contain
416  *                  the handle to the newly created synchronization object
417  *
418  * @return      0 if the operation is succesful
419  *              FRSH_ERR_TOO_MANY_SYNCH_OBJS if the number of synchronization
420  *              objects in the system has already exceeded the maximum
421  *
422  *   FRSH_ERR_TOO_MANY_SYNCH_OBJS : if the number of synchronization
423  *      objects in the system has already exceeded the maximum\n
424  *   .
425  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
426  *      scheduled under FRSH\n
427  *   .
428  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not running
429  *
430  **/
431 int frsh_synchobj_create
432     (frsh_synchobj_handle_t *synch_handle);
433
434
435 /**
436  * frsh_synchobj_destroy()
437  *
438  * This operation destroys the synchronization object (created by a
439  * previous call to frsh_synchobj_create) that is referenced by the
440  * synch_handle variable. After calling this operation, the
441  * synch_handle variable can not be used until it is initialized again
442  * by a call to frsh_synchobj_create.
443  *
444  * @param synch_handle the handle to the synchronization object
445  *            to be destroyed
446  *
447  * @return 0 if no error \n
448  *   FRSH_ERR_BAD_ARGUMENT :  if synch_handle is 0\n
449  *   FRSH_ERR_INVALID_SYNCH_OBJ_HANDLE if the handle is not valid\n
450  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
451  *                            scheduled under FRSH \n
452  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
453  *                            running
454  *
455  * @sa frsh_synchobj_create
456  **/
457 int frsh_synchobj_destroy
458     (const frsh_synchobj_handle_t synchobj_handle);
459
460 /**
461  * frsh_synchobj_wait()
462  *
463  * This operation is invoked by threads associated with bounded
464  * workload vres to indicate that a job has been completed (and
465  * that the scheduler may reassign the unused capacity of the current
466  * job to other vres).
467  *
468  * As a difference with frsh_timed_wait(), here the vres
469  * specifies to be awakened by the arrival of a signal operation
470  * instead of at a precise point of time.
471  *
472  * This function can also be called to schedule the first job of a
473  * recently created vres and make it start when a signal operation
474  * has been sent from another thread.
475  *
476  * The vres' budget will be made zero for the remainder of the vres'
477  * period, and FRSH will not replenish it until an event has been
478  * notified to the synchronisation object by another vres.
479  *
480  * It can happen that the synchronisation object has notification
481  * events queued from the past, in this case one of the events is
482  * dequeued immediately and the vres won't have to wait for another
483  * one.
484  *
485  * At the time of reception of a notification event (wether in the
486  * future or in the past), all pending budget replenishments (if any)
487  * are made effective. Once the vres has a positive budget and the
488  * scheduler schedules the calling thread again, the call returns and
489  * the vres continues executing.
490  *
491  * If the synchronisation object is destroyed while the vres was
492  * waiting on it, the vres will be awaken and the function will
493  * return with a code FRSH_ERR_INVALID_SYNCH_OBJ_HANDLE
494  *
495  * Except for those parameters equal to NULL pointers, the system
496  * reports the current period and budget for the current job, it informs
497  * if the deadline of the previous job was missed or not, and whether the
498  * budget of the previous job was overrun or not.
499  *
500  * In a system with hierarchical scheduling, since this call makes the
501  * budget of the current period zero, the other threads in the same
502  * vres are not run. As mentioned above, only when the call finishes
503  * the budget may be replenished.
504  *
505  * @param synch_handle   Synchronisation object upon which the vres
506  *                       will be waiting.
507  * @param next_budget[out]  Upon return of this function, the variable
508  *                          pointed by this function will be equal to
509  *                                      the current vres budget. If this parameter is
510  *                                      set to NULL, no action is taken
511  * @param next_period[out]  The vres period upon return (ignored if NULL).
512  * @param was_deadline_missed Upon return whether the deadline was
513  *                           missed in the previous period.
514  * @param was_budget_overran
515  *
516  *
517  * @return 0 if success \n
518  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
519  *               running \n
520  *   FRSH_ERR_INTERNAL_ERROR : erroneous binding or malfunction of the FRSH
521  *     main scheduler \n
522  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not scheduled
523  *     under FRSH \n
524  *   FRSH_ERR_NOT_BOUND : if the calling thread does not have a valid
525  *            vres bound to it \n
526  *   FRSH_ERR_BAD_ARGUMENT : if synch_handle is 0 \n
527  *   FRSH_ERR_INVALID_SYNCH_OBJ_HANDLE if the synch_handle is not
528  *          valid or was destroyed while the vres was waiting on it. \n
529  *   FRSH_ERR_VRES_WORKLOAD_NOT_COMPATIBLE: if the kind of workload of the vres
530  *     is not FRSH_BOUNDED
531  *
532  **/
533 int frsh_synchobj_wait
534   (const frsh_synchobj_handle_t  synch_handle,
535    frsh_rel_time_t               *next_budget,
536    frsh_rel_time_t               *next_period,
537    bool                          *was_deadline_missed,
538    bool                          *was_budget_overran);
539
540
541 /**
542  * frsh_synchobj_wait_with_timeout()
543  *
544  * This call is the same as frsh_synchobj_wait() but with an extra
545  * absolute timeout. The timed_out argument, indicates whether the
546  * function returned because of the expiration of the timeout or not.
547  *
548  * @return 0 if no error \n
549  *   FRSH_ERR_INVALID_SYNCH_OBJ_HANDLE if the synch_handle is not
550  *        valid \n
551  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
552  *        running \n
553  *   FRSH_ERR_INTERNAL_ERROR : erroneous binding or malfunction of the FRSH
554  *     main scheduler \n
555  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not scheduled
556  *     under FRSH \n
557  *   FRSH_ERR_NOT_BOUND : if the calling thread does not have a valid
558  *     vres bound to it \n
559  *   FRSH_ERR_BAD_ARGUMENT :  if the synch_handle given is not valid or the
560  *     abs_timeout argument is NULL or its value is in the past\n
561  *   FRSH_ERR_VRES_WORKLOAD_NOT_COMPATIBLE: if the kind of workload of the vres
562  *     is not FRSH_BOUNDED
563  *
564  **/
565 int frsh_synchobj_wait_with_timeout
566   (const frsh_synchobj_handle_t  synch_handle,
567    const frsh_abs_time_t         *abs_timeout,
568    bool                          *timed_out,
569    frsh_rel_time_t               *next_budget,
570    frsh_rel_time_t               *next_period,
571    bool                          *was_deadline_missed,
572    bool                          *was_budget_overran);
573
574 /**
575  * frsh_synchobj_signal()
576  *
577  * This function sends a notification event to the synchronization object
578  * specified as parameter. If there is at least one vres waiting on
579  * the synchronization object, it is awaken. If more than one vres
580  * are waiting, just one of them is awaken. However, which one is
581  * awaken is implementation dependent. If no vres is waiting on the
582  * synchronization object, the notification event is queued.
583  *
584  * @param [in] synch_handle the handle of the synchronization object to
585  *                 notify.
586  *
587  * @return 0 if no error \n
588  *    FRSH_ERR_BAD_ARGUMENT :  if synch_handle is 0 \n
589  *    FRSH_ERR_INVALID_SYNCH_OBJ_HANDLE if the handle is not valid \n
590  *    FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
591  *            scheduled under FRSH \n
592  *    FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
593  *            running \n
594  *    FRSH_ERR_TOO_MANY_EVENTS_IN_SYNCH_OBJ : if the number of events stored
595  *            in the synchronization object reaches the maximum defined in the
596  *            configuration parameter header file
597  *
598  * @sa frsh_synchobj_wait, frsh_synchobj_wait_with_timeout
599  **/
600 int frsh_synchobj_signal
601     (const frsh_synchobj_handle_t synch_handle);
602
603 /**
604  * frsh_timed_wait()
605  *
606  * This operation is invoked by threads associated with bounded
607  * workload vres to indicate that a job has been completed (and
608  * that the scheduler may reassign the unused capacity of the current
609  * job to other vres). It is also invoked when the first job of
610  * such threads has to be scheduled.
611  *
612  * As an effect, the system will make the current vres's budget zero
613  * for the remainder of the vres's period, and will not replenish
614  * the budget until the specified absolute time.  At that time, all
615  * pending budget replenishments (if any) are made effective. Once the
616  * vres has a positive budget and the scheduler schedules the
617  * calling thread again, the call returns and at that time, except for
618  * those parameters equal to NULL pointers, the system reports the
619  * current period and budget for the current job, whether the deadline
620  * of the previous job was missed or not, and whether the budget of
621  * the previous job was overrun or not.
622  *
623  * In a system with hierarchical scheduling, since this call makes the
624  * budget zero, the other threads in the same vres are not run. As
625  * mentioned abobe, only when the call finishes the budget may be
626  * replenished.
627  *
628  * @param [in] abs_time     absolute time at which the budget will be
629  *                          replenished
630  *
631  * @param [out] next_budget upon return of this function, the variable
632  *                          pointed by this function will be equal to
633  *                          the current vres budget. If this parameter is
634  *                          set to NULL, no action is taken.
635  *
636  * @param [out] next_period upon return of this function, the variable
637  *                          pointed by this function will be equal to
638  *                          the current vres period. If this parameter is
639  *                          set to NULL, no action is taken.
640  *
641  * @param [out] was_deadline_missed upon return of this function, the
642  *                          variable pointed by this function will be
643  *                          equal to true if the previous vres deadline
644  *                          was missed, to false otherwise. If this
645  *                          parameter is set to NULL, no action is
646  *                          taken.
647  *
648  * @param [out] was_budget_overrun upon return of this function, the
649  *                          variable pointed by this function will be
650  *                          equal to true if the previous vres budget was
651  *                          overrun, to false otherwise. If this
652  *                          parameter is set to NULL, no action is
653  *                          taken.
654  *
655  * @return  0 if the operation is successful \n
656  *   FRSH_ERR_TIME_SPEC_IN_THE_PAST if the absolute time specification
657  *            is in the past. \n
658  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong
659  *             or not running \n
660  *   FRSH_ERR_INTERNAL_ERROR : erroneous binding or malfunction of the FRSH
661  *                                    main scheduler \n
662  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling
663  *                      thread is not scheduled under FRSH \n
664  *   FRSH_ERR_NOT_BOUND : if the calling thread does not have a valid
665  *     vres bound to it \n
666  *   FRSH_ERR_BAD_ARGUMENT :  if abs_time is NULL \n
667  *   FRSH_ERR_VRES_WORKLOAD_NOT_COMPATIBLE: if the kind of workload of the vres
668  *     is not FRSH_BOUNDED
669  *
670  *
671  * @sa frsh_synchobj_wait, frsh_synchobj_wait_with_timeout
672  **/
673 int frsh_timed_wait
674   (const frsh_abs_time_t *abs_time,
675    frsh_rel_time_t       *next_budget,
676    frsh_rel_time_t       *next_period,
677    bool                  *was_deadline_missed,
678    bool                  *was_budget_overran);
679
680
681
682 /**
683  * frsh_vresperiod_wait()
684  *
685  * Suspend the calling thread until the start of the specified virtual
686  * resource period of a vres_id, for a synchronized workload.
687  *
688  * Virtual resource periods are numbered. The number of the current
689  * period can be obtained through the frsh_vres_get_period_number()
690  * operation.
691  **/
692 int frsh_vresperiod_wait(unsigned long period_num,
693                          frsh_rel_time_t       *next_budget,
694                          frsh_rel_time_t       *next_period,
695                          bool                  *was_deadline_missed,
696                          bool                  *was_budget_overran);
697
698
699 /**
700  * frsh_vres_get_period()
701  *
702  * Return the current period number, for a virtual resource of
703  * synchronized workload.
704  **/
705 int frsh_vres_get_period_number
706    (const frsh_vres_id_t vres,
707     long *period_num);
708
709
710
711
712 /*@}*/
713
714 ///////////////////////////////////////////////////////////////////
715 //                 CONTRACT NEGOCIATION OPERATIONS
716 ///////////////////////////////////////////////////////////////////
717
718 /**
719  * @defgroup negotiate Negotiate contract functions
720  * @ingroup core
721  *
722  * The following functions are used to negotiate contracts and thus
723  * creating vres which are the execution image of a contract.
724  * Besides, these functions allow to assign and unassign threads to
725  * vres.
726  *
727  * In the case of more than one thread per vres please refer to the
728  * hierarchical module.
729  *
730  * @{
731  **/
732
733
734
735 /**
736  * frsh_contract_negotiate()
737  *
738  * The operation negotiates a contract and if accepted it will return
739  * a vres_id.  It will also check that the given contract_label is unique
740  * within the node.
741  *
742  * If the on-line admission test is enabled, it determines whether the
743  * contract can be admitted or not based on the current contracts
744  * established in the system. Then it creates the vres and
745  * recalculates all necessary parameters for the contracts already
746  * present in the system.
747  *
748  * This is a potentially blocking operation, it returns when the
749  * system has either rejected the contract, or admitted it and made it
750  * effective. No thread is bound to the newly created vres, which
751  * will be idle until a thread is bound to it via frsh_thread_bind()
752  * or frsh_thread_create_and_bind().
753  *
754  * This operation can only be executed by threads that are already
755  * bound to an active vres and therefore are being scheduled by the
756  * frsh scheduler.
757  *
758  * @return 0 if successful \n
759  *   FRSH_ERR_CONTRACT_REJECTED:  The contract is not accepted.\n
760  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
761  *                running \n
762  *   FRSH_ERR_INTERNAL_ERROR : erroneous binding or malfunction of the FRSH
763  *     main scheduler \n
764  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not scheduled
765  *     under FRSH scheduler \n
766  *   FRSH_ERR_BAD_ARGUMENT :  if the contract or vres arguments are NULL \n
767  *   FRSH_ERR_TOO_MANY_VRES : if there is no space for more vres
768  *     (the maximum number of them is already reached) \n
769  *   FRSH_ERR_CONTRACT_LABEL_ALREADY_EXISTS : contract_label is not unique.
770  *
771  **/
772 int frsh_contract_negotiate
773   (const frsh_contract_t *contract,
774    frsh_vres_id_t        *vres);
775
776 /**
777  * frsh_thread_create_and_bind()
778  *
779  * This operation creates a thread and binds it to an existing vres.
780  *
781  * This is the preferred way to add threads to the application because
782  * we make sure that the thread won't become unbound.
783  *
784  * The vres must not have any thread binded to it.  If you want to
785  * bind more than one thread to the vres you must use the
786  * hierarchical module.
787  *
788  * The frsh_thread_attr_t parameter is overwritten as necessary to
789  * introduce the adequate scheduling policy and priority,  according
790  * to the preemption level given in the contract and the
791  * frsh_priority_map() function defined by the user.
792  *
793  * @param[in] vres_id       vres with which the thread will be bound.
794  * @param[out] thread         frsh_thread_id returned by the system.
795  * @param attr         pthread_attr taken and maybe corrected
796  *                                by the system.  Ignored if NULL
797  * @param[in] thread_code     Thread function (void func(void *) )
798  *                                that will constitute the main of the
799  *                                thread.
800  * @param[in] arg             Argument for data to be passed at the
801  *                            thread. Set to NUL if you don't want
802  *                            to do anything like this.
803  *
804  * @return  0 if successful \n
805  *   FRSH_ERR_BAD_ARGUMENT : if the contract or vres arguments are
806  *               NULL \n
807  *   FRSH_ERR_CONTRACT_REJECTED : if the contract is rejected. \n
808  *   .
809  *   It may also return all the errors that may be returned by the
810  *     fosa_thread_create() function call
811  *
812  **/
813 int frsh_thread_create_and_bind
814   (const frsh_vres_id_t vres,
815    frsh_thread_id_t     *thread,
816    frsh_thread_attr_t   *attr,
817    frsh_thread_code_t   thread_code,
818    void                 *arg);
819
820
821 /**
822  * frsh_thread_create_in_background()
823  *
824  * This function creates a "background contract" that does not need to
825  * be negotiated, creates a threads and binds to the new vres.
826  *
827  * This method is suggested as a way to initate components and
828  * plugins.  The event that triggers the component activation should
829  * arrive to an application thread that could use this function to
830  * create a thread used in its initialisation.
831  *
832  * The attribute parameter is overwritten as necessary to introduce
833  * the adequate scheduling policy and priority.
834  *
835  * @param[in]  thread_code   Function to be executed by the new
836  *                           thread.
837  * @param[in]  thread_arg    General pointer that will be passed to
838  *                           the new thread as initialisation data.
839  * @param attr               Pthread attribute with thread params
840  * @param[in]  contract_label   Contract label for the new vres.
841  * @param[out] thread_id     Id of the new thread.
842  * @param[out] vres_id       vres_id of the new vres.
843  *
844  * @return 0 if successful \n
845  *   FRSH_ERR_BAD_ARGUMENT : Any problems with the argument \n
846  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is
847  *               not scheduled under FRSH \n
848  *   FRSH_ERR_CONTRACT_LABEL_ALREADY_EXISTS : contract_label is not unique \n
849  *   FRSH_ERR_TOO_MANY_VRES : if there is no space for more vres
850  *     (the maximum number of them is already reached)
851  *
852  **/
853 int frsh_thread_create_in_background
854     (frsh_thread_code_t thread_code,
855      const void *thread_arg,
856      const char *contract_label,
857      frsh_thread_attr_t *attr,
858      frsh_thread_id_t *thread_id,
859      frsh_vres_id_t *vres_id);
860
861
862
863 /**
864  * frsh_thread_join_in_background()
865  *
866  * This function is aimed for external threads to join FRSH in the
867  * least disturbing possible mode.
868  *
869  * Upon calling this function by a thread which has been created
870  * natively in the OS, it joins the framework and gets inmediately
871  * bound to an on-the-fly generated background contract.
872  *
873  * In order to gain a higher priority then it can renegotiate a
874  * contract for its vres with the needed parameters.
875  *
876  * @param[in] resource_id:   Resource (CPU) in which the vres will
877  *                           be associated.
878  *
879  * @param[in] resource_type: Resource type (should be CPU) for the
880  *                           contract.
881  *
882  * @param[in] label:         Label to be added to the contract.
883  *
884  * @param[out] vres_id:      Resulting vres_id from the on-the-fly
885  *                           generated background contract.
886  *
887  * @return  0 if successful\n
888  *          FRSH_ERR_NOT_CONTRACTED_VRES if the thread is already
889  *          bound to a vres.
890  **/
891 int frsh_thread_join_in_background(frsh_resource_id_t resource_id,
892                                    frsh_resource_type_t resource_type,
893                                    const char *label,
894                                    frsh_vres_id_t *vres_id);
895
896
897
898 /**
899  * frsh_thread_bind()
900  *
901  * This operation associates a thread with a vres, which means that
902  * it starts consuming the vres's budget  and is executed according
903  * to the contract established for that vres. If the thread is
904  * already bound to another vres, it is effectively unbound from it
905  * and bound to the specified one.
906  *
907  * It fails if the vres's policy is different than FRSH_NONE, or if
908  * there is already a thread bound to this vres.  In order to bind
909  * more than one vres to the same thread you must use the
910  * hierarchical module.
911  *
912  * Implementation dependent issue: In order to allow the usage of
913  * application defined schedulers, the given thread must not have the
914  * scheduling policy SCHED_APP and at the same time be attached to an
915  * application scheduler different than the frsh scheduler.
916  *
917  * @return 0 if successful \n
918  *   FRSH_ERR_INTERNAL_ERROR : erroneous binding or malfunction of the FRSH
919  *     main scheduler \n
920  *   FRSH_ERR_UNKNOWN_APPSCHEDULED_THREAD : if the thread is attached to
921  *     an application defined scheduler different than the frsh
922  *     scheduler \n
923  *   FRSH_ERR_BAD_ARGUMENT : if the vres value does not complain with the
924  *     expected format or valid range or the given thread does not
925  *     exist \n
926  *   FRSH_ERR_NOT_CONTRACTED_VRES : if the referenced vres is not
927  *      valid \n
928  *   FRSH_ERR_VRES_WORKLOAD_NOT_COMPATIBLE: if the kind of workload
929  *     of the vres is FRSH_OVERHEAD \n
930  *   FRSH_ERR_ALREADY_BOUND : if the given vres has a thread already
931  *     bound
932  *
933  **/
934 int frsh_thread_bind
935   (const frsh_vres_id_t   vres,
936    const frsh_thread_id_t thread);
937
938
939 /**
940  * frsh_thread_unbind()
941  *
942  * This operation unbinds a thread from a vres.  Since threads with
943  * no vres associated are not allowed to execute, they remain in a
944  * dormant state until they are either eliminated or bound again.
945  *
946  * If the thread is inside a critical section the effects of this call
947  * are deferred until the critical section is ended
948  *
949  * Implementation dependent issue: in the implementation with an
950  * application scheduler, the thread is still attached to the frsh
951  * scheduler, but suspended.
952  *
953  * @return 0 if successful \n
954  *   FRSH_ERR_INTERNAL_ERROR : erroneous binding or malfunction of the FRSH
955  *     main scheduler \n
956  *   FRSH_ERR_BAD_ARGUMENT : if the given thread does not exist \n
957  *   FRSH_ERR_NOT_SCHEDULED_THREAD : if the given thread is not scheduled
958  *     under FRSH \n
959  *   FRSH_ERR_UNKNOWN_APPSCHEDULED_THREAD : if the thread is attached to
960  *     an application defined scheduler different than the frsh
961  *     scheduler \n
962  *   FRSH_ERR_NOT_BOUND : if the given thread does not have a valid
963  *     vres bound to it
964  **/
965 int frsh_thread_unbind(const frsh_thread_id_t thread);
966
967 /**
968  * frsh_thread_get_vres_id()
969  *
970  * This operation stores the Id of the vres associated with the
971  * specified thread in the variable pointed to by vres. It returns
972  * an error if the thread does not exist, it is not under the control
973  * of the scheduling framework, or is not bound.
974  *
975  * @return 0 if no error \n
976  *   FRSH_ERR_NOT_SCHEDULED_THREAD : if the given thread is not scheduled
977  *     under FRSH \n
978  *   FRSH_ERR_NOT_BOUND : if the given thread does not have a valid
979  *     vres bound to it \n
980  *   FRSH_ERR_BAD_ARGUMENT : if the given thread does not exist or the
981  *     vres argument is NULL
982  *
983  **/
984 int frsh_thread_get_vres_id(const frsh_thread_id_t       thread,
985                     frsh_vres_id_t *vres_id);
986
987 /**
988  * frsh_vres_get_priority()
989  *
990  * This operation stores the priority currently associated
991  * with the specified vres in the variable pointed to by
992  * priority. It returns an error if the vres_id is not recognised.
993  *
994  * @return 0 if no error \n
995  *   FRSH_ERR_BAD_ARGUMENT :  if the contract argument is NULL or the value
996  *     of the vres argument is not in range \n
997  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
998  *     scheduled under FRSH \n
999  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1000  *        running \n
1001  *   FRSH_ERR_NOT_CONTRACTED_VRES: if the vres of the calling thread
1002  *     has been cancelled or it is not valid
1003  *
1004  **/
1005 int frsh_vres_get_priority
1006      (frsh_vres_id_t            vres_id,
1007       int *priority);
1008
1009 /**
1010  * frsh_vres_get_contract()
1011  *
1012  * This operation stores the contract parameters currently associated
1013  * with the specified vres in the variable pointed to by
1014  * contract. It returns an error if the vres_id is not recognised.
1015  *
1016  * @return 0 if no error \n
1017  *   FRSH_ERR_BAD_ARGUMENT :  if the contract argument is NULL or the value
1018  *     of the vres argument is not in range \n
1019  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1020  *     scheduled under FRSH \n
1021  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1022  *        running \n
1023  *   FRSH_ERR_NOT_CONTRACTED_VRES: if the vres of the calling thread
1024  *     has been cancelled or it is not valid
1025  *
1026  **/
1027 int frsh_vres_get_contract
1028   (const frsh_vres_id_t vres,
1029     frsh_contract_t *contract);
1030
1031 /**
1032  * frsh_resource_get_vres_from_label()
1033  *
1034  * This operation retrieves the vres_id whose contract_label
1035  * corresponds to the parameter in the resource_id and resource_type
1036  * specified.
1037  *
1038  * The contract label must refer to a contract negotiated
1039  * in the same processing node in which the call is being
1040  * made. Otherwise an error is returned.
1041  *
1042  * @return 0 if no error \n
1043  *   FRSH_ERR_BAD_ARGUMENT :  if the contract argument is NULL or the
1044  *     contract_label is NULL \n
1045  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1046  *     scheduled under FRSH \n
1047  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1048  *     running \n
1049  *   FRSH_ERR_CONTRACT_LABEL_UNKNOWN: if the contract_label is not known
1050  **/
1051 int frsh_resource_get_vres_from_label
1052   (const char *contract_label,
1053    const frsh_resource_type_t resource_type,
1054    const frsh_resource_id_t resource_id,
1055    frsh_vres_id_t *vres);
1056
1057
1058
1059 /**
1060  * frsh_contract_cancel()
1061  *
1062  * The operation eliminates the specified vres
1063  * and recalculates all necessary parameters for the contracts
1064  * remaining in the system. This is a potentially blocking operation;
1065  * it returns when the system has made the changes effective.
1066  *
1067  * Note that the thread is not eliminated.  We leave the application
1068  * the option to either freeze it for a later use or bind it to
1069  * another vres.
1070  *
1071  * @return 0 if successful \n
1072  *   FRSH_ERR_BAD_ARGUMENT :  if the value of vres is not in range \n
1073  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1074  *     scheduled under FRSH \n
1075  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1076  *     running \n
1077  *   FRSH_ERR_NOT_CONTRACTED_VRES : if the vres of the calling thread
1078  *     has been cancelled or it is not valid
1079  *
1080  **/
1081 int frsh_contract_cancel (const frsh_vres_id_t vres);
1082
1083
1084 /**
1085  * frsh_contract_renegotiate_sync()
1086  *
1087  * The operation renegotiates a contract for an existing vres. If
1088  * the on-line admission test is enabled it determines whether the
1089  * contract can be admitted or not based on the current contracts
1090  * established in the system. If it cannot be admitted, the old
1091  * contract remains in effect and an error is returned. If it can be
1092  * admitted, it recalculates all necessary parameters for the
1093  * contracts already present in the system and returns zero. This is a
1094  * potentially blocking operation; it returns when the system has
1095  * either rejected the new contract, or admitted it and made it
1096  * effective.
1097  *
1098  *  @return 0 if successful \n
1099  *   FRSH_ERR_BAD_ARGUMENT :  if the new_contract argument is NULL or the
1100  *     value of the vres argument is not in range \n
1101  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1102  *     scheduled under FRSH \n
1103  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1104  *     running \n
1105  *   FRSH_ERR_NOT_CONTRACTED_VRES : if the vres of the calling thread
1106  *     has been cancelled or it is not valid \n
1107  *   FRSH_ERR_CONTRACT_REJECTED : if the renegotiation fails
1108  *
1109  **/
1110 int frsh_contract_renegotiate_sync
1111   (const frsh_contract_t *new_contract,
1112    const frsh_vres_id_t vres);
1113
1114
1115 /**
1116  * frsh_contract_renegotiate_async()
1117  *
1118  * The operation enqueues a renegotiate operation for an existing
1119  * vres, and returns immediately. The renegotiate operation is
1120  * performed asynchronously, as soon as it is practical; meanwhile the
1121  * system operation will continue normally. When the renegotiation is
1122  * made, if the on-line admission test is enabled it determines
1123  * whether the contract can be admitted or not based on the current
1124  * contracts established in the system. If it cannot be admitted, the
1125  * old contract remains in effect. If it can be admitted, it
1126  * recalculates all necessary parameters for the contracts already
1127  * present in the system.
1128  *
1129  * When the operation is completed, notification is made to the
1130  * caller, if requested, via a signal. The status of the operation (in
1131  * progress, admitted, rejected) can be checked with the
1132  * frsh_vres_get_renegotiation_status() operation.  The argument
1133  * sig_notify can be FRSH_NULL_SIGNAL (no notification), or any FRSH
1134  * signal value and in this case signal_info is to be sent with the signal.
1135  *
1136  * @param[in] new_contract   New contract parameters for the new
1137  *                           situation if approved.
1138  * @param[in] vres           vres_id on which to do the renegotiation.
1139  * @param[in] signal_to_notify  Signal number to use to notify vres of
1140  *                           the negotiation result.  If
1141  *                           FRSH_NULL_SIGNAL,  no signal will be raised.
1142  * @param[in] signal_info:   Associated info that will come with the
1143  *                           signal.  This parameter will be ignored
1144  *                           if signal_to_notify == FRSH_NULL_SIGNAL.
1145  *
1146  * @return 0 if no error \n
1147  *   FRSH_ERR_BAD_ARGUMENT :  if the new_contract argument is NULL, the
1148  *     value of the vres argument is not in range or sig_notify is
1149  *     neither NULL nor a valid POSIX signal \n
1150  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1151  *     scheduled under FRSH \n
1152  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1153  *     running \n
1154  *   FRSH_ERR_NOT_CONTRACTED_VRES : if the vres of the calling thread
1155  *     has been cancelled or it is not valid
1156  *
1157  **/
1158 int frsh_contract_renegotiate_async
1159   (const frsh_contract_t *new_contract,
1160    const frsh_vres_id_t vres,
1161    const frsh_signal_t                     signal_to_notify,
1162    const frsh_signal_info_t                signal_info);
1163
1164
1165 /**
1166  * frsh_vres_get_renegotiation_status()
1167  *
1168  * The operation reports on the status of the last renegotiation
1169  * operation enqueued for the specified vres. It is callable even
1170  * after notification of the completion of such operation, if
1171  * requested.
1172  *
1173  * If the vres is not and has not been involved in any of the
1174  * frsh_contract_renegotiate_async() or frsh_group_change_mode_async()
1175  * operations, the status returned is FRSH_RS_NOT_REQUESTED
1176  *
1177  * @return 0 if no error \n
1178  *   FRSH_ERR_BAD_ARGUMENT :  if the renegotiation_status argument is
1179  *     NULL or the value of the vres argument is not in range \n
1180  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1181  *     scheduled under FRSH \n
1182  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1183  *     running \n
1184  *   FRSH_ERR_NOT_CONTRACTED_VRES : if the vres of the calling thread
1185  *     has been cancelled or it is not valid
1186  *
1187  **/
1188 int frsh_vres_get_renegotiation_status
1189   (const frsh_vres_id_t vres,
1190    frsh_renegotiation_status_t *renegotiation_status);
1191
1192 /*@}*/
1193
1194 ////////////////////////////////////////////////////////////////////////
1195 //           CHANGE OF MODE: GROUPS OF CONTRACTS
1196 ////////////////////////////////////////////////////////////////////////
1197 /**
1198  * @defgroup groupofcontracts Group of contracts
1199  * @ingroup core
1200  *
1201  * The following functions are used to negotiate atomically more than
1202  * one contract.  This allows to build complex applications by
1203  * splitting them in individual contracts that are negotiated
1204  * simultaneously and atomically.
1205  *
1206  * @{
1207  **/
1208
1209 /**
1210  * frsh_group_change_mode_sync()
1211  *
1212  * This function performs a set of negotiation operations which can
1213  * include: adding new contracts (neg), modifying existing vres (reneg)
1214  * or cancelling existing vres (cancel).
1215  *
1216  * If one of the group operations has a NULL value, unless it causes an
1217  * inconsistency the system will suppose that no operation of that
1218  * type (neg, reneg or cancel) should be done.
1219  *
1220  * The virtual resources resulting from negotiations of new contracts are
1221  * returned in the parameter 'new_vres' which must be provided by the user.
1222  *
1223  * If the on-line admission test is enabled, FRSH analizes the
1224  * schedulability of the context that would  result in the new
1225  * contract situation with removed, changed and added contracts.
1226  *
1227  * A successful return code will mean that all contracts have been
1228  * accepted  and all required operations (creation, cancellation or
1229  * update of vres) have been carried out to reach the new running
1230  * context.
1231  *
1232  * If any of the contracts is not accepted a corresponding error shall be
1233  * returned and no changes will be made to the previously running context.
1234  *
1235  * This call is a synchronous, potentially blocking operation.  It
1236  * returns when the system has rejected the contracts or accepted
1237  * and made them effective.
1238  *
1239  * @param[in] contracts_to_neg    List of new contracts to negotiate
1240  * @param[in] contracts_to_reneg  List of contracts to renegotiate
1241  * @param[in] vres_to_reneg       List of vres to renegotiate
1242  * @param[in] vres_to_cancel      List of vres to cancel
1243  * @param[out] new_vres           List of vres of new contracts.
1244  *
1245  * @return 0 if no error \n
1246  *    FRSH_ERR_BAD_ARGUMENT Invalid pointer or group identifier. \n
1247  *    FRSH_ERR_CONTRACT_LABEL_ALREADY_EXISTS contract_label not unique. \n
1248  *    FRSH_ERR_NOT_CONTRACTED_VRES: One of the provided vres_ids
1249  *       is not recognised. \n
1250  *    FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD The calling thread
1251  *       is not scheduled under FRSH. \n
1252  *    FRSH_ERR_INVALID_SCHEDULER_REPLY: The scheduler is wrong or
1253  *       not running. \n
1254  *    FRSH_ERR_INTERNAL_ERROR:  Erroneous binding or malfunction of
1255  *       FRSH main scheduler. \n
1256  *    FRSH_ERR_REJECTED_CONTRACT:  The negotiation of one of the
1257  *       proposed contracts has failed.
1258  **/
1259 int frsh_group_change_mode_sync
1260                             (const frsh_contracts_group_t *contracts_to_neg,
1261                              const frsh_contracts_group_t *contracts_to_reneg,
1262                              const frsh_vres_group_t      *vres_to_reneg,
1263                              const frsh_vres_group_t      *vres_to_cancel,
1264                              frsh_vres_group_t            *new_vres);
1265
1266 /**
1267  * frsh_group_change_mode_async()
1268  *
1269  * This is an asynchronous (non-blocking) version of the previous function,
1270  * frsh_group_change_mode_sync() and thus, it returns inmediately.
1271  *
1272  * The status of the change of mode and the identifiers for new virtual
1273  * resources must be requested with the function 'frsh_group_get_status'
1274  * by using the return parameter 'group'.
1275  *
1276  * As in the asynchronous renegotiations, when the operation is completed,
1277  * the user is notified with a signal so he can check the final result with
1278  * 'frsh_group_get_status'. In case that FRSH_NULL_SIGNAL is used, no
1279  * signal will be sent to the user.
1280  *
1281  * @param[in] contracts_to_neg    List of new contracts to negotiate
1282  * @param[in] contracts_to_reneg  List of contracts to renegotiate
1283  * @param[in] vres_to_reneg       List of vres to renegotiate
1284  * @param[in] vres_to_cancel      List of vres to cancel
1285  * @param[in] signal              Signal number to notify completion of
1286  *                                the change of mode. If FRSH_NULL_SIGNAL,
1287  *                                no signal will be raised.
1288  * @param[in] signal_info         Data associated to the signal
1289  * @param[out] group              The group identifier to get the status
1290  *
1291  * @return 0 if no error \n
1292  *    FRSH_ERR_BAD_ARGUMENT Invalid pointer, signal or group identifier. \n
1293  *    FRSH_ERR_CONTRACT_LABEL_ALREADY_EXISTS contract_label not unique. \n
1294  *    FRSH_ERR_NOT_CONTRACTED_VRES: One of the provided vres_ids
1295  *       is not recognised. \n
1296  *    FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD The calling thread
1297  *       is not scheduled under FRSH. \n
1298  *    FRSH_ERR_INVALID_SCHEDULER_REPLY: The scheduler is wrong or
1299  *       not running. \n
1300  *    FRSH_ERR_INTERNAL_ERROR:  Erroneous binding or malfunction of
1301  *       FRSH main scheduler.
1302  **/
1303 int frsh_group_change_mode_async
1304                             (const frsh_contracts_group_t *contracts_to_neg,
1305                              const frsh_contracts_group_t *contracts_to_reneg,
1306                              const frsh_vres_group_t      *vres_to_reneg,
1307                              const frsh_vres_group_t      *vres_to_cancel,
1308                              const frsh_signal_t          signal,
1309                              const frsh_signal_info_t     signal_info,
1310                              frsh_group_id_t              *group);
1311
1312 /**
1313  * frsh_group_get_status()
1314  *
1315  * This function is similar to 'frsh_vres_get_renegotiation_status' but it
1316  * is intented for group negotiations (changes of mode).
1317  *
1318  * The operation reports on the status of the last negotiation
1319  * operation enqueued for the specified group identifier.
1320  *
1321  * The status value can be one of the following values:
1322  *
1323  *    - FRSH_RS_IN_PROGRESS: the change of mode is in progress
1324  *    - FRSH_RS_REJECTED: the change of mode was not accepted
1325  *    - FRSH_RS_ADMITTED: the change of mode was accepted
1326  *    - FRSH_RS_NOT_REQUESTED: no change of mode has been requested yet
1327  *
1328  * When status returns 'FRSH_RS_ADMITTED', the array 'new_vres' contains
1329  * the values for the new virtual resources (if there was any).
1330  *
1331  * This function frees the group identifier when status returns something
1332  * different than FRSH_RS_IN_PROGRESS so further calls to this function may
1333  * return inconsistent values.
1334  *
1335  * @param[in]  group     The group identifier
1336  * @param[out] status    The status of the change of mode
1337  * @param[out] new_vres  List of vres for the negotiation of new contracts.
1338  *
1339  * @return 0 if no error \n
1340  *    FRSH_ERR_BAD_ARGUMENT Invalid pointer, signal or group identifier. \n
1341  *    FRSH_ERR_CONTRACT_LABEL_ALREADY_EXISTS contract_label not unique. \n
1342  *    FRSH_ERR_NOT_CONTRACTED_VRES: One of the provided vres_ids
1343  *       is not recognised. \n
1344  *    FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD The calling thread
1345  *       is not scheduled under FRSH. \n
1346  *    FRSH_ERR_INVALID_SCHEDULER_REPLY: The scheduler is wrong or
1347  *       not running. \n
1348  *    FRSH_ERR_INTERNAL_ERROR:  Erroneous binding or malfunction of
1349  *       FRSH main scheduler.
1350  **/
1351 int frsh_group_get_status(const frsh_group_id_t       group,
1352                           frsh_renegotiation_status_t *status,
1353                           frsh_vres_group_t           *new_vres);
1354
1355
1356 /*@}*/
1357
1358
1359 ////////////////////////////////////////////////////
1360 //           OBTAINING INFORMATION FROM THE SCHEDULER
1361 ////////////////////////////////////////////////////
1362
1363 /**
1364  * @defgroup getschedinfo Obtaining information from the scheduler
1365  *
1366  * @ingroup core
1367  *
1368  * The following functions are used to obtain available budget and
1369  * resource usage in the system.  They can be used to adapt the
1370  * execution of threads according to the load of the system.
1371  *
1372  * @{
1373  **/
1374
1375
1376 /**
1377  * frsh_config_is_admission_test_enabled()
1378  *
1379  * Returns true if the system is
1380  * configured with the on-line admission test enabled, or false
1381  * otherwise.  This situation can only be changed at compile time.
1382  **/
1383 bool frsh_config_is_admission_test_enabled();
1384
1385 /**
1386  * frsh_vres_get_usage()
1387  *
1388  * This function stores the current execution time spent by the
1389  * threads bound to the specified vres in the variable pointed to by
1390  * cpu_time.
1391  *
1392  * @return 0 if successful \n
1393  *   FRSH_ERR_BAD_ARGUMENT : if the value of the vres argument is not in range or
1394  *     cpu_time is NULL \n
1395  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1396  *     scheduled under FRSH \n
1397  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1398  *     running \n
1399  *   FRSH_ERR_NOT_CONTRACTED_VRESR : if the vres of the calling thread
1400  *     has been cancelled or it is not valid
1401  *
1402  **/
1403 int frsh_vres_get_usage
1404    (const frsh_vres_id_t vres,
1405     frsh_rel_time_t *spent);
1406
1407
1408 /**
1409  * frsh_vres_get_job_usage()
1410  *
1411  * Get the execution time of the current job of the specified virtual
1412  * resource for a bounded or synchronized workload.
1413  **/
1414 int frsh_vres_get_job_usage
1415     (const frsh_vres_id_t vres,
1416      frsh_rel_time_t *spent);
1417
1418
1419 /**
1420  * frsh_vres_get_remaining_budget()
1421  *
1422  * This function stores in the variable pointed to by budget the
1423  * remaining execution-time budget associated with the specified
1424  * vres.
1425  *
1426  *  @return 0 if successful \n
1427  *   FRSH_ERR_BAD_ARGUMENT : if the value of the vres argument is not in range or
1428  *     budget is NULL \n
1429  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1430  *     scheduled under FRSH \n
1431  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1432  *     running \n
1433  *   FRSH_ERR_NOT_CONTRACTED_VRES : if the vres of the calling thread
1434  *     has been cancelled or it is not valid
1435  *
1436  **/
1437 int frsh_vres_get_remaining_budget
1438    (const frsh_vres_id_t vres,
1439     frsh_rel_time_t *budget);
1440
1441
1442 /**
1443  * frsh_vres_get_budget_and_period()
1444  *
1445  * This function stores in the variables
1446  * pointed to by budget and period, the execution-time budget and the
1447  * period respectively associated with the specified vres. If any of
1448  * these pointers is NULL, the corresponding information is not stored.
1449  *
1450  * @return 0 if successful \n
1451  *   FRSH_ERR_BAD_ARGUMENT : if the value of the vres argument is not in range,
1452  *     or budget and period are both NULL \n
1453  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1454  *     scheduled under FRSH \n
1455  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1456  *     running \n
1457  *   FRSH_ERR_NOT_CONTRACTED_VRES : if the vres of the calling thread
1458  *     has been cancelled or it is not valid
1459  *
1460  **/
1461 int frsh_vres_get_budget_and_period
1462    (const frsh_vres_id_t vres,
1463     frsh_rel_time_t *budget,
1464     frsh_rel_time_t *period);
1465
1466 /*@}*/
1467
1468 /////////////////////////////////////////////////////////////////////
1469 //           SERVICE THREAD TUNING
1470 /////////////////////////////////////////////////////////////////////
1471 /**
1472  * @defgroup servthtuning Service thread tuning
1473  *
1474  * @ingroup core
1475  *
1476  * The following functions are used to obtain available budget and
1477  * resource usage in the system.  They can be used to adapt the
1478  * execution of threads to the load of the system.
1479  *
1480  * @{
1481  **/
1482
1483 /**
1484  * frsh_service_thread_set_data()
1485  *
1486  * This function allows the application to change the period and
1487  * budget of the service thread that makes the
1488  * negotiations. Increasing the utilization of this thread makes the
1489  * negotiations faster, but introduces additional load in the system
1490  * that may decrease the bandwidth available for the vres. For this
1491  * call, the system will make a schedulability analysis to determine
1492  * if the new situation is acceptable or not. This is reported back in
1493  * the variable pointed to by accepted. If the new service thread data
1494  * is accepted, the system will reassign budgets and periods to the
1495  * vres according to the new bandwidth available, in the same way
1496  * as it does for a regular contract negotiation.
1497  *
1498  * When its budget is exhausted, the service thread may run in the
1499  * background
1500  *
1501  * The service thread starts with a default budget and period that are
1502  * configurable
1503  *
1504  * Implementation dependency: in the fixed priority implementtaion of
1505  * frsh, the default priority is lower than the priority of any vres,
1506  * but higher than the background. According to the
1507  * implementation-dependent module the priority is adjustable by means
1508  * of a function that changes its preemption level
1509  *
1510  * @return 0 if successful \n
1511  *   FRSH_ERR_BAD_ARGUMENT : if any of the pointer arguments is NULL or
1512  *     the budget value is greater than the period value \n
1513  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1514  *     scheduled under FRSH \n
1515  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1516  *     running \n
1517  *   FRSH_ERR_NOT_CONTRACTED_VRES : if the vres of the calling thread
1518  *     has been cancelled or it is not valid
1519  *
1520  **/
1521 int frsh_service_thread_set_data (const frsh_rel_time_t *budget,
1522                                   const frsh_rel_time_t *period,
1523                                   bool                  *accepted);
1524
1525 /**
1526  * frsh_service_thread_get_data()
1527  *
1528  * this function returns in the variables pointed by budget and
1529  * period, respectively, the current budget and period of the service
1530  * thread.
1531  *
1532  * @return 0 if successful \n
1533  *   FRSH_ERR_BAD_ARGUMENT : if any of the pointer arguments is NULL \n
1534  *   FRSH_ERR_NOT_SCHEDULED_CALLING_THREAD : if the calling thread is not
1535  *     scheduled under FRSH \n
1536  *   FRSH_ERR_INVALID_SCHEDULER_REPLY : the scheduler is wrong or not
1537  *     running \n
1538  *   FRSH_ERR_NOT_CONTRACTED_VRES : if the vres of the calling thread
1539  *     has been cancelled or it is not valid
1540  *
1541  **/
1542 int frsh_service_thread_get_data
1543    (frsh_rel_time_t *budget,
1544     frsh_rel_time_t *period);
1545
1546
1547 /*@}*/
1548
1549
1550 ////////////////////////////////////////////////////////////////////////
1551 //           BACKGROUND MANAGEMENT
1552 ////////////////////////////////////////////////////////////////////////
1553
1554 //A round-robin background scheduling policy is available for those
1555 //threads that do not have real-time requirements. Because some of
1556 //these threads may require sharing information with other threads run
1557 //by regular vres.  Special background contracts may be created for
1558 //specifying the synchronization requirements.
1559
1560 //The way of specifying a background contract is by setting
1561 //contract_type to FRSH_CT_BACKGROUND. Negotiation may fail if the contract uses
1562 //shared_objects. If the contract has no shared_objects the returned
1563 //vres id represents the background and may be used to bind more
1564 //than one thread. If the contract has shared objects a vres is
1565 //created to keep track of them, but the associated threads are
1566 //executed in the background, together with the other background
1567 //threads
1568
1569
1570 FRSH_CPP_END_DECLS
1571
1572 #endif // _FRSH_CORE_H_