Logo AND Algorithmique Numérique Distribuée

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