Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use of busy wait and not posix for te moment.
[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
17 /** Emitter function  */
18 int master(int argc, char *argv[])
19 {
20   long number_of_tasks = atol(argv[1]);
21   long slaves_count = atol(argv[4]);
22
23   int i;
24   for (i = 0; i < number_of_tasks; i++) {
25     m_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       TRACE_msg_set_task_category(task, "compute");
31     } else if (i % 3) {
32       task = MSG_task_create("task_request", 10, 10, NULL);
33       TRACE_msg_set_task_category(task, "request");
34     } else {
35       task = MSG_task_create("task_data", 10, 10000000, NULL);
36       TRACE_msg_set_task_category(task, "data");
37     }
38     MSG_task_send(task, "master_mailbox");
39   }
40
41   for (i = 0; i < slaves_count; i++) {
42     m_task_t finalize = MSG_task_create("finalize", 0, 1000, 0);
43     TRACE_msg_set_task_category(finalize, "finalize");
44     MSG_task_send(finalize, "master_mailbox");
45   }
46
47   return 0;
48 }
49
50 /** Receiver function  */
51 int slave(int argc, char *argv[])
52 {
53   m_task_t task = NULL;
54
55   while (1) {
56     MSG_task_receive(&(task), "master_mailbox");
57
58     if (!strcmp(MSG_task_get_name(task), "finalize")) {
59       MSG_task_destroy(task);
60       break;
61     }
62
63     MSG_task_execute(task);
64     MSG_task_destroy(task);
65     task = NULL;
66   }
67   return 0;
68 }
69
70 /** Main function */
71 int main(int argc, char *argv[])
72 {
73   MSG_error_t res = MSG_OK;
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 }