Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 20 | #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 Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 27 | #include <regex.h> |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | #include <vppinfra/vec.h> |
| 30 | #include <vppinfra/lock.h> |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 31 | #include "stat_client.h" |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 32 | #include <stdatomic.h> |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 33 | |
| 34 | typedef struct |
| 35 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 36 | uint64_t current_epoch; |
| 37 | stat_segment_shared_header_t *shared_header; |
| 38 | stat_segment_directory_entry_t *directory_vector; |
| 39 | ssize_t memory_size; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 40 | } stat_client_main_t; |
| 41 | |
| 42 | stat_client_main_t stat_client_main; |
| 43 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 44 | static int |
| 45 | recv_fd (int sock) |
| 46 | { |
| 47 | struct msghdr msg = { 0 }; |
| 48 | struct cmsghdr *cmsg; |
| 49 | int fd = -1; |
| 50 | char iobuf[1]; |
| 51 | struct iovec io = {.iov_base = iobuf,.iov_len = sizeof (iobuf) }; |
| 52 | union |
| 53 | { |
| 54 | char buf[CMSG_SPACE (sizeof (fd))]; |
| 55 | struct cmsghdr align; |
| 56 | } u; |
| 57 | msg.msg_iov = &io; |
| 58 | msg.msg_iovlen = 1; |
| 59 | msg.msg_control = u.buf; |
| 60 | msg.msg_controllen = sizeof (u.buf); |
| 61 | |
| 62 | ssize_t size; |
| 63 | if ((size = recvmsg (sock, &msg, 0)) < 0) |
| 64 | { |
| 65 | perror ("recvmsg failed"); |
| 66 | return -1; |
| 67 | } |
| 68 | cmsg = CMSG_FIRSTHDR (&msg); |
| 69 | if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) |
| 70 | { |
| 71 | memmove (&fd, CMSG_DATA (cmsg), sizeof (fd)); |
| 72 | } |
| 73 | return fd; |
| 74 | } |
| 75 | |
| 76 | static stat_segment_directory_entry_t * |
| 77 | get_stat_vector (void) |
| 78 | { |
| 79 | stat_client_main_t *sm = &stat_client_main; |
| 80 | ASSERT (sm->shared_header); |
| 81 | return stat_segment_pointer (sm->shared_header, |
| 82 | sm->shared_header->directory_offset); |
| 83 | } |
| 84 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 85 | int |
| 86 | stat_segment_connect (char *socket_name) |
| 87 | { |
| 88 | stat_client_main_t *sm = &stat_client_main; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 89 | int mfd = -1; |
| 90 | int sock; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 91 | |
| 92 | memset (sm, 0, sizeof (*sm)); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 93 | if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 94 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 95 | perror ("Couldn't open socket"); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 96 | return -1; |
| 97 | } |
| 98 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 99 | struct sockaddr_un un = { 0 }; |
| 100 | un.sun_family = AF_UNIX; |
| 101 | strncpy ((char *) un.sun_path, socket_name, sizeof (un.sun_path) - 1); |
| 102 | if (connect (sock, (struct sockaddr *) &un, sizeof (struct sockaddr_un)) < |
| 103 | 0) |
| 104 | { |
Dave Barach | 819d5fd | 2018-10-02 16:33:56 -0400 | [diff] [blame^] | 105 | close (sock); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 106 | perror ("connect"); |
| 107 | return -1; |
| 108 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 109 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 110 | if ((mfd = recv_fd (sock)) < 0) |
| 111 | { |
Dave Barach | 819d5fd | 2018-10-02 16:33:56 -0400 | [diff] [blame^] | 112 | close (sock); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 113 | fprintf (stderr, "Receiving file descriptor failed\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | close (sock); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 117 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 118 | /* mmap shared memory segment. */ |
| 119 | void *memaddr; |
| 120 | struct stat st = { 0 }; |
| 121 | |
| 122 | if (fstat (mfd, &st) == -1) |
| 123 | { |
Dave Barach | 819d5fd | 2018-10-02 16:33:56 -0400 | [diff] [blame^] | 124 | close (sock); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 125 | perror ("mmap"); |
| 126 | return -1; |
| 127 | } |
| 128 | if ((memaddr = |
| 129 | mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED) |
| 130 | { |
Dave Barach | 819d5fd | 2018-10-02 16:33:56 -0400 | [diff] [blame^] | 131 | close (sock); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 132 | perror ("mmap"); |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | sm->memory_size = st.st_size; |
| 137 | sm->shared_header = memaddr; |
| 138 | sm->directory_vector = |
| 139 | stat_segment_pointer (memaddr, sm->shared_header->directory_offset); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | stat_segment_disconnect (void) |
| 146 | { |
| 147 | stat_client_main_t *sm = &stat_client_main; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 148 | munmap (sm->shared_header, sm->memory_size); |
| 149 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 153 | double |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 154 | stat_segment_heartbeat (void) |
| 155 | { |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 156 | stat_client_main_t *sm = &stat_client_main; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 157 | stat_segment_directory_entry_t *vec = get_stat_vector (); |
| 158 | double *hb = stat_segment_pointer (sm->shared_header, vec[4].offset); |
| 159 | return *hb; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | stat_segment_data_t |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 163 | copy_data (stat_segment_directory_entry_t * ep) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 164 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 165 | stat_client_main_t *sm = &stat_client_main; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 166 | stat_segment_data_t result = { 0 }; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 167 | int i; |
| 168 | vlib_counter_t **combined_c; /* Combined counter */ |
| 169 | counter_t **simple_c; /* Simple counter */ |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 170 | counter_t *error_base; |
| 171 | uint64_t *offset_vector; |
| 172 | |
| 173 | assert (sm->shared_header); |
| 174 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 175 | result.type = ep->type; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 176 | result.name = strdup (ep->name); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 177 | switch (ep->type) |
| 178 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 179 | case STAT_DIR_TYPE_SCALAR_INDEX: |
| 180 | result.scalar_value = ep->value; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 181 | break; |
| 182 | |
| 183 | case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: |
Ole Troan | bf49590 | 2018-10-02 22:00:29 +0200 | [diff] [blame] | 184 | if (ep->offset == 0) |
| 185 | return result; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 186 | simple_c = stat_segment_pointer (sm->shared_header, ep->offset); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 187 | result.simple_counter_vec = vec_dup (simple_c); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 188 | offset_vector = |
| 189 | stat_segment_pointer (sm->shared_header, ep->offset_vector); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 190 | for (i = 0; i < vec_len (simple_c); i++) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 191 | { |
| 192 | counter_t *cb = |
| 193 | stat_segment_pointer (sm->shared_header, offset_vector[i]); |
| 194 | result.simple_counter_vec[i] = vec_dup (cb); |
| 195 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 196 | break; |
| 197 | |
| 198 | case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: |
Ole Troan | bf49590 | 2018-10-02 22:00:29 +0200 | [diff] [blame] | 199 | if (ep->offset == 0) |
| 200 | return result; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 201 | combined_c = stat_segment_pointer (sm->shared_header, ep->offset); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 202 | result.combined_counter_vec = vec_dup (combined_c); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 203 | offset_vector = |
| 204 | stat_segment_pointer (sm->shared_header, ep->offset_vector); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 205 | for (i = 0; i < vec_len (combined_c); i++) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 206 | { |
| 207 | vlib_counter_t *cb = |
| 208 | stat_segment_pointer (sm->shared_header, offset_vector[i]); |
| 209 | result.combined_counter_vec[i] = vec_dup (cb); |
| 210 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 211 | break; |
| 212 | |
| 213 | case STAT_DIR_TYPE_ERROR_INDEX: |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 214 | error_base = |
| 215 | stat_segment_pointer (sm->shared_header, |
| 216 | sm->shared_header->error_offset); |
| 217 | result.error_value = error_base[ep->index]; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 218 | break; |
| 219 | |
| 220 | default: |
| 221 | fprintf (stderr, "Unknown type: %d", ep->type); |
| 222 | } |
| 223 | return result; |
| 224 | } |
| 225 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 226 | void |
| 227 | stat_segment_data_free (stat_segment_data_t * res) |
| 228 | { |
| 229 | int i, j; |
| 230 | for (i = 0; i < vec_len (res); i++) |
| 231 | { |
| 232 | switch (res[i].type) |
| 233 | { |
| 234 | case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: |
| 235 | for (j = 0; j < vec_len (res[i].simple_counter_vec); j++) |
| 236 | vec_free (res[i].simple_counter_vec[j]); |
| 237 | vec_free (res[i].simple_counter_vec); |
| 238 | break; |
| 239 | case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: |
| 240 | for (j = 0; j < vec_len (res[i].combined_counter_vec); j++) |
| 241 | vec_free (res[i].combined_counter_vec[j]); |
| 242 | vec_free (res[i].combined_counter_vec); |
| 243 | break; |
| 244 | default: |
| 245 | ; |
| 246 | } |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 247 | free (res[i].name); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 248 | } |
| 249 | vec_free (res); |
| 250 | } |
| 251 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 252 | |
| 253 | typedef struct |
| 254 | { |
| 255 | uint64_t epoch; |
| 256 | } stat_segment_access_t; |
| 257 | |
| 258 | static void |
| 259 | stat_segment_access_start (stat_segment_access_t * sa) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 260 | { |
| 261 | stat_client_main_t *sm = &stat_client_main; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 262 | stat_segment_shared_header_t *shared_header = sm->shared_header; |
| 263 | sa->epoch = shared_header->epoch; |
| 264 | while (shared_header->in_progress != 0) |
| 265 | ; |
| 266 | } |
| 267 | |
| 268 | static bool |
| 269 | stat_segment_access_end (stat_segment_access_t * sa) |
| 270 | { |
| 271 | stat_client_main_t *sm = &stat_client_main; |
| 272 | stat_segment_shared_header_t *shared_header = sm->shared_header; |
| 273 | |
| 274 | if (shared_header->epoch != sa->epoch || shared_header->in_progress) |
| 275 | return false; |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | uint32_t * |
| 280 | stat_segment_ls (uint8_t ** patterns) |
| 281 | { |
| 282 | stat_client_main_t *sm = &stat_client_main; |
| 283 | stat_segment_access_t sa; |
| 284 | |
| 285 | uint32_t *dir = 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 286 | regex_t regex[vec_len (patterns)]; |
| 287 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 288 | int i, j; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 289 | for (i = 0; i < vec_len (patterns); i++) |
| 290 | { |
| 291 | int rv = regcomp (®ex[i], (char *) patterns[i], 0); |
| 292 | if (rv) |
| 293 | { |
| 294 | fprintf (stderr, "Could not compile regex %s\n", patterns[i]); |
| 295 | return dir; |
| 296 | } |
| 297 | } |
| 298 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 299 | stat_segment_access_start (&sa); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 300 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 301 | stat_segment_directory_entry_t *counter_vec = get_stat_vector (); |
| 302 | for (j = 0; j < vec_len (counter_vec); j++) |
| 303 | { |
| 304 | for (i = 0; i < vec_len (patterns); i++) |
| 305 | { |
| 306 | int rv = regexec (®ex[i], counter_vec[j].name, 0, NULL, 0); |
| 307 | if (rv == 0) |
| 308 | { |
| 309 | vec_add1 (dir, j); |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | if (vec_len (patterns) == 0) |
| 314 | vec_add1 (dir, j); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 315 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 316 | |
| 317 | for (i = 0; i < vec_len (patterns); i++) |
| 318 | regfree (®ex[i]); |
| 319 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 320 | if (!stat_segment_access_end (&sa)) |
| 321 | { |
| 322 | /* Failed, clean up */ |
| 323 | vec_free (dir); |
| 324 | return 0; |
| 325 | |
| 326 | } |
| 327 | |
| 328 | /* Update last version */ |
| 329 | sm->current_epoch = sa.epoch; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 330 | return dir; |
| 331 | } |
| 332 | |
| 333 | stat_segment_data_t * |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 334 | stat_segment_dump (uint32_t * stats) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 335 | { |
| 336 | int i; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 337 | stat_client_main_t *sm = &stat_client_main; |
| 338 | stat_segment_directory_entry_t *ep; |
| 339 | stat_segment_data_t *res = 0; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 340 | stat_segment_access_t sa; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 341 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 342 | /* Has directory been update? */ |
| 343 | if (sm->shared_header->epoch != sm->current_epoch) |
| 344 | return 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 345 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 346 | stat_segment_access_start (&sa); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 347 | for (i = 0; i < vec_len (stats); i++) |
| 348 | { |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 349 | /* Collect counter */ |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 350 | ep = vec_elt_at_index (sm->directory_vector, stats[i]); |
| 351 | vec_add1 (res, copy_data (ep)); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 352 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 353 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 354 | if (stat_segment_access_end (&sa)) |
| 355 | return res; |
| 356 | |
| 357 | fprintf (stderr, "Epoch changed while reading, invalid results\n"); |
| 358 | // TODO increase counter |
| 359 | return 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 360 | } |
| 361 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 362 | /* Wrapper for accessing vectors from other languages */ |
| 363 | int |
| 364 | stat_segment_vec_len (void *vec) |
| 365 | { |
| 366 | return vec_len (vec); |
| 367 | } |
| 368 | |
| 369 | /* Create a vector from a string (or add to existing) */ |
| 370 | u8 ** |
| 371 | stat_segment_string_vector (u8 ** string_vector, char *string) |
| 372 | { |
| 373 | u8 *name = 0; |
| 374 | name = vec_dup ((u8 *) string); |
| 375 | vec_add1 (string_vector, (u8 *) name); |
| 376 | return string_vector; |
| 377 | } |
| 378 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 379 | stat_segment_data_t * |
| 380 | stat_segment_dump_entry (uint32_t index) |
| 381 | { |
| 382 | stat_client_main_t *sm = &stat_client_main; |
| 383 | stat_segment_directory_entry_t *ep; |
| 384 | stat_segment_data_t *res = 0; |
| 385 | stat_segment_access_t sa; |
| 386 | |
| 387 | stat_segment_access_start (&sa); |
| 388 | |
| 389 | /* Collect counter */ |
| 390 | ep = vec_elt_at_index (sm->directory_vector, index); |
| 391 | vec_add1 (res, copy_data (ep)); |
| 392 | |
| 393 | if (stat_segment_access_end (&sa)) |
| 394 | return res; |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | char * |
| 399 | stat_segment_index_to_name (uint32_t index) |
| 400 | { |
| 401 | char *name; |
| 402 | stat_segment_directory_entry_t *counter_vec = get_stat_vector (); |
| 403 | stat_segment_directory_entry_t *ep; |
| 404 | ep = vec_elt_at_index (counter_vec, index); |
| 405 | name = strdup (ep->name); |
| 406 | return name; |
| 407 | } |
| 408 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 409 | /* |
| 410 | * fd.io coding-style-patch-verification: ON |
| 411 | * |
| 412 | * Local Variables: |
| 413 | * eval: (c-set-style "gnu") |
| 414 | * End: |
| 415 | */ |