aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/interrupt.h
blob: 60c01fa024845c0dadb6aaf73461ea8436fffe8a (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
/*
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef included_clib_interrupt_h
#define included_clib_interrupt_h

#include <vppinfra/clib.h>
#include <vppinfra/bitops.h> /* for count_set_bits */
#include <vppinfra/vec.h>

typedef struct
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
  int n_int;
  uword n_uword_alloc;
} clib_interrupt_header_t;

void clib_interrupt_init (void **data, uword n_interrupts);
void clib_interrupt_resize (void **data, uword n_interrupts);

static_always_inline void
clib_interrupt_free (void **data)
{
  if (data[0])
    {
      clib_mem_free (data[0]);
      data[0] = 0;
    }
}

static_always_inline int
clib_interrupt_get_n_int (void *d)
{
  clib_interrupt_header_t *h = d;
  if (h)
    return h->n_int;
  return 0;
}

static_always_inline uword *
clib_interrupt_get_bitmap (void *d)
{
  return d + sizeof (clib_interrupt_header_t);
}

static_always_inline uword *
clib_interrupt_get_atomic_bitmap (void *d)
{
  clib_interrupt_header_t *h = d;
  return clib_interrupt_get_bitmap (d) + h->n_uword_alloc;
}

static_always_inline void
clib_interrupt_set (void *in, int int_num)
{
  uword *bmp = clib_interrupt_get_bitmap (in);
  uword mask = 1ULL << (int_num & (uword_bits - 1));
  bmp += int_num >> log2_uword_bits;

  ASSERT (int_num < ((clib_interrupt_header_t *) in)->n_int);

  *bmp |= mask;
}

static_always_inline void
clib_interrupt_set_atomic (void *in, int int_num)
{
  uword *bmp = clib_interrupt_get_atomic_bitmap (in);
  uword mask = 1ULL << (int_num & (uword_bits - 1));
  bmp += int_num >> log2_uword_bits;

  ASSERT (int_num < ((clib_interrupt_header_t *) in)->n_int);

  __atomic_fetch_or (bmp, mask, __ATOMIC_RELAXED);
}

static_always_inline void
clib_interrupt_clear (void *in, int int_num)
{
  uword *bmp = clib_interrupt_get_bitmap (in);
  uword *abm = clib_interrupt_get_atomic_bitmap (in);
  uword mask = 1ULL << (int_num & (uword_bits - 1));
  uword off = int_num >> log2_uword_bits;

  ASSERT (int_num < ((clib_interrupt_header_t *) in)->n_int);

  bmp[off] |= __atomic_exchange_n (abm + off, 0, __ATOMIC_SEQ_CST);
  bmp[off] &= ~mask;
}

static_always_inline int
clib_interrupt_get_next (void *in, int last)
{
  uword *bmp = clib_interrupt_get_bitmap (in);
  uword *abm = clib_interrupt_get_atomic_bitmap (in);
  clib_interrupt_header_t *h = in;
  uword bmp_uword, off;

  ASSERT (last >= -1 && last < h->n_int);

  off = (last + 1) >> log2_uword_bits;

  last -= off << log2_uword_bits;
  bmp[off] |= __atomic_exchange_n (abm + off, 0, __ATOMIC_SEQ_CST);
  bmp_uword = bmp[off] & ~pow2_mask (last + 1);

next:
  if (bmp_uword)
    return (off << log2_uword_bits) + count_trailing_zeros (bmp_uword);

  off++;

  if (off > h->n_int >> log2_uword_bits)
    return -1;

  bmp[off] |= __atomic_exchange_n (abm + off, 0, __ATOMIC_SEQ_CST);
  bmp_uword = bmp[off];

  goto next;
}

#endif /* included_clib_interrupt_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
n class="o">= j + 1; ed->event = i; } } if (dump_file == 0) dump_file = "/tmp/g2_test.elog"; error = elog_write_file (em, dump_file, 1 /* flush ring */ ); if (error) clib_error_report (error); } int test_elog_main (unformat_input_t * input) { clib_error_t *error = 0; u32 i, n_iter, seed, max_events; elog_main_t _em, *em = &_em; u32 verbose; f64 min_sample_time; char *dump_file, *load_file, *merge_file, **merge_files; u8 *tag, **tags; f64 align_tweak; f64 *align_tweaks; int g2_test; n_iter = 100; max_events = 100000; seed = 1; verbose = 0; dump_file = 0; load_file = 0; merge_files = 0; tags = 0; align_tweaks = 0; min_sample_time = 2; g2_test = 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "iter %d", &n_iter)) ; else if (unformat (input, "seed %d", &seed)) ; else if (unformat (input, "dump %s", &dump_file)) ; else if (unformat (input, "load %s", &load_file)) ; else if (unformat (input, "tag %s", &tag)) vec_add1 (tags, tag); else if (unformat (input, "merge %s", &merge_file)) vec_add1 (merge_files, merge_file); else if (unformat (input, "verbose %=", &verbose, 1)) ; else if (unformat (input, "max-events %d", &max_events)) ; else if (unformat (input, "sample-time %f", &min_sample_time)) ; else if (unformat (input, "align-tweak %f", &align_tweak)) vec_add1 (align_tweaks, align_tweak); else if (unformat (input, "g2-test %=", &g2_test, 1)) ; else { error = clib_error_create ("unknown input `%U'\n", format_unformat_error, input); goto done; } } if (g2_test) { g2_test_pattern (em, g2_test, dump_file); return (0); } #ifdef CLIB_UNIX if (load_file) { if ((error = elog_read_file (em, load_file))) goto done; } else if (merge_files) { uword i; elog_main_t *ems; vec_clone (ems, merge_files); /* Supply default tags as needed */ if (vec_len (tags) < vec_len (ems)) { for (i = vec_len (tags); i < vec_len (ems); i++) vec_add1 (tags, format (0, "F%d%c", i, 0)); } elog_init (em, max_events); for (i = 0; i < vec_len (ems); i++) { if ((error = elog_read_file (i == 0 ? em : &ems[i], merge_files[i]))) goto done; if (i > 0) { align_tweak = 0.0; if (i <= vec_len (align_tweaks)) align_tweak = align_tweaks[i - 1]; elog_merge (em, tags[0], &ems[i], tags[i], align_tweak); tags[0] = 0; } } } else #endif /* CLIB_UNIX */ { f64 t[2]; elog_init (em, max_events); elog_enable_disable (em, 1); t[0] = unix_time_now (); for (i = 0; i < n_iter; i++) { u32 j, n, sum; n = 1 + (random_u32 (&seed) % 128); sum = 0; for (j = 0; j < n; j++) sum += random_u32 (&seed); { ELOG_TYPE_XF (e); ELOG (em, e, sum); } { ELOG_TYPE_XF (e); ELOG (em, e, sum + 1); } { struct { u32 string_index; f32 f; } *d; ELOG_TYPE_DECLARE (e) = { .format = "fumble %s %.9f",.format_args = "t4f4",.n_enum_strings = 4,.enum_strings = { "string0", "string1", "string2", "string3",},}; d = ELOG_DATA (em, e); d->string_index = sum & 3; d->f = (sum & 0xff) / 128.; } { ELOG_TYPE_DECLARE (e) = { .format = "bar %d.%d.%d.%d",.format_args = "i1i1i1i1",}; ELOG_TRACK (my_track); u8 *d = ELOG_TRACK_DATA (em, e, my_track); d[0] = i + 0; d[1] = i + 1; d[2] = i + 2; d[3] = i + 3; } { ELOG_TYPE_DECLARE (e) = { .format = "bar `%s'",.format_args = "s20",}; struct { char s[20]; } *d; u8 *v; d = ELOG_DATA (em, e); v = format (0, "foo %d%c", i, 0); clib_memcpy (d->s, v, clib_min (vec_len (v), sizeof (d->s))); } { ELOG_TYPE_DECLARE (e) = { .format = "bar `%s'",.format_args = "T4",}; struct { u32 offset; } *d; d = ELOG_DATA (em, e); d->offset = elog_string (em, "string table %d", i); } } do { t[1] = unix_time_now (); } while (t[1] - t[0] < min_sample_time); } #ifdef CLIB_UNIX if (dump_file) { if ((error = elog_write_file (em, dump_file, 0 /* do not flush ring */ ))) goto done; } #endif if (verbose) { elog_event_t *e, *es; es = elog_get_events (em); vec_foreach (e, es) { clib_warning ("%18.9f: %12U %U\n", e->time, format_elog_track_name, em, e, format_elog_event, em, e); } } done: if (error) clib_error_report (error); return 0; } #ifdef CLIB_UNIX int main (int argc, char *argv[]) { unformat_input_t i; int r; clib_mem_init (0, 3ULL << 30); unformat_init_command_line (&i, argv); r = test_elog_main (&i); unformat_free (&i); return r; } #endif /** * @brief GDB callable function: vl - Return vector length of vector * * @param *p - void - address of vector * * @return length - u32 * */ u32 vl (void *p) { return vec_len (p); } /** * @brief GDB callable function: pe - call pool_elts - number of elements in a pool * * @param *v - void - address of pool * * @return number - uword * */ #include <vppinfra/pool.h> uword pe (void *v) { return (pool_elts (v)); } #include <vppinfra/hash.h> uword he (void *v) { return (hash_elts (v)); } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */