Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further improve the MSG doc by documenting the examples
[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 /** @addtogroup MSG_examples
8  * 
9  * - <b>tracing/categories.c</b> example with the declaration of multiple categories
10  */
11
12 #include <stdio.h>
13 #include "msg/msg.h"
14 #include "xbt/sysdep.h"         /* calloc, printf */
15
16 /* Create a log channel to have nice outputs. */
17 #include "xbt/log.h"
18 #include "xbt/asserts.h"
19 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
20                              "Messages specific for this msg example");
21 int master(int argc, char *argv[]);
22 int slave(int argc, char *argv[]);
23
24 /** Emitter function  */
25 int master(int argc, char *argv[])
26 {
27   long number_of_tasks = atol(argv[1]);
28   long slaves_count = atol(argv[4]);
29
30   int i;
31   for (i = 0; i < number_of_tasks; i++) {
32     m_task_t task = NULL;
33
34     //creating task and setting its category
35     if (i % 2) {
36       task = MSG_task_create("task_compute", 10000000, 0, NULL);
37       TRACE_msg_set_task_category(task, "compute");
38     } else if (i % 3) {
39       task = MSG_task_create("task_request", 10, 10, NULL);
40       TRACE_msg_set_task_category(task, "request");
41     } else {
42       task = MSG_task_create("task_data", 10, 10000000, NULL);
43       TRACE_msg_set_task_category(task, "data");
44     }
45     MSG_task_send(task, "master_mailbox");
46   }
47
48   for (i = 0; i < slaves_count; i++) {
49     m_task_t finalize = MSG_task_create("finalize", 0, 1000, 0);
50     TRACE_msg_set_task_category(finalize, "finalize");
51     MSG_task_send(finalize, "master_mailbox");
52   }
53
54   return 0;
55 }
56
57 /** Receiver function  */
58 int slave(int argc, char *argv[])
59 {
60   m_task_t task = NULL;
61
62   while (1) {
63     MSG_task_receive(&(task), "master_mailbox");
64
65     if (!strcmp(MSG_task_get_name(task), "finalize")) {
66       MSG_task_destroy(task);
67       break;
68     }
69
70     MSG_task_execute(task);
71     MSG_task_destroy(task);
72     task = NULL;
73   }
74   return 0;
75 }
76
77 /** Main function */
78 int main(int argc, char *argv[])
79 {
80   MSG_global_init(&argc, argv);
81   if (argc < 3) {
82     printf("Usage: %s platform_file deployment_file\n", argv[0]);
83     exit(1);
84   }
85
86   char *platform_file = argv[1];
87   char *deployment_file = argv[2];
88   MSG_create_environment(platform_file);
89
90   //declaring user categories with RGB colors
91   TRACE_category_with_color ("compute", "1 0 0"); //red
92   TRACE_category_with_color ("request", "0 1 0"); //green
93   TRACE_category_with_color ("data", "0 0 1");    //blue
94   TRACE_category_with_color ("finalize", "0 0 0");//black
95
96   MSG_function_register("master", master);
97   MSG_function_register("slave", slave);
98   MSG_launch_application(deployment_file);
99
100   MSG_main();
101   MSG_clean();
102   return 0;
103 }