Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge conflict resolved
[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 int master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18
19 /** Emitter function  */
20 int master(int argc, char *argv[])
21 {
22   long number_of_tasks = atol(argv[1]);
23   long slaves_count = atol(argv[4]);
24
25   int i;
26   for (i = 0; i < number_of_tasks; i++) {
27     m_task_t task = NULL;
28
29     //creating task and setting its category
30     if (i % 2) {
31       task = MSG_task_create("task_compute", 10000000, 0, NULL);
32       TRACE_msg_set_task_category(task, "compute");
33     } else if (i % 3) {
34       task = MSG_task_create("task_request", 10, 10, NULL);
35       TRACE_msg_set_task_category(task, "request");
36     } else {
37       task = MSG_task_create("task_data", 10, 10000000, NULL);
38       TRACE_msg_set_task_category(task, "data");
39     }
40     MSG_task_send(task, "master_mailbox");
41   }
42
43   for (i = 0; i < slaves_count; i++) {
44     m_task_t finalize = MSG_task_create("finalize", 0, 1000, 0);
45     TRACE_msg_set_task_category(finalize, "finalize");
46     MSG_task_send(finalize, "master_mailbox");
47   }
48
49   return 0;
50 }
51
52 /** Receiver function  */
53 int slave(int argc, char *argv[])
54 {
55   m_task_t task = NULL;
56
57   while (1) {
58     MSG_task_receive(&(task), "master_mailbox");
59
60     if (!strcmp(MSG_task_get_name(task), "finalize")) {
61       MSG_task_destroy(task);
62       break;
63     }
64
65     MSG_task_execute(task);
66     MSG_task_destroy(task);
67     task = NULL;
68   }
69   return 0;
70 }
71
72 /** Main function */
73 int main(int argc, char *argv[])
74 {
75   MSG_global_init(&argc, argv);
76   if (argc < 3) {
77     printf("Usage: %s platform_file deployment_file\n", argv[0]);
78     exit(1);
79   }
80
81   char *platform_file = argv[1];
82   char *deployment_file = argv[2];
83   MSG_create_environment(platform_file);
84
85   //declaring user categories with RGB colors
86   TRACE_category_with_color ("compute", "1 0 0"); //red
87   TRACE_category_with_color ("request", "0 1 0"); //green
88   TRACE_category_with_color ("data", "0 0 1");    //blue
89   TRACE_category_with_color ("finalize", "0 0 0");//black
90
91   MSG_function_register("master", master);
92   MSG_function_register("slave", slave);
93   MSG_launch_application(deployment_file);
94
95   MSG_main();
96   MSG_clean();
97   return 0;
98 }