Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix a race condition spotted by TSan
[simgrid.git] / src / msg / instr_msg_task.cpp
1 /* Copyright (c) 2010, 2012-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 #include "mc/mc.h"
8 #include "src/instr/instr_private.h"
9 #include "src/msg/msg_private.h"
10
11 #include <atomic>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_msg, instr, "MSG instrumentation");
14
15 void TRACE_msg_set_task_category(msg_task_t task, const char *category)
16 {
17   xbt_assert(task->category == nullptr, "Task %p(%s) already has a category (%s).",
18       task, task->name, task->category);
19
20   //if user provides a nullptr category, task is no longer traced
21   if (category == nullptr) {
22     xbt_free (task->category);
23     task->category = nullptr;
24     XBT_DEBUG("MSG task %p(%s), category removed", task, task->name);
25     return;
26   }
27
28   //set task category
29   task->category = xbt_strdup (category);
30   XBT_DEBUG("MSG task %p(%s), category %s", task, task->name, task->category);
31 }
32
33 /* MSG_task_create related function*/
34 void TRACE_msg_task_create(msg_task_t task)
35 {
36   static std::atomic_ullong counter{0};
37   task->counter = counter++;
38   task->category = nullptr;
39   
40   if(MC_is_active())
41     MC_ignore_heap(&(task->counter), sizeof(task->counter));
42
43   XBT_DEBUG("CREATE %p, %lld", task, task->counter);
44 }
45
46 /* MSG_task_execute related functions */
47 void TRACE_msg_task_execute_start(msg_task_t task)
48 {
49   XBT_DEBUG("EXEC,in %p, %lld, %s", task, task->counter, task->category);
50
51   if (TRACE_msg_process_is_enabled()){
52     int len = INSTR_DEFAULT_STR_SIZE;
53     char str[INSTR_DEFAULT_STR_SIZE];
54
55     container_t process_container = PJ_container_get (instr_process_id(MSG_process_self(), str, len));
56     type_t type = PJ_type_get ("MSG_PROCESS_STATE", process_container->type);
57     val_t value = PJ_value_get ("task_execute", type);
58     new PushStateEvent (MSG_get_clock(), process_container, type, value);
59   }
60 }
61
62 void TRACE_msg_task_execute_end(msg_task_t task)
63 {
64   XBT_DEBUG("EXEC,out %p, %lld, %s", task, task->counter, task->category);
65
66   if (TRACE_msg_process_is_enabled()){
67     int len = INSTR_DEFAULT_STR_SIZE;
68     char str[INSTR_DEFAULT_STR_SIZE];
69
70     container_t process_container = PJ_container_get (instr_process_id(MSG_process_self(), str, len));
71     type_t type = PJ_type_get ("MSG_PROCESS_STATE", process_container->type);
72     new PopStateEvent (MSG_get_clock(), process_container, type);
73   }
74 }
75
76 /* MSG_task_destroy related functions */
77 void TRACE_msg_task_destroy(msg_task_t task)
78 {
79   XBT_DEBUG("DESTROY %p, %lld, %s", task, task->counter, task->category);
80
81   //free category
82   xbt_free(task->category);
83   task->category = nullptr;
84 }
85
86 /* MSG_task_get related functions */
87 void TRACE_msg_task_get_start()
88 {
89   XBT_DEBUG("GET,in");
90
91   if (TRACE_msg_process_is_enabled()){
92     int len = INSTR_DEFAULT_STR_SIZE;
93     char str[INSTR_DEFAULT_STR_SIZE];
94
95     container_t process_container = PJ_container_get (instr_process_id(MSG_process_self(), str, len));
96     type_t type = PJ_type_get ("MSG_PROCESS_STATE", process_container->type);
97     val_t value = PJ_value_get ("receive", type);
98     new PushStateEvent (MSG_get_clock(), process_container, type, value);
99   }
100 }
101
102 void TRACE_msg_task_get_end(double start_time, msg_task_t task)
103 {
104   XBT_DEBUG("GET,out %p, %lld, %s", task, task->counter, task->category);
105
106   if (TRACE_msg_process_is_enabled()){
107     int len = INSTR_DEFAULT_STR_SIZE;
108     char str[INSTR_DEFAULT_STR_SIZE];
109
110     container_t process_container = PJ_container_get (instr_process_id(MSG_process_self(), str, len));
111     type_t type = PJ_type_get ("MSG_PROCESS_STATE", process_container->type);
112     new PopStateEvent (MSG_get_clock(), process_container, type);
113
114     char key[INSTR_DEFAULT_STR_SIZE];
115     snprintf (key, INSTR_DEFAULT_STR_SIZE, "p%lld", task->counter);
116     type = PJ_type_get ("MSG_PROCESS_TASK_LINK", PJ_type_get_root());
117     new EndLinkEvent(MSG_get_clock(), PJ_container_get_root(), type, process_container, "SR", key);
118   }
119 }
120
121 /* MSG_task_put related functions */
122 int TRACE_msg_task_put_start(msg_task_t task)
123 {
124   XBT_DEBUG("PUT,in %p, %lld, %s", task, task->counter, task->category);
125
126   if (TRACE_msg_process_is_enabled()){
127     int len = INSTR_DEFAULT_STR_SIZE;
128     char str[INSTR_DEFAULT_STR_SIZE];
129
130     container_t process_container = PJ_container_get (instr_process_id(MSG_process_self(), str, len));
131     type_t type = PJ_type_get ("MSG_PROCESS_STATE", process_container->type);
132     val_t value = PJ_value_get ("send", type);
133     new PushStateEvent (MSG_get_clock(), process_container, type, value);
134
135     char key[INSTR_DEFAULT_STR_SIZE];
136     snprintf (key, INSTR_DEFAULT_STR_SIZE, "p%lld", task->counter);
137     type = PJ_type_get ("MSG_PROCESS_TASK_LINK", PJ_type_get_root());
138     new StartLinkEvent(MSG_get_clock(), PJ_container_get_root(), type, process_container, "SR", key);
139   }
140
141   return 1;
142 }
143
144 void TRACE_msg_task_put_end()
145 {
146   XBT_DEBUG("PUT,out");
147
148   if (TRACE_msg_process_is_enabled()){
149     int len = INSTR_DEFAULT_STR_SIZE;
150     char str[INSTR_DEFAULT_STR_SIZE];
151
152     container_t process_container = PJ_container_get (instr_process_id(MSG_process_self(), str, len));
153     type_t type = PJ_type_get ("MSG_PROCESS_STATE", process_container->type);
154     new PopStateEvent (MSG_get_clock(), process_container, type);
155   }
156 }