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