Skip to content

Commit

Permalink
Remove GetDiffStats() and instead iterate through the current map
Browse files Browse the repository at this point in the history
to send the accumulated counter and then clear it so that only new
keys are sent in the statistics.
Closes-Bug: #1494473

Change-Id: Ice58e1761a4200f8ad7b82bce81aba929a744950
  • Loading branch information
Megh Bhatt committed Sep 17, 2015
1 parent f095c7d commit 3568b12
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 189 deletions.
4 changes: 2 additions & 2 deletions src/analytics/collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ void Collector::GetGeneratorUVEInfo(vector<ModuleServerState> &genlist) {
const SandeshGenerator * const gen = gm_it->second;

vector<SandeshStats> ssv;
gen->GetStatistics(ssv);
gen->GetStatistics(&ssv);
vector<SandeshLogLevelStats> lsv;
gen->GetStatistics(lsv);
gen->GetStatistics(&lsv);
vector<SandeshStatsInfo> ssiv;
SandeshStatsInfo ssi;
ssi.set_hostname(Sandesh::source());
Expand Down
2 changes: 1 addition & 1 deletion src/analytics/db_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ void DbHandler::GetSandeshStats(std::string *drop_level,
*drop_level = Sandesh::LevelToString(drop_level_);
if (vdropmstats) {
tbb::mutex::scoped_lock lock(smutex_);
dropped_msg_stats_.Get(*vdropmstats);
dropped_msg_stats_.Get(vdropmstats);
}
}

Expand Down
38 changes: 0 additions & 38 deletions src/analytics/diffstats.h

This file was deleted.

6 changes: 3 additions & 3 deletions src/analytics/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ void Generator::UpdateStatistics(const VizMsg *vmsg) {
statistics_.Update(vmsg);
}

void Generator::GetStatistics(vector<SandeshStats> &ssv) const {
void Generator::GetStatistics(vector<SandeshStats> *ssv) const {
tbb::mutex::scoped_lock lock(smutex_);
statistics_.Get(ssv);
}

void Generator::GetStatistics(vector<SandeshLogLevelStats> &lsv) const {
void Generator::GetStatistics(vector<SandeshLogLevelStats> *lsv) const {
tbb::mutex::scoped_lock lock(smutex_);
statistics_.Get(lsv);
}
Expand All @@ -68,7 +68,7 @@ void Generator::SendSandeshMessageStatistics() {
vector<SandeshMessageInfo> smv;
{
tbb::mutex::scoped_lock lock(smutex_);
statistics_.Get(smv);
statistics_.Get(&smv);
}
SandeshMessageStat sms;
sms.set_name(ToString());
Expand Down
4 changes: 2 additions & 2 deletions src/analytics/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Generator {

bool ReceiveSandeshMsg(const VizMsg *vmsg, bool rsc);
void SendSandeshMessageStatistics();
void GetStatistics(std::vector<SandeshStats> &ssv) const;
void GetStatistics(std::vector<SandeshLogLevelStats> &lsv) const;
void GetStatistics(std::vector<SandeshStats> *ssv) const;
void GetStatistics(std::vector<SandeshLogLevelStats> *lsv) const;

private:
void UpdateStatistics(const VizMsg *vmsg);
Expand Down
56 changes: 21 additions & 35 deletions src/analytics/protobuf_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <io/io_types.h>
#include <io/udp_server.h>

#include "analytics/diffstats.h"
#include "analytics/self_describing_message.pb.h"
#include "analytics/protobuf_server.h"
#include "analytics/protobuf_server_impl.h"
Expand Down Expand Up @@ -491,28 +490,32 @@ class ProtobufServer::ProtobufServerImpl {
}
void GetRxDiff(
std::vector<SocketEndpointMessageStats> *semsv) {
tbb::mutex::scoped_lock lock(mutex_);
// Send diffs
GetDiffStats<EndpointStatsMessageMap,
EndpointMessageKey, MessageInfo,
SocketEndpointMessageStats>(rx_stats_map_, o_rx_stats_map_,
*semsv);
GetRxInternal(semsv, true);
}
void GetRx(
std::vector<SocketEndpointMessageStats> *semsv) {
GetRxInternal(semsv, false);
}
private:
class MessageInfo;

void GetRxInternal(
std::vector<SocketEndpointMessageStats> *semsv,
bool clear_stats) {
tbb::mutex::scoped_lock lock(mutex_);
BOOST_FOREACH(
const EndpointStatsMessageMap::value_type &esmm_value,
rx_stats_map_) {
const EndpointMessageKey &key(esmm_value.first);
const MessageInfo *msg_info(esmm_value.second);
SocketEndpointMessageStats sems;
msg_info->Get(key, sems);
msg_info->Get(key, &sems);
semsv->push_back(sems);
}
if (clear_stats) {
rx_stats_map_.clear();
}
}
private:
class MessageInfo;

void Update(const boost::asio::ip::udp::endpoint &remote_endpoint,
const std::string &message_name, uint64_t bytes,
Expand All @@ -534,7 +537,7 @@ class ProtobufServer::ProtobufServerImpl {
std::string> EndpointMessageKey;
typedef boost::ptr_map<EndpointMessageKey,
MessageInfo> EndpointStatsMessageMap;
EndpointStatsMessageMap rx_stats_map_, o_rx_stats_map_;
EndpointStatsMessageMap rx_stats_map_;
tbb::mutex mutex_;

//
Expand All @@ -558,38 +561,21 @@ class ProtobufServer::ProtobufServerImpl {
last_timestamp_ = UTCTimestampUsec();
}
void Get(const EndpointMessageKey &key,
SocketEndpointMessageStats &sems) const {
SocketEndpointMessageStats *sems) const {
const boost::asio::ip::udp::endpoint remote_endpoint(
key.first);
const std::string &message_name(key.second);
std::stringstream ss;
ss << remote_endpoint;
sems.set_endpoint_name(ss.str());
sems.set_message_name(message_name);
sems.set_messages(messages_);
sems.set_bytes(bytes_);
sems.set_errors(errors_);
sems.set_last_timestamp(last_timestamp_);
sems->set_endpoint_name(ss.str());
sems->set_message_name(message_name);
sems->set_messages(messages_);
sems->set_bytes(bytes_);
sems->set_errors(errors_);
sems->set_last_timestamp(last_timestamp_);
}

private:
friend MessageInfo operator+(const MessageInfo &a,
const MessageInfo &b) {
MessageInfo sum;
sum.messages_ = a.messages_ + b.messages_;
sum.bytes_ = a.bytes_ + b.bytes_;
sum.errors_ = a.errors_ + b.errors_;
return sum;
}
friend MessageInfo operator-(const MessageInfo &a,
const MessageInfo &b) {
MessageInfo diff;
diff.messages_ = a.messages_ - b.messages_;
diff.bytes_ = a.bytes_ - b.bytes_;
diff.errors_ = a.errors_ - b.errors_;
diff.last_timestamp_ = a.last_timestamp_ - b.last_timestamp_;
return diff;
}
uint64_t messages_;
uint64_t bytes_;
uint64_t errors_;
Expand Down
33 changes: 14 additions & 19 deletions src/analytics/test/viz_message_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,21 @@ TEST_F(VizMessageTest, Stats) {
stats_.Update(&vmsgp);
// Verify Gets - SandeshStats, SandeshLogLevelStats, SandeshMessageInfo
std::vector<SandeshStats> vsstats;
stats_.Get(vsstats);
stats_.Get(&vsstats);
ASSERT_EQ(1, vsstats.size());
EXPECT_STREQ("VNSwitchErrorMsg", vsstats[0].get_message_type().c_str());
EXPECT_EQ(1, vsstats[0].get_messages());
EXPECT_EQ(xmlmessage.size(), vsstats[0].get_bytes());
vsstats.clear();
std::vector<SandeshLogLevelStats> vsllstats;
stats_.Get(vsllstats);
stats_.Get(&vsllstats);
ASSERT_EQ(1, vsllstats.size());
EXPECT_STREQ("SYS_DEBUG", vsllstats[0].get_level().c_str());
EXPECT_EQ(1, vsllstats[0].get_messages());
EXPECT_EQ(xmlmessage.size(), vsllstats[0].get_bytes());
vsllstats.clear();
std::vector<SandeshMessageInfo> vsmi;
stats_.Get(vsmi);
stats_.Get(&vsmi);
ASSERT_EQ(1, vsmi.size());
EXPECT_STREQ("VNSwitchErrorMsg", vsmi[0].get_type().c_str());
EXPECT_STREQ("SYS_DEBUG", vsmi[0].get_level().c_str());
Expand All @@ -156,19 +156,19 @@ TEST_F(VizMessageTest, Stats) {
// Send same message, update stats
stats_.Update(&vmsgp);
// Verify updates
stats_.Get(vsstats);
stats_.Get(&vsstats);
ASSERT_EQ(1, vsstats.size());
EXPECT_STREQ("VNSwitchErrorMsg", vsstats[0].get_message_type().c_str());
EXPECT_EQ(2, vsstats[0].get_messages());
EXPECT_EQ(xmlmessage.size() * 2, vsstats[0].get_bytes());
vsstats.clear();
stats_.Get(vsllstats);
stats_.Get(&vsllstats);
ASSERT_EQ(1, vsllstats.size());
EXPECT_STREQ("SYS_DEBUG", vsllstats[0].get_level().c_str());
EXPECT_EQ(2, vsllstats[0].get_messages());
EXPECT_EQ(xmlmessage.size() * 2, vsllstats[0].get_bytes());
vsllstats.clear();
stats_.Get(vsmi);
stats_.Get(&vsmi);
ASSERT_EQ(1, vsmi.size());
EXPECT_STREQ("VNSwitchErrorMsg", vsmi[0].get_type().c_str());
EXPECT_STREQ("SYS_DEBUG", vsmi[0].get_level().c_str());
Expand All @@ -194,7 +194,7 @@ TEST_F(VizMessageTest, Stats) {
// Update stats
stats_.Update(&vmsgp_object);
// Verify Gets - SandeshStats, SandeshLogLevelStats, SandeshMessageInfo
stats_.Get(vsstats);
stats_.Get(&vsstats);
ASSERT_EQ(2, vsstats.size());
EXPECT_STREQ("VNSwitchErrorMsg", vsstats[0].get_message_type().c_str());
EXPECT_STREQ("VNSwitchErrorMsgObject", vsstats[1].get_message_type().c_str());
Expand All @@ -204,23 +204,18 @@ TEST_F(VizMessageTest, Stats) {
EXPECT_EQ(xmlmessage_object.size(), vsstats[1].get_bytes());
vsstats.clear();
// Only SYSTEM and SYSLOG have level
stats_.Get(vsllstats);
stats_.Get(&vsllstats);
ASSERT_EQ(1, vsllstats.size());
EXPECT_STREQ("SYS_DEBUG", vsllstats[0].get_level().c_str());
EXPECT_EQ(2, vsllstats[0].get_messages());
EXPECT_EQ(xmlmessage.size() * 2, vsllstats[0].get_bytes());
vsllstats.clear();
stats_.Get(vsmi);
ASSERT_EQ(2, vsmi.size());
EXPECT_STREQ("VNSwitchErrorMsg", vsmi[0].get_type().c_str());
EXPECT_STREQ("SYS_DEBUG", vsmi[0].get_level().c_str());
// Diffs
EXPECT_EQ(0, vsmi[0].get_messages());
EXPECT_EQ(0, vsmi[0].get_bytes());
EXPECT_STREQ("VNSwitchErrorMsgObject", vsmi[1].get_type().c_str());
EXPECT_STREQ("INVALID", vsmi[1].get_level().c_str());
EXPECT_EQ(1, vsmi[1].get_messages());
EXPECT_EQ(xmlmessage_object.size(), vsmi[1].get_bytes());
stats_.Get(&vsmi);
ASSERT_EQ(1, vsmi.size());
EXPECT_STREQ("VNSwitchErrorMsgObject", vsmi[0].get_type().c_str());
EXPECT_STREQ("INVALID", vsmi[0].get_level().c_str());
EXPECT_EQ(1, vsmi[0].get_messages());
EXPECT_EQ(xmlmessage_object.size(), vsmi[0].get_bytes());
vsmi.clear();
// Delete message
vmsgp_object.msg = NULL;
Expand Down

0 comments on commit 3568b12

Please sign in to comment.