aboutsummaryrefslogtreecommitdiffstats
path: root/src/vpp-api/client/stat_client.c
blob: ad7078ea74531548a8ba57e6c947744969a6f842 (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
/*
 *------------------------------------------------------------------
 * stat_client.c - Library for access to VPP statistics segment
 *
 * Copyright (c) 2018 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.
 *------------------------------------------------------------------
 */

#include <vlib/vlib.h>
#include <vppinfra/socket.h>
#include <svm/ssvm.h>
#include <vpp/stats/stats.h>
#include <regex.h>
#include "stat_client.h"

typedef struct
{
  u64 current_epoch;
  volatile int segment_ready;
  ssvm_private_t stat_segment;	/* mapped stats segment object */
  ssvm_shared_header_t *shared_header;
  clib_spinlock_t *stat_segment_lockp;	/* Spinlock for the stats segment */
  uword *counter_vector_by_name;
  u64 *error_base;
} stat_client_main_t;

stat_client_main_t stat_client_main;

int
stat_segment_connect (char *socket_name)
{
  stat_client_main_t *sm = &stat_client_main;
  ssvm_private_t *ssvmp = &sm->stat_segment;
  clib_socket_t s = { 0 };
  clib_error_t *err;
  int fd = -1, retval;

  memset (sm, 0, sizeof (*sm));
  s.config = socket_name;
  s.flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET;
  err = clib_socket_init (&s);
  if (err)
    {
      clib_error_report (err);
      return -1;
    }
  err = clib_socket_recvmsg (&s, 0, 0, &fd, 1);
  if (err)
    {
      clib_error_report (err);
      return -1;
    }
  clib_socket_close (&s);

  memset (ssvmp, 0, sizeof (*ssvmp));
  ssvmp->fd = fd;

  /* Note: this closes memfd.fd */
  retval = ssvm_slave_init_memfd (ssvmp);
  if (retval)
    {
      fprintf (stderr, "WARNING: segment map returned %d\n", retval);
      return -1;
    }

  ASSERT (ssvmp && ssvmp->sh);

  /* Pick up the segment lock from the shared memory header */
  sm->shared_header = ssvmp->sh;
  sm->stat_segment_lockp = (clib_spinlock_t *) (sm->shared_header->opaque[0]);
  sm->segment_ready = 1;

  sm->counter_vector_by_name =
    (uword *) sm->shared_header->opaque[STAT_SEGMENT_OPAQUE_DIR];

  return 0;
}

void
stat_segment_disconnect (void)
{
  stat_client_main_t *sm = &stat_client_main;
  ssvm_delete_memfd (&sm->stat_segment);
  return;
}

/*
 * The application needs to register which counters it is interested
 * in.
 */
stat_segment_cached_pointer_t *
stat_segment_register (u8 * stats[])
{
  int i;
  uword *p;
  stat_client_main_t *sm = &stat_client_main;
  stat_segment_cached_pointer_t *cp, *cached_pointer_vec = 0;

  for (i = 0; i < vec_len (stats); i++)
    {
      p = hash_get_mem (sm->counter_vector_by_name, stats[i]);
      if (p == 0)
	{
	  fprintf (stderr, "WARN: %s not in directory!\n", stats[i]);
	  continue;
	}
      vec_add2 (cached_pointer_vec, cp, 1);
      cp->name = strdup ((char *) stats[i]);	// Point to p->key instead?
    }
  return cached_pointer_vec;
}

static u64 *
get_error_base (u32 thread_index)
{
  u64 *error_base = 0;
  uword *p;
  stat_client_main_t *sm = &stat_client_main;
  stat_segment_directory_entry_t *ep;

  /* Special case /err/0/counter_vector */
  p = hash_get_mem (sm->counter_vector_by_name,
		    format (0, "/err/%d/counter_vector", thread_index));
  if (p)
    {
      ep = (stat_segment_directory_entry_t *) (p[0]);
      error_base = ep->value;
    }
  return error_base;
}

f64
stat_segment_heartbeat (void)
{
  f64 *heartbeat = 0;
  uword *p;
  stat_client_main_t *sm = &stat_client_main;
  stat_segment_directory_entry_t *ep;

  /* Special case /err/0/counter_vector */
  p = hash_get_mem (sm->counter_vector_by_name,
		    format (0, "/sys/heartbeat%c", 0));
  if (p)
    {
      ep = (stat_segment_directory_entry_t *) (p[0]);
      heartbeat = ep->value;
    }
  return *heartbeat;
}

static void
maybe_update_cached_pointers (stat_segment_cached_pointer_t * cached_pointers)
{
  stat_client_main_t *sm = &stat_client_main;
  stat_segment_cached_pointer_t *cp;
  uword *p;
  int i;

  /* Cached pointers OK? */
  if (sm->current_epoch ==
      (u64) sm->shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH])
    return;

  /* Special case /err/0/counter_vector */
  sm->error_base = get_error_base (0);

  /* Nope, fix them... */
  for (i = 0; i < vec_len (cached_pointers); i++)
    {
      cp = &cached_pointers[i];

      p = hash_get_mem (sm->counter_vector_by_name, cp->name);
      if (p == 0)
	{
	  fprintf (stderr, "WARN: %s not in directory!\n", cp->name);
	  continue;
	}
      cp->ep = (stat_segment_directory_entry_t *) (p[0]);
    }

  /* And remember that we did... */
  sm->current_epoch =
    (u64) sm->shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH];
}

stat_segment_data_t
copy_data (stat_segment_directory_entry_t * ep, u64 * error_base, char *name)
{
  stat_segment_data_t result = { 0 };
  u32 error_index;
  int i;
  vlib_counter_t **combined_c;	/* Combined counter */
  counter_t **simple_c;		/* Simple counter */
  result.type = ep->type;
  result.name = name;
  switch (ep->type)
    {
    case STAT_DIR_TYPE_SCALAR_POINTER:
      result.scalar_value = *(f64 *) ep->value;
      break;

    case STAT_DIR_TYPE_VECTOR_POINTER:
      result.vector_pointer = ep->value;
      break;

    case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
      simple_c = ep->value;
      result.simple_counter_vec = vec_dup (simple_c);
      for (i = 0; i < vec_len (simple_c); i++)
	result.simple_counter_vec[i] = vec_dup (simple_c[i]);
      break;

    case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
      combined_c = ep->value;
      result.combined_counter_vec = vec_dup (combined_c);
      for (i = 0; i < vec_len (combined_c); i++)
	result.combined_counter_vec[i] = vec_dup (combined_c[i]);
      break;

    case STAT_DIR_TYPE_ERROR_INDEX:
      error_index = (uintptr_t) ep->value;
      result.error_value = error_base[error_index];
      break;

    default:
      fprintf (stderr, "Unknown type: %d", ep->type);
    }
  return result;
}

stat_segment_data_t *
stat_segment_collect (stat_segment_cached_pointer_t * cached_pointers)
{
  stat_client_main_t *sm = &stat_client_main;
  stat_segment_data_t *res = 0;
  int i;

  /* Grab the stats segment lock */
  clib_spinlock_lock (sm->stat_segment_lockp);

  /* see if we need to update cached pointers */
  maybe_update_cached_pointers (cached_pointers);

  for (i = 0; i < vec_len (cached_pointers); i++)
    {
      vec_add1 (res,
		copy_data (cached_pointers[i].ep, sm->error_base,
			   cached_pointers[i].name));
    }

  /* Drop the lock */
  clib_spinlock_unlock (sm->stat_segment_lockp);

  return res;
}

void
stat_segment_data_free (stat_segment_data_t * res)
{
  int i, j;
  for (i = 0; i < vec_len (res); i++)
    {
      switch (res[i].type)
	{
	case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
	  for (j = 0; j < vec_len (res[i].simple_counter_vec); j++)
	    vec_free (res[i].simple_counter_vec[j]);
	  vec_free (res[i].simple_counter_vec);
	  break;
	case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
	  for (j = 0; j < vec_len (res[i].combined_counter_vec); j++)
	    vec_free (res[i].combined_counter_vec[j]);
	  vec_free (res[i].combined_counter_vec);
	  break;
	default:
	  ;
	}
    }
  vec_free (res);
}

u8 **
stat_segment_ls (u8 ** patterns)
{
  stat_client_main_t *sm = &stat_client_main;
  hash_pair_t *p;
  u8 **dir = 0;
  regex_t regex[vec_len (patterns)];

  int i;
  for (i = 0; i < vec_len (patterns); i++)
    {
      int rv = regcomp (&regex[i], (char *) patterns[i], 0);
      if (rv)
	{
	  fprintf (stderr, "Could not compile regex %s\n", patterns[i]);
	  return dir;
	}
    }

  clib_spinlock_lock (sm->stat_segment_lockp);

  /* *INDENT-OFF* */
  hash_foreach_pair (p, sm->counter_vector_by_name,
  ({
    for (i = 0; i < vec_len(patterns); i++) {
      int rv = regexec(&regex[i], (char *)p->key, 0, NULL, 0);
      if (rv == 0) {
	vec_add1 (dir, (u8 *)p->key);
	break;
      }
    }
    if (vec_len(patterns) == 0)
      vec_add1 (dir, (u8 *)p->key);
  }));
  /* *INDENT-ON* */

  clib_spinlock_unlock (sm->stat_segment_lockp);

  for (i = 0; i < vec_len (patterns); i++)
    regfree (&regex[i]);

  return dir;
}

stat_segment_data_t *
stat_segment_dump (u8 * stats[])
{
  int i;
  uword *p;
  stat_client_main_t *sm = &stat_client_main;
  stat_segment_directory_entry_t *ep;
  stat_segment_data_t *res = 0;

  clib_spinlock_lock (sm->stat_segment_lockp);

  sm->error_base = get_error_base (0);
  for (i = 0; i < vec_len (stats); i++)
    {
      p = hash_get_mem (sm->counter_vector_by_name, stats[i]);
      if (p == 0)
	{
	  fprintf (stderr, "WARN: %s not in directory!\n", stats[i]);
	  continue;
	}
      /* Collect counter */
      ep = (stat_segment_directory_entry_t *) (p[0]);
      vec_add1 (res, copy_data (ep, sm->error_base, (char *) stats[i]));
    }
  clib_spinlock_unlock (sm->stat_segment_lockp);

  return res;
}

/* Wrapper for accessing vectors from other languages */
int
stat_segment_vec_len (void *vec)
{
  return vec_len (vec);
}

/* Create a vector from a string (or add to existing) */
u8 **
stat_segment_string_vector (u8 ** string_vector, char *string)
{
  u8 *name = 0;
  name = vec_dup ((u8 *) string);
  vec_add1 (string_vector, (u8 *) name);
  return string_vector;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */