Logo AND Algorithmique Numérique Distribuée

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