Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / doc / doxygen / inside_extending.doc
1 /*!
2 \page inside_extending Extending SimGrid
3
4 \tableofcontents
5
6 \section simgrid_dev_guide_model How to add a new model?
7 The figure below shows the architecture of the SURF layer. This layer is composed
8 of different kinds of models representing the different systems we want to
9 model (i.e., cpu, network, storage, workstation, virtual machine).
10
11 A model in SimGrid is composed of three classes: Model, Resource and Action
12 (\ref SURF_interface "surf_interface.hpp").
13
14 \image html surf++.png
15 \image latex surf++.pdf "surf++" width=\textwidth
16
17 Actually there are five kind of models: CpuModel, NetworkModel, WorkstationModel,
18 WorkstationVMModel and StorageModel. For each kind of model, there is an
19 interface (e.g.: \ref SURF_cpu_interface "cpu_interface.hpp") and some implementations (e.g.: cpu_cas01.hpp,
20 cpu_ti.hpp).
21
22 The CPU model Cas01, for instance, is initialized by the function
23     void surf_cpu_model_init_Cas01()
24
25 The different network models that are offered by simgrid are stored in the array
26 that is defined as follows:
27
28 s_surf_model_description_t surf_network_model_description[] = {
29
30 \subsection simgrid_dev_guide_model_implem How to implement a new model?
31
32 If you want to create a new implementation of a kind of model you must extend
33 the classes of the corresponding interfaces.
34
35 For instance, if you want to add a new cup model called `Plop`, create two files
36 cpu_plop.hpp and cpu_plop_cpp which contains classes CpuPlopModel, CpuPlop and
37 CpuPlopAction implementating respectively the interfaces CpuModel, Cpu and
38 CpuAction. You also need to define a initializing function like this:
39
40 ~~~~
41 void surf_cpu_model_init_plop()
42 {
43   xbt_assert(!surf_cpu_model_pm);
44
45   surf_cpu_model_pm = new CpuPlopModel();
46
47   simgrid::surf::on_postparse.connect(cpu_add_traces);
48
49   xbt_dynar_push(model_list, &surf_cpu_model_pm);
50 }
51 ~~~~
52
53 and add an entry in the corresponding array in surf_interface.cpp
54
55 ~~~~
56 s_surf_model_description_t surf_cpu_model_description[] = {
57   {"Cas01",
58    "Simplistic CPU model (time=size/power).",
59    surf_cpu_model_init_Cas01},
60   {"Plop",
61    "The new plop CPU model.",
62    surf_cpu_model_init_plop},
63   {NULL, NULL, NULL}      // this array must be NULL terminated
64 };
65 ~~~~
66
67 \subsection simgrid_dev_guide_model_kind How to add a new kind of model?
68
69 If you want to create a new kind of model, you must create a new interface
70 where you extend the classes Model, Resource and Action, and then create an
71 implementation of this interface.
72
73
74 \section simgrid_dev_guide_surf_callbacks How to use surf callbacks?
75
76 Adding features to surf could also be handle by using surf callbacks (instead
77 of adding new implementation model). The list of available callbacks is
78 accessible there \ref SURF_callbacks. An example of using surf callbacks is the
79 energy plugin. If you want to add a plugin you need to define callback function
80 and to connect them to callbacks handler in an initialization function.
81
82 ~~~~
83 static void MyNetworkLinkCreatedCallback(NetworkLinkPtr cpu){
84   // your code
85 }
86
87 static void MyNetworkLinkDestructedCallback(NetworkLinkPtr cpu){
88   // your code
89 }
90
91 static void MyNetworkCommunicationCallback(NetworkActionPtr cpu,
92                                            RoutingEdgePtr src,
93                                            RoutingEdgePtr dst){
94   // your code
95 }
96
97 void sg_my_network_plugin_init() {
98   networkLinkCreatedCallbacks.connect(MyNetworkLinkCreatedCallback);
99   networkLinkDestructedCallbacks.connect(MyNetworkLinkDestructedCallback);
100   networkCommunicationCallbacks.connect(MyNetworkCommunicationCallback);
101 }
102 ~~~~
103
104 Then you need to add an entry in surf_interface.cpp refering to your
105 initialization function.
106
107 ~~~~
108 s_surf_model_description_t surf_plugin_description[] = {
109                   {"Energy",
110                    "Cpu energy consumption.",
111                    sg_energy_plugin_init},
112                   {"MyNetworkPlugin",
113                    "My network plugin.",
114                    sg_my_network_plugin_init},
115                   {NULL, NULL, NULL}      // this array must be NULL terminated
116 };
117 ~~~~
118
119 \section simgrid_dev_guide_simcall How to add a new simcall?
120
121 A simcall is used to go from user mode to kernel mode. There is some
122 sort of popping dance involved, as we want to isolate the user
123 contextes from their environment (so that they can run in parallel and
124 so that we can model-check them).
125
126 In short, just add a line to src/simix/simcalls.in and run the
127 src/simix/simcalls.py script. It will guide you about how to implement
128 your simcall. Please keep reading this section (only) if you want to
129 understand how it goes.
130
131
132 The workflow of a simcall is the following:
133
134 - `<ret> simcall_<name>(<args>)`
135  - `simcall_BODY_<name>(<args>)`
136   - Initializes the simcall (store the arguments in position)
137   - If maestro, executes the simcall directly (and return)
138   - If not, call `SIMIX_process_yield` to give back the control to maestro
139   - ========== KERNEL MODE ==========
140   - `SIMIX_simcall_handle` large switch (on simcall) doing for each:
141    - `simcall_HANDLER_<name>(simcall, <args>)` (the manual code handling the simcall)
142    - If the simcall is not marked as "blocking" in its definition,
143      call `SIMIX_simcall_answer(simcall)` that adds back the issuer
144      process to the list of processes to run in the next scheduling round.
145      It is thus the responsability of the blocking simcalls to call
146      `SIMIX_simcall_answer(simcall)` themselves in their handler.
147
148 Note that empty HANDLERs can be omitted. These functions usually do
149 some parameter checking, or retrieve some information about the
150 simcall issuer, but when there no need for such things, the handler
151 can be omited. In that case, we directly call the function
152 `simcall_<name>(<args>)`.
153
154 To simplify the simcall creation, a python script generates most of
155 the code and give helpers for the remaining stuff. That script reads
156 the simcall definitions from src/simix/simcalls.in, checks that both
157 `simcall_<name>()` and `simcall_HANDLER()` are defined somewhere, and
158 generates the following files:
159
160 - smx_popping_accessors.h:
161   Helper functions to get and set simcall arguments and results
162 - smx_popping_bodies.cpp:
163   The BODY function of each simcall
164 - smx_popping_enum.c:
165   Definition of type `enum e_smx_simcall_t` (one value per existing simcall)
166 - smx_popping_generated.cpp:
167   Definitions of `simcall_names[]` (debug name of each simcall), and
168   SIMIX_simcall_enter() that deals with the simcall from within the kernel
169
170 The simcall.in file list all the simcalls in sections. A line starting by "##"
171 define a new section which will be replace by a "ifdef" in the generated code.
172
173 \section simgrid_dev_guide_tag What is How to add a new tag for xml files?
174
175 You should not do something like that. Please work instead to make XML
176 avoidable, ie to make the C++ interface nice and usable.