VOM: stats fixes
[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   const stat_client::stat_data_vec_t& sd = m_client.dump();
65   for (auto& sde : sd) {
66     std::string name;
67
68     if (sde.name().empty())
69       continue;
70
71     name = sde.name();
72
73     switch (sde.type()) {
74       case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
75       case STAT_DIR_TYPE_ERROR_INDEX:
76       case STAT_DIR_TYPE_SCALAR_INDEX:
77       case STAT_DIR_TYPE_ILLEGAL:
78         break;
79
80       case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: {
81         vlib_counter_t** data;
82
83         data = sde.get_stat_segment_combined_counter_data();
84
85         if (name.find("/if") != std::string::npos)
86           name.erase(0, 4);
87         for (auto& i : m_stat_itf_indexes) {
88           counter_t count;
89
90           for (int k = 0; k < m_client.vec_len(data); k++) {
91             count.packets += data[k][i].packets;
92             count.bytes += data[k][i].bytes;
93           }
94           std::shared_ptr<interface> itf = interface::find(i);
95           if (itf)
96             itf->set(count, name);
97         }
98         break;
99       }
100     }
101   }
102
103   for (auto& i : m_stat_itf_indexes) {
104     std::shared_ptr<interface> itf = interface::find(i);
105     if (itf)
106       itf->publish_stats();
107   }
108 }
109
110 } // namespace VOM
111
112 /*
113  * fd.io coding-style-patch-verification: ON
114  *
115  * Local Variables:
116  * eval: (c-set-style "mozilla")
117  * End:
118  */