Skip to content

Commit

Permalink
Cleanup code to handle legacy sampler record
Browse files Browse the repository at this point in the history
  • Loading branch information
phaag committed Feb 22, 2024
1 parent a988a40 commit eb5bef5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/include/exporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ int InitExporterList(void);

int AddExporterInfo(exporter_info_record_t *exporter_record);

int AddSamplerInfo(sampler_record_t *sampler_record);
int AddSamplerRecord(sampler_record_t *sampler_record);

int AddSamplerLegacyRecord(samplerV0_record_t *sampler_record);

int AddExporterStat(exporter_stats_record_t *stat_record);

Expand Down
32 changes: 20 additions & 12 deletions src/nfdump/exporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ int AddExporterInfo(exporter_info_record_t *exporter_record) {
return 1;
} // End of AddExporterInfo

int AddSamplerInfo(sampler_record_t *sampler_record) {
if (sampler_record->size != sizeof(sampler_record_t) && sampler_record->size != sizeof(samplerV0_record_t)) {
int AddSamplerLegacyRecord(samplerV0_record_t *sampler_record) {
if (sampler_record->size != sizeof(samplerV0_record_t)) {
LogError("Corrupt sampler record in %s line %d\n", __FILE__, __LINE__);
return 0;
}

sampler_record_t convert_record = {0};
sampler_record_t *record = sampler_record;
sampler_record_t *record = &convert_record;
if (sampler_record->size == sizeof(samplerV0_record_t)) {
samplerV0_record_t *samplerV0_record = (samplerV0_record_t *)sampler_record;

Expand All @@ -190,8 +190,12 @@ int AddSamplerInfo(sampler_record_t *sampler_record) {
convert_record.spaceInterval = samplerV0_record->interval - 1;
record = &convert_record;
}
return AddSamplerRecord(record);

uint32_t id = record->exporter_sysid;
} // End of AddSamplerLegacyRecord

int AddSamplerRecord(sampler_record_t *sampler_record) {
uint32_t id = sampler_record->exporter_sysid;
if (id >= MAX_EXPORTERS) {
LogError("Corrupt sampler record in %s line %d\n", __FILE__, __LINE__);
return 0;
Expand All @@ -204,10 +208,10 @@ int AddSamplerInfo(sampler_record_t *sampler_record) {

sampler_t **sampler = &exporter_list[id]->sampler;
while (*sampler) {
if (memcmp((void *)&(*sampler)->record, (void *)record, sizeof(sampler_record_t)) == 0) {
if (memcmp((void *)&(*sampler)->record, (void *)sampler_record, sizeof(sampler_record_t)) == 0) {
// Found identical sampler already registered
dbg_printf("Identical sampler already registered: %u, algorithm: %u, packet interval: %u, packet space: %u\n", record->exporter_sysid,
record->algorithm, record->packetInterval, record->spaceInterval);
sampler_record->algorithm, sampler_record->packetInterval, sampler_record->spaceInterval);
return 2;
}
sampler = &((*sampler)->next);
Expand All @@ -219,9 +223,9 @@ int AddSamplerInfo(sampler_record_t *sampler_record) {
return 0;
}
(*sampler)->next = NULL;
record->exporter_sysid = exporter_list[id]->info.sysid;
sampler_record->exporter_sysid = exporter_list[id]->info.sysid;

memcpy((void *)&(*sampler)->record, (void *)record, sizeof(sampler_record_t));
memcpy((void *)&(*sampler)->record, (void *)sampler_record, sizeof(sampler_record_t));
dbg_printf("Insert sampler record for exporter at slot %i:\n", id);

#ifdef DEVEL
Expand All @@ -237,7 +241,7 @@ int AddSamplerInfo(sampler_record_t *sampler_record) {
#endif

return 1;
} // End of AddSamplerInfo
} // End of AddSamplerRecord

int AddExporterStat(exporter_stats_record_t *stat_record) {
if (stat_record->header.size < sizeof(exporter_stats_record_t)) {
Expand Down Expand Up @@ -369,15 +373,19 @@ void PrintExporters(void) {
case ExporterInfoRecordType:
found = 1;
if (!AddExporterInfo((exporter_info_record_t *)record)) {
LogError("Failed to add Exporter Record\n");
LogError("Failed to add exporter record\n");
}
break;
case ExporterStatRecordType:
AddExporterStat((exporter_stats_record_t *)record);
break;
case SamplerRecordType:
if (!AddSamplerInfo((sampler_record_t *)record)) {
LogError("Failed to add Sampler Record\n");
if (!AddSamplerRecord((sampler_record_t *)record)) {
LogError("Failed to add sampler record\n");
}
case SamplerLegacyRecordType:
if (!AddSamplerLegacyRecord((samplerV0_record_t *)record)) {
LogError("Failed to add legacy sampler record\n");
}
break;
}
Expand Down
5 changes: 4 additions & 1 deletion src/nfdump/nfdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,11 @@ static stat_record_t process_data(void *engine, char *wfile, int element_stat, i
case ExporterStatRecordType:
AddExporterStat((exporter_stats_record_t *)record_ptr);
break;
case SamplerLegacyRecordType: {
if (AddSamplerLegacyRecord((samplerV0_record_t *)record_ptr) == 0) LogError("Failed to add legacy Sampler Record\n");
} break;
case SamplerRecordType: {
int ret = AddSamplerInfo((sampler_record_t *)record_ptr);
int ret = AddSamplerRecord((sampler_record_t *)record_ptr);
if (ret != 0) {
if (write_file && ret == 1) AppendToBuffer(nffile_w, (void *)record_ptr, record_ptr->size);
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/nfsen/nfprofile.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ static void process_data(profile_channel_info_t *channels, unsigned int num_chan
LogError("Failed to add Exporter Record");
}
} break;
case SamplerLegacyRecordType: {
if (AddSamplerLegacyRecord((samplerV0_record_t *)record_ptr) == 0) LogError("Failed to add legacy Sampler Record\n");
} break;
case SamplerRecordType: {
int err = AddSamplerInfo((sampler_record_t *)record_ptr);
int err = AddSamplerRecord((sampler_record_t *)record_ptr);
if (err != 0) {
for (int j = 0; j < num_channels; j++) {
if (channels[j].nffile != NULL && err == 1) {
Expand Down

0 comments on commit eb5bef5

Please sign in to comment.