Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | from __future__ import print_function |
| 4 | from cffi import FFI |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 5 | import time |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 6 | |
| 7 | ffi = FFI() |
| 8 | ffi.cdef(""" |
| 9 | typedef uint64_t counter_t; |
| 10 | typedef struct { |
| 11 | counter_t packets; |
| 12 | counter_t bytes; |
| 13 | } vlib_counter_t; |
| 14 | |
| 15 | typedef enum { |
| 16 | STAT_DIR_TYPE_ILLEGAL = 0, |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 17 | STAT_DIR_TYPE_SCALAR_INDEX, |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 18 | STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE, |
| 19 | STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED, |
| 20 | STAT_DIR_TYPE_ERROR_INDEX, |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 21 | } stat_directory_type_t; |
| 22 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 23 | typedef struct |
| 24 | { |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 25 | stat_directory_type_t type; |
| 26 | union { |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 27 | uint64_t offset; |
| 28 | uint64_t index; |
| 29 | uint64_t value; |
| 30 | }; |
| 31 | uint64_t offset_vector; |
| 32 | char name[128]; // TODO change this to pointer to "somewhere" |
| 33 | } stat_segment_directory_entry_t; |
| 34 | |
| 35 | typedef struct |
| 36 | { |
| 37 | char *name; |
| 38 | stat_directory_type_t type; |
| 39 | union |
| 40 | { |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 41 | double scalar_value; |
| 42 | uint64_t error_value; |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 43 | counter_t **simple_counter_vec; |
| 44 | vlib_counter_t **combined_counter_vec; |
| 45 | }; |
| 46 | } stat_segment_data_t; |
| 47 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 48 | typedef struct |
| 49 | { |
| 50 | uint64_t epoch; |
| 51 | uint64_t in_progress; |
| 52 | uint64_t directory_offset; |
| 53 | uint64_t error_offset; |
| 54 | uint64_t stats_offset; |
| 55 | } stat_segment_shared_header_t; |
| 56 | |
| 57 | typedef struct |
| 58 | { |
| 59 | uint64_t current_epoch; |
| 60 | stat_segment_shared_header_t *shared_header; |
| 61 | stat_segment_directory_entry_t *directory_vector; |
| 62 | ssize_t memory_size; |
| 63 | } stat_client_main_t; |
| 64 | |
| 65 | stat_client_main_t * stat_client_get(void); |
| 66 | void stat_client_free(stat_client_main_t * sm); |
| 67 | int stat_segment_connect_r (char *socket_name, stat_client_main_t * sm); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 68 | int stat_segment_connect (char *socket_name); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 69 | void stat_segment_disconnect_r (stat_client_main_t * sm); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 70 | void stat_segment_disconnect (void); |
| 71 | |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 72 | uint32_t *stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 73 | uint32_t *stat_segment_ls (uint8_t ** pattern); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 74 | stat_segment_data_t *stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm); |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 75 | stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 76 | void stat_segment_data_free (stat_segment_data_t * res); |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 77 | |
| 78 | double stat_segment_heartbeat_r (stat_client_main_t * sm); |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 79 | double stat_segment_heartbeat (void); |
| 80 | int stat_segment_vec_len(void *vec); |
| 81 | uint8_t **stat_segment_string_vector(uint8_t **string_vector, char *string); |
| 82 | """) |
| 83 | |
| 84 | |
| 85 | # Utility functions |
| 86 | def make_string_vector(api, strings): |
| 87 | vec = ffi.NULL |
| 88 | if type(strings) is not list: |
| 89 | strings = [strings] |
| 90 | for s in strings: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 91 | vec = api.stat_segment_string_vector(vec, ffi.new("char []", |
| 92 | s.encode())) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 93 | return vec |
| 94 | |
| 95 | |
| 96 | def make_string_list(api, vec): |
| 97 | vec_len = api.stat_segment_vec_len(vec) |
| 98 | return [ffi.string(vec[i]) for i in range(vec_len)] |
| 99 | |
| 100 | |
| 101 | # 2-dimensonal array of thread, index |
| 102 | def simple_counter_vec_list(api, e): |
| 103 | vec = [] |
| 104 | for thread in range(api.stat_segment_vec_len(e)): |
| 105 | len_interfaces = api.stat_segment_vec_len(e[thread]) |
| 106 | if_per_thread = [e[thread][interfaces] |
| 107 | for interfaces in range(len_interfaces)] |
| 108 | vec.append(if_per_thread) |
| 109 | return vec |
| 110 | |
| 111 | |
| 112 | def vlib_counter_dict(c): |
| 113 | return {'packets': c.packets, |
| 114 | 'bytes': c.bytes} |
| 115 | |
| 116 | |
| 117 | def combined_counter_vec_list(api, e): |
| 118 | vec = [] |
| 119 | for thread in range(api.stat_segment_vec_len(e)): |
| 120 | len_interfaces = api.stat_segment_vec_len(e[thread]) |
| 121 | if_per_thread = [vlib_counter_dict(e[thread][interfaces]) |
| 122 | for interfaces in range(len_interfaces)] |
| 123 | vec.append(if_per_thread) |
| 124 | return vec |
| 125 | |
| 126 | |
| 127 | def stat_entry_to_python(api, e): |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 128 | # Scalar index |
| 129 | if e.type == 1: |
| 130 | return e.scalar_value |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 131 | if e.type == 2: |
| 132 | return simple_counter_vec_list(api, e.simple_counter_vec) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 133 | if e.type == 3: |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 134 | return combined_counter_vec_list(api, e.combined_counter_vec) |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 135 | if e.type == 4: |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 136 | return e.error_value |
| 137 | return None |
| 138 | |
| 139 | |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 140 | class VPPStatsIOError(IOError): |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 141 | message = "Stat segment client connection returned: " \ |
| 142 | "%(retval)s %(strerror)s." |
| 143 | |
| 144 | strerror = {-1: "Stat client couldn't open socket", |
| 145 | -2: "Stat client socket open but couldn't connect", |
| 146 | -3: "Receiving file descriptor failed", |
| 147 | -4: "mmap fstat failed", |
| 148 | -5: "mmap map failed" |
| 149 | } |
| 150 | |
| 151 | def __init__(self, message=None, **kwargs): |
| 152 | if 'retval' in kwargs: |
| 153 | self.retval = kwargs['retval'] |
| 154 | kwargs['strerror'] = self.strerror[int(self.retval)] |
| 155 | |
| 156 | if not message: |
| 157 | try: |
| 158 | message = self.message % kwargs |
| 159 | except Exception as e: |
| 160 | message = self.message |
| 161 | else: |
| 162 | message = message % kwargs |
| 163 | |
| 164 | super(VPPStatsIOError, self).__init__(message) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 165 | |
| 166 | |
| 167 | class VPPStatsClientLoadError(RuntimeError): |
| 168 | pass |
| 169 | |
| 170 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 171 | class VPPStats(object): |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 172 | VPPStatsIOError = VPPStatsIOError |
| 173 | |
| 174 | default_socketname = '/var/run/stats.sock' |
| 175 | sharedlib_name = 'libvppapiclient.so' |
| 176 | |
| 177 | def __init__(self, socketname=default_socketname, timeout=10): |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 178 | try: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 179 | self.api = ffi.dlopen(VPPStats.sharedlib_name) |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 180 | except Exception: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 181 | raise VPPStatsClientLoadError("Could not open: %s" % |
| 182 | VPPStats.sharedlib_name) |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 183 | self.client = self.api.stat_client_get() |
| 184 | |
| 185 | poll_end_time = time.time() + timeout |
| 186 | while time.time() < poll_end_time: |
| 187 | rv = self.api.stat_segment_connect_r(socketname.encode(), |
| 188 | self.client) |
| 189 | if rv == 0: |
| 190 | break |
| 191 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 192 | if rv != 0: |
Paul Vinciguerra | 356824f | 2019-01-05 19:37:27 -0800 | [diff] [blame] | 193 | raise VPPStatsIOError(retval=rv) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 194 | |
| 195 | def heartbeat(self): |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 196 | return self.api.stat_segment_heartbeat_r(self.client) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 197 | |
| 198 | def ls(self, patterns): |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 199 | return self.api.stat_segment_ls_r(make_string_vector(self.api, |
| 200 | patterns), |
| 201 | self.client) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 202 | |
| 203 | def dump(self, counters): |
| 204 | stats = {} |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 205 | rv = self.api.stat_segment_dump_r(counters, self.client) |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 206 | # Raise exception and retry |
| 207 | if rv == ffi.NULL: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 208 | raise VPPStatsIOError() |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 209 | rv_len = self.api.stat_segment_vec_len(rv) |
| 210 | for i in range(rv_len): |
Ole Troan | 45d5c87 | 2018-09-18 10:23:01 +0200 | [diff] [blame] | 211 | n = ffi.string(rv[i].name).decode() |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 212 | e = stat_entry_to_python(self.api, rv[i]) |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 213 | if e is not None: |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 214 | stats[n] = e |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 215 | return stats |
| 216 | |
Ole Troan | 58492a8 | 2018-09-04 13:19:12 +0200 | [diff] [blame] | 217 | def get_counter(self, name): |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 218 | retries = 0 |
| 219 | while True: |
| 220 | try: |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 221 | d = self.ls(name) |
| 222 | s = self.dump(d) |
| 223 | if len(s) > 1: |
| 224 | raise AttributeError('Matches multiple counters {}' |
| 225 | .format(name)) |
| 226 | k, v = s.popitem() |
| 227 | return v |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 228 | except VPPStatsIOError as e: |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 229 | if retries > 10: |
| 230 | return None |
| 231 | retries += 1 |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 232 | |
| 233 | def disconnect(self): |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 234 | self.api.stat_segment_disconnect_r(self.client) |
| 235 | self.api.stat_client_free(self.client) |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 236 | |
| 237 | def set_errors(self): |
| 238 | '''Return all errors counters > 0''' |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 239 | retries = 0 |
| 240 | while True: |
| 241 | try: |
| 242 | error_names = self.ls(['/err/']) |
| 243 | error_counters = self.dump(error_names) |
| 244 | break |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 245 | except VPPStatsIOError as e: |
Ole Troan | b4603a7 | 2018-09-18 11:06:33 +0200 | [diff] [blame] | 246 | if retries > 10: |
| 247 | return None |
| 248 | retries += 1 |
Paul Vinciguerra | 5642109 | 2018-11-21 16:34:09 -0800 | [diff] [blame] | 249 | |
Ole Troan | 7320210 | 2018-08-31 00:29:48 +0200 | [diff] [blame] | 250 | return {k: error_counters[k] |
| 251 | for k in error_counters.keys() if error_counters[k]} |
| 252 | |
| 253 | def set_errors_str(self): |
| 254 | '''Return all errors counters > 0 pretty printed''' |
| 255 | s = 'ERRORS:\n' |
| 256 | error_counters = self.set_errors() |
| 257 | for k in sorted(error_counters): |
| 258 | s += '{:<60}{:>10}\n'.format(k, error_counters[k]) |
| 259 | return s |