Logo AND Algorithmique Numérique Distribuée

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