VOM: fixes for 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   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         break;
78
79       case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
80         if (name.find("/if") != std::string::npos)
81           name.erase(0, 4);
82         for (auto& i : m_stat_itf_indexes) {
83           counter_t count = {.packets = 0, .bytes = 0 };
84           for (int k = 0; k < m_client.vec_len(
85                                 sde.get_stat_segment_combined_counter_data());
86                k++) {
87             count.packets +=
88               sde.get_stat_segment_combined_counter_data()[k][i].packets;
89             count.bytes +=
90               sde.get_stat_segment_combined_counter_data()[k][i].bytes;
91           }
92           std::shared_ptr<interface> itf = interface::find(i);
93           if (itf)
94             itf->set(count, name);
95         }
96         break;
97
98       default:;
99     }
100   }
101
102   for (auto& i : m_stat_itf_indexes) {
103     std::shared_ptr<interface> itf = interface::find(i);
104     if (itf)
105       itf->publish_stats();
106   }
107 }
108
109 } // namespace VOM
110
111 /*
112  * fd.io coding-style-patch-verification: ON
113  *
114  * Local Variables:
115  * eval: (c-set-style "mozilla")
116  * End:
117  */