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