Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
when parallel ctests are performed, using the default tracing filename may cause...
[simgrid.git] / testsuite / surf / surf_usage.c
1 /* A few basic tests for the surf library                                   */
2
3 /* Copyright (c) 2004-2012. 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 "simgrid/sg_config.h"
15 #include "surf/surf.h"
16 #include "surf/surf_resource.h"
17 #include "surf/surfxml_parse.h" // for reset callback
18
19 #include "xbt/log.h"
20 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test,
21                              "Messages specific for surf example");
22
23 const char *string_action(e_surf_action_state_t state);
24 const char *string_action(e_surf_action_state_t state)
25 {
26   switch (state) {
27   case (SURF_ACTION_READY):
28     return "SURF_ACTION_READY";
29   case (SURF_ACTION_RUNNING):
30     return "SURF_ACTION_RUNNING";
31   case (SURF_ACTION_FAILED):
32     return "SURF_ACTION_FAILED";
33   case (SURF_ACTION_DONE):
34     return "SURF_ACTION_DONE";
35   case (SURF_ACTION_NOT_IN_THE_SYSTEM):
36     return "SURF_ACTION_NOT_IN_THE_SYSTEM";
37   default:
38     return "INVALID STATE";
39   }
40 }
41
42 void test(char *platform);
43 void test(char *platform)
44 {
45   void *cpuA = NULL;
46   void *cpuB = NULL;
47   void *cardA = NULL;
48   void *cardB = NULL;
49   surf_action_t actionA = NULL;
50   surf_action_t actionB = NULL;
51   surf_action_t actionC = NULL;
52   e_surf_action_state_t stateActionA;
53   e_surf_action_state_t stateActionB;
54   e_surf_action_state_t stateActionC;
55   double now = -1.0;
56   xbt_cfg_set_parse(_sg_cfg_set, "cpu/model:Cas01");
57   xbt_cfg_set_parse(_sg_cfg_set, "network/model:CM02");
58   parse_platform_file(platform);
59
60   /*********************** CPU ***********************************/
61   XBT_DEBUG("%p", surf_cpu_model);
62   cpuA = surf_cpu_resource_by_name("Cpu A");
63   cpuB = surf_cpu_resource_by_name("Cpu B");
64
65   /* Let's check that those two processors exist */
66   XBT_DEBUG("%s : %p", surf_resource_name(cpuA), cpuA);
67   XBT_DEBUG("%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   XBT_DEBUG("actionA : %p (%s)", actionA, string_action(stateActionA));
81   XBT_DEBUG("actionB : %p (%s)", actionB, string_action(stateActionB));
82   XBT_DEBUG("actionC : %p (%s)", actionB, string_action(stateActionC));
83
84   /*********************** Network *******************************/
85   XBT_DEBUG("%p", surf_network_model);
86   cardA = sg_routing_edge_by_name_or_null("Cpu A");
87   cardB = sg_routing_edge_by_name_or_null("Cpu B");
88
89   /* Let's check that those two processors exist */
90   XBT_DEBUG("%s : %p", surf_resource_name(cardA), cardA);
91   XBT_DEBUG("%s : %p", surf_resource_name(cardB), cardB);
92
93   /* Let's do something on it */
94   surf_network_model->extension.network.communicate(cardA, cardB,
95                                                     150.0, -1.0);
96
97   surf_solve(-1.0);                 /* Takes traces into account. Returns 0.0 */
98   do {
99     surf_action_t action = NULL;
100     now = surf_get_clock();
101     XBT_DEBUG("Next Event : %g", now);
102     XBT_DEBUG("\t CPU actions");
103     while ((action =
104             xbt_swag_extract(surf_cpu_model->states.failed_action_set))) {
105       XBT_DEBUG("\t * Failed : %p", action);
106       action->model_type->action_unref(action);
107     }
108     while ((action =
109             xbt_swag_extract(surf_cpu_model->states.done_action_set))) {
110       XBT_DEBUG("\t * Done : %p", action);
111       action->model_type->action_unref(action);
112     }
113     XBT_DEBUG("\t Network actions");
114     while ((action =
115             xbt_swag_extract(surf_network_model->states.
116                              failed_action_set))) {
117       XBT_DEBUG("\t * Failed : %p", action);
118       action->model_type->action_unref(action);
119     }
120     while ((action =
121             xbt_swag_extract(surf_network_model->states.
122                              done_action_set))) {
123       XBT_DEBUG("\t * Done : %p", action);
124       action->model_type->action_unref(action);
125     }
126
127   } while ((xbt_swag_size(surf_network_model->states.running_action_set) ||
128             xbt_swag_size(surf_cpu_model->states.running_action_set)) &&
129            surf_solve(-1.0) >= 0.0);
130
131   XBT_DEBUG("Simulation Terminated");
132 }
133
134 #ifdef __BORLANDC__
135 #pragma argsused
136 #endif
137
138 int main(int argc, char **argv)
139 {
140   surf_init(&argc, argv);       /* Initialize some common structures */
141   if (argc == 1) {
142     fprintf(stderr, "Usage : %s platform.xml\n", argv[0]);
143     return 1;
144   }
145   test(argv[1]);
146
147   surf_exit();
148   return 0;
149 }