Skip to content

Commit

Permalink
northd: Refactor meter code to avoid duplication.
Browse files Browse the repository at this point in the history
Use the band_cmp() function instead of duplicating code.  Also simplify
some of the band related code.

Suggested-by: Ales Musil <amusil@redhat.com>
Signed-off-by: Dumitru Ceara <dceara@redhat.com>
Acked-by: Mark Michelson <mmichels@redhat.com>
Acked-by: Ales Musil <amusil@redhat.com>
  • Loading branch information
dceara committed Aug 30, 2023
1 parent 484eaf5 commit 4138f7b
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions northd/northd.c
Original file line number Diff line number Diff line change
Expand Up @@ -16632,9 +16632,9 @@ band_cmp(const void *band1_, const void *band2_)
const struct band_entry *band2p = band2_;

if (band1p->rate != band2p->rate) {
return band1p->rate > band2p->rate ? -1 : 1;
return band1p->rate - band2p->rate;
} else if (band1p->burst_size != band2p->burst_size) {
return band1p->burst_size > band2p->burst_size ? -1 : 1;
return band1p->burst_size - band2p->burst_size;
} else {
return strcmp(band1p->action, band2p->action);
}
Expand All @@ -16648,17 +16648,6 @@ bands_need_update(const struct nbrec_meter *nb_meter,
return true;
}

/* A single band is the most common scenario, so speed up that
* check. */
if (nb_meter->n_bands == 1) {
struct nbrec_meter_band *nb_band = nb_meter->bands[0];
struct sbrec_meter_band *sb_band = sb_meter->bands[0];

return !(nb_band->rate == sb_band->rate
&& nb_band->burst_size == sb_band->burst_size
&& !strcmp(sb_band->action, nb_band->action));
}

/* Place the Northbound entries in sorted order. */
struct band_entry *nb_bands;
nb_bands = xmalloc(sizeof *nb_bands * nb_meter->n_bands);
Expand All @@ -16685,15 +16674,12 @@ bands_need_update(const struct nbrec_meter *nb_meter,

bool need_update = false;
for (size_t i = 0; i < nb_meter->n_bands; i++) {
if (nb_bands[i].rate != sb_bands[i].rate
|| nb_bands[i].burst_size != sb_bands[i].burst_size
|| strcmp(nb_bands[i].action, sb_bands[i].action)) {
if (band_cmp(&nb_bands[i], &sb_bands[i])) {
need_update = true;
goto done;
break;
}
}

done:
free(nb_bands);
free(sb_bands);

Expand Down

0 comments on commit 4138f7b

Please sign in to comment.