Skip to content

Commit 17a6d61

Browse files
author
Shuotian Cheng
authored
[saihelper]: Create initSaiRedis function and put related code inside it (sonic-net#254)
Move SAI Redis initialization related code outside main function. In the future, global variables could be eliminated to make code cleaner. getTimestamp could be moved to common library as well.
1 parent c592731 commit 17a6d61

File tree

3 files changed

+100
-80
lines changed

3 files changed

+100
-80
lines changed

orchagent/main.cpp

+1-79
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,6 @@ string gRecordFile;
4646
/* Global database mutex */
4747
mutex gDbMutex;
4848

49-
string getTimestamp()
50-
{
51-
char buffer[64];
52-
struct timeval tv;
53-
gettimeofday(&tv, NULL);
54-
55-
size_t size = strftime(buffer, 32 ,"%Y-%m-%d.%T.", localtime(&tv.tv_sec));
56-
snprintf(&buffer[size], 32, "%06ld", tv.tv_usec);
57-
58-
return string(buffer);
59-
}
60-
6149
void usage()
6250
{
6351
cout << "usage: orchagent [-h] [-r record_type] [-d record_location] [-b batch_size] [-m MAC]" << endl;
@@ -80,7 +68,6 @@ void sighup_handler(int signo)
8068
gLogRotate = true;
8169

8270
sai_attribute_t attr;
83-
8471
attr.id = SAI_REDIS_SWITCH_ATTR_PERFORM_LOG_ROTATE;
8572
attr.value.booldata = true;
8673

@@ -160,74 +147,9 @@ int main(int argc, char **argv)
160147
SWSS_LOG_NOTICE("--- Starting Orchestration Agent ---");
161148

162149
initSaiApi();
163-
164-
/*
165-
* NOTE: Notice that all redis attributes here are using SAI_NULL_OBJECT_ID
166-
* as switch id, because thsoe operations don't require actual switch to be
167-
* performed, and they should be executed before creating switch.
168-
*/
150+
initSaiRedis(record_location);
169151

170152
sai_attribute_t attr;
171-
172-
/* Disable/enable SAI Redis recording */
173-
if (gSairedisRecord)
174-
{
175-
attr.id = SAI_REDIS_SWITCH_ATTR_RECORDING_OUTPUT_DIR;
176-
attr.value.s8list.count = record_location.size();
177-
attr.value.s8list.list = (signed char *) record_location.c_str();
178-
179-
status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
180-
if (status != SAI_STATUS_SUCCESS)
181-
{
182-
SWSS_LOG_ERROR("Failed to set SAI Redis recording output folder to %s, rv:%d", record_location.c_str(), status);
183-
exit(EXIT_FAILURE);
184-
}
185-
}
186-
187-
attr.id = SAI_REDIS_SWITCH_ATTR_RECORD;
188-
attr.value.booldata = gSairedisRecord;
189-
190-
status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
191-
if (status != SAI_STATUS_SUCCESS)
192-
{
193-
SWSS_LOG_ERROR("Failed to set SAI Redis recording to %s, rv:%d", gSairedisRecord ? "true" : "false", status);
194-
exit(EXIT_FAILURE);
195-
}
196-
197-
/* Disable/enable SwSS recording */
198-
if (gSwssRecord)
199-
{
200-
gRecordFile = record_location + "/" + "swss." + getTimestamp() + ".rec";
201-
gRecordOfs.open(gRecordFile);
202-
if (!gRecordOfs.is_open())
203-
{
204-
SWSS_LOG_ERROR("Failed to open SwSS recording file %s", gRecordFile.c_str());
205-
exit(EXIT_FAILURE);
206-
}
207-
}
208-
209-
attr.id = SAI_REDIS_SWITCH_ATTR_NOTIFY_SYNCD;
210-
attr.value.s32 = SAI_REDIS_NOTIFY_SYNCD_INIT_VIEW;
211-
status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
212-
213-
if (status != SAI_STATUS_SUCCESS)
214-
{
215-
SWSS_LOG_ERROR("Failed to notify syncd INIT_VIEW, rv:%d", status);
216-
exit(EXIT_FAILURE);
217-
}
218-
SWSS_LOG_NOTICE("Notify syncd INIT_VIEW");
219-
220-
attr.id = SAI_REDIS_SWITCH_ATTR_USE_PIPELINE;
221-
attr.value.booldata = true;
222-
223-
status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
224-
if (status != SAI_STATUS_SUCCESS)
225-
{
226-
SWSS_LOG_ERROR("Failed to enable redis pipeline, rv:%d", status);
227-
exit(EXIT_FAILURE);
228-
}
229-
SWSS_LOG_NOTICE("Enable redis pipeline");
230-
231153
vector<sai_attribute_t> attrs;
232154

233155
attr.id = SAI_SWITCH_ATTR_INIT_SWITCH;

orchagent/saihelper.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ extern "C" {
33
#include "saistatus.h"
44
}
55

6+
#include <fstream>
67
#include <map>
8+
#include <sys/time.h>
9+
710
#include <logger.h>
11+
#include <sairedis.h>
12+
813
#include "saihelper.h"
914

1015
using namespace std;
@@ -35,6 +40,12 @@ sai_acl_api_t* sai_acl_api;
3540
sai_mirror_api_t* sai_mirror_api;
3641
sai_fdb_api_t* sai_fdb_api;
3742

43+
extern sai_object_id_t gSwitchId;
44+
extern bool gSairedisRecord;
45+
extern bool gSwssRecord;
46+
extern ofstream gRecordOfs;
47+
extern string gRecordFile;
48+
3849
map<string, string> gProfileMap;
3950

4051
const char *test_profile_get_value (
@@ -140,3 +151,87 @@ void initSaiApi()
140151
sai_log_set(SAI_API_SCHEDULER_GROUP, SAI_LOG_LEVEL_NOTICE);
141152
sai_log_set(SAI_API_ACL, SAI_LOG_LEVEL_NOTICE);
142153
}
154+
155+
string getTimestamp()
156+
{
157+
char buffer[64];
158+
struct timeval tv;
159+
gettimeofday(&tv, NULL);
160+
161+
size_t size = strftime(buffer, 32 ,"%Y-%m-%d.%T.", localtime(&tv.tv_sec));
162+
snprintf(&buffer[size], 32, "%06ld", tv.tv_usec);
163+
164+
return string(buffer);
165+
}
166+
167+
void initSaiRedis(const string &record_location)
168+
{
169+
/**
170+
* NOTE: Notice that all Redis attributes here are using SAI_NULL_OBJECT_ID
171+
* as the switch ID, because those operations don't require actual switch
172+
* to be performed, and they should be executed before creating switch.
173+
*/
174+
175+
/* Disable/enable SAI Redis recording */
176+
sai_attribute_t attr;
177+
attr.id = SAI_REDIS_SWITCH_ATTR_RECORD;
178+
attr.value.booldata = gSairedisRecord;
179+
180+
sai_status_t status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
181+
if (status != SAI_STATUS_SUCCESS)
182+
{
183+
SWSS_LOG_ERROR("Failed to %s SAI Redis recording, rv:%d",
184+
gSairedisRecord ? "enable" : "disable", status);
185+
exit(EXIT_FAILURE);
186+
}
187+
188+
if (gSairedisRecord)
189+
{
190+
attr.id = SAI_REDIS_SWITCH_ATTR_RECORDING_OUTPUT_DIR;
191+
attr.value.s8list.count = record_location.size();
192+
attr.value.s8list.list = (signed char *) record_location.c_str();
193+
194+
status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
195+
if (status != SAI_STATUS_SUCCESS)
196+
{
197+
SWSS_LOG_ERROR("Failed to set SAI Redis recording output folder to %s, rv:%d",
198+
record_location.c_str(), status);
199+
exit(EXIT_FAILURE);
200+
}
201+
}
202+
203+
/* Disable/enable SwSS recording */
204+
if (gSwssRecord)
205+
{
206+
gRecordFile = record_location + "/" + "swss." + getTimestamp() + ".rec";
207+
gRecordOfs.open(gRecordFile);
208+
if (!gRecordOfs.is_open())
209+
{
210+
SWSS_LOG_ERROR("Failed to open SwSS recording file %s", gRecordFile.c_str());
211+
exit(EXIT_FAILURE);
212+
}
213+
}
214+
215+
attr.id = SAI_REDIS_SWITCH_ATTR_USE_PIPELINE;
216+
attr.value.booldata = true;
217+
218+
status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
219+
if (status != SAI_STATUS_SUCCESS)
220+
{
221+
SWSS_LOG_ERROR("Failed to enable redis pipeline, rv:%d", status);
222+
exit(EXIT_FAILURE);
223+
}
224+
SWSS_LOG_NOTICE("Enable redis pipeline");
225+
226+
attr.id = SAI_REDIS_SWITCH_ATTR_NOTIFY_SYNCD;
227+
attr.value.s32 = SAI_REDIS_NOTIFY_SYNCD_INIT_VIEW;
228+
status = sai_switch_api->set_switch_attribute(gSwitchId, &attr);
229+
230+
if (status != SAI_STATUS_SUCCESS)
231+
{
232+
SWSS_LOG_ERROR("Failed to notify syncd INIT_VIEW, rv:%d", status);
233+
exit(EXIT_FAILURE);
234+
}
235+
SWSS_LOG_NOTICE("Notify syncd INIT_VIEW");
236+
237+
}

orchagent/saihelper.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#pragma once
22

3-
void initSaiApi();
3+
#include <string>
4+
5+
void initSaiApi();
6+
void initSaiRedis(const std::string &record_location);

0 commit comments

Comments
 (0)