Logo AND Algorithmique Numérique Distribuée

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