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 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 34 | |
| 35 | stat_client_main_t stat_client_main; |
| 36 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 37 | stat_client_main_t * |
| 38 | stat_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 | |
| 46 | void |
| 47 | stat_client_free (stat_client_main_t * sm) |
| 48 | { |
| 49 | free (sm); |
| 50 | } |
| 51 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 52 | static int |
| 53 | recv_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 | |
| 84 | static stat_segment_directory_entry_t * |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 85 | get_stat_vector_r (stat_client_main_t * sm) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 86 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 87 | ASSERT (sm->shared_header); |
| 88 | return stat_segment_pointer (sm->shared_header, |
| 89 | sm->shared_header->directory_offset); |
| 90 | } |
| 91 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 92 | static stat_segment_directory_entry_t * |
| 93 | get_stat_vector (void) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 94 | { |
| 95 | stat_client_main_t *sm = &stat_client_main; |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 96 | return get_stat_vector_r (sm); |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | stat_segment_connect_r (char *socket_name, stat_client_main_t * sm) |
| 101 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 102 | int mfd = -1; |
| 103 | int sock; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 104 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 105 | clib_memset (sm, 0, sizeof (*sm)); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 106 | if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 107 | { |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 108 | perror ("Stat client couldn't open socket"); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 112 | struct sockaddr_un un = { 0 }; |
| 113 | un.sun_family = AF_UNIX; |
| 114 | strncpy ((char *) un.sun_path, socket_name, sizeof (un.sun_path) - 1); |
| 115 | if (connect (sock, (struct sockaddr *) &un, sizeof (struct sockaddr_un)) < |
| 116 | 0) |
| 117 | { |
Dave Barach | 819d5fd | 2018-10-02 16:33:56 -0400 | [diff] [blame] | 118 | close (sock); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 119 | return -1; |
| 120 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 121 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 122 | if ((mfd = recv_fd (sock)) < 0) |
| 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 | fprintf (stderr, "Receiving file descriptor failed\n"); |
| 126 | return -1; |
| 127 | } |
| 128 | close (sock); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 129 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 130 | /* mmap shared memory segment. */ |
| 131 | void *memaddr; |
| 132 | struct stat st = { 0 }; |
| 133 | |
| 134 | if (fstat (mfd, &st) == -1) |
| 135 | { |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 136 | perror ("mmap fstat failed"); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | if ((memaddr = |
| 140 | mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED) |
| 141 | { |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 142 | perror ("mmap map failed"); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | sm->memory_size = st.st_size; |
| 147 | sm->shared_header = memaddr; |
| 148 | sm->directory_vector = |
| 149 | stat_segment_pointer (memaddr, sm->shared_header->directory_offset); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 154 | int |
| 155 | stat_segment_connect (char *socket_name) |
| 156 | { |
| 157 | stat_client_main_t *sm = &stat_client_main; |
| 158 | return stat_segment_connect_r (socket_name, sm); |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | stat_segment_disconnect_r (stat_client_main_t * sm) |
| 163 | { |
| 164 | munmap (sm->shared_header, sm->memory_size); |
| 165 | return; |
| 166 | } |
| 167 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 168 | void |
| 169 | stat_segment_disconnect (void) |
| 170 | { |
| 171 | stat_client_main_t *sm = &stat_client_main; |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 172 | return stat_segment_disconnect_r (sm); |
| 173 | } |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 174 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 175 | double |
| 176 | stat_segment_heartbeat_r (stat_client_main_t * sm) |
| 177 | { |
| 178 | stat_segment_directory_entry_t *vec = get_stat_vector_r (sm); |
| 179 | double *hb = stat_segment_pointer (sm->shared_header, vec[4].offset); |
| 180 | return *hb; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 183 | double |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 184 | stat_segment_heartbeat (void) |
| 185 | { |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 186 | stat_client_main_t *sm = &stat_client_main; |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 187 | return stat_segment_heartbeat_r (sm); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | stat_segment_data_t |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 191 | copy_data (stat_segment_directory_entry_t * ep, stat_client_main_t * sm) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 192 | { |
| 193 | stat_segment_data_t result = { 0 }; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 194 | int i; |
| 195 | vlib_counter_t **combined_c; /* Combined counter */ |
| 196 | counter_t **simple_c; /* Simple counter */ |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 197 | counter_t *error_base; |
| 198 | uint64_t *offset_vector; |
| 199 | |
| 200 | assert (sm->shared_header); |
| 201 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 202 | result.type = ep->type; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 203 | result.name = strdup (ep->name); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 204 | switch (ep->type) |
| 205 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 206 | case STAT_DIR_TYPE_SCALAR_INDEX: |
| 207 | result.scalar_value = ep->value; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 208 | break; |
| 209 | |
| 210 | case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: |
Ole Troan | bf49590 | 2018-10-02 22:00:29 +0200 | [diff] [blame] | 211 | if (ep->offset == 0) |
| 212 | return result; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 213 | simple_c = stat_segment_pointer (sm->shared_header, ep->offset); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 214 | result.simple_counter_vec = vec_dup (simple_c); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 215 | offset_vector = |
| 216 | stat_segment_pointer (sm->shared_header, ep->offset_vector); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 217 | for (i = 0; i < vec_len (simple_c); i++) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 218 | { |
| 219 | counter_t *cb = |
| 220 | stat_segment_pointer (sm->shared_header, offset_vector[i]); |
| 221 | result.simple_counter_vec[i] = vec_dup (cb); |
| 222 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 223 | break; |
| 224 | |
| 225 | case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: |
Ole Troan | bf49590 | 2018-10-02 22:00:29 +0200 | [diff] [blame] | 226 | if (ep->offset == 0) |
| 227 | return result; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 228 | combined_c = stat_segment_pointer (sm->shared_header, ep->offset); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 229 | result.combined_counter_vec = vec_dup (combined_c); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 230 | offset_vector = |
| 231 | stat_segment_pointer (sm->shared_header, ep->offset_vector); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 232 | for (i = 0; i < vec_len (combined_c); i++) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 233 | { |
| 234 | vlib_counter_t *cb = |
| 235 | stat_segment_pointer (sm->shared_header, offset_vector[i]); |
| 236 | result.combined_counter_vec[i] = vec_dup (cb); |
| 237 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 238 | break; |
| 239 | |
| 240 | case STAT_DIR_TYPE_ERROR_INDEX: |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 241 | error_base = |
| 242 | stat_segment_pointer (sm->shared_header, |
| 243 | sm->shared_header->error_offset); |
| 244 | result.error_value = error_base[ep->index]; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 245 | break; |
| 246 | |
| 247 | default: |
| 248 | fprintf (stderr, "Unknown type: %d", ep->type); |
| 249 | } |
| 250 | return result; |
| 251 | } |
| 252 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 253 | void |
| 254 | stat_segment_data_free (stat_segment_data_t * res) |
| 255 | { |
| 256 | int i, j; |
| 257 | for (i = 0; i < vec_len (res); i++) |
| 258 | { |
| 259 | switch (res[i].type) |
| 260 | { |
| 261 | case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: |
| 262 | for (j = 0; j < vec_len (res[i].simple_counter_vec); j++) |
| 263 | vec_free (res[i].simple_counter_vec[j]); |
| 264 | vec_free (res[i].simple_counter_vec); |
| 265 | break; |
| 266 | case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: |
| 267 | for (j = 0; j < vec_len (res[i].combined_counter_vec); j++) |
| 268 | vec_free (res[i].combined_counter_vec[j]); |
| 269 | vec_free (res[i].combined_counter_vec); |
| 270 | break; |
| 271 | default: |
| 272 | ; |
| 273 | } |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 274 | free (res[i].name); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 275 | } |
| 276 | vec_free (res); |
| 277 | } |
| 278 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 279 | |
| 280 | typedef struct |
| 281 | { |
| 282 | uint64_t epoch; |
| 283 | } stat_segment_access_t; |
| 284 | |
| 285 | static void |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 286 | stat_segment_access_start (stat_segment_access_t * sa, |
| 287 | stat_client_main_t * sm) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 288 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 289 | stat_segment_shared_header_t *shared_header = sm->shared_header; |
| 290 | sa->epoch = shared_header->epoch; |
| 291 | while (shared_header->in_progress != 0) |
| 292 | ; |
| 293 | } |
| 294 | |
| 295 | static bool |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 296 | stat_segment_access_end (stat_segment_access_t * sa, stat_client_main_t * sm) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 297 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 298 | stat_segment_shared_header_t *shared_header = sm->shared_header; |
| 299 | |
| 300 | if (shared_header->epoch != sa->epoch || shared_header->in_progress) |
| 301 | return false; |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | uint32_t * |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 306 | stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 307 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 308 | stat_segment_access_t sa; |
| 309 | |
| 310 | uint32_t *dir = 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 311 | regex_t regex[vec_len (patterns)]; |
| 312 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 313 | int i, j; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 314 | for (i = 0; i < vec_len (patterns); i++) |
| 315 | { |
| 316 | int rv = regcomp (®ex[i], (char *) patterns[i], 0); |
| 317 | if (rv) |
| 318 | { |
| 319 | fprintf (stderr, "Could not compile regex %s\n", patterns[i]); |
| 320 | return dir; |
| 321 | } |
| 322 | } |
| 323 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 324 | stat_segment_access_start (&sa, sm); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 325 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 326 | stat_segment_directory_entry_t *counter_vec = get_stat_vector_r (sm); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 327 | for (j = 0; j < vec_len (counter_vec); j++) |
| 328 | { |
| 329 | for (i = 0; i < vec_len (patterns); i++) |
| 330 | { |
| 331 | int rv = regexec (®ex[i], counter_vec[j].name, 0, NULL, 0); |
| 332 | if (rv == 0) |
| 333 | { |
| 334 | vec_add1 (dir, j); |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | if (vec_len (patterns) == 0) |
| 339 | vec_add1 (dir, j); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 340 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 341 | |
| 342 | for (i = 0; i < vec_len (patterns); i++) |
| 343 | regfree (®ex[i]); |
| 344 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 345 | if (!stat_segment_access_end (&sa, sm)) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 346 | { |
| 347 | /* Failed, clean up */ |
| 348 | vec_free (dir); |
| 349 | return 0; |
| 350 | |
| 351 | } |
| 352 | |
| 353 | /* Update last version */ |
| 354 | sm->current_epoch = sa.epoch; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 355 | return dir; |
| 356 | } |
| 357 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 358 | uint32_t * |
| 359 | stat_segment_ls (uint8_t ** patterns) |
| 360 | { |
| 361 | stat_client_main_t *sm = &stat_client_main; |
| 362 | return stat_segment_ls_r ((uint8_t **) patterns, sm); |
| 363 | } |
| 364 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 365 | stat_segment_data_t * |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 366 | stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm) |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 367 | { |
| 368 | int i; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 369 | stat_segment_directory_entry_t *ep; |
| 370 | stat_segment_data_t *res = 0; |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 371 | stat_segment_access_t sa; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 372 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 373 | /* Has directory been update? */ |
| 374 | if (sm->shared_header->epoch != sm->current_epoch) |
| 375 | return 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 376 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 377 | stat_segment_access_start (&sa, sm); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 378 | for (i = 0; i < vec_len (stats); i++) |
| 379 | { |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 380 | /* Collect counter */ |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 381 | ep = vec_elt_at_index (sm->directory_vector, stats[i]); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 382 | vec_add1 (res, copy_data (ep, sm)); |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 383 | } |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 384 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 385 | if (stat_segment_access_end (&sa, sm)) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 386 | return res; |
| 387 | |
| 388 | fprintf (stderr, "Epoch changed while reading, invalid results\n"); |
| 389 | // TODO increase counter |
| 390 | return 0; |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 393 | stat_segment_data_t * |
| 394 | stat_segment_dump (uint32_t * stats) |
| 395 | { |
| 396 | stat_client_main_t *sm = &stat_client_main; |
| 397 | return stat_segment_dump_r (stats, sm); |
| 398 | } |
| 399 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 400 | /* Wrapper for accessing vectors from other languages */ |
| 401 | int |
| 402 | stat_segment_vec_len (void *vec) |
| 403 | { |
| 404 | return vec_len (vec); |
| 405 | } |
| 406 | |
Ole Troan | 8254018 | 2018-10-22 09:41:29 +0200 | [diff] [blame] | 407 | void |
| 408 | stat_segment_vec_free (void *vec) |
| 409 | { |
| 410 | vec_free (vec); |
| 411 | } |
| 412 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 413 | /* Create a vector from a string (or add to existing) */ |
| 414 | u8 ** |
| 415 | stat_segment_string_vector (u8 ** string_vector, char *string) |
| 416 | { |
| 417 | u8 *name = 0; |
| 418 | name = vec_dup ((u8 *) string); |
| 419 | vec_add1 (string_vector, (u8 *) name); |
| 420 | return string_vector; |
| 421 | } |
| 422 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 423 | stat_segment_data_t * |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 424 | stat_segment_dump_entry_r (uint32_t index, stat_client_main_t * sm) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 425 | { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 426 | stat_segment_directory_entry_t *ep; |
| 427 | stat_segment_data_t *res = 0; |
| 428 | stat_segment_access_t sa; |
| 429 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 430 | stat_segment_access_start (&sa, sm); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 431 | |
| 432 | /* Collect counter */ |
| 433 | ep = vec_elt_at_index (sm->directory_vector, index); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 434 | vec_add1 (res, copy_data (ep, sm)); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 435 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 436 | if (stat_segment_access_end (&sa, sm)) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 437 | return res; |
| 438 | return 0; |
| 439 | } |
| 440 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 441 | stat_segment_data_t * |
| 442 | stat_segment_dump_entry (uint32_t index) |
| 443 | { |
| 444 | stat_client_main_t *sm = &stat_client_main; |
| 445 | return stat_segment_dump_entry_r (index, sm); |
| 446 | } |
| 447 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 448 | char * |
| 449 | stat_segment_index_to_name (uint32_t index) |
| 450 | { |
| 451 | char *name; |
| 452 | stat_segment_directory_entry_t *counter_vec = get_stat_vector (); |
| 453 | stat_segment_directory_entry_t *ep; |
| 454 | ep = vec_elt_at_index (counter_vec, index); |
| 455 | name = strdup (ep->name); |
| 456 | return name; |
| 457 | } |
| 458 | |
Ole Troan | 2fee167 | 2018-08-23 13:00:53 +0200 | [diff] [blame] | 459 | /* |
| 460 | * fd.io coding-style-patch-verification: ON |
| 461 | * |
| 462 | * Local Variables: |
| 463 | * eval: (c-set-style "gnu") |
| 464 | * End: |
| 465 | */ |