Skip to content

Commit 6bac82b

Browse files
authored
Improve memory usage by move ZMQ serialize buffer from ZmqProducerStateTable to ZmqClient (#955)
#### Why I did it Every ZmqProducerStateTable will allocate 16MB buffer, this can be improve by share same buffer in ZmqClient. #### How I did it Improve memory usage by move ZMQ serialize buffer from ZmqProducerStateTable to ZmqClient. ##### Work item tracking #### How to verify it Pass all test cases. #### Which release branch to backport (provide reason below if selected) <!-- - Note we only backport fixes to a release branch, *not* features! - Please also provide a reason for the backporting below. - e.g. - [x] 202006 --> - [ ] 201811 - [ ] 201911 - [ ] 202006 - [ ] 202012 - [ ] 202106 - [ ] 202111 #### Description for the changelog Improve memory usage by move ZMQ serialize buffer from ZmqProducerStateTable to ZmqClient. #### Link to config_db schema for YANG module changes <!-- Provide a link to config_db schema for the table for which YANG model is defined Link should point to correct section on https://github.com/Azure/SONiC/wiki/Configuration. --> #### A picture of a cute animal (not mandatory but encouraged)
1 parent ebd2afb commit 6bac82b

File tree

5 files changed

+14
-24
lines changed

5 files changed

+14
-24
lines changed

common/c-api/zmqclient.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ void SWSSZmqClient_sendMsg(SWSSZmqClient zmqc, const char *dbName, const char *t
2727
SWSSKeyOpFieldValuesArray arr) {
2828
SWSSTry({
2929
vector<KeyOpFieldsValuesTuple> kcos = takeKeyOpFieldValuesArray(arr);
30-
size_t bufSize = BinarySerializer::serializedSize(dbName, tableName, kcos);
31-
vector<char> v(bufSize);
3230
((ZmqClient *)zmqc)
33-
->sendMsg(string(dbName), string(tableName), kcos, v);
31+
->sendMsg(string(dbName), string(tableName), kcos);
3432
});
3533
}

common/zmqclient.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void ZmqClient::initialize(const std::string& endpoint, const std::string& vrf)
5151
m_context = nullptr;
5252
m_socket = nullptr;
5353
m_vrf = vrf;
54+
m_sendbuffer.resize(MQ_RESPONSE_MAX_COUNT);
5455

5556
connect();
5657
}
@@ -116,12 +117,11 @@ void ZmqClient::connect()
116117
void ZmqClient::sendMsg(
117118
const std::string& dbName,
118119
const std::string& tableName,
119-
const std::vector<KeyOpFieldsValuesTuple>& kcos,
120-
std::vector<char>& sendbuffer)
120+
const std::vector<KeyOpFieldsValuesTuple>& kcos)
121121
{
122122
int serializedlen = (int)BinarySerializer::serializeBuffer(
123-
sendbuffer.data(),
124-
sendbuffer.size(),
123+
m_sendbuffer.data(),
124+
m_sendbuffer.size(),
125125
dbName,
126126
tableName,
127127
kcos);
@@ -144,7 +144,7 @@ void ZmqClient::sendMsg(
144144
std::lock_guard<std::mutex> lock(m_socketMutex);
145145

146146
// Use none block mode to use all bandwidth: http://api.zeromq.org/2-1%3Azmq-send
147-
rc = zmq_send(m_socket, sendbuffer.data(), serializedlen, ZMQ_NOBLOCK);
147+
rc = zmq_send(m_socket, m_sendbuffer.data(), serializedlen, ZMQ_NOBLOCK);
148148
}
149149

150150
if (rc >= 0)

common/zmqclient.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ class ZmqClient
2222

2323
void sendMsg(const std::string& dbName,
2424
const std::string& tableName,
25-
const std::vector<KeyOpFieldsValuesTuple>& kcos,
26-
std::vector<char>& sendbuffer);
25+
const std::vector<KeyOpFieldsValuesTuple>& kcos);
2726
private:
2827
void initialize(const std::string& endpoint, const std::string& vrf);
2928

@@ -38,6 +37,8 @@ class ZmqClient
3837
bool m_connected;
3938

4039
std::mutex m_socketMutex;
40+
41+
std::vector<char> m_sendbuffer;
4142
};
4243

4344
}

common/zmqproducerstatetable.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ ZmqProducerStateTable::ZmqProducerStateTable(RedisPipeline *pipeline, const stri
3838

3939
void ZmqProducerStateTable::initialize(DBConnector *db, const std::string &tableName, bool dbPersistence)
4040
{
41-
m_sendbuffer.resize(MQ_RESPONSE_MAX_COUNT);
42-
4341
if (dbPersistence)
4442
{
4543
SWSS_LOG_DEBUG("Database persistence enabled, tableName: %s", tableName.c_str());
@@ -64,8 +62,7 @@ void ZmqProducerStateTable::set(
6462
m_zmqClient.sendMsg(
6563
m_dbName,
6664
m_tableNameStr,
67-
kcos,
68-
m_sendbuffer);
65+
kcos);
6966

7067
if (m_asyncDBUpdater != nullptr)
7168
{
@@ -93,8 +90,7 @@ void ZmqProducerStateTable::del(
9390
m_zmqClient.sendMsg(
9491
m_dbName,
9592
m_tableNameStr,
96-
kcos,
97-
m_sendbuffer);
93+
kcos);
9894

9995
if (m_asyncDBUpdater != nullptr)
10096
{
@@ -112,8 +108,7 @@ void ZmqProducerStateTable::set(const std::vector<KeyOpFieldsValuesTuple> &value
112108
m_zmqClient.sendMsg(
113109
m_dbName,
114110
m_tableNameStr,
115-
values,
116-
m_sendbuffer);
111+
values);
117112

118113
if (m_asyncDBUpdater != nullptr)
119114
{
@@ -136,8 +131,7 @@ void ZmqProducerStateTable::del(const std::vector<std::string> &keys)
136131
m_zmqClient.sendMsg(
137132
m_dbName,
138133
m_tableNameStr,
139-
kcos,
140-
m_sendbuffer);
134+
kcos);
141135

142136
if (m_asyncDBUpdater != nullptr)
143137
{
@@ -157,8 +151,7 @@ void ZmqProducerStateTable::send(const std::vector<KeyOpFieldsValuesTuple> &kcos
157151
m_zmqClient.sendMsg(
158152
m_dbName,
159153
m_tableNameStr,
160-
kcos,
161-
m_sendbuffer);
154+
kcos);
162155

163156
if (m_asyncDBUpdater != nullptr)
164157
{

common/zmqproducerstatetable.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ class ZmqProducerStateTable : public ProducerStateTable
4242
void initialize(DBConnector *db, const std::string &tableName, bool dbPersistence);
4343

4444
ZmqClient& m_zmqClient;
45-
46-
std::vector<char> m_sendbuffer;
4745

4846
const std::string m_dbName;
4947
const std::string m_tableNameStr;

0 commit comments

Comments
 (0)