Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace xbt_heap by boost::heap::fibonacci_heap
[simgrid.git] / src / surf / surf_interface.hpp
1 /* Copyright (c) 2004-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SURF_MODEL_H_
8 #define SURF_MODEL_H_
9
10 #include <xbt.h>
11 #include <string>
12 #include <vector>
13 #include <memory>
14 #include <boost/function.hpp>
15 #include <boost/intrusive/list.hpp>
16 #include <boost/heap/fibonacci_heap.hpp>
17 #include <boost/smart_ptr.hpp>
18 #include "surf/trace_mgr.h"
19 #include "xbt/lib.h"
20 #include "surf/surf_routing.h"
21 #include "simgrid/platf_interface.h"
22 #include "surf/surf.h"
23 #include "surf/surf_private.h"
24 #include "internal_config.h"
25
26 #ifdef LIBSIGC
27 #include <sigc++/sigc++.h>
28 #define surf_callback(arg1, ...)  sigc::signal<arg1,__VA_ARGS__>
29 #define surf_callback_connect(callback, fun_ptr) callback.connect(sigc::ptr_fun(fun_ptr))
30 #define surf_callback_emit(callback, ...) callback.emit(__VA_ARGS__)
31 #else
32 #include <boost/signals2.hpp>
33 #define surf_callback(arg1, ...)  boost::signals2::signal<arg1(__VA_ARGS__)>
34 #define surf_callback_connect(callback, fun_ptr) callback.connect(fun_ptr)
35 #define surf_callback_emit(callback, ...) callback(__VA_ARGS__)
36 #endif
37
38 extern tmgr_history_t history;
39 #define NO_MAX_DURATION -1.0
40
41 using namespace std;
42
43 /*********
44  * Utils *
45  *********/
46
47 /* user-visible parameters */
48 extern double sg_tcp_gamma;
49 extern double sg_sender_gap;
50 extern double sg_latency_factor;
51 extern double sg_bandwidth_factor;
52 extern double sg_weight_S_parameter;
53 extern int sg_network_crosstraffic;
54 #ifdef HAVE_GTNETS
55 extern double sg_gtnets_jitter;
56 extern int sg_gtnets_jitter_seed;
57 #endif
58 extern xbt_dynar_t surf_path;
59
60 extern "C" {
61 XBT_PUBLIC(double) surf_get_clock(void);
62 }
63
64 extern double sg_sender_gap;
65 XBT_PUBLIC(int)  SURF_CPU_LEVEL;    //Surf cpu level
66
67 extern surf_callback(void, void) surfExitCallbacks;
68
69 int __surf_is_absolute_file_path(const char *file_path);
70
71 /***********
72  * Classes *
73  ***********/
74 //class Model;
75 typedef Model* ModelPtr;
76
77 //class Resource;
78 typedef Resource* ResourcePtr;
79
80 //class Action;
81 typedef Action* ActionPtr;
82
83 typedef boost::intrusive::list<Action> ActionList;
84 typedef ActionList* ActionListPtr;
85 typedef boost::intrusive::list_base_hook<> actionHook;
86
87 struct lmmTag;
88 typedef boost::intrusive::list<Action, boost::intrusive::base_hook<boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > > > ActionLmmList;
89 typedef ActionLmmList* ActionLmmListPtr;
90 typedef boost::intrusive::list_base_hook<boost::intrusive::tag<lmmTag> > actionLmmHook;
91
92
93 template <typename K, typename V>
94 class Heap {
95   class HeapItem;
96   struct compare_PI : binary_function <boost::shared_ptr<HeapItem>, boost::shared_ptr<HeapItem>, bool> {
97     bool operator() (boost::shared_ptr<HeapItem> x, boost::shared_ptr<HeapItem> y) const {
98       return (x->p_key==y->p_key) ? x->m_i<y->m_i : x->p_key>y->p_key;}
99   };
100 public:
101   typedef typename boost::heap::fibonacci_heap<boost::shared_ptr<HeapItem>, boost::heap::compare<compare_PI > >::handle_type HeapHandle;
102   typedef boost::shared_ptr<HeapHandle> HeapHandleSPtr;
103 private:
104   class HeapItem {
105   public:
106     HeapItem(K key, V value, int i)
107      : p_key(key), p_value(value), m_i(i) {
108     }
109     K p_key;
110     V p_value;
111     int m_i;
112     HeapHandleSPtr sp_handle;
113   };
114   boost::heap::fibonacci_heap<boost::shared_ptr<HeapItem>, boost::heap::compare<compare_PI > > m_heap;
115 public:
116   int m_i;
117   Heap() : m_i(0) {}
118   HeapHandleSPtr push(K key, V value) {
119     boost::shared_ptr<HeapItem> hi(new HeapItem(key, value, m_i++));
120     hi->sp_handle = boost::shared_ptr<HeapHandle>(new HeapHandle(m_heap.push(hi)));
121     return hi->sp_handle;
122   }
123   void pop() {
124     HeapHandleSPtr hl = m_heap.top()->sp_handle;
125     m_heap.pop();
126     hl->node_ = NULL;
127   }
128   void erase(HeapHandleSPtr handle) {
129     if (used(handle)) {
130       m_heap.erase(*handle);
131       handle->node_ = NULL;
132     }
133   }
134   K topKey() {return m_heap.top()->p_key;}
135   V topValue() {return m_heap.top()->p_value;}
136   int size() {return m_heap.size();}
137   bool empty() {return m_heap.empty();}
138   bool used(HeapHandleSPtr handle) {return handle && handle->node_!=NULL;}
139 };
140
141 typedef Heap<double, ActionPtr> ActionHeap;
142
143 enum heap_action_type{
144   LATENCY = 100,
145   MAX_DURATION,
146   NORMAL,
147   NOTSET
148 };
149
150 /*********
151  * Trace *
152  *********/
153 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
154 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
155 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
156 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
157 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
158 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
159 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
160
161 /*********
162  * Model *
163  *********/
164 XBT_PUBLIC_DATA(xbt_dynar_t) model_list;
165
166 /** @ingroup SURF_interface
167  * @brief SURF model interface class
168  * @details A model is an object which handle the interactions between its Resources and its Actions
169  */
170 XBT_PUBLIC_CLASS Model {
171 public:
172   /**
173    * @brief Model constructor
174    *
175    * @param name the name of the model
176    */
177   Model(const char *name);
178
179   /**
180    * @brief Model destructor
181    */
182   virtual ~Model();
183
184   virtual void addTraces() =0;
185
186   /**
187    * @brief Get the name of the current Model
188    *
189    * @return The name of the current Model
190    */
191   const char *getName() {return p_name;}
192
193   /**
194    * @brief Get the set of [actions](@ref Action) in *ready* state
195    *
196    * @return The set of [actions](@ref Action) in *ready* state
197    */
198   virtual ActionListPtr getReadyActionSet() {return p_readyActionSet;}
199
200   /**
201    * @brief Get the set of [actions](@ref Action) in *running* state
202    *
203    * @return The set of [actions](@ref Action) in *running* state
204    */
205   virtual ActionListPtr getRunningActionSet() {return p_runningActionSet;}
206
207   /**
208    * @brief Get the set of [actions](@ref Action) in *failed* state
209    *
210    * @return The set of [actions](@ref Action) in *failed* state
211    */
212   virtual ActionListPtr getFailedActionSet() {return p_failedActionSet;}
213
214   /**
215    * @brief Get the set of [actions](@ref Action) in *done* state
216    *
217    * @return The set of [actions](@ref Action) in *done* state
218    */
219   virtual ActionListPtr getDoneActionSet() {return p_doneActionSet;}
220
221   /**
222    * @brief Get the set of modified [actions](@ref Action)
223    *
224    * @return The set of modified [actions](@ref Action)
225    */
226   virtual ActionLmmListPtr getModifiedSet() {return p_modifiedSet;}
227
228   /**
229    * @brief Get the maxmin system of the current Model
230    *
231    * @return The maxmin system of the current Model
232    */
233   lmm_system_t getMaxminSystem() {return p_maxminSystem;}
234
235   /**
236    * @brief Get the update mechanism of the current Model
237    * @see e_UM_t
238    *
239    * @return [description]
240    */
241   e_UM_t getUpdateMechanism() {return p_updateMechanism;}
242
243   /**
244    * @brief Get Action heap
245    * @details [TODO]
246    *
247    * @return The Action heap
248    */
249   ActionHeap *getActionHeap() {return p_actionHeap;}
250
251   /**
252    * @brief share the resources
253    * @details Share the resources between the actions
254    *
255    * @param now The current time of the simulation
256    * @return The delta of time till the next action will finish
257    */
258   virtual double shareResources(double now);
259   virtual double shareResourcesLazy(double now);
260   virtual double shareResourcesFull(double now);
261   double shareResourcesMaxMin(ActionListPtr running_actions,
262                                       lmm_system_t sys,
263                                       void (*solve) (lmm_system_t));
264
265   /**
266    * @brief Update state of actions
267    * @details Update action to the current time
268    *
269    * @param now The current time of the simulation
270    * @param delta The delta of time since the last update
271    */
272   virtual void updateActionsState(double now, double delta);
273   virtual void updateActionsStateLazy(double now, double delta);
274   virtual void updateActionsStateFull(double now, double delta);
275
276 protected:
277   ActionLmmListPtr p_modifiedSet;
278   lmm_system_t p_maxminSystem;
279   e_UM_t p_updateMechanism;
280   int m_selectiveUpdate;
281   ActionHeap *p_actionHeap;
282
283 private:
284   const char *p_name;
285
286   ActionListPtr p_readyActionSet; /**< Actions in state SURF_ACTION_READY */
287   ActionListPtr p_runningActionSet; /**< Actions in state SURF_ACTION_RUNNING */
288   ActionListPtr p_failedActionSet; /**< Actions in state SURF_ACTION_FAILED */
289   ActionListPtr p_doneActionSet; /**< Actions in state SURF_ACTION_DONE */
290 };
291
292 /************
293  * Resource *
294  ************/
295
296 /** @ingroup SURF_interface
297  * @brief Resource which have a metric handled by a maxmin system
298  */
299 typedef struct {
300   double scale;             /**< The scale of the metric */
301   double peak;              /**< The peak of the metric */
302   tmgr_trace_event_t event; /**< The associated trace event associated to the metric */
303 } s_surf_metric_t;
304
305 /** @ingroup SURF_interface
306  * @brief SURF resource interface class
307  * @details A resource represent an element of a component (e.g.: a link for the network)
308  */
309 XBT_PUBLIC_CLASS Resource {
310 public:
311   /**
312    * @brief Resource constructor
313    */
314   Resource();
315
316   /**
317    * @brief Resource constructor
318    *
319    * @param model Model associated to this Resource
320    * @param name The name of the Resource
321    * @param props Dictionary of properties associated to this Resource
322    */
323   Resource(ModelPtr model, const char *name, xbt_dict_t props);
324
325   /**
326    * @brief Resource constructor
327    *
328    * @param model Model associated to this Resource
329    * @param name The name of the Resource
330    * @param props Dictionary of properties associated to this Resource
331    * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
332    */
333   Resource(ModelPtr model, const char *name, xbt_dict_t props, lmm_constraint_t constraint);
334
335   /**
336    * @brief Resource constructor
337    *
338    * @param model Model associated to this Resource
339    * @param name The name of the Resource
340    * @param props Dictionary of properties associated to this Resource
341    * @param stateInit the initial state of the Resource
342    */
343   Resource(ModelPtr model, const char *name, xbt_dict_t props, e_surf_resource_state_t stateInit);
344
345   /**
346    * @brief Resource destructor
347    */
348   virtual ~Resource();
349
350   /**
351    * @brief Get the Model of the current Resource
352    *
353    * @return The Model of the current Resource
354    */
355   ModelPtr getModel();
356
357   /**
358    * @brief Get the name of the current Resource
359    *
360    * @return The name of the current Resource
361    */
362   const char *getName();
363
364   /**
365    * @brief Get the properties of the current Resource
366    *
367    * @return The properties of the current Resource
368    */
369   virtual xbt_dict_t getProperties();
370
371   /**
372    * @brief Update the state of the current Resource
373    * @details [TODO]
374    *
375    * @param event_type [TODO]
376    * @param value [TODO]
377    * @param date [TODO]
378    */
379   virtual void updateState(tmgr_trace_event_t event_type, double value, double date)=0;
380
381   /**
382    * @brief Check if the current Resource is used
383    * @return true if the current Resource is used, false otherwise
384    */
385   virtual bool isUsed()=0;
386
387   /**
388    * @brief Check if the current Resource is active
389    *
390    * @return true if the current Resource is active, false otherwise
391    */
392   bool isOn();
393
394   /**
395    * @brief Turn on the current Resource
396    */
397   void turnOn();
398
399   /**
400    * @brief Turn off the current Resource
401    */
402   void turnOff();
403
404   /**
405    * @brief Get the [state](\ref e_surf_resource_state_t) of the current Resource
406    *
407    * @return The state of the currenrt Resource
408    */
409   virtual e_surf_resource_state_t getState();
410
411   /**
412    * @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource
413    *
414    * @param state The new state of the current Resource
415    */
416   virtual void setState(e_surf_resource_state_t state);
417
418 private:
419   const char *p_name;
420   xbt_dict_t p_properties;
421   ModelPtr p_model;
422   bool m_running;
423   e_surf_resource_state_t m_stateCurrent;
424
425   /* LMM */
426 public:
427   /**
428    * @brief Get the lmm constraint associated to this Resource if it is part of a LMM component
429    *
430    * @return The lmm constraint associated to this Resource
431    */
432   lmm_constraint_t getConstraint();
433 private:
434   lmm_constraint_t p_constraint;
435 };
436
437 /**********
438  * Action *
439  **********/
440
441 /** @ingroup SURF_interface
442  * @brief SURF action interface class
443  * @details An action is an event generated by a resource (e.g.: a communication for the network)
444  */
445 XBT_PUBLIC_CLASS Action : public actionHook, public actionLmmHook {
446 private:
447   /**
448    * @brief Common initializations for the constructors
449    */
450   void initialize(ModelPtr model, double cost, bool failed,
451                   lmm_variable_t var = NULL);
452
453 public:
454   /**
455    * @brief Action constructor
456    *
457    * @param model The Model associated to this Action
458    * @param cost The cost of the Action
459    * @param failed If the action is impossible (e.g.: execute something on a switched off workstation)
460    */
461   Action(ModelPtr model, double cost, bool failed);
462
463   /**
464    * @brief Action constructor
465    *
466    * @param model The Model associated to this Action
467    * @param cost The cost of the Action
468    * @param failed If the action is impossible (e.g.: execute something on a switched off workstation)
469    * @param var The lmm variable associated to this Action if it is part of a LMM component
470    */
471   Action(ModelPtr model, double cost, bool failed, lmm_variable_t var);
472
473   /**
474    * @brief Action destructor
475    */
476   virtual ~Action();
477
478   /**
479    * @brief Finish the action
480    */
481   void finish();
482
483   /**
484    * @brief Get the [state](\ref e_surf_action_state_t) of the current Action
485    *
486    * @return The state of the current Action
487    */
488   e_surf_action_state_t getState(); /**< get the state*/
489
490   /**
491    * @brief Set the [state](\ref e_surf_action_state_t) of the current Action
492    *
493    * @param state The new state of the current Action
494    */
495   virtual void setState(e_surf_action_state_t state);
496
497   /**
498    * @brief Get the bound of the current Action
499    *
500    * @return The bound of the current Action
501    */
502   double getBound();
503
504   /**
505    * @brief Set the bound of the current Action
506    *
507    * @param bound The new bound of the current Action
508    */
509   void setBound(double bound);
510
511   /**
512    * @brief Get the start time of the current action
513    *
514    * @return The start time of the current action
515    */
516   double getStartTime();
517
518   /**
519    * @brief Get the finish time of the current action
520    *
521    * @return The finish time of the current action
522    */
523   double getFinishTime();
524
525   /**
526    * @brief Get the data associated to the current action
527    *
528    * @return The data associated to the current action
529    */
530   void *getData() {return p_data;}
531
532   /**
533    * @brief Set the data associated to the current action
534    *
535    * @param data The new data associated to the current action
536    */
537   void setData(void* data);
538
539   /**
540    * @brief Get the maximum duration of the current action
541    *
542    * @return The maximum duration of the current action
543    */
544   double getMaxDuration() {return m_maxDuration;}
545
546   /**
547    * @brief Get the category associated to the current action
548    *
549    * @return The category associated to the current action
550    */
551   char *getCategory() {return p_category;}
552
553   /**
554    * @brief Get the cost of the current action
555    *
556    * @return The cost of the current action
557    */
558   double getCost() {return m_cost;}
559
560   /**
561    * @brief Set the cost of the current action
562    *
563    * @param cost The new cost of the current action
564    */
565   void setCost(double cost) {m_cost = cost;}
566
567   /**
568    * @brief Update the maximum duration of the current action
569    *
570    * @param delta [TODO]
571    */
572   void updateMaxDuration(double delta) {double_update(&m_maxDuration, delta,sg_surf_precision);}
573
574   /**
575    * @brief Update the remaining time of the current action
576    *
577    * @param delta [TODO]
578    */
579   void updateRemains(double delta) {double_update(&m_remains, delta, sg_maxmin_precision*sg_surf_precision);}
580
581   /**
582    * @brief Set the remaining time of the current action
583    *
584    * @param value The new remaining time of the current action
585    */
586   void setRemains(double value) {m_remains = value;}
587
588   /**
589    * @brief Set the finish time of the current action
590    *
591    * @param value The new Finush time of the current action
592    */
593   void setFinishTime(double value) {m_finish = value;}
594
595   /**
596    * @brief Add a reference to the current action
597    */
598   void ref();
599
600   /**
601    * @brief Remove a reference to the current action
602    * @details If the Action has no more reference, we destroy it
603    *
604    * @return true if the action was destroyed and false if someone still has references on it
605    */
606   virtual int unref();
607
608   /**
609    * @brief Cancel the current Action if running
610    */
611   virtual void cancel();
612
613   /**
614    * @brief Recycle an Action
615    */
616   virtual void recycle(){};
617
618   /**
619    * @brief Suspend the current Action
620    */
621   virtual void suspend();
622
623   /**
624    * @brief Resume the current Action
625    */
626   virtual void resume();
627
628   /**
629    * @brief Check if the current action is running
630    *
631    * @return true if the current Action is suspended, false otherwise
632    */
633   virtual bool isSuspended();
634
635   /**
636    * @brief Set the maximum duration of the current Action
637    *
638    * @param duration The new maximum duration of the current Action
639    */
640   virtual void setMaxDuration(double duration);
641
642   /**
643    * @brief Set the priority of the current Action
644    *
645    * @param priority The new priority of the current Action
646    */
647   virtual void setPriority(double priority);
648
649 #ifdef HAVE_TRACING
650   /**
651    * @brief Set the category of the current Action
652    *
653    * @param category The new category of the current Action
654    */
655   void setCategory(const char *category);
656 #endif
657
658   /**
659    * @brief Get the remaining time of the current action after updating the resource
660    *
661    * @return The remaining time
662    */
663   virtual double getRemains();
664
665   /**
666    * @brief Get the remaining time of the current action without updating the resource
667    *
668    * @return The remaining time
669    */
670   double getRemainsNoUpdate();
671
672   /**
673    * @brief Get the priority of the current Action
674    *
675    * @return The priority of the current Action
676    */
677   double getPriority() {return m_priority;};
678
679   /**
680    * @brief Get the state set in which the action is
681    * @details [TODO]
682    *
683    * @return The state set in which the action is
684    */
685   ActionListPtr getStateSet() {return p_stateSet;};
686
687   s_xbt_swag_hookup_t p_stateHookup;
688
689   ModelPtr getModel() {return p_model;}
690
691 protected:
692   ActionListPtr p_stateSet;
693   double m_priority; /**< priority (1.0 by default) */
694   int    m_refcount;
695   double m_remains; /**< How much of that cost remains to be done in the currently running task */
696   double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */
697   double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */
698
699 private:
700   int resourceUsed(void *resource_id);
701
702   /**
703    * @brief Share the resources to the actions
704    * @details [TODO]
705    *
706    * @param now [TODO]
707    * @return in how much time the next action may terminatedescription]
708    */
709   double shareResources(double now);
710
711   /**
712    * @brief Update the current action state
713    * @details [TODO]
714    *
715    * @param now [TODO]
716    * @param delta [TODO]
717    */
718   void updateActionsState(double now, double delta);
719
720   /**
721    * @brief Update the [TODO]
722    * @details [TODO]
723    *
724    * @param id [TODO]
725    * @param event_type [TODO]
726    * @param value [TODO]
727    * @param time [TODO]
728    */
729   void updateResourceState(void *id, tmgr_trace_event_t event_type,
730                                  double value, double time);
731
732   ActionLmmListPtr p_modifiedSet;
733   int m_selectiveUpdate;
734   bool m_failed;
735   double m_start; /**< start time  */
736   char *p_category;               /**< tracing category for categorized resource utilization monitoring */
737
738   #ifdef HAVE_LATENCY_BOUND_TRACKING
739   int m_latencyLimited;               /**< Set to 1 if is limited by latency, 0 otherwise */
740   #endif
741   double    m_cost;
742   ModelPtr p_model;
743   void *p_data; /**< for your convenience */
744
745   /* LMM */
746 public:
747   void heapInsert(double key, enum heap_action_type hat);
748   void heapRemove();
749   enum heap_action_type getHeapActionType() {return m_hat;}
750   virtual void updateRemainingLazy(double now);
751   lmm_variable_t getVariable() {return p_variable;}
752   double getLastUpdate() {return m_lastUpdate;}
753   void refreshLastUpdate() {m_lastUpdate = surf_get_clock();}
754   bool is_linked() {return actionLmmHook::is_linked();}
755   void gapRemove();
756
757 protected:
758   lmm_variable_t p_variable;
759   double m_lastValue;
760   double m_lastUpdate;
761   int m_suspended;
762   ActionHeap::HeapHandleSPtr sp_heapHandle;
763   enum heap_action_type m_hat;
764 };
765
766 #endif /* SURF_MODEL_H_ */