Logo AND Algorithmique Numérique Distribuée

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