stats: add timeout for in_progress access to stat segment
[vpp.git] / src / vpp-api / client / stat_client.h
1 /*
2  * stat_client.h - Library for access to VPP statistics segment
3  *
4  * Copyright (c) 2018 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef included_stat_client_h
18 #define included_stat_client_h
19
20 #define STAT_VERSION_MAJOR     1
21 #define STAT_VERSION_MINOR     2
22
23 #include <stdint.h>
24 #include <unistd.h>
25 #include <vlib/counter_types.h>
26 #include <time.h>
27 #include <stdbool.h>
28 #include <vpp/stats/stat_segment_shared.h>
29
30 /* Default socket to exchange segment fd */
31 /* TODO: Get from runtime directory */
32 #define STAT_SEGMENT_SOCKET_FILE "/run/vpp/stats.sock"
33 #define STAT_SEGMENT_SOCKET_FILENAME "stats.sock"
34
35 typedef struct
36 {
37   char *name;
38   stat_directory_type_t type;
39   union
40   {
41     double scalar_value;
42     counter_t *error_vector;
43     counter_t **simple_counter_vec;
44     vlib_counter_t **combined_counter_vec;
45     uint8_t **name_vector;
46   };
47 } stat_segment_data_t;
48
49 typedef struct
50 {
51   uint64_t current_epoch;
52   stat_segment_shared_header_t *shared_header;
53   stat_segment_directory_entry_t *directory_vector;
54   ssize_t memory_size;
55   uint64_t timeout;
56 } stat_client_main_t;
57
58 extern stat_client_main_t stat_client_main;
59
60 stat_client_main_t *stat_client_get (void);
61 void stat_client_free (stat_client_main_t * sm);
62 int stat_segment_connect_r (const char *socket_name, stat_client_main_t * sm);
63 int stat_segment_connect (const char *socket_name);
64 void stat_segment_disconnect_r (stat_client_main_t * sm);
65 void stat_segment_disconnect (void);
66 uint8_t **stat_segment_string_vector (uint8_t ** string_vector,
67                                       const char *string);
68 int stat_segment_vec_len (void *vec);
69 void stat_segment_vec_free (void *vec);
70 uint32_t *stat_segment_ls_r (uint8_t ** patterns, stat_client_main_t * sm);
71 uint32_t *stat_segment_ls (uint8_t ** pattern);
72 stat_segment_data_t *stat_segment_dump_r (uint32_t * stats,
73                                           stat_client_main_t * sm);
74 stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec);
75 stat_segment_data_t *stat_segment_dump_entry_r (uint32_t index,
76                                                 stat_client_main_t * sm);
77 stat_segment_data_t *stat_segment_dump_entry (uint32_t index);
78
79 void stat_segment_data_free (stat_segment_data_t * res);
80 double stat_segment_heartbeat_r (stat_client_main_t * sm);
81 double stat_segment_heartbeat (void);
82
83 char *stat_segment_index_to_name_r (uint32_t index, stat_client_main_t * sm);
84 char *stat_segment_index_to_name (uint32_t index);
85 uint64_t stat_segment_version (void);
86 uint64_t stat_segment_version_r (stat_client_main_t * sm);
87
88 typedef struct
89 {
90   uint64_t epoch;
91 } stat_segment_access_t;
92
93 /*
94  * Returns 0 on success, -1 on failure (timeout)
95  */
96 static inline uint64_t
97 _time_now_nsec (void)
98 {
99   struct timespec ts;
100   clock_gettime (CLOCK_REALTIME, &ts);
101   return 1e9 * ts.tv_sec + ts.tv_nsec;
102 }
103
104 static inline int
105 stat_segment_access_start (stat_segment_access_t * sa,
106                            stat_client_main_t * sm)
107 {
108   stat_segment_shared_header_t *shared_header = sm->shared_header;
109   uint64_t max_time;
110
111   sa->epoch = shared_header->epoch;
112   if (sm->timeout)
113     {
114       max_time = _time_now_nsec () + sm->timeout;
115       while (shared_header->in_progress != 0 && _time_now_nsec () < max_time)
116         ;
117     }
118   else
119     {
120       while (shared_header->in_progress != 0)
121         ;
122     }
123   sm->directory_vector = (stat_segment_directory_entry_t *)
124     stat_segment_pointer (sm->shared_header,
125                           sm->shared_header->directory_offset);
126   if (sm->timeout)
127     return _time_now_nsec () < max_time ? 0 : -1;
128   return 0;
129 }
130
131 /*
132  * set maximum number of nano seconds to wait for in_progress state
133  */
134 static inline void
135 stat_segment_set_timeout_nsec (stat_client_main_t * sm, uint64_t timeout)
136 {
137   sm->timeout = timeout;
138 }
139
140 static inline bool
141 stat_segment_access_end (stat_segment_access_t * sa, stat_client_main_t * sm)
142 {
143   stat_segment_shared_header_t *shared_header = sm->shared_header;
144
145   if (shared_header->epoch != sa->epoch || shared_header->in_progress)
146     return false;
147   return true;
148 }
149
150
151 #endif /* included_stat_client_h */
152
153 /*
154  * fd.io coding-style-patch-verification: ON
155  *
156  * Local Variables:
157  * eval: (c-set-style "gnu")
158  * End:
159  */