VOM: stats
[vpp.git] / extras / vom / vom / stat_reader.cpp
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "vom/stat_reader.hpp"
17 #include "vom/interface.hpp"
18
19 namespace VOM {
20
21 stat_reader::stat_indexes_t stat_reader::m_stat_itf_indexes;
22
23 stat_reader::stat_reader()
24   : m_client()
25 {
26 }
27
28 stat_reader::stat_reader(stat_client sc)
29   : m_client(sc)
30 {
31 }
32
33 stat_reader::~stat_reader()
34 {
35 }
36
37 int
38 stat_reader::connect()
39 {
40   return m_client.connect();
41 }
42
43 void
44 stat_reader::disconnect()
45 {
46   m_client.disconnect();
47 }
48
49 void
50 stat_reader::registers(const interface& intf)
51 {
52   m_stat_itf_indexes.insert(intf.handle_i().value());
53 }
54
55 void
56 stat_reader::unregisters(const interface& intf)
57 {
58   m_stat_itf_indexes.erase(intf.handle_i().value());
59 }
60
61 void
62 stat_reader::read()
63 {
64   std::set<std::shared_ptr<interface>> itfs_w_stats;
65   const stat_client::stat_data_vec_t& sd = m_client.dump();
66
67   for (auto& sde : sd) {
68     std::string name;
69
70     if (sde.name().empty())
71       continue;
72
73     name = sde.name();
74
75     if (name.find("/if") != std::string::npos)
76       name.erase(0, 4);
77
78     switch (sde.type()) {
79       case STAT_DIR_TYPE_ERROR_INDEX:
80       case STAT_DIR_TYPE_SCALAR_INDEX:
81       case STAT_DIR_TYPE_ILLEGAL:
82         break;
83
84       case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: {
85         uint64_t** data;
86
87         data = sde.get_stat_segment_simple_counter_data();
88
89         for (auto& i : m_stat_itf_indexes) {
90           counter_t count;
91
92           for (int k = 0; k < m_client.vec_len(data); k++) {
93             count.packets += data[k][i];
94           }
95
96           std::shared_ptr<interface> itf = interface::find(i);
97           if (itf) {
98             itf->set(count, name);
99             itfs_w_stats.insert(itf);
100           }
101         }
102         break;
103       }
104
105       case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: {
106         vlib_counter_t** data;
107
108         data = sde.get_stat_segment_combined_counter_data();
109
110         for (auto& i : m_stat_itf_indexes) {
111           counter_t count;
112
113           for (int k = 0; k < m_client.vec_len(data); k++) {
114             count.packets += data[k][i].packets;
115             count.bytes += data[k][i].bytes;
116           }
117
118           std::shared_ptr<interface> itf = interface::find(i);
119           if (itf) {
120             itf->set(count, name);
121             itfs_w_stats.insert(itf);
122           }
123         }
124         break;
125       }
126     }
127   }
128   for (auto itf : itfs_w_stats) {
129     itf->publish_stats();
130   }
131 }
132
133 } // namespace VOM
134
135 /*
136  * fd.io coding-style-patch-verification: ON
137  *
138  * Local Variables:
139  * eval: (c-set-style "mozilla")
140  * End:
141  */