blob: b0c83919f0378b7e07bcc35576452e7152141295 [file] [log] [blame]
Ole Troan2fee1672018-08-23 13:00:53 +02001/*
2 *------------------------------------------------------------------
3 * stat_client.c - Library for access to VPP statistics segment
4 *
5 * Copyright (c) 2018 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
Ole Troan58492a82018-09-04 13:19:12 +020020#include <stdio.h>
21#include <errno.h>
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <sys/un.h>
25#include <stdbool.h>
26#include <sys/stat.h>
Ole Troan2fee1672018-08-23 13:00:53 +020027#include <regex.h>
Ole Troan58492a82018-09-04 13:19:12 +020028#include <assert.h>
29#include <vppinfra/vec.h>
30#include <vppinfra/lock.h>
Ole Troan58492a82018-09-04 13:19:12 +020031#include <stdatomic.h>
Ole Troan2152e1c2018-11-23 23:07:13 +010032#include <vpp/stats/stat_segment.h>
Dave Barach531969e2019-08-14 09:35:41 -040033#include <vpp-api/client/stat_client.h>
Ole Troan2fee1672018-08-23 13:00:53 +020034
35stat_client_main_t stat_client_main;
36
Paul Vinciguerra56421092018-11-21 16:34:09 -080037stat_client_main_t *
38stat_client_get (void)
39{
40 stat_client_main_t *sm;
41 sm = (stat_client_main_t *) malloc (sizeof (stat_client_main_t));
42 clib_memset (sm, 0, sizeof (stat_client_main_t));
43 return sm;
44}
45
46void
47stat_client_free (stat_client_main_t * sm)
48{
49 free (sm);
50}
51
Ole Troan58492a82018-09-04 13:19:12 +020052static int
53recv_fd (int sock)
54{
55 struct msghdr msg = { 0 };
56 struct cmsghdr *cmsg;
57 int fd = -1;
58 char iobuf[1];
59 struct iovec io = {.iov_base = iobuf,.iov_len = sizeof (iobuf) };
60 union
61 {
62 char buf[CMSG_SPACE (sizeof (fd))];
63 struct cmsghdr align;
64 } u;
65 msg.msg_iov = &io;
66 msg.msg_iovlen = 1;
67 msg.msg_control = u.buf;
68 msg.msg_controllen = sizeof (u.buf);
69
70 ssize_t size;
71 if ((size = recvmsg (sock, &msg, 0)) < 0)
72 {
73 perror ("recvmsg failed");
74 return -1;
75 }
76 cmsg = CMSG_FIRSTHDR (&msg);
77 if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
78 {
79 memmove (&fd, CMSG_DATA (cmsg), sizeof (fd));
80 }
81 return fd;
82}
83
84static stat_segment_directory_entry_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -080085get_stat_vector_r (stat_client_main_t * sm)
Ole Troan58492a82018-09-04 13:19:12 +020086{
Ole Troan58492a82018-09-04 13:19:12 +020087 ASSERT (sm->shared_header);
Ole Troan7d29e322020-07-21 08:46:08 +020088 return stat_segment_adjust (sm,
89 (void *) sm->shared_header->directory_vector);
Ole Troan58492a82018-09-04 13:19:12 +020090}
91
Paul Vinciguerra56421092018-11-21 16:34:09 -080092int
Neale Ranns318d7942018-12-03 01:53:32 -080093stat_segment_connect_r (const char *socket_name, stat_client_main_t * sm)
Paul Vinciguerra56421092018-11-21 16:34:09 -080094{
Ole Troan58492a82018-09-04 13:19:12 +020095 int mfd = -1;
96 int sock;
Ole Troan2fee1672018-08-23 13:00:53 +020097
Dave Barachb7b92992018-10-17 10:38:51 -040098 clib_memset (sm, 0, sizeof (*sm));
Ole Troan58492a82018-09-04 13:19:12 +020099 if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
Ole Troan2fee1672018-08-23 13:00:53 +0200100 {
Paul Vinciguerra56421092018-11-21 16:34:09 -0800101 perror ("Stat client couldn't open socket");
Ole Troan2fee1672018-08-23 13:00:53 +0200102 return -1;
103 }
104
Ole Troan58492a82018-09-04 13:19:12 +0200105 struct sockaddr_un un = { 0 };
106 un.sun_family = AF_UNIX;
107 strncpy ((char *) un.sun_path, socket_name, sizeof (un.sun_path) - 1);
108 if (connect (sock, (struct sockaddr *) &un, sizeof (struct sockaddr_un)) <
109 0)
110 {
Dave Barach819d5fd2018-10-02 16:33:56 -0400111 close (sock);
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800112 return -2;
Ole Troan58492a82018-09-04 13:19:12 +0200113 }
Ole Troan2fee1672018-08-23 13:00:53 +0200114
Ole Troan58492a82018-09-04 13:19:12 +0200115 if ((mfd = recv_fd (sock)) < 0)
116 {
Dave Barach819d5fd2018-10-02 16:33:56 -0400117 close (sock);
Ole Troan58492a82018-09-04 13:19:12 +0200118 fprintf (stderr, "Receiving file descriptor failed\n");
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800119 return -3;
Ole Troan58492a82018-09-04 13:19:12 +0200120 }
121 close (sock);
Ole Troan2fee1672018-08-23 13:00:53 +0200122
Ole Troan58492a82018-09-04 13:19:12 +0200123 /* mmap shared memory segment. */
124 void *memaddr;
125 struct stat st = { 0 };
126
127 if (fstat (mfd, &st) == -1)
128 {
Ole Troan8ae3a532019-03-10 21:26:14 +0100129 close (mfd);
Paul Vinciguerra56421092018-11-21 16:34:09 -0800130 perror ("mmap fstat failed");
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800131 return -4;
Ole Troan58492a82018-09-04 13:19:12 +0200132 }
133 if ((memaddr =
134 mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED)
135 {
Ole Troan8ae3a532019-03-10 21:26:14 +0100136 close (mfd);
Paul Vinciguerra56421092018-11-21 16:34:09 -0800137 perror ("mmap map failed");
Paul Vinciguerra356824f2019-01-05 19:37:27 -0800138 return -5;
Ole Troan58492a82018-09-04 13:19:12 +0200139 }
140
Ole Troan8ae3a532019-03-10 21:26:14 +0100141 close (mfd);
Ole Troan58492a82018-09-04 13:19:12 +0200142 sm->memory_size = st.st_size;
143 sm->shared_header = memaddr;
144 sm->directory_vector =
Ole Troan7d29e322020-07-21 08:46:08 +0200145 stat_segment_adjust (sm, (void *) sm->shared_header->directory_vector);
Ole Troan2fee1672018-08-23 13:00:53 +0200146
147 return 0;
148}
149
Paul Vinciguerra56421092018-11-21 16:34:09 -0800150int
Neale Ranns318d7942018-12-03 01:53:32 -0800151stat_segment_connect (const char *socket_name)
Paul Vinciguerra56421092018-11-21 16:34:09 -0800152{
153 stat_client_main_t *sm = &stat_client_main;
154 return stat_segment_connect_r (socket_name, sm);
155}
156
157void
158stat_segment_disconnect_r (stat_client_main_t * sm)
159{
160 munmap (sm->shared_header, sm->memory_size);
161 return;
162}
163
Ole Troan2fee1672018-08-23 13:00:53 +0200164void
165stat_segment_disconnect (void)
166{
167 stat_client_main_t *sm = &stat_client_main;
Paul Vinciguerra56421092018-11-21 16:34:09 -0800168 return stat_segment_disconnect_r (sm);
169}
Ole Troan58492a82018-09-04 13:19:12 +0200170
Paul Vinciguerra56421092018-11-21 16:34:09 -0800171double
172stat_segment_heartbeat_r (stat_client_main_t * sm)
173{
Ole Troancbb8f582019-04-15 08:53:46 +0200174 stat_segment_access_t sa;
175 stat_segment_directory_entry_t *ep;
176
Arthur de Kerhordb023802021-03-11 10:26:54 -0800177 /* Has directory been updated? */
Ole Troancbb8f582019-04-15 08:53:46 +0200178 if (sm->shared_header->epoch != sm->current_epoch)
179 return 0;
Ole Troanfdc67802020-08-10 11:59:20 +0200180 if (stat_segment_access_start (&sa, sm))
181 return 0;
Ole Troancbb8f582019-04-15 08:53:46 +0200182 ep = vec_elt_at_index (sm->directory_vector, STAT_COUNTER_HEARTBEAT);
183 if (!stat_segment_access_end (&sa, sm))
184 return 0.0;
185 return ep->value;
Ole Troan2fee1672018-08-23 13:00:53 +0200186}
187
Ole Troan58492a82018-09-04 13:19:12 +0200188double
Ole Troan2fee1672018-08-23 13:00:53 +0200189stat_segment_heartbeat (void)
190{
Ole Troan2fee1672018-08-23 13:00:53 +0200191 stat_client_main_t *sm = &stat_client_main;
Paul Vinciguerra56421092018-11-21 16:34:09 -0800192 return stat_segment_heartbeat_r (sm);
Ole Troan2fee1672018-08-23 13:00:53 +0200193}
194
Ole Troan65c56c82020-10-21 11:55:28 +0200195#define stat_vec_dup(S,V) \
196 ({ \
197 __typeof__ ((V)[0]) * _v(v) = 0; \
198 if (V && ((void *)V > (void *)S->shared_header) && \
199 (((void*)V + vec_bytes(V)) < \
200 ((void *)S->shared_header + S->memory_size))) \
201 _v(v) = vec_dup(V); \
202 _v(v); \
203})
204
Arthur de Kerhordb023802021-03-11 10:26:54 -0800205static counter_t *
206stat_vec_simple_init (counter_t c)
207{
208 counter_t *v = 0;
209 vec_add1 (v, c);
210 return v;
211}
212
213static vlib_counter_t *
214stat_vec_combined_init (vlib_counter_t c)
215{
216 vlib_counter_t *v = 0;
217 vec_add1 (v, c);
218 return v;
219}
220
221/*
222 * If index2 is specified copy out the column (the indexed value across all
223 * threads), otherwise copy out all values.
224 */
Neale Ranns796c84b2019-03-28 07:56:23 -0700225static stat_segment_data_t
Arthur de Kerhordb023802021-03-11 10:26:54 -0800226copy_data (stat_segment_directory_entry_t *ep, u32 index2, char *name,
227 stat_client_main_t *sm)
Ole Troan2fee1672018-08-23 13:00:53 +0200228{
229 stat_segment_data_t result = { 0 };
Ole Troan2fee1672018-08-23 13:00:53 +0200230 int i;
231 vlib_counter_t **combined_c; /* Combined counter */
232 counter_t **simple_c; /* Simple counter */
Ole Troan7d29e322020-07-21 08:46:08 +0200233 uint64_t *error_vector;
Ole Troan58492a82018-09-04 13:19:12 +0200234
235 assert (sm->shared_header);
236
Ole Troan2fee1672018-08-23 13:00:53 +0200237 result.type = ep->type;
Arthur de Kerhordb023802021-03-11 10:26:54 -0800238 result.name = strdup (name ? name : ep->name);
239
Ole Troan2fee1672018-08-23 13:00:53 +0200240 switch (ep->type)
241 {
Ole Troan58492a82018-09-04 13:19:12 +0200242 case STAT_DIR_TYPE_SCALAR_INDEX:
243 result.scalar_value = ep->value;
Ole Troan2fee1672018-08-23 13:00:53 +0200244 break;
245
246 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
Ole Troan7d29e322020-07-21 08:46:08 +0200247 simple_c = stat_segment_adjust (sm, ep->data);
Ole Troan65c56c82020-10-21 11:55:28 +0200248 result.simple_counter_vec = stat_vec_dup (sm, simple_c);
Ole Troan2fee1672018-08-23 13:00:53 +0200249 for (i = 0; i < vec_len (simple_c); i++)
Ole Troan58492a82018-09-04 13:19:12 +0200250 {
Ole Troan7d29e322020-07-21 08:46:08 +0200251 counter_t *cb = stat_segment_adjust (sm, simple_c[i]);
Arthur de Kerhordb023802021-03-11 10:26:54 -0800252 if (index2 != ~0)
253 result.simple_counter_vec[i] = stat_vec_simple_init (cb[index2]);
254 else
255 result.simple_counter_vec[i] = stat_vec_dup (sm, cb);
Ole Troan58492a82018-09-04 13:19:12 +0200256 }
Ole Troan2fee1672018-08-23 13:00:53 +0200257 break;
258
259 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
Ole Troan7d29e322020-07-21 08:46:08 +0200260 combined_c = stat_segment_adjust (sm, ep->data);
Ole Troan65c56c82020-10-21 11:55:28 +0200261 result.combined_counter_vec = stat_vec_dup (sm, combined_c);
Ole Troan2fee1672018-08-23 13:00:53 +0200262 for (i = 0; i < vec_len (combined_c); i++)
Ole Troan58492a82018-09-04 13:19:12 +0200263 {
Ole Troan7d29e322020-07-21 08:46:08 +0200264 vlib_counter_t *cb = stat_segment_adjust (sm, combined_c[i]);
Arthur de Kerhordb023802021-03-11 10:26:54 -0800265 if (index2 != ~0)
266 result.combined_counter_vec[i] =
267 stat_vec_combined_init (cb[index2]);
268 else
269 result.combined_counter_vec[i] = stat_vec_dup (sm, cb);
Ole Troan58492a82018-09-04 13:19:12 +0200270 }
Ole Troan2fee1672018-08-23 13:00:53 +0200271 break;
272
273 case STAT_DIR_TYPE_ERROR_INDEX:
Ole Troan233e4682019-05-16 15:01:34 +0200274 /* Gather errors from all threads into a vector */
Ole Troan7d29e322020-07-21 08:46:08 +0200275 error_vector =
276 stat_segment_adjust (sm, (void *) sm->shared_header->error_vector);
277 vec_validate (result.error_vector, vec_len (error_vector) - 1);
278 for (i = 0; i < vec_len (error_vector); i++)
Ole Troan233e4682019-05-16 15:01:34 +0200279 {
Ole Troan7d29e322020-07-21 08:46:08 +0200280 counter_t *cb = stat_segment_adjust (sm, (void *) error_vector[i]);
Ole Troan233e4682019-05-16 15:01:34 +0200281 result.error_vector[i] = cb[ep->index];
282 }
Ole Troan2fee1672018-08-23 13:00:53 +0200283 break;
284
Ole Troan703908a2019-02-26 16:37:03 +0100285 case STAT_DIR_TYPE_NAME_VECTOR:
Ole Troan7d29e322020-07-21 08:46:08 +0200286 {
287 uint8_t **name_vector = stat_segment_adjust (sm, ep->data);
Ole Troan65c56c82020-10-21 11:55:28 +0200288 result.name_vector = stat_vec_dup (sm, name_vector);
Ole Troan7d29e322020-07-21 08:46:08 +0200289 for (i = 0; i < vec_len (name_vector); i++)
290 {
291 u8 *name = stat_segment_adjust (sm, name_vector[i]);
Ole Troan65c56c82020-10-21 11:55:28 +0200292 result.name_vector[i] = stat_vec_dup (sm, name);
Ole Troan7d29e322020-07-21 08:46:08 +0200293 }
294 }
Ole Troan703908a2019-02-26 16:37:03 +0100295 break;
296
Arthur de Kerhordb023802021-03-11 10:26:54 -0800297 case STAT_DIR_TYPE_SYMLINK:
298 /* Gather info from all threads into a vector */
299 {
300 stat_segment_directory_entry_t *ep2;
301 ep2 = vec_elt_at_index (sm->directory_vector, ep->index1);
302 return copy_data (ep2, ep->index2, ep->name, sm);
303 }
304
Vladimir Isaev72e31bc2020-02-04 11:54:27 +0300305 case STAT_DIR_TYPE_EMPTY:
306 break;
307
Ole Troan2fee1672018-08-23 13:00:53 +0200308 default:
Ole Troan703908a2019-02-26 16:37:03 +0100309 fprintf (stderr, "Unknown type: %d\n", ep->type);
Ole Troan2fee1672018-08-23 13:00:53 +0200310 }
311 return result;
312}
313
Ole Troan2fee1672018-08-23 13:00:53 +0200314void
315stat_segment_data_free (stat_segment_data_t * res)
316{
317 int i, j;
318 for (i = 0; i < vec_len (res); i++)
319 {
320 switch (res[i].type)
321 {
322 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
323 for (j = 0; j < vec_len (res[i].simple_counter_vec); j++)
324 vec_free (res[i].simple_counter_vec[j]);
325 vec_free (res[i].simple_counter_vec);
326 break;
327 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
328 for (j = 0; j < vec_len (res[i].combined_counter_vec); j++)
329 vec_free (res[i].combined_counter_vec[j]);
330 vec_free (res[i].combined_counter_vec);
331 break;
Ole Troan3c70c052020-08-10 16:25:21 +0200332 case STAT_DIR_TYPE_NAME_VECTOR:
333 for (j = 0; j < vec_len (res[i].name_vector); j++)
334 vec_free (res[i].name_vector[j]);
335 vec_free (res[i].name_vector);
336 break;
337 case STAT_DIR_TYPE_ERROR_INDEX:
338 vec_free (res[i].error_vector);
339 break;
Ole Troan65c56c82020-10-21 11:55:28 +0200340 case STAT_DIR_TYPE_SCALAR_INDEX:
Steven Luong0a214802022-01-28 14:12:53 -0800341 case STAT_DIR_TYPE_EMPTY:
Ole Troan65c56c82020-10-21 11:55:28 +0200342 break;
Ole Troan2fee1672018-08-23 13:00:53 +0200343 default:
Ole Troan3c70c052020-08-10 16:25:21 +0200344 assert (0);
Ole Troan2fee1672018-08-23 13:00:53 +0200345 }
Ole Troan58492a82018-09-04 13:19:12 +0200346 free (res[i].name);
Ole Troan2fee1672018-08-23 13:00:53 +0200347 }
348 vec_free (res);
349}
350
Ole Troan58492a82018-09-04 13:19:12 +0200351uint32_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -0800352stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm)
Ole Troan58492a82018-09-04 13:19:12 +0200353{
Ole Troan58492a82018-09-04 13:19:12 +0200354 stat_segment_access_t sa;
355
356 uint32_t *dir = 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200357 regex_t regex[vec_len (patterns)];
358
Ole Troan58492a82018-09-04 13:19:12 +0200359 int i, j;
Ole Troan2fee1672018-08-23 13:00:53 +0200360 for (i = 0; i < vec_len (patterns); i++)
361 {
Mohsin Kazmi90a9fdf2018-12-04 14:31:15 +0000362 int rv = regcomp (&regex[i], (const char *) patterns[i], 0);
Ole Troan2fee1672018-08-23 13:00:53 +0200363 if (rv)
364 {
365 fprintf (stderr, "Could not compile regex %s\n", patterns[i]);
366 return dir;
367 }
368 }
369
Ole Troanfdc67802020-08-10 11:59:20 +0200370 if (stat_segment_access_start (&sa, sm))
371 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200372
Paul Vinciguerra56421092018-11-21 16:34:09 -0800373 stat_segment_directory_entry_t *counter_vec = get_stat_vector_r (sm);
Ole Troan58492a82018-09-04 13:19:12 +0200374 for (j = 0; j < vec_len (counter_vec); j++)
375 {
376 for (i = 0; i < vec_len (patterns); i++)
377 {
378 int rv = regexec (&regex[i], counter_vec[j].name, 0, NULL, 0);
379 if (rv == 0)
380 {
381 vec_add1 (dir, j);
382 break;
383 }
384 }
385 if (vec_len (patterns) == 0)
386 vec_add1 (dir, j);
Ole Troan2fee1672018-08-23 13:00:53 +0200387 }
Ole Troan2fee1672018-08-23 13:00:53 +0200388
389 for (i = 0; i < vec_len (patterns); i++)
390 regfree (&regex[i]);
391
Paul Vinciguerra56421092018-11-21 16:34:09 -0800392 if (!stat_segment_access_end (&sa, sm))
Ole Troan58492a82018-09-04 13:19:12 +0200393 {
394 /* Failed, clean up */
395 vec_free (dir);
396 return 0;
397
398 }
399
400 /* Update last version */
401 sm->current_epoch = sa.epoch;
Ole Troan2fee1672018-08-23 13:00:53 +0200402 return dir;
403}
404
Paul Vinciguerra56421092018-11-21 16:34:09 -0800405uint32_t *
406stat_segment_ls (uint8_t ** patterns)
407{
408 stat_client_main_t *sm = &stat_client_main;
409 return stat_segment_ls_r ((uint8_t **) patterns, sm);
410}
411
Ole Troan2fee1672018-08-23 13:00:53 +0200412stat_segment_data_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -0800413stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm)
Ole Troan2fee1672018-08-23 13:00:53 +0200414{
415 int i;
Ole Troan2fee1672018-08-23 13:00:53 +0200416 stat_segment_directory_entry_t *ep;
417 stat_segment_data_t *res = 0;
Ole Troan58492a82018-09-04 13:19:12 +0200418 stat_segment_access_t sa;
Ole Troan2fee1672018-08-23 13:00:53 +0200419
Ole Troan58492a82018-09-04 13:19:12 +0200420 /* Has directory been update? */
421 if (sm->shared_header->epoch != sm->current_epoch)
422 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200423
Ole Troanfdc67802020-08-10 11:59:20 +0200424 if (stat_segment_access_start (&sa, sm))
425 return 0;
426
Ole Troan2fee1672018-08-23 13:00:53 +0200427 for (i = 0; i < vec_len (stats); i++)
428 {
Ole Troan2fee1672018-08-23 13:00:53 +0200429 /* Collect counter */
Ole Troan58492a82018-09-04 13:19:12 +0200430 ep = vec_elt_at_index (sm->directory_vector, stats[i]);
Arthur de Kerhordb023802021-03-11 10:26:54 -0800431 vec_add1 (res, copy_data (ep, ~0, 0, sm));
Ole Troan2fee1672018-08-23 13:00:53 +0200432 }
Ole Troan2fee1672018-08-23 13:00:53 +0200433
Paul Vinciguerra56421092018-11-21 16:34:09 -0800434 if (stat_segment_access_end (&sa, sm))
Ole Troan58492a82018-09-04 13:19:12 +0200435 return res;
436
437 fprintf (stderr, "Epoch changed while reading, invalid results\n");
438 // TODO increase counter
439 return 0;
Ole Troan2fee1672018-08-23 13:00:53 +0200440}
441
Paul Vinciguerra56421092018-11-21 16:34:09 -0800442stat_segment_data_t *
443stat_segment_dump (uint32_t * stats)
444{
445 stat_client_main_t *sm = &stat_client_main;
446 return stat_segment_dump_r (stats, sm);
447}
448
Ole Troan73202102018-08-31 00:29:48 +0200449/* Wrapper for accessing vectors from other languages */
450int
451stat_segment_vec_len (void *vec)
452{
453 return vec_len (vec);
454}
455
Ole Troan82540182018-10-22 09:41:29 +0200456void
457stat_segment_vec_free (void *vec)
458{
459 vec_free (vec);
460}
461
Ole Troan73202102018-08-31 00:29:48 +0200462/* Create a vector from a string (or add to existing) */
Mohsin Kazmi90a9fdf2018-12-04 14:31:15 +0000463uint8_t **
464stat_segment_string_vector (uint8_t ** string_vector, const char *string)
Ole Troan73202102018-08-31 00:29:48 +0200465{
Mohsin Kazmi90a9fdf2018-12-04 14:31:15 +0000466 uint8_t *name = 0;
467 size_t len = strlen (string);
468
469 vec_validate_init_c_string (name, string, len);
470 vec_add1 (string_vector, name);
Ole Troan73202102018-08-31 00:29:48 +0200471 return string_vector;
472}
473
Ole Troan58492a82018-09-04 13:19:12 +0200474stat_segment_data_t *
Paul Vinciguerra56421092018-11-21 16:34:09 -0800475stat_segment_dump_entry_r (uint32_t index, stat_client_main_t * sm)
Ole Troan58492a82018-09-04 13:19:12 +0200476{
Ole Troan58492a82018-09-04 13:19:12 +0200477 stat_segment_directory_entry_t *ep;
478 stat_segment_data_t *res = 0;
479 stat_segment_access_t sa;
480
Arthur de Kerhordb023802021-03-11 10:26:54 -0800481 /* Has directory been update? */
482 if (sm->shared_header->epoch != sm->current_epoch)
483 return 0;
484
Ole Troanfdc67802020-08-10 11:59:20 +0200485 if (stat_segment_access_start (&sa, sm))
486 return 0;
Ole Troan58492a82018-09-04 13:19:12 +0200487
488 /* Collect counter */
489 ep = vec_elt_at_index (sm->directory_vector, index);
Arthur de Kerhordb023802021-03-11 10:26:54 -0800490 vec_add1 (res, copy_data (ep, ~0, 0, sm));
Ole Troan58492a82018-09-04 13:19:12 +0200491
Paul Vinciguerra56421092018-11-21 16:34:09 -0800492 if (stat_segment_access_end (&sa, sm))
Ole Troan58492a82018-09-04 13:19:12 +0200493 return res;
494 return 0;
495}
496
Paul Vinciguerra56421092018-11-21 16:34:09 -0800497stat_segment_data_t *
498stat_segment_dump_entry (uint32_t index)
499{
500 stat_client_main_t *sm = &stat_client_main;
501 return stat_segment_dump_entry_r (index, sm);
502}
503
Ole Troan58492a82018-09-04 13:19:12 +0200504char *
Ole Troancbb8f582019-04-15 08:53:46 +0200505stat_segment_index_to_name_r (uint32_t index, stat_client_main_t * sm)
506{
507 stat_segment_directory_entry_t *ep;
508 stat_segment_access_t sa;
509 stat_segment_directory_entry_t *vec;
510
511 /* Has directory been update? */
512 if (sm->shared_header->epoch != sm->current_epoch)
513 return 0;
Ole Troanfdc67802020-08-10 11:59:20 +0200514 if (stat_segment_access_start (&sa, sm))
515 return 0;
Ole Troancbb8f582019-04-15 08:53:46 +0200516 vec = get_stat_vector_r (sm);
517 ep = vec_elt_at_index (vec, index);
518 if (!stat_segment_access_end (&sa, sm))
519 return 0;
520 return strdup (ep->name);
521}
522
523char *
Ole Troan58492a82018-09-04 13:19:12 +0200524stat_segment_index_to_name (uint32_t index)
525{
Ole Troancbb8f582019-04-15 08:53:46 +0200526 stat_client_main_t *sm = &stat_client_main;
527 return stat_segment_index_to_name_r (index, sm);
Ole Troan58492a82018-09-04 13:19:12 +0200528}
529
Ole Troanb63dbc52019-06-14 10:26:14 +0200530uint64_t
531stat_segment_version_r (stat_client_main_t * sm)
532{
533 ASSERT (sm->shared_header);
534 return sm->shared_header->version;
535}
536
537uint64_t
538stat_segment_version (void)
539{
540 stat_client_main_t *sm = &stat_client_main;
541 return stat_segment_version_r (sm);
542}
543
Ole Troan2fee1672018-08-23 13:00:53 +0200544/*
545 * fd.io coding-style-patch-verification: ON
546 *
547 * Local Variables:
548 * eval: (c-set-style "gnu")
549 * End:
550 */