Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Need to set DIR_IS_LINK for use pwd -P
[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   surf_action_t commAB = 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   surf_parse_reset_callbacks();
56   surf_cpu_model_init_Cas01(platform);  /* Now it is possible to use CPUs */
57   surf_network_model_init_CM02(platform);       /* Now it is possible to use eth0 */
58   parse_platform_file(platform);
59
60   /*********************** CPU ***********************************/
61   DEBUG1("%p", surf_cpu_model);
62   cpuA = surf_model_resource_by_name(surf_cpu_model, "Cpu A");
63   cpuB = surf_model_resource_by_name(surf_cpu_model, "Cpu B");
64
65   /* Let's check that those two processors exist */
66   DEBUG2("%s : %p", surf_resource_name(cpuA), cpuA);
67   DEBUG2("%s : %p", surf_resource_name(cpuB), cpuB);
68
69   /* Let's do something on it */
70   actionA = surf_cpu_model->extension.cpu.execute(cpuA, 1000.0);
71   actionB = surf_cpu_model->extension.cpu.execute(cpuB, 1000.0);
72   actionC = surf_cpu_model->extension.cpu.sleep(cpuB, 7.32);
73
74   /* Use whatever calling style you want... */
75   stateActionA = surf_cpu_model->action_state_get(actionA);     /* When you know actionA model type */
76   stateActionB = actionB->model_type->action_state_get(actionB);        /* If you're unsure about it's model type */
77   stateActionC = surf_cpu_model->action_state_get(actionC);     /* When you know actionA model type */
78
79   /* And just look at the state of these tasks */
80   DEBUG2("actionA : %p (%s)", actionA, string_action(stateActionA));
81   DEBUG2("actionB : %p (%s)", actionB, string_action(stateActionB));
82   DEBUG2("actionC : %p (%s)", actionB, string_action(stateActionC));
83
84   /*********************** Network *******************************/
85   DEBUG1("%p", surf_network_model);
86   cardA = surf_model_resource_by_name(surf_network_model, "Cpu A");
87   cardB = surf_model_resource_by_name(surf_network_model, "Cpu B");
88
89   /* Let's check that those two processors exist */
90   DEBUG2("%s : %p", surf_resource_name(cardA), cardA);
91   DEBUG2("%s : %p", surf_resource_name(cardB), cardB);
92
93   /* Let's do something on it */
94   commAB =
95       surf_network_model->extension.network.communicate("Cpu A", "Cpu B",
96                                                         150.0, -1.0);
97
98   surf_solve(-1.0);                 /* Takes traces into account. Returns 0.0 */
99   do {
100     surf_action_t action = NULL;
101     now = surf_get_clock();
102     DEBUG1("Next Event : %g", now);
103     DEBUG0("\t CPU actions");
104     while ((action =
105             xbt_swag_extract(surf_cpu_model->states.failed_action_set))) {
106       DEBUG1("\t * Failed : %p", action);
107       action->model_type->action_unref(action);
108     }
109     while ((action =
110             xbt_swag_extract(surf_cpu_model->states.done_action_set))) {
111       DEBUG1("\t * Done : %p", action);
112       action->model_type->action_unref(action);
113     }
114     DEBUG0("\t Network actions");
115     while ((action =
116             xbt_swag_extract(surf_network_model->states.
117                              failed_action_set))) {
118       DEBUG1("\t * Failed : %p", action);
119       action->model_type->action_unref(action);
120     }
121     while ((action =
122             xbt_swag_extract(surf_network_model->states.
123                              done_action_set))) {
124       DEBUG1("\t * Done : %p", action);
125       action->model_type->action_unref(action);
126     }
127
128   } while ((xbt_swag_size(surf_network_model->states.running_action_set) ||
129             xbt_swag_size(surf_cpu_model->states.running_action_set)) &&
130            surf_solve(-1.0) >= 0.0);
131
132   DEBUG0("Simulation Terminated");
133 }
134
135 #ifdef __BORLANDC__
136 #pragma argsused
137 #endif
138
139 int main(int argc, char **argv)
140 {
141   surf_init(&argc, argv);       /* Initialize some common structures */
142   if (argc == 1) {
143     fprintf(stderr, "Usage : %s platform.xml\n", argv[0]);
144     return 1;
145   }
146   test(argv[1]);
147
148   surf_exit();
149   return 0;
150 }