Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] categories may have colors registered in the trace file
[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,
15                              "Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file,
20                      const char *application_file);
21
22 /** Emitter function  */
23 int master(int argc, char *argv[])
24 {
25   long number_of_tasks = atol(argv[1]);
26   long slaves_count = atol(argv[4]);
27
28   int i;
29   for (i = 0; i < number_of_tasks; i++) {
30     m_task_t task = NULL;
31
32     //creating task and setting its category
33     if (i % 2) {
34       task = MSG_task_create("task_compute", 10000000, 0, NULL);
35       TRACE_msg_set_task_category(task, "compute");
36     } else if (i % 3) {
37       task = MSG_task_create("task_request", 10, 10, NULL);
38       TRACE_msg_set_task_category(task, "request");
39     } else {
40       task = MSG_task_create("task_data", 10, 10000000, NULL);
41       TRACE_msg_set_task_category(task, "data");
42     }
43     MSG_task_send(task, "master_mailbox");
44   }
45
46   for (i = 0; i < slaves_count; i++) {
47     m_task_t finalize = MSG_task_create("finalize", 0, 1000, 0);
48     TRACE_msg_set_task_category(finalize, "finalize");
49     MSG_task_send(finalize, "master_mailbox");
50   }
51
52   return 0;
53 }
54
55 /** Receiver function  */
56 int slave(int argc, char *argv[])
57 {
58   m_task_t task = NULL;
59   int res;
60
61   while (1) {
62     res = MSG_task_receive(&(task), "master_mailbox");
63
64     if (!strcmp(MSG_task_get_name(task), "finalize")) {
65       MSG_task_destroy(task);
66       break;
67     }
68
69     MSG_task_execute(task);
70     MSG_task_destroy(task);
71     task = NULL;
72   }
73   return 0;
74 }
75
76 /** Test function */
77 MSG_error_t test_all(const char *platform_file,
78                      const char *application_file)
79 {
80   MSG_error_t res = MSG_OK;
81
82   {                             /*  Simulation setting */
83     MSG_set_channel_number(0);
84     MSG_create_environment(platform_file);
85   }
86   {                             /*   Application deployment */
87     MSG_function_register("master", master);
88     MSG_function_register("slave", slave);
89     MSG_launch_application(application_file);
90   }
91   res = MSG_main();
92
93   INFO1("Simulation time %g", MSG_get_clock());
94   return res;
95 }
96
97
98 /** Main function */
99 int main(int argc, char *argv[])
100 {
101   MSG_error_t res = MSG_OK;
102
103   MSG_global_init(&argc, argv);
104   if (argc < 3) {
105     printf("Usage: %s platform_file deployment_file\n", argv[0]);
106     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
107     exit(1);
108   }
109
110   //declaring user categories with RGB colors
111   TRACE_category_with_color ("compute", "1 0 0"); //red
112   TRACE_category_with_color ("request", "0 1 0"); //green
113   TRACE_category_with_color ("data", "0 0 1");    //blue
114   TRACE_category_with_color ("finalize", "0 0 0");//black
115
116   res = test_all(argv[1], argv[2]);
117   MSG_clean();
118
119   if (res == MSG_OK)
120     return 0;
121   else
122     return 1;
123 }                               /* end_of_main */