Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
no more printf (using DEBUG)
[simgrid.git] / testsuite / surf / surf_usage.c
1 /*      $Id$     */
2
3 /* A few basic tests for the surf library                                   */
4
5 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include <stdio.h>
11 #include "surf/surf.h"
12
13 #include "xbt/log.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test,"Messages specific for surf example");
15
16 const char *string_action(e_surf_action_state_t state);
17 const char *string_action(e_surf_action_state_t state)
18 {
19   switch (state) {
20   case (SURF_ACTION_READY):
21     return "SURF_ACTION_READY";
22   case (SURF_ACTION_RUNNING):
23     return "SURF_ACTION_RUNNING";
24   case (SURF_ACTION_FAILED):
25     return "SURF_ACTION_FAILED";
26   case (SURF_ACTION_DONE):
27     return "SURF_ACTION_DONE";
28   case (SURF_ACTION_NOT_IN_THE_SYSTEM):
29     return "SURF_ACTION_NOT_IN_THE_SYSTEM";
30   default:
31     return "INVALID STATE";
32   }
33 }
34
35
36 void test(char *platform);
37 void test(char *platform)
38 {
39   void *cpuA = NULL;
40   void *cpuB = NULL;
41   void *cardA = NULL;
42   void *cardB = NULL;
43   surf_action_t actionA = NULL;
44   surf_action_t actionB = NULL;
45   surf_action_t actionC = NULL;
46   surf_action_t commAB = NULL;
47   e_surf_action_state_t stateActionA;
48   e_surf_action_state_t stateActionB;
49   e_surf_action_state_t stateActionC;
50   double now = -1.0;
51
52   surf_cpu_resource_init_Cas01(platform);       /* Now it is possible to use CPUs */
53   surf_network_resource_init_CM02(platform);    /* Now it is possible to use eth0 */
54
55   /*********************** CPU ***********************************/
56   DEBUG1("%p \n", surf_cpu_resource);
57   cpuA = surf_cpu_resource->common_public->name_service("Cpu A");
58   cpuB = surf_cpu_resource->common_public->name_service("Cpu B");
59
60   /* Let's check that those two processors exist */
61   DEBUG2("%s : %p\n",
62          surf_cpu_resource->common_public->get_resource_name(cpuA), cpuA);
63   DEBUG2("%s : %p\n",
64          surf_cpu_resource->common_public->get_resource_name(cpuB), cpuB);
65
66   /* Let's do something on it */
67   actionA = surf_cpu_resource->extension_public->execute(cpuA, 1000.0);
68   actionB = surf_cpu_resource->extension_public->execute(cpuB, 1000.0);
69   actionC = surf_cpu_resource->extension_public->sleep(cpuB, 7.32);
70
71   /* Use whatever calling style you want... */
72   stateActionA = surf_cpu_resource->common_public->action_get_state(actionA);   /* When you know actionA resource type */
73   stateActionB = actionB->resource_type->common_public->action_get_state(actionB);      /* If you're unsure about it's resource type */
74   stateActionC = surf_cpu_resource->common_public->action_get_state(actionC);   /* When you know actionA resource type */
75
76   /* And just look at the state of these tasks */
77   DEBUG2("actionA : %p (%s)\n", actionA, string_action(stateActionA));
78   DEBUG2("actionB : %p (%s)\n", actionB, string_action(stateActionB));
79   DEBUG2("actionC : %p (%s)\n", actionB, string_action(stateActionC));
80
81   /*********************** Network *******************************/
82   DEBUG1("%p \n", surf_network_resource);
83   cardA = surf_network_resource->common_public->name_service("Cpu A");
84   cardB = surf_network_resource->common_public->name_service("Cpu B");
85
86   /* Let's check that those two processors exist */
87   DEBUG2("%s : %p\n",
88          surf_network_resource->common_public->get_resource_name(cardA),
89          cardA);
90   DEBUG2("%s : %p\n",
91          surf_network_resource->common_public->get_resource_name(cardB),
92          cardB);
93
94   /* Let's do something on it */
95   commAB =
96       surf_network_resource->extension_public->communicate(cardA, cardB,
97                                                            150.0,-1.0);
98
99   surf_solve();                 /* Takes traces into account. Returns 0.0 */
100   do {
101     surf_action_t action = NULL;
102     now = surf_get_clock();
103     DEBUG1("Next Event : " "%lg" "\n", now);
104     DEBUG0("\t CPU actions\n");
105     while ((action =
106             xbt_swag_extract(surf_cpu_resource->common_public->states.
107                              failed_action_set))) {
108       DEBUG1("\t * Failed : %p\n", action);
109       action->resource_type->common_public->action_free(action);
110     }
111     while ((action =
112             xbt_swag_extract(surf_cpu_resource->common_public->states.
113                              done_action_set))) {
114       DEBUG1("\t * Done : %p\n", action);
115       action->resource_type->common_public->action_free(action);
116     }
117     DEBUG0("\t Network actions\n");
118     while ((action =
119             xbt_swag_extract(surf_network_resource->common_public->states.
120                              failed_action_set))) {
121       DEBUG1("\t * Failed : %p\n", action);
122       action->resource_type->common_public->action_free(action);
123     }
124     while ((action =
125             xbt_swag_extract(surf_network_resource->common_public->states.
126                              done_action_set))) {
127       DEBUG1("\t * Done : %p\n", action);
128       action->resource_type->common_public->action_free(action);
129     }
130
131   } while (surf_solve()>=0.0);
132
133   DEBUG0("Simulation Terminated\n");
134
135   surf_finalize();
136 }
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.txt\n",argv[0]);
143      return 1;
144   }
145   test(argv[1]);
146   return 0;
147 }