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 | { |
| 124 | perror ("mmap"); |
| 125 | return -1; |
| 126 | } |
| 127 | if ((memaddr = |
| 128 | mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED) |
| 129 | { |
| 130 | perror ("mmap"); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | sm->memory_size = st.st_size; |
| 135 | sm->shared_header = memaddr; |
| 136 | sm->directory_vector = |
| 137 | stat_segment_pointer (memaddr, sm->shared_header->directory_offset); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | stat_segment_disconnect (void) |
| 144 | { |
| 145 | stat_client_main_t *sm = &stat_client_main; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 146 | munmap (sm->shared_header, sm->memory_size); |
| 147 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 151 | double |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 152 | stat_segment_heartbeat (void) |
| 153 | { |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 154 | stat_client_main_t *sm = &stat_client_main; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 155 | stat_segment_directory_entry_t *vec = get_stat_vector (); |
| 156 | double *hb = stat_segment_pointer (sm->shared_header, vec[4].offset); |
| 157 | return *hb; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | stat_segment_data_t |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 161 | copy_data (stat_segment_directory_entry_t * ep) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 162 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 163 | stat_client_main_t *sm = &stat_client_main; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 164 | stat_segment_data_t result = { 0 }; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 165 | int i; |
| 166 | vlib_counter_t **combined_c; /* Combined counter */ |
| 167 | counter_t **simple_c; /* Simple counter */ |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 168 | counter_t *error_base; |
| 169 | uint64_t *offset_vector; |
| 170 | |
| 171 | assert (sm->shared_header); |
| 172 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 173 | result.type = ep->type; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 174 | result.name = strdup (ep->name); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 175 | switch (ep->type) |
| 176 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 177 | case STAT_DIR_TYPE_SCALAR_INDEX: |
| 178 | result.scalar_value = ep->value; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 179 | break; |
| 180 | |
| 181 | case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: |
Ole Troan | bf49590 | 2018-10-02 22:00:29 +0200 | [diff] [blame] | 182 | if (ep->offset == 0) |
| 183 | return result; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 184 | simple_c = stat_segment_pointer (sm->shared_header, ep->offset); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 185 | result.simple_counter_vec = vec_dup (simple_c); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 186 | offset_vector = |
| 187 | stat_segment_pointer (sm->shared_header, ep->offset_vector); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 188 | for (i = 0; i < vec_len (simple_c); i++) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 189 | { |
| 190 | counter_t *cb = |
| 191 | stat_segment_pointer (sm->shared_header, offset_vector[i]); |
| 192 | result.simple_counter_vec[i] = vec_dup (cb); |
| 193 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 194 | break; |
| 195 | |
| 196 | case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: |
Ole Troan | bf49590 | 2018-10-02 22:00:29 +0200 | [diff] [blame] | 197 | if (ep->offset == 0) |
| 198 | return result; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 199 | combined_c = stat_segment_pointer (sm->shared_header, ep->offset); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 200 | result.combined_counter_vec = vec_dup (combined_c); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 201 | offset_vector = |
| 202 | stat_segment_pointer (sm->shared_header, ep->offset_vector); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 203 | for (i = 0; i < vec_len (combined_c); i++) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 204 | { |
| 205 | vlib_counter_t *cb = |
| 206 | stat_segment_pointer (sm->shared_header, offset_vector[i]); |
| 207 | result.combined_counter_vec[i] = vec_dup (cb); |
| 208 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 209 | break; |
| 210 | |
| 211 | case STAT_DIR_TYPE_ERROR_INDEX: |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 212 | error_base = |
| 213 | stat_segment_pointer (sm->shared_header, |
| 214 | sm->shared_header->error_offset); |
| 215 | result.error_value = error_base[ep->index]; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 216 | break; |
| 217 | |
| 218 | default: |
| 219 | fprintf (stderr, "Unknown type: %d", ep->type); |
| 220 | } |
| 221 | return result; |
| 222 | } |
| 223 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 224 | void |
| 225 | stat_segment_data_free (stat_segment_data_t * res) |
| 226 | { |
| 227 | int i, j; |
| 228 | for (i = 0; i < vec_len (res); i++) |
| 229 | { |
| 230 | switch (res[i].type) |
| 231 | { |
| 232 | case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: |
| 233 | for (j = 0; j < vec_len (res[i].simple_counter_vec); j++) |
| 234 | vec_free (res[i].simple_counter_vec[j]); |
| 235 | vec_free (res[i].simple_counter_vec); |
| 236 | break; |
| 237 | case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: |
| 238 | for (j = 0; j < vec_len (res[i].combined_counter_vec); j++) |
| 239 | vec_free (res[i].combined_counter_vec[j]); |
| 240 | vec_free (res[i].combined_counter_vec); |
| 241 | break; |
| 242 | default: |
| 243 | ; |
| 244 | } |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 245 | free (res[i].name); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 246 | } |
| 247 | vec_free (res); |
| 248 | } |
| 249 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 250 | |
| 251 | typedef struct |
| 252 | { |
| 253 | uint64_t epoch; |
| 254 | } stat_segment_access_t; |
| 255 | |
| 256 | static void |
| 257 | stat_segment_access_start (stat_segment_access_t * sa) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 258 | { |
| 259 | stat_client_main_t *sm = &stat_client_main; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 260 | stat_segment_shared_header_t *shared_header = sm->shared_header; |
| 261 | sa->epoch = shared_header->epoch; |
| 262 | while (shared_header->in_progress != 0) |
| 263 | ; |
| 264 | } |
| 265 | |
| 266 | static bool |
| 267 | stat_segment_access_end (stat_segment_access_t * sa) |
| 268 | { |
| 269 | stat_client_main_t *sm = &stat_client_main; |
| 270 | stat_segment_shared_header_t *shared_header = sm->shared_header; |
| 271 | |
| 272 | if (shared_header->epoch != sa->epoch || shared_header->in_progress) |
| 273 | return false; |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | uint32_t * |
| 278 | stat_segment_ls (uint8_t ** patterns) |
| 279 | { |
| 280 | stat_client_main_t *sm = &stat_client_main; |
| 281 | stat_segment_access_t sa; |
| 282 | |
| 283 | uint32_t *dir = 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 284 | regex_t regex[vec_len (patterns)]; |
| 285 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 286 | int i, j; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 287 | for (i = 0; i < vec_len (patterns); i++) |
| 288 | { |
| 289 | int rv = regcomp (®ex[i], (char *) patterns[i], 0); |
| 290 | if (rv) |
| 291 | { |
| 292 | fprintf (stderr, "Could not compile regex %s\n", patterns[i]); |
| 293 | return dir; |
| 294 | } |
| 295 | } |
| 296 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 297 | stat_segment_access_start (&sa); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 298 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 299 | stat_segment_directory_entry_t *counter_vec = get_stat_vector (); |
| 300 | for (j = 0; j < vec_len (counter_vec); j++) |
| 301 | { |
| 302 | for (i = 0; i < vec_len (patterns); i++) |
| 303 | { |
| 304 | int rv = regexec (®ex[i], counter_vec[j].name, 0, NULL, 0); |
| 305 | if (rv == 0) |
| 306 | { |
| 307 | vec_add1 (dir, j); |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | if (vec_len (patterns) == 0) |
| 312 | vec_add1 (dir, j); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 313 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 314 | |
| 315 | for (i = 0; i < vec_len (patterns); i++) |
| 316 | regfree (®ex[i]); |
| 317 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 318 | if (!stat_segment_access_end (&sa)) |
| 319 | { |
| 320 | /* Failed, clean up */ |
| 321 | vec_free (dir); |
| 322 | return 0; |
| 323 | |
| 324 | } |
| 325 | |
| 326 | /* Update last version */ |
| 327 | sm->current_epoch = sa.epoch; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 328 | return dir; |
| 329 | } |
| 330 | |
| 331 | stat_segment_data_t * |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 332 | stat_segment_dump (uint32_t * stats) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 333 | { |
| 334 | int i; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 335 | stat_client_main_t *sm = &stat_client_main; |
| 336 | stat_segment_directory_entry_t *ep; |
| 337 | stat_segment_data_t *res = 0; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 338 | stat_segment_access_t sa; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 339 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 340 | /* Has directory been update? */ |
| 341 | if (sm->shared_header->epoch != sm->current_epoch) |
| 342 | return 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 343 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 344 | stat_segment_access_start (&sa); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 345 | for (i = 0; i < vec_len (stats); i++) |
| 346 | { |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 347 | /* Collect counter */ |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 348 | ep = vec_elt_at_index (sm->directory_vector, stats[i]); |
| 349 | vec_add1 (res, copy_data (ep)); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 350 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 351 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 352 | if (stat_segment_access_end (&sa)) |
| 353 | return res; |
| 354 | |
| 355 | fprintf (stderr, "Epoch changed while reading, invalid results\n"); |
| 356 | // TODO increase counter |
| 357 | return 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 360 | /* Wrapper for accessing vectors from other languages */ |
| 361 | int |
| 362 | stat_segment_vec_len (void *vec) |
| 363 | { |
| 364 | return vec_len (vec); |
| 365 | } |
| 366 | |
Ole Troan | 8254018 | 2018-10-22 09:41:29 +0200 | [diff] [blame] | 367 | void |
| 368 | stat_segment_vec_free (void *vec) |
| 369 | { |
| 370 | vec_free (vec); |
| 371 | } |
| 372 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 373 | /* Create a vector from a string (or add to existing) */ |
| 374 | u8 ** |
| 375 | stat_segment_string_vector (u8 ** string_vector, char *string) |
| 376 | { |
| 377 | u8 *name = 0; |
| 378 | name = vec_dup ((u8 *) string); |
| 379 | vec_add1 (string_vector, (u8 *) name); |
| 380 | return string_vector; |
| 381 | } |
| 382 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 383 | stat_segment_data_t * |
| 384 | stat_segment_dump_entry (uint32_t index) |
| 385 | { |
| 386 | stat_client_main_t *sm = &stat_client_main; |
| 387 | stat_segment_directory_entry_t *ep; |
| 388 | stat_segment_data_t *res = 0; |
| 389 | stat_segment_access_t sa; |
| 390 | |
| 391 | stat_segment_access_start (&sa); |
| 392 | |
| 393 | /* Collect counter */ |
| 394 | ep = vec_elt_at_index (sm->directory_vector, index); |
| 395 | vec_add1 (res, copy_data (ep)); |
| 396 | |
| 397 | if (stat_segment_access_end (&sa)) |
| 398 | return res; |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | char * |
| 403 | stat_segment_index_to_name (uint32_t index) |
| 404 | { |
| 405 | char *name; |
| 406 | stat_segment_directory_entry_t *counter_vec = get_stat_vector (); |
| 407 | stat_segment_directory_entry_t *ep; |
| 408 | ep = vec_elt_at_index (counter_vec, index); |
| 409 | name = strdup (ep->name); |
| 410 | return name; |
| 411 | } |
| 412 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 413 | /* |
| 414 | * fd.io coding-style-patch-verification: ON |
| 415 | * |
| 416 | * Local Variables: |
| 417 | * eval: (c-set-style "gnu") |
| 418 | * End: |
| 419 | */ |