|
| 1 | +#include "sai_redis.h" |
| 2 | +#include "meta/saiserialize.h" |
| 3 | + |
| 4 | +sai_status_t internal_redis_get_stats_process( |
| 5 | + _In_ sai_object_type_t object_type, |
| 6 | + _In_ uint32_t count, |
| 7 | + _Out_ uint64_t *counter_list, |
| 8 | + _In_ swss::KeyOpFieldsValuesTuple &kco) |
| 9 | +{ |
| 10 | + SWSS_LOG_ENTER(); |
| 11 | + |
| 12 | + // key: sai_status |
| 13 | + // field: stat_id |
| 14 | + // value: stat_value |
| 15 | + |
| 16 | + const auto &key = kfvKey(kco); |
| 17 | + const auto &values = kfvFieldsValues(kco); |
| 18 | + |
| 19 | + auto str_sai_status = key; |
| 20 | + |
| 21 | + sai_status_t status; |
| 22 | + |
| 23 | + sai_deserialize_status(str_sai_status, status); |
| 24 | + |
| 25 | + if (status == SAI_STATUS_SUCCESS) |
| 26 | + { |
| 27 | + uint32_t i = 0; |
| 28 | + for (const auto &v : values) |
| 29 | + { |
| 30 | + if (i >= count) |
| 31 | + { |
| 32 | + SWSS_LOG_ERROR("Received more values than expected"); |
| 33 | + status = SAI_STATUS_FAILURE; |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + uint64_t value = 0; |
| 38 | + |
| 39 | + value = stoull(fvValue(v)); |
| 40 | + counter_list[i] = value; |
| 41 | + i++; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return status; |
| 46 | +} |
| 47 | + |
| 48 | +template <class T> struct stat_traits {}; |
| 49 | + |
| 50 | +template <> |
| 51 | +struct stat_traits<sai_port_stat_t> |
| 52 | +{ |
| 53 | + typedef std::string (*serialize_stat)(sai_port_stat_t); |
| 54 | + static constexpr serialize_stat serialize_stat_fn = sai_serialize_port_stat; |
| 55 | +}; |
| 56 | + |
| 57 | +template <> |
| 58 | +struct stat_traits<sai_queue_stat_t> |
| 59 | +{ |
| 60 | + typedef std::string (*serialize_stat)(sai_queue_stat_t); |
| 61 | + static constexpr serialize_stat serialize_stat_fn = sai_serialize_queue_stat; |
| 62 | +}; |
| 63 | + |
| 64 | +template <> |
| 65 | +struct stat_traits<sai_ingress_priority_group_stat_t> |
| 66 | +{ |
| 67 | + typedef std::string (*serialize_stat)(sai_ingress_priority_group_stat_t); |
| 68 | + static constexpr serialize_stat serialize_stat_fn = sai_serialize_ingress_priority_group_stat; |
| 69 | +}; |
| 70 | + |
| 71 | + |
| 72 | +template <class T> |
| 73 | +std::vector<swss::FieldValueTuple> serialize_counter_id_list( |
| 74 | + _In_ uint32_t count, |
| 75 | + _In_ const T *counter_id_list) |
| 76 | +{ |
| 77 | + SWSS_LOG_ENTER(); |
| 78 | + |
| 79 | + std::vector<swss::FieldValueTuple> values; |
| 80 | + |
| 81 | + for (uint32_t i = 0; i < count; i++) |
| 82 | + { |
| 83 | + std::string field = stat_traits<T>::serialize_stat_fn(counter_id_list[i]); |
| 84 | + values.emplace_back(field, ""); |
| 85 | + } |
| 86 | + |
| 87 | + return std::move(values); |
| 88 | +} |
| 89 | + |
| 90 | +template <typename T> |
| 91 | +sai_status_t internal_redis_generic_get_stats( |
| 92 | + _In_ sai_object_type_t object_type, |
| 93 | + _In_ const std::string &serialized_object_id, |
| 94 | + _In_ uint32_t count, |
| 95 | + _In_ const T *counter_id_list, |
| 96 | + _Out_ uint64_t *counter_list) |
| 97 | +{ |
| 98 | + SWSS_LOG_ENTER(); |
| 99 | + |
| 100 | + std::vector<swss::FieldValueTuple> entry = serialize_counter_id_list( |
| 101 | + count, |
| 102 | + counter_id_list); |
| 103 | + |
| 104 | + std::string str_object_type = sai_serialize_object_type(object_type); |
| 105 | + |
| 106 | + std::string key = str_object_type + ":" + serialized_object_id; |
| 107 | + |
| 108 | + SWSS_LOG_DEBUG("generic get stats key: %s, fields: %lu", key.c_str(), entry.size()); |
| 109 | + |
| 110 | + if (g_record) |
| 111 | + { |
| 112 | + // XXX don't know which character to use for get stats. For now its 'm' |
| 113 | + recordLine("m|" + key + "|" + joinFieldValues(entry)); |
| 114 | + } |
| 115 | + |
| 116 | + // get is special, it will not put data |
| 117 | + // into asic view, only to message queue |
| 118 | + g_asicState->set(key, entry, "get_stats"); |
| 119 | + |
| 120 | + // wait for response |
| 121 | + |
| 122 | + swss::Select s; |
| 123 | + |
| 124 | + s.addSelectable(g_redisGetConsumer.get()); |
| 125 | + |
| 126 | + while (true) |
| 127 | + { |
| 128 | + SWSS_LOG_DEBUG("wait for get_stats response"); |
| 129 | + |
| 130 | + swss::Selectable *sel; |
| 131 | + |
| 132 | + int fd; |
| 133 | + |
| 134 | + int result = s.select(&sel, &fd, GET_RESPONSE_TIMEOUT); |
| 135 | + |
| 136 | + if (result == swss::Select::OBJECT) |
| 137 | + { |
| 138 | + swss::KeyOpFieldsValuesTuple kco; |
| 139 | + |
| 140 | + g_redisGetConsumer->pop(kco); |
| 141 | + |
| 142 | + const std::string &op = kfvOp(kco); |
| 143 | + const std::string &opkey = kfvKey(kco); |
| 144 | + |
| 145 | + SWSS_LOG_DEBUG("response: op = %s, key = %s", opkey.c_str(), op.c_str()); |
| 146 | + |
| 147 | + if (op != "getresponse") // ignore non response messages |
| 148 | + { |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + sai_status_t status = internal_redis_get_stats_process( |
| 153 | + object_type, |
| 154 | + count, |
| 155 | + counter_list, |
| 156 | + kco); |
| 157 | + |
| 158 | + if (g_record) |
| 159 | + { |
| 160 | + const auto &str_status = kfvKey(kco); |
| 161 | + const auto &values = kfvFieldsValues(kco); |
| 162 | + |
| 163 | + // first serialized is status |
| 164 | + recordLine("M|" + str_status + "|" + joinFieldValues(values)); |
| 165 | + } |
| 166 | + |
| 167 | + SWSS_LOG_DEBUG("generic get status: %d", status); |
| 168 | + |
| 169 | + return status; |
| 170 | + } |
| 171 | + |
| 172 | + SWSS_LOG_ERROR("generic get failed due to SELECT operation result"); |
| 173 | + break; |
| 174 | + } |
| 175 | + |
| 176 | + if (g_record) |
| 177 | + { |
| 178 | + recordLine("M|SAI_STATUS_FAILURE"); |
| 179 | + } |
| 180 | + |
| 181 | + SWSS_LOG_ERROR("generic get stats failed to get response"); |
| 182 | + |
| 183 | + return SAI_STATUS_FAILURE; |
| 184 | +} |
| 185 | + |
| 186 | +template <typename T> |
| 187 | +sai_status_t redis_generic_get_stats( |
| 188 | + _In_ sai_object_type_t object_type, |
| 189 | + _In_ sai_object_id_t object_id, |
| 190 | + _In_ uint32_t count, |
| 191 | + _In_ const T* counter_id_list, |
| 192 | + _Out_ uint64_t *counter_list) |
| 193 | +{ |
| 194 | + SWSS_LOG_ENTER(); |
| 195 | + |
| 196 | + std::string str_object_id = sai_serialize_object_id(object_id); |
| 197 | + |
| 198 | + return internal_redis_generic_get_stats( |
| 199 | + object_type, |
| 200 | + str_object_id, |
| 201 | + count, |
| 202 | + counter_id_list, |
| 203 | + counter_list); |
| 204 | +} |
| 205 | + |
| 206 | +#define DECLARE_REDIS_GENERIC_GET_STATS(type) \ |
| 207 | + template \ |
| 208 | + sai_status_t redis_generic_get_stats<sai_ ## type ## _stat_t>( \ |
| 209 | + _In_ sai_object_type_t object_type, \ |
| 210 | + _In_ sai_object_id_t object_id, \ |
| 211 | + _In_ uint32_t count, \ |
| 212 | + _In_ const sai_ ## type ## _stat_t *counter_id_list, \ |
| 213 | + _Out_ uint64_t *counter_list); \ |
| 214 | + |
| 215 | +DECLARE_REDIS_GENERIC_GET_STATS(port); |
| 216 | +DECLARE_REDIS_GENERIC_GET_STATS(queue); |
| 217 | +DECLARE_REDIS_GENERIC_GET_STATS(ingress_priority_group); |
0 commit comments