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