Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stop messing with the surf internals from the test suite, so that these internals...
[simgrid.git] / testsuite / surf / surf_usage.c
1 /* A few basic tests for the surf library                                   */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #ifdef __BORLANDC__
10 #pragma hdrstop
11 #endif
12
13 #include <stdio.h>
14 #include "surf/surf.h"
15 #include "surf/surfxml_parse.h" // for reset callback
16
17 #include "xbt/log.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test,
19                              "Messages specific for surf example");
20
21 const char *string_action(e_surf_action_state_t state);
22 const char *string_action(e_surf_action_state_t state)
23 {
24   switch (state) {
25   case (SURF_ACTION_READY):
26     return "SURF_ACTION_READY";
27   case (SURF_ACTION_RUNNING):
28     return "SURF_ACTION_RUNNING";
29   case (SURF_ACTION_FAILED):
30     return "SURF_ACTION_FAILED";
31   case (SURF_ACTION_DONE):
32     return "SURF_ACTION_DONE";
33   case (SURF_ACTION_NOT_IN_THE_SYSTEM):
34     return "SURF_ACTION_NOT_IN_THE_SYSTEM";
35   default:
36     return "INVALID STATE";
37   }
38 }
39
40 void test(char *platform);
41 void test(char *platform)
42 {
43   void *cpuA = NULL;
44   void *cpuB = NULL;
45   void *cardA = NULL;
46   void *cardB = NULL;
47   surf_action_t actionA = NULL;
48   surf_action_t actionB = NULL;
49   surf_action_t actionC = NULL;
50   e_surf_action_state_t stateActionA;
51   e_surf_action_state_t stateActionB;
52   e_surf_action_state_t stateActionC;
53   double now = -1.0;
54   xbt_cfg_set_parse(_surf_cfg_set, "cpu/model:Cas01");
55   xbt_cfg_set_parse(_surf_cfg_set, "network/model:CM02");
56   parse_platform_file(platform);
57
58   /*********************** CPU ***********************************/
59   XBT_DEBUG("%p", surf_cpu_model);
60   cpuA = surf_cpu_resource_by_name("Cpu A");
61   cpuB = surf_cpu_resource_by_name("Cpu B");
62
63   /* Let's check that those two processors exist */
64   XBT_DEBUG("%s : %p", surf_resource_name(cpuA), cpuA);
65   XBT_DEBUG("%s : %p", surf_resource_name(cpuB), cpuB);
66
67   /* Let's do something on it */
68   actionA = surf_cpu_model->extension.cpu.execute(cpuA, 1000.0);
69   actionB = surf_cpu_model->extension.cpu.execute(cpuB, 1000.0);
70   actionC = surf_cpu_model->extension.cpu.sleep(cpuB, 7.32);
71
72   /* Use whatever calling style you want... */
73   stateActionA = surf_cpu_model->action_state_get(actionA);     /* When you know actionA model type */
74   stateActionB = actionB->model_type->action_state_get(actionB);        /* If you're unsure about it's model type */
75   stateActionC = surf_cpu_model->action_state_get(actionC);     /* When you know actionA model type */
76
77   /* And just look at the state of these tasks */
78   XBT_DEBUG("actionA : %p (%s)", actionA, string_action(stateActionA));
79   XBT_DEBUG("actionB : %p (%s)", actionB, string_action(stateActionB));
80   XBT_DEBUG("actionC : %p (%s)", actionB, string_action(stateActionC));
81
82   /*********************** Network *******************************/
83   XBT_DEBUG("%p", surf_network_model);
84   cardA = surf_network_resource_by_name("Cpu A");
85   cardB = surf_network_resource_by_name("Cpu B");
86
87   /* Let's check that those two processors exist */
88   XBT_DEBUG("%s : %p", surf_resource_name(cardA), cardA);
89   XBT_DEBUG("%s : %p", surf_resource_name(cardB), cardB);
90
91   /* Let's do something on it */
92   surf_network_model->extension.network.communicate("Cpu A", "Cpu B",
93                                                     150.0, -1.0);
94
95   surf_solve(-1.0);                 /* Takes traces into account. Returns 0.0 */
96   do {
97     surf_action_t action = NULL;
98     now = surf_get_clock();
99     XBT_DEBUG("Next Event : %g", now);
100     XBT_DEBUG("\t CPU actions");
101     while ((action =
102             xbt_swag_extract(surf_cpu_model->states.failed_action_set))) {
103       XBT_DEBUG("\t * Failed : %p", action);
104       action->model_type->action_unref(action);
105     }
106     while ((action =
107             xbt_swag_extract(surf_cpu_model->states.done_action_set))) {
108       XBT_DEBUG("\t * Done : %p", action);
109       action->model_type->action_unref(action);
110     }
111     XBT_DEBUG("\t Network actions");
112     while ((action =
113             xbt_swag_extract(surf_network_model->states.
114                              failed_action_set))) {
115       XBT_DEBUG("\t * Failed : %p", action);
116       action->model_type->action_unref(action);
117     }
118     while ((action =
119             xbt_swag_extract(surf_network_model->states.
120                              done_action_set))) {
121       XBT_DEBUG("\t * Done : %p", action);
122       action->model_type->action_unref(action);
123     }
124
125   } while ((xbt_swag_size(surf_network_model->states.running_action_set) ||
126             xbt_swag_size(surf_cpu_model->states.running_action_set)) &&
127            surf_solve(-1.0) >= 0.0);
128
129   XBT_DEBUG("Simulation Terminated");
130 }
131
132 #ifdef __BORLANDC__
133 #pragma argsused
134 #endif
135
136 int main(int argc, char **argv)
137 {
138   surf_init(&argc, argv);       /* Initialize some common structures */
139   if (argc == 1) {
140     fprintf(stderr, "Usage : %s platform.xml\n", argv[0]);
141     return 1;
142   }
143   test(argv[1]);
144
145   surf_exit();
146   return 0;
147 }