ARGOBOTS
log.c
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include "abti.h"
9 
10 #ifdef ABT_CONFIG_USE_DEBUG_LOG
11 ABTD_XSTREAM_LOCAL ABTI_log *lp_ABTI_log = NULL;
12 
13 void ABTI_log_init(void)
14 {
15  lp_ABTI_log = (ABTI_log *)ABTU_malloc(sizeof(ABTI_log));
16  lp_ABTI_log->p_sched = NULL;
17 }
18 
19 void ABTI_log_finalize(void)
20 {
21  ABTU_free(lp_ABTI_log);
22  lp_ABTI_log = NULL;
23 }
24 
25 void ABTI_log_print(FILE *fh, const char *format, ...)
26 {
27  if (gp_ABTI_global->use_logging == ABT_FALSE)
28  return;
29 
30  va_list list;
31  va_start(list, format);
32  vfprintf(fh, format, list);
33  va_end(list);
34  fflush(fh);
35 }
36 
37 void ABTI_log_event(FILE *fh, const char *format, ...)
38 {
39  if (gp_ABTI_global->use_logging == ABT_FALSE)
40  return;
41  ABTI_local *p_local = ABTI_local_get_local_uninlined();
42 
43  ABT_unit_type type = ABTI_self_get_type(p_local);
44  ABTI_xstream *p_xstream = NULL;
45  ABTI_thread *p_thread = NULL;
46  ABTI_task *p_task = NULL;
47  char *prefix_fmt = NULL, *prefix = NULL;
48  char *newfmt;
49  uint64_t tid;
50  int rank;
51  int tid_len = 0, rank_len = 0;
52  size_t newfmt_len;
53 
54  switch (type) {
56  p_xstream = p_local->p_xstream;
57  p_thread = p_local->p_thread;
58  if (p_thread == NULL) {
59  if (p_xstream && p_xstream->type != ABTI_XSTREAM_TYPE_PRIMARY) {
60  prefix_fmt = "<U%" PRIu64 ":E%d> %s";
61  rank = p_xstream->rank;
62  tid = 0;
63  } else {
64  prefix = "<U0:E0> ";
65  prefix_fmt = "%s%s";
66  }
67  } else {
68  rank = p_xstream->rank;
69  if (lp_ABTI_log->p_sched) {
70  prefix_fmt = "<S%" PRIu64 ":E%d> %s";
71  tid = lp_ABTI_log->p_sched->id;
72  } else {
73  prefix_fmt = "<U%" PRIu64 ":E%d> %s";
74  tid = ABTI_thread_get_id(p_thread);
75  }
76  }
77  break;
78 
79  case ABT_UNIT_TYPE_TASK:
80  p_xstream = p_local->p_xstream;
81  rank = p_xstream->rank;
82  p_task = p_local->p_task;
83  if (lp_ABTI_log->p_sched) {
84  prefix_fmt = "<S%" PRIu64 ":E%d> %s";
85  tid = lp_ABTI_log->p_sched->id;
86  } else {
87  prefix_fmt = "<T%" PRIu64 ":E%d> %s";
88  tid = ABTI_task_get_id(p_task);
89  }
90  break;
91 
92  case ABT_UNIT_TYPE_EXT:
93  prefix = "<EXT> ";
94  prefix_fmt = "%s%s";
95  break;
96 
97  default:
98  prefix = "<UNKNOWN> ";
99  prefix_fmt = "%s%s";
100  break;
101  }
102 
103  if (prefix == NULL) {
104  tid_len = ABTU_get_int_len(tid);
105  rank_len = ABTU_get_int_len(rank);
106  newfmt_len = 6 + tid_len + rank_len + strlen(format);
107  newfmt = (char *)ABTU_malloc(newfmt_len + 1);
108  sprintf(newfmt, prefix_fmt, tid, rank, format);
109  } else {
110  newfmt_len = strlen(prefix) + strlen(format);
111  newfmt = (char *)ABTU_malloc(newfmt_len + 1);
112  sprintf(newfmt, prefix_fmt, prefix, format);
113  }
114 
115  va_list list;
116  va_start(list, format);
117  vfprintf(fh, newfmt, list);
118  va_end(list);
119  fflush(fh);
120 
121  ABTU_free(newfmt);
122 }
123 
124 void ABTI_log_debug(FILE *fh, char *path, int line, const char *format, ...)
125 {
126  if (gp_ABTI_global->use_debug == ABT_FALSE)
127  return;
128 
129  int line_len;
130  size_t newfmt_len;
131  char *newfmt;
132 
133  line_len = ABTU_get_int_len(line);
134  newfmt_len = strlen(path) + line_len + 4 + strlen(format);
135  newfmt = (char *)ABTU_malloc(newfmt_len + 1);
136  sprintf(newfmt, "[%s:%d] %s", path, line, format);
137 
138  va_list list;
139  va_start(list, format);
140  vfprintf(fh, newfmt, list);
141  va_end(list);
142  fflush(fh);
143 
144  ABTU_free(newfmt);
145 }
146 
147 void ABTI_log_pool_push(ABTI_pool *p_pool, ABT_unit unit,
148  ABTI_native_thread_id producer_id)
149 {
150  if (gp_ABTI_global->use_logging == ABT_FALSE)
151  return;
152 
153  ABTI_thread *p_thread = NULL;
154  ABTI_task *p_task = NULL;
155  switch (p_pool->u_get_type(unit)) {
157  p_thread = ABTI_thread_get_ptr(p_pool->u_get_thread(unit));
158  if (p_thread->p_last_xstream) {
159  LOG_EVENT("[U%" PRIu64 ":E%d] pushed to P%" PRIu64 " "
160  "(producer: NT %p)\n",
161  ABTI_thread_get_id(p_thread),
162  p_thread->p_last_xstream->rank, p_pool->id,
163  (void *)producer_id);
164  } else {
165  LOG_EVENT("[U%" PRIu64 "] pushed to P%" PRIu64 " "
166  "(producer: NT %p)\n",
167  ABTI_thread_get_id(p_thread), p_pool->id,
168  (void *)producer_id);
169  }
170  break;
171 
172  case ABT_UNIT_TYPE_TASK:
173  p_task = ABTI_task_get_ptr(p_pool->u_get_task(unit));
174  if (p_task->p_xstream) {
175  LOG_EVENT("[T%" PRIu64 ":E%d] pushed to P%" PRIu64 " "
176  "(producer: NT %p)\n",
177  ABTI_task_get_id(p_task), p_task->p_xstream->rank,
178  p_pool->id, (void *)producer_id);
179  } else {
180  LOG_EVENT("[T%" PRIu64 "] pushed to P%" PRIu64 " "
181  "(producer: NT %p)\n",
182  ABTI_task_get_id(p_task), p_pool->id,
183  (void *)producer_id);
184  }
185  break;
186 
187  default:
188  ABTI_ASSERT(0);
189  break;
190  }
191 }
192 
193 void ABTI_log_pool_remove(ABTI_pool *p_pool, ABT_unit unit,
194  ABTI_native_thread_id consumer_id)
195 {
196  if (gp_ABTI_global->use_logging == ABT_FALSE)
197  return;
198 
199  ABTI_thread *p_thread = NULL;
200  ABTI_task *p_task = NULL;
201  switch (p_pool->u_get_type(unit)) {
203  p_thread = ABTI_thread_get_ptr(p_pool->u_get_thread(unit));
204  if (p_thread->p_last_xstream) {
205  LOG_EVENT("[U%" PRIu64 ":E%d] removed from "
206  "P%" PRIu64 " (consumer: NT %p)\n",
207  ABTI_thread_get_id(p_thread),
208  p_thread->p_last_xstream->rank, p_pool->id,
209  (void *)consumer_id);
210  } else {
211  LOG_EVENT("[U%" PRIu64 "] removed from P%" PRIu64 " "
212  "(consumer: NT %p)\n",
213  ABTI_thread_get_id(p_thread), p_pool->id,
214  (void *)consumer_id);
215  }
216  break;
217 
218  case ABT_UNIT_TYPE_TASK:
219  p_task = ABTI_task_get_ptr(p_pool->u_get_task(unit));
220  if (p_task->p_xstream) {
221  LOG_EVENT("[T%" PRIu64 ":E%d] removed from "
222  "P%" PRIu64 " (consumer: NT %p)\n",
223  ABTI_task_get_id(p_task), p_task->p_xstream->rank,
224  p_pool->id, (void *)consumer_id);
225  } else {
226  LOG_EVENT("[T%" PRIu64 "] removed from P%" PRIu64 " "
227  "(consumer: NT %p)\n",
228  ABTI_task_get_id(p_task), p_pool->id,
229  (void *)consumer_id);
230  }
231  break;
232 
233  default:
234  ABTI_ASSERT(0);
235  break;
236  }
237 }
238 
239 void ABTI_log_pool_pop(ABTI_pool *p_pool, ABT_unit unit)
240 {
241  if (gp_ABTI_global->use_logging == ABT_FALSE)
242  return;
243  if (unit == ABT_UNIT_NULL)
244  return;
245 
246  ABTI_thread *p_thread = NULL;
247  ABTI_task *p_task = NULL;
248  switch (p_pool->u_get_type(unit)) {
250  p_thread = ABTI_thread_get_ptr(p_pool->u_get_thread(unit));
251  if (p_thread->p_last_xstream) {
252  LOG_EVENT("[U%" PRIu64 ":E%d] popped from "
253  "P%" PRIu64 "\n",
254  ABTI_thread_get_id(p_thread),
255  p_thread->p_last_xstream->rank, p_pool->id);
256  } else {
257  LOG_EVENT("[U%" PRIu64 "] popped from P%" PRIu64 "\n",
258  ABTI_thread_get_id(p_thread), p_pool->id);
259  }
260  break;
261 
262  case ABT_UNIT_TYPE_TASK:
263  p_task = ABTI_task_get_ptr(p_pool->u_get_task(unit));
264  if (p_task->p_xstream) {
265  LOG_EVENT("[T%" PRIu64 ":E%d] popped from "
266  "P%" PRIu64 "\n",
267  ABTI_task_get_id(p_task), p_task->p_xstream->rank,
268  p_pool->id);
269  } else {
270  LOG_EVENT("[T%" PRIu64 "] popped from P%" PRIu64 "\n",
271  ABTI_task_get_id(p_task), p_pool->id);
272  }
273  break;
274 
275  default:
276  ABTI_ASSERT(0);
277  break;
278  }
279 }
280 
281 #endif /* ABT_CONFIG_USE_DEBUG_LOG */
int ABTU_get_int_len(size_t num)
Definition: util.c:23
struct ABT_unit_opaque * ABT_unit
Definition: abt.h:275
#define ABT_UNIT_NULL
Definition: abt.h:343
static void * ABTU_malloc(size_t size)
Definition: abtu.h:39
#define ABT_FALSE
Definition: abt.h:224
ABTI_global * gp_ABTI_global
Definition: global.c:14
#define LOG_EVENT(fmt,...)
Definition: abti_log.h:60
ABT_unit_type
Definition: abt.h:170
static void ABTU_free(void *ptr)
Definition: abtu.h:32