Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] new public function to get declared categories
[simgrid.git] / examples / msg / tracing / ms.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 /** @addtogroup MSG_examples
8  * 
9  * - <b>tracing/ms.c</b> This is a master/slave program where the master creates
10  * tasks, send them to the slaves. For each task received, the slave executes
11  * it and then destroys it. This program uses several tracing functions that
12  * enable the tracing of categorized resource utilization, the use of trace marks,
13  * and user variables associated to the hosts of the platform file.
14  * You might want to run this program with the following parameters:
15  * --cfg=tracing/categorized:1
16  * --cfg=tracing/uncategorized:1
17  * --cfg=triva/categorized:triva_cat.plist
18  * --cfg=triva/uncategorized:triva_uncat.plist
19  * (See \ref tracing_tracing_options for details)
20  */
21
22 #include <stdio.h>
23 #include "msg/msg.h"
24 #include "xbt/sysdep.h"         /* calloc, printf */
25
26 /* Create a log channel to have nice outputs. */
27 #include "xbt/log.h"
28 #include "xbt/asserts.h"
29 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
30                              "Messages specific for this msg example");
31
32 int master(int argc, char *argv[]);
33 int slave(int argc, char *argv[]);
34
35 /** Emitter function  */
36 int master(int argc, char *argv[])
37 {
38   long number_of_tasks = atol(argv[1]);
39   double task_comp_size = atof(argv[2]);
40   double task_comm_size = atof(argv[3]);
41   long slaves_count = atol(argv[4]);
42
43   //setting the variable "is_master" (previously declared) to value 1
44   TRACE_host_variable_set(MSG_host_self()->name, "is_master", 1);
45
46   TRACE_mark("msmark", "start_send_tasks");
47   int i;
48   for (i = 0; i < number_of_tasks; i++) {
49     m_task_t task = NULL;
50     task = MSG_task_create("task", task_comp_size, task_comm_size, NULL);
51
52     //setting the variable "task_creation" to value i
53     TRACE_host_variable_set(MSG_host_self()->name, "task_creation", i);
54
55     //setting the category of task to "compute"
56     //the category of a task must be defined before it is sent or executed
57     MSG_task_set_category(task, "compute");
58     MSG_task_send(task, "master_mailbox");
59   }
60   TRACE_mark("msmark", "finish_send_tasks");
61
62   for (i = 0; i < slaves_count; i++) {
63     m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
64     MSG_task_set_category(finalize, "finalize");
65     MSG_task_send(finalize, "master_mailbox");
66   }
67
68   return 0;
69 }
70
71 /** Receiver function  */
72 int slave(int argc, char *argv[])
73 {
74   m_task_t task = NULL;
75
76   TRACE_host_variable_set(MSG_host_self()->name, "is_slave", 1);
77   while (1) {
78     MSG_task_receive(&(task), "master_mailbox");
79
80     if (!strcmp(MSG_task_get_name(task), "finalize")) {
81       MSG_task_destroy(task);
82       break;
83     }
84     //adding the value returned by MSG_task_get_compute_duration(task)
85     //to the variable "task_computation"
86     TRACE_host_variable_add(MSG_host_self()->name,
87                             "task_computation",
88                             MSG_task_get_compute_duration(task));
89     MSG_task_execute(task);
90     MSG_task_destroy(task);
91     task = NULL;
92   }
93   return 0;
94 }
95
96 /** Main function */
97 int main(int argc, char *argv[])
98 {
99   MSG_global_init(&argc, argv);
100   if (argc < 3) {
101     printf("Usage: %s platform_file deployment_file\n", argv[0]);
102     exit(1);
103   }
104
105   char *platform_file = argv[1];
106   char *deployment_file = argv[2];
107   MSG_create_environment(platform_file);
108
109   //declaring user variables
110   TRACE_host_variable_declare("is_slave");
111   TRACE_host_variable_declare("is_master");
112   TRACE_host_variable_declare("task_creation");
113   TRACE_host_variable_declare("task_computation");
114
115   //declaring user markers
116   TRACE_declare_mark("msmark");
117
118   //declaring user categories with RGB colors (values from 0 to 1)
119   TRACE_category_with_color ("compute", "1 0 0");  //compute is red
120   TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
121   //categories without user-defined colors receive
122   //random colors generated by the tracing system
123   TRACE_category ("request");
124   TRACE_category_with_color ("report", NULL);
125
126   MSG_function_register("master", master);
127   MSG_function_register("slave", slave);
128   MSG_launch_application(deployment_file);
129
130   MSG_main();
131   MSG_clean();
132   return 0;
133 }                               /* end_of_main */