Logo AND Algorithmique Numérique Distribuée

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