Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix copyright headers
[simgrid.git] / examples / msg / tracing / categories.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include "msg/msg.h"
9 #include "xbt/sysdep.h" /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
15
16 int master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18 MSG_error_t test_all(const char *platform_file, const char *application_file);
19
20 /** Emitter function  */
21 int master(int argc, char *argv[])
22 {
23   long number_of_tasks = atol(argv[1]);
24   long slaves_count = atol(argv[4]);
25
26   int i;
27   for (i = 0; i < number_of_tasks; i++) {
28     m_task_t task = NULL;
29
30     //creating task and setting its category
31     if (i%2){
32       task = MSG_task_create ("task_compute", 10000000, 0, NULL);
33       TRACE_msg_set_task_category (task, "compute");
34     }else if (i%3){
35       task = MSG_task_create ("task_request", 10, 10, NULL);
36       TRACE_msg_set_task_category (task, "request");
37     }else{
38       task = MSG_task_create ("task_data", 10, 10000000, NULL);
39       TRACE_msg_set_task_category (task, "data");
40     }
41     MSG_task_send(task, "master_mailbox");
42   }
43   
44    for (i = 0; i < slaves_count; i++) { 
45      m_task_t finalize = MSG_task_create ("finalize", 0, 1000, 0);
46      TRACE_msg_set_task_category(finalize, "finalize");
47      MSG_task_send(finalize, "master_mailbox");
48    } 
49   
50   return 0;
51 }
52
53 /** Receiver function  */
54 int slave(int argc, char *argv[])
55 {
56   m_task_t task = NULL;
57   int res;
58
59   while(1) {
60     res = MSG_task_receive(&(task), "master_mailbox");
61
62     if (!strcmp(MSG_task_get_name(task),"finalize")) {
63       MSG_task_destroy(task);
64         break;
65     }
66
67     MSG_task_execute(task);
68     MSG_task_destroy(task);
69     task = NULL;
70   }
71   return 0;
72 }
73
74 /** Test function */
75 MSG_error_t test_all(const char *platform_file,
76                             const char *application_file)
77 {
78   MSG_error_t res = MSG_OK;
79
80   {                             /*  Simulation setting */
81     MSG_set_channel_number(0);
82     MSG_create_environment(platform_file);
83   }
84   {                            /*   Application deployment */
85     MSG_function_register("master", master);
86     MSG_function_register("slave", slave);
87     MSG_launch_application(application_file);
88   }
89   res = MSG_main();
90   
91   INFO1("Simulation time %g",MSG_get_clock());
92   return res;
93 }
94
95
96 /** Main function */
97 int main(int argc, char *argv[])
98 {
99   MSG_error_t res = MSG_OK;
100
101   //starting the simulation tracing
102   TRACE_start ("categories.trace");
103
104   //declaring user categories
105   TRACE_category ("compute");
106   TRACE_category ("request");
107   TRACE_category ("data");
108   TRACE_category ("finalize");
109
110   MSG_global_init(&argc,argv);
111   if (argc < 3) {
112      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
113      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
114      exit(1);
115   }
116   res = test_all(argv[1],argv[2]);
117   MSG_clean();
118
119   //ending the simulation tracing
120   TRACE_end();
121
122   if(res==MSG_OK)
123     return 0;
124   else
125     return 1;
126 } /* end_of_main */