Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
revise documentation of tracing examples
[simgrid.git] / examples / msg / trace-categories / trace-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>Setting Categories: trace-categories/trace-categories.c</b>. This example declares several tracing categories
10  * that are used to classify tasks. When the program is executed, the tracing mechanism registers the resource
11  * utilization of hosts and links according to these categories. You might want to run this program with the following
12  * options: <i>--cfg=tracing:yes</i>, <i>--cfg=tracing/categorized:yes</i>, <i>--cfg=tracing/uncategorized:yes</i>,
13  * <i>--cfg=viva/categorized:viva_cat.plist</i>, and <i>--cfg=viva/uncategorized:viva_uncat.plist</i>.
14  */
15
16 #include "simgrid/msg.h"
17
18 static int master(int argc, char *argv[])
19 {
20   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
21   long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
22
23   int i;
24   for (i = 0; i < number_of_tasks; i++) {
25     msg_task_t task = NULL;
26
27     //creating task and setting its category
28     if (i % 2) {
29       task = MSG_task_create("task_compute", 10000000, 0, NULL);
30       MSG_task_set_category(task, "compute");
31     } else if (i % 3) {
32       task = MSG_task_create("task_request", 10, 10, NULL);
33       MSG_task_set_category(task, "request");
34     } else {
35       task = MSG_task_create("task_data", 10, 10000000, NULL);
36       MSG_task_set_category(task, "data");
37     }
38     MSG_task_send(task, "master_mailbox");
39   }
40
41   for (i = 0; i < workers_count; i++) {
42     msg_task_t finalize = MSG_task_create("finalize", 0, 1000, 0);
43     MSG_task_set_category(finalize, "finalize");
44     MSG_task_send(finalize, "master_mailbox");
45   }
46
47   return 0;
48 }
49
50 static int worker(int argc, char *argv[])
51 {
52   msg_task_t task = NULL;
53
54   while (1) {
55     MSG_task_receive(&(task), "master_mailbox");
56
57     if (!strcmp(MSG_task_get_name(task), "finalize")) {
58       MSG_task_destroy(task);
59       break;
60     }
61
62     MSG_task_execute(task);
63     MSG_task_destroy(task);
64     task = NULL;
65   }
66   return 0;
67 }
68
69 int main(int argc, char *argv[])
70 {
71   MSG_init(&argc, argv);
72   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
73              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
74
75   MSG_create_environment(argv[1]);
76
77   //declaring user categories with RGB colors
78   TRACE_category_with_color ("compute", "1 0 0"); //red
79   TRACE_category_with_color ("request", "0 1 0"); //green
80   TRACE_category_with_color ("data", "0 0 1");    //blue
81   TRACE_category_with_color ("finalize", "0 0 0");//black
82
83   MSG_function_register("master", master);
84   MSG_function_register("worker", worker);
85   MSG_launch_application(argv[2]);
86
87   MSG_main();
88   return 0;
89 }