Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
43963e314a5f5311d44295f37fec2c813271cb95
[simgrid.git] / examples / msg / tracing / categories.c
1 /* Copyright (c) 2010-2015. 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> This is a master/slave program where the master creates tasks, send them to the slaves.
10  * For each task received, the slave executes it and then destroys it. This program declares several tracing categories
11  * that are used to classify tasks. When the program is executed, the tracing mechanism registers the resource
12  * utilization of hosts and links according to these categories. You might want to run this program with the following
13  * parameters:
14  * --cfg=tracing:yes
15  * --cfg=tracing/categorized:yes
16  * --cfg=tracing/uncategorized:yes
17  * --cfg=viva/categorized:viva_cat.plist
18  * --cfg=viva/uncategorized:viva_uncat.plist
19  * (See \ref tracing_tracing_options for details)
20  */
21
22 #include <stdio.h>
23 #include "simgrid/msg.h"
24
25 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
26
27 static int master(int argc, char *argv[])
28 {
29   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
30   long slaves_count = xbt_str_parse_int(argv[4], "Invalid amount of slaves: %s");
31
32   int i;
33   for (i = 0; i < number_of_tasks; i++) {
34     msg_task_t task = NULL;
35
36     //creating task and setting its category
37     if (i % 2) {
38       task = MSG_task_create("task_compute", 10000000, 0, NULL);
39       MSG_task_set_category(task, "compute");
40     } else if (i % 3) {
41       task = MSG_task_create("task_request", 10, 10, NULL);
42       MSG_task_set_category(task, "request");
43     } else {
44       task = MSG_task_create("task_data", 10, 10000000, NULL);
45       MSG_task_set_category(task, "data");
46     }
47     MSG_task_send(task, "master_mailbox");
48   }
49
50   for (i = 0; i < slaves_count; i++) {
51     msg_task_t finalize = MSG_task_create("finalize", 0, 1000, 0);
52     MSG_task_set_category(finalize, "finalize");
53     MSG_task_send(finalize, "master_mailbox");
54   }
55
56   return 0;
57 }
58
59 static int slave(int argc, char *argv[])
60 {
61   msg_task_t task = NULL;
62
63   while (1) {
64     MSG_task_receive(&(task), "master_mailbox");
65
66     if (!strcmp(MSG_task_get_name(task), "finalize")) {
67       MSG_task_destroy(task);
68       break;
69     }
70
71     MSG_task_execute(task);
72     MSG_task_destroy(task);
73     task = NULL;
74   }
75   return 0;
76 }
77
78 int main(int argc, char *argv[])
79 {
80   MSG_init(&argc, argv);
81   if (argc < 3) {
82     printf("Usage: %s platform_file deployment_file\n", argv[0]);
83     exit(1);
84   }
85
86   MSG_create_environment(argv[1]);
87
88   //declaring user categories with RGB colors
89   TRACE_category_with_color ("compute", "1 0 0"); //red
90   TRACE_category_with_color ("request", "0 1 0"); //green
91   TRACE_category_with_color ("data", "0 0 1");    //blue
92   TRACE_category_with_color ("finalize", "0 0 0");//black
93
94   MSG_function_register("master", master);
95   MSG_function_register("slave", slave);
96   MSG_launch_application(argv[2]);
97
98   MSG_main();
99   return 0;
100 }