aboutsummaryrefslogtreecommitdiff
path: root/libgomp/affinity-fmt.c
blob: 30b7ef973220a21531fe7863794494eb6837388d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* Copyright (C) 2018-2022 Free Software Foundation, Inc.
   Contributed by Jakub Jelinek <jakub@redhat.com>.

   This file is part of the GNU Offloading and Multi Processing Library
   (libgomp).

   Libgomp is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   more details.

   Under Section 7 of GPL version 3, you are granted additional
   permissions described in the GCC Runtime Library Exception, version
   3.1, as published by the Free Software Foundation.

   You should have received a copy of the GNU General Public License and
   a copy of the GCC Runtime Library Exception along with this program;
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   <http://www.gnu.org/licenses/>.  */

#include "libgomp.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>  /* For PRIx64.  */
#endif
#ifdef HAVE_UNAME
#include <sys/utsname.h>
#endif

ialias_redirect (omp_get_team_num)
ialias_redirect (omp_get_num_teams)

bool
gomp_print_string (const char *str, size_t len)
{
  return fwrite (str, 1, len, stderr) != len;
}

void
gomp_set_affinity_format (const char *format, size_t len)
{
  if (len < gomp_affinity_format_len)
    memcpy (gomp_affinity_format_var, format, len);
  else
    {
      char *p;
      if (gomp_affinity_format_len)
	p = gomp_realloc (gomp_affinity_format_var, len + 1);
      else
	p = gomp_malloc (len + 1);
      memcpy (p, format, len);
      gomp_affinity_format_var = p;
      gomp_affinity_format_len = len + 1;
    }
  gomp_affinity_format_var[len] = '\0';
}

void
omp_set_affinity_format (const char *format)
{
  gomp_set_affinity_format (format, strlen (format));
}

size_t
omp_get_affinity_format (char *buffer, size_t size)
{
  size_t len = strlen (gomp_affinity_format_var);
  if (size)
    {
      if (len < size)
	memcpy (buffer, gomp_affinity_format_var, len + 1);
      else
	{
	  memcpy (buffer, gomp_affinity_format_var, size - 1);
	  buffer[size - 1] = '\0';
	}
    }
  return len;
}

void
gomp_display_string (char *buffer, size_t size, size_t *ret,
		     const char *str, size_t len)
{
  size_t r = *ret;
  if (size && r < size)
    {
      size_t l = len;
      if (size - r < len)
	l = size - r;
      memcpy (buffer + r, str, l);
    }
  *ret += len;
  if (__builtin_expect (r > *ret, 0))
    gomp_fatal ("overflow in omp_capture_affinity");
}

static void
gomp_display_repeat (char *buffer, size_t size, size_t *ret,
		     char c, size_t len)
{
  size_t r = *ret;
  if (size && r < size)
    {
      size_t l = len;
      if (size - r < len)
	l = size - r;
      memset (buffer + r, c, l);
    }
  *ret += len;
  if (__builtin_expect (r > *ret, 0))
    gomp_fatal ("overflow in omp_capture_affinity");
}

static void
gomp_display_num (char *buffer, size_t size, size_t *ret,
		  bool zero, bool right, size_t sz, char *buf)
{
  size_t l = strlen (buf);
  if (sz == (size_t) -1 || l >= sz)
    {
      gomp_display_string (buffer, size, ret, buf, l);
      return;
    }
  if (zero)
    {
      if (buf[0] == '-')
	gomp_display_string (buffer, size, ret, buf, 1);
      else if (buf[0] == '0' && buf[1] == 'x')
	gomp_display_string (buffer, size, ret, buf, 2);
      gomp_display_repeat (buffer, size, ret, '0', sz - l);
      if (buf[0] == '-')
	gomp_display_string (buffer, size, ret, buf + 1, l - 1);
      else if (buf[0] == '0' && buf[1] == 'x')
	gomp_display_string (buffer, size, ret, buf + 2, l - 2);
      else
	gomp_display_string (buffer, size, ret, buf, l);
    }
  else if (right)
    {
      gomp_display_repeat (buffer, size, ret, ' ', sz - l);
      gomp_display_string (buffer, size, ret, buf, l);
    }
  else
    {
      gomp_display_string (buffer, size, ret, buf, l);
      gomp_display_repeat (buffer, size, ret, ' ', sz - l);
    }
}

static void
gomp_display_int (char *buffer, size_t size, size_t *ret,
		  bool zero, bool right, size_t sz, int num)
{
  char buf[3 * sizeof (int) + 2];
  sprintf (buf, "%d", num);
  gomp_display_num (buffer, size, ret, zero, right, sz, buf);
}

static void
gomp_display_string_len (char *buffer, size_t size, size_t *ret,
			 bool right, size_t sz, char *str, size_t len)
{
  if (sz == (size_t) -1 || len >= sz)
    {
      gomp_display_string (buffer, size, ret, str, len);
      return;
    }

  if (right)
    {
      gomp_display_repeat (buffer, size, ret, ' ', sz - len);
      gomp_display_string (buffer, size, ret, str, len);
    }
  else
    {
      gomp_display_string (buffer, size, ret, str, len);
      gomp_display_repeat (buffer, size, ret, ' ', sz - len);
    }
}

static void
gomp_display_hostname (char *buffer, size_t size, size_t *ret,
		       bool right, size_t sz)
{
#ifdef HAVE_GETHOSTNAME
  {
    char buf[256];
    char *b = buf;
    size_t len = 256;
    do
      {
	b[len - 1] = '\0';
	if (gethostname (b, len - 1) == 0)
	  {
	    size_t l = strlen (b);
	    if (l < len - 1)
	      {
		gomp_display_string_len (buffer, size, ret,
					 right, sz, b, l);
		if (b != buf)
		  free (b);
		return;
	      }
	  }
	if (len == 1048576)
	  break;
	len = len * 2;
	if (len == 512)
	  b = gomp_malloc (len);
	else
	  b = gomp_realloc (b, len);
      }
    while (1);
    if (b != buf)
      free (b);
  }
#endif
#ifdef HAVE_UNAME
  {
    struct utsname buf;
    if (uname (&buf) == 0)
      {
	gomp_display_string_len (buffer, size, ret, right, sz,
				 buf.nodename, strlen (buf.nodename));
	return;
      }
  }
#endif
  gomp_display_string_len (buffer, size, ret, right, sz, "node", 4);
}

struct affinity_types_struct {
  char long_str[18];
  char long_len;
  char short_c; };

static struct affinity_types_struct affinity_types[] =
{
#define AFFINITY_TYPE(l, s) \
  { #l, sizeof (#l) - 1, s }
  AFFINITY_TYPE (team_num, 't'),
  AFFINITY_TYPE (num_teams, 'T'),
  AFFINITY_TYPE (nesting_level, 'L'),
  AFFINITY_TYPE (thread_num, 'n'),
  AFFINITY_TYPE (num_threads, 'N'),
  AFFINITY_TYPE (ancestor_tnum, 'a'),
  AFFINITY_TYPE (host, 'H'),
  AFFINITY_TYPE (process_id, 'P'),
  AFFINITY_TYPE (native_thread_id, 'i'),
  AFFINITY_TYPE (thread_affinity, 'A')
#undef AFFINITY_TYPE
};

size_t
gomp_display_affinity (char *buffer, size_t size,
		       const char *format, gomp_thread_handle handle,
		       struct gomp_team_state *ts, unsigned int place)
{
  size_t ret = 0;
  do
    {
      const char *p = strchr (format, '%');
      bool zero = false;
      bool right = false;
      size_t sz = -1;
      char c;
      int val;
      if (p == NULL)
	p = strchr (format, '\0');
      if (p != format)
	gomp_display_string (buffer, size, &ret,
			     format, p - format);
      if (*p == '\0')
	break;
      p++;
      if (*p == '%')
	{
	  gomp_display_string (buffer, size, &ret, "%", 1);
	  format = p + 1;
	  continue;
	}
      if (*p == '0')
	{
	  zero = true;
	  p++;
	  if (*p != '.')
	    gomp_fatal ("leading zero not followed by dot in affinity format");
	}
      if (*p == '.')
	{
	  right = true;
	  p++;
	}
      if (*p >= '1' && *p <= '9')
	{
	  char *end;
	  sz = strtoul (p, &end, 10);
	  p = end;
	}
      else if (zero || right)
	gomp_fatal ("leading zero or right justification in affinity format "
		    "requires size");
      c = *p;
      if (c == '{')
	{
	  int i;
	  for (i = 0;
	       i < sizeof (affinity_types) / sizeof (affinity_types[0]); ++i)
	    if (strncmp (p + 1, affinity_types[i].long_str,
			 affinity_types[i].long_len) == 0
		&& p[affinity_types[i].long_len + 1] == '}')
	      {
		c = affinity_types[i].short_c;
		p += affinity_types[i].long_len + 1;
		break;
	      }
	  if (c == '{')
	    {
	      char *q = strchr (p + 1, '}');
	      if (q)
		gomp_fatal ("unsupported long type name '%.*s' in affinity "
			    "format", (int) (q - (p + 1)), p + 1);
	      else
		gomp_fatal ("unterminated long type name '%s' in affinity "
			    "format", p + 1);
	    }
	}
      switch (c)
	{
	case 't':
	  val = omp_get_team_num ();
	  goto do_int;
	case 'T':
	  val = omp_get_num_teams ();
	  goto do_int;
	case 'L':
	  val = ts->level;
	  goto do_int;
	case 'n':
	  val = ts->team_id;
	  goto do_int;
	case 'N':
	  val = ts->team ? ts->team->nthreads : 1;
	  goto do_int;
	case 'a':
	  val = ts->team ? ts->team->prev_ts.team_id : -1;
	  goto do_int;
	case 'H':
	  gomp_display_hostname (buffer, size, &ret, right, sz);
	  break;
	case 'P':
#ifdef HAVE_GETPID
	  val = getpid ();
#else
	  val = 0;
#endif
	  goto do_int;
	case 'i':
#if defined(LIBGOMP_USE_PTHREADS) && defined(__GNUC__)
	  {
	    char buf[3 * (sizeof (handle) + sizeof (uintptr_t) + sizeof (int))
		     + 4];
	    /* This macro returns expr unmodified for integral or pointer
	       types and 0 for anything else (e.g. aggregates).  */
#define gomp_nonaggregate(expr) \
  __builtin_choose_expr (__builtin_classify_type (expr) == 1		    \
			 || __builtin_classify_type (expr) == 5, expr, 0)
	    /* This macro returns expr unmodified for integral types,
	       (uintptr_t) (expr) for pointer types and 0 for anything else
	       (e.g. aggregates).  */
#define gomp_integral(expr) \
  __builtin_choose_expr (__builtin_classify_type (expr) == 5,		    \
			 (uintptr_t) gomp_nonaggregate (expr),		    \
			 gomp_nonaggregate (expr))

	    if (sizeof (gomp_integral (handle)) == sizeof (unsigned long))
	      sprintf (buf, "0x%lx", (unsigned long) gomp_integral (handle));
#if defined (HAVE_INTTYPES_H) && defined (PRIx64)
	    else if (sizeof (gomp_integral (handle)) == sizeof (uint64_t))
	      sprintf (buf, "0x%" PRIx64, (uint64_t) gomp_integral (handle));
#else
	    else if (sizeof (gomp_integral (handle))
		     == sizeof (unsigned long long))
	      sprintf (buf, "0x%llx",
		       (unsigned long long) gomp_integral (handle));
#endif
	    else
	      sprintf (buf, "0x%x", (unsigned int) gomp_integral (handle));
	    gomp_display_num (buffer, size, &ret, zero, right, sz, buf);
	    break;
	  }
#else
	  val = 0;
	  goto do_int;
#endif
	case 'A':
	  if (sz == (size_t) -1)
	    gomp_display_affinity_place (buffer, size, &ret,
					 place - 1);
	  else if (right)
	    {
	      size_t len = 0;
	      gomp_display_affinity_place (NULL, 0, &len, place - 1);
	      if (len < sz)
		gomp_display_repeat (buffer, size, &ret, ' ', sz - len);
	      gomp_display_affinity_place (buffer, size, &ret, place - 1);
	    }
	  else
	    {
	      size_t start = ret;
	      gomp_display_affinity_place (buffer, size, &ret, place - 1);
	      if (ret - start < sz)
		gomp_display_repeat (buffer, size, &ret, ' ', sz - (ret - start));
	    }
	  break;
	do_int:
	  gomp_display_int (buffer, size, &ret, zero, right, sz, val);
	  break;
	default:
	  gomp_fatal ("unsupported type %c in affinity format", c);
	}
      format = p + 1;
    }
  while (1);
  return ret;
}

size_t
omp_capture_affinity (char *buffer, size_t size, const char *format)
{
  struct gomp_thread *thr = gomp_thread ();
  size_t ret
    = gomp_display_affinity (buffer, size,
			     format && *format
			     ? format : gomp_affinity_format_var,
			     gomp_thread_self (), &thr->ts, thr->place);
  if (size)
    {
      if (ret >= size)
	buffer[size - 1] = '\0';
      else
	buffer[ret] = '\0';
    }
  return ret;
}
ialias (omp_capture_affinity)

void
omp_display_affinity (const char *format)
{
  char buf[512];
  char *b;
  size_t ret = ialias_call (omp_capture_affinity) (buf, sizeof buf, format);
  if (ret < sizeof buf)
    {
      buf[ret] = '\n';
      gomp_print_string (buf, ret + 1);
      return;
    }
  b = gomp_malloc (ret + 1);
  ialias_call (omp_capture_affinity) (b, ret + 1, format);
  b[ret] = '\n';
  gomp_print_string (b, ret + 1);
  free (b);
}

void
gomp_display_affinity_thread (gomp_thread_handle handle,
			      struct gomp_team_state *ts, unsigned int place)
{
  char buf[512];
  char *b;
  size_t ret = gomp_display_affinity (buf, sizeof buf, gomp_affinity_format_var,
				      handle, ts, place);
  if (ret < sizeof buf)
    {
      buf[ret] = '\n';
      gomp_print_string (buf, ret + 1);
      return;
    }
  b = gomp_malloc (ret + 1);
  gomp_display_affinity (b, ret + 1, gomp_affinity_format_var,
  			 handle, ts, place);
  b[ret] = '\n';
  gomp_print_string (b, ret + 1);
  free (b);
}