vpp_papi: Use new style classes.
[vpp.git] / src / vpp-api / python / vpp_papi / vpp_stats.py
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4 from cffi import FFI
5 import time
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,
17   STAT_DIR_TYPE_SCALAR_INDEX,
18   STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE,
19   STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED,
20   STAT_DIR_TYPE_ERROR_INDEX,
21 } stat_directory_type_t;
22
23 typedef struct
24 {
25   stat_directory_type_t type;
26   union {
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   {
41     double scalar_value;
42     uint64_t error_value;
43     counter_t **simple_counter_vec;
44     vlib_counter_t **combined_counter_vec;
45   };
46 } stat_segment_data_t;
47
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);
68 int stat_segment_connect (char *socket_name);
69 void stat_segment_disconnect_r (stat_client_main_t * sm);
70 void stat_segment_disconnect (void);
71
72 uint32_t *stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm);
73 uint32_t *stat_segment_ls (uint8_t ** pattern);
74 stat_segment_data_t *stat_segment_dump_r (uint32_t * stats, stat_client_main_t * sm);
75 stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec);
76 void stat_segment_data_free (stat_segment_data_t * res);
77
78 double stat_segment_heartbeat_r (stat_client_main_t * sm);
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:
91         vec = api.stat_segment_string_vector(vec, ffi.new("char []",
92                                                           s.encode()))
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):
128     # Scalar index
129     if e.type == 1:
130         return e.scalar_value
131         return None
132     if e.type == 2:
133         return simple_counter_vec_list(api, e.simple_counter_vec)
134     if e.type == 3:
135         return combined_counter_vec_list(api, e.combined_counter_vec)
136     if e.type == 4:
137         return e.error_value
138     return None
139
140
141 class VPPStats(object):
142     def __init__(self, socketname='/var/run/stats.sock', timeout=10):
143         try:
144             self.api = ffi.dlopen('libvppapiclient.so')
145         except Exception:
146             raise RuntimeError("Could not open: libvppapiclient.so")
147         self.client = self.api.stat_client_get()
148
149         poll_end_time = time.time() + timeout
150         while time.time() < poll_end_time:
151             rv = self.api.stat_segment_connect_r(socketname.encode(),
152                                                  self.client)
153             if rv == 0:
154                 break
155
156         if rv != 0:
157             raise IOError()
158
159     def heartbeat(self):
160         return self.api.stat_segment_heartbeat_r(self.client)
161
162     def ls(self, patterns):
163         return self.api.stat_segment_ls_r(make_string_vector(self.api,
164                                                              patterns),
165                                           self.client)
166
167     def dump(self, counters):
168         stats = {}
169         rv = self.api.stat_segment_dump_r(counters, self.client)
170         # Raise exception and retry
171         if rv == ffi.NULL:
172             raise IOError()
173         rv_len = self.api.stat_segment_vec_len(rv)
174         for i in range(rv_len):
175             n = ffi.string(rv[i].name).decode()
176             e = stat_entry_to_python(self.api, rv[i])
177             if e is not None:
178                 stats[n] = e
179         return stats
180
181     def get_counter(self, name):
182         retries = 0
183         while True:
184             try:
185                 dir = self.ls(name)
186                 return self.dump(dir).values()[0]
187             except Exception as e:
188                 if retries > 10:
189                     return None
190                 retries += 1
191
192     def disconnect(self):
193         self.api.stat_segment_disconnect_r(self.client)
194         self.api.stat_client_free(self.client)
195
196     def set_errors(self):
197         '''Return all errors counters > 0'''
198         retries = 0
199         while True:
200             try:
201                 error_names = self.ls(['/err/'])
202                 error_counters = self.dump(error_names)
203                 break
204             except Exception as e:
205                 if retries > 10:
206                     return None
207                 retries += 1
208
209         return {k: error_counters[k]
210                 for k in error_counters.keys() if error_counters[k]}
211
212     def set_errors_str(self):
213         '''Return all errors counters > 0 pretty printed'''
214         s = 'ERRORS:\n'
215         error_counters = self.set_errors()
216         for k in sorted(error_counters):
217             s += '{:<60}{:>10}\n'.format(k, error_counters[k])
218         return s