Skip to content

Commit f4a7651

Browse files
alvasMancodebot
authored andcommitted
support: created helper file for engineering pretty printing and moved common code there
1 parent d04ebf9 commit f4a7651

File tree

4 files changed

+134
-185
lines changed

4 files changed

+134
-185
lines changed

apps/services/metrics_log_helper.cpp

Lines changed: 5 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -16,92 +16,6 @@
1616

1717
using namespace srsran;
1818

19-
static std::string scaled_fmt_integer(uint64_t num)
20-
{
21-
static constexpr std::array<const char*, 8> suffixes = {"", "k", "M", "G", "T", "P", "E", "Z"};
22-
static const std::array<uint64_t, 8> max_nums = []() {
23-
std::array<uint64_t, 8> nums{0};
24-
for (unsigned i = 0, e = nums.size(); i != e; ++i) {
25-
nums[i] = (uint64_t)std::pow(10, i * 3);
26-
}
27-
return nums;
28-
}();
29-
30-
if (num < max_nums[1]) {
31-
return fmt::format("{}", num);
32-
}
33-
34-
for (unsigned i = 1, e = max_nums.size() - 1; i != e; ++i) {
35-
if (num < max_nums[i + 1]) {
36-
return fmt::format("{:.3g}{}", num / static_cast<double>(max_nums[i]), suffixes[i]);
37-
}
38-
}
39-
40-
return "Invalid number";
41-
}
42-
43-
static std::string float_to_string(float f, int digits, int field_width)
44-
{
45-
std::ostringstream os;
46-
int precision;
47-
48-
if (std::isnan(f) || std::abs(f) < 0.0001f) {
49-
f = 0.f;
50-
precision = digits - 1;
51-
} else {
52-
precision = digits - (int)(std::log10(std::abs(f + 0.0001f)) - 2 * DBL_EPSILON);
53-
}
54-
55-
precision = std::max(precision, 0);
56-
57-
os << std::fixed << std::setprecision(precision) << f;
58-
return os.str();
59-
}
60-
61-
static std::string float_to_eng_string(float f, int digits)
62-
{
63-
static char const* const prefixes[2][9] = {
64-
{
65-
"",
66-
"m",
67-
"u",
68-
"n",
69-
"p",
70-
"f",
71-
"a",
72-
"z",
73-
"y",
74-
},
75-
{
76-
"",
77-
"k",
78-
"M",
79-
"G",
80-
"T",
81-
"P",
82-
"E",
83-
"Z",
84-
"Y",
85-
},
86-
};
87-
88-
const int degree = (f == 0.f) ? 0 : std::lrint(std::floor(std::log10(std::abs(f)) / 3));
89-
90-
std::string factor;
91-
92-
if (std::abs(degree) < 9) {
93-
factor = prefixes[(degree < 0) ? 0 : 1][std::abs(degree)];
94-
} else {
95-
return "failed";
96-
}
97-
98-
const double scaled = f * std::pow(1000.0, -degree);
99-
if (degree != 0) {
100-
return float_to_string(scaled, digits, 5) + factor;
101-
}
102-
return float_to_string(scaled, digits, 5 - factor.length()) + factor;
103-
}
104-
10519
void metrics_log_helper::report_metrics(const scheduler_cell_metrics& metrics)
10620
{
10721
fmt::memory_buffer buffer;
@@ -135,7 +49,7 @@ void metrics_log_helper::report_metrics(const scheduler_cell_metrics& metrics)
13549

13650
fmt::format_to(buffer, " dl_mcs={}", int(ue.dl_mcs.to_uint()));
13751
if (ue.dl_brate_kbps > 0) {
138-
fmt::format_to(buffer, " dl_brate_kbps={}", float_to_eng_string(ue.dl_brate_kbps * 1e3, 1));
52+
fmt::format_to(buffer, " dl_brate_kbps={}", float_to_eng_string(ue.dl_brate_kbps * 1e3, 1, false));
13953
} else {
14054
fmt::format_to(buffer, " dl_brate_kbps={}", 0);
14155
}
@@ -147,7 +61,7 @@ void metrics_log_helper::report_metrics(const scheduler_cell_metrics& metrics)
14761
} else {
14862
fmt::format_to(buffer, " dl_error_rate={}%", 0);
14963
}
150-
fmt::format_to(buffer, " dl_bs={}", scaled_fmt_integer(ue.dl_bs));
64+
fmt::format_to(buffer, " dl_bs={}", scaled_fmt_integer(ue.dl_bs, /*right_align=*/false));
15165

15266
if (!std::isnan(ue.pusch_snr_db) && !iszero(ue.pusch_snr_db)) {
15367
fmt::format_to(buffer, " pusch_snr_db={:.1f}", std::clamp(ue.pusch_snr_db, -99.9f, 99.9f));
@@ -167,7 +81,7 @@ void metrics_log_helper::report_metrics(const scheduler_cell_metrics& metrics)
16781

16882
fmt::format_to(buffer, " ul_mcs={}", ue.ul_mcs.to_uint());
16983
if (ue.ul_brate_kbps > 0) {
170-
fmt::format_to(buffer, " ul_brate_kbps={}", float_to_eng_string(ue.ul_brate_kbps * 1e3, 1));
84+
fmt::format_to(buffer, " ul_brate_kbps={}", float_to_eng_string(ue.ul_brate_kbps * 1e3, 1, false));
17185
} else {
17286
fmt::format_to(buffer, " ul_brate_kbps={}", 0);
17387
}
@@ -180,9 +94,9 @@ void metrics_log_helper::report_metrics(const scheduler_cell_metrics& metrics)
18094
} else {
18195
fmt::format_to(buffer, " ul_error_rate={}%", 0);
18296
}
183-
fmt::format_to(buffer, " bsr={}", scaled_fmt_integer(ue.bsr));
97+
fmt::format_to(buffer, " bsr={}", scaled_fmt_integer(ue.bsr, /*right_align=*/false));
18498
if (ue.last_ta.has_value()) {
185-
fmt::format_to(buffer, " last_ta={}s", float_to_eng_string(ue.last_ta->to_seconds<float>(), 0));
99+
fmt::format_to(buffer, " last_ta={}s", float_to_eng_string(ue.last_ta->to_seconds<float>(), 0, false));
186100
} else {
187101
fmt::format_to(buffer, " last_ta=n/a");
188102
}

apps/services/metrics_plotter_stdout.cpp

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,14 @@
99
*/
1010

1111
#include "metrics_plotter_stdout.h"
12+
#include "srsran/support/engineering_notation.h"
1213
#include "srsran/support/math_utils.h"
1314
#include <cfloat>
1415
#include <iomanip>
1516
#include <ostream>
1617

1718
using namespace srsran;
1819

19-
static std::string scaled_fmt_integer(uint64_t num)
20-
{
21-
static constexpr std::array<const char*, 8> suffixes = {"", "k", "M", "G", "T", "P", "E", "Z"};
22-
static const std::array<uint64_t, 8> max_nums = []() {
23-
std::array<uint64_t, 8> nums{0};
24-
for (unsigned i = 0, e = nums.size(); i != e; ++i) {
25-
nums[i] = (uint64_t)std::pow(10, i * 3);
26-
}
27-
return nums;
28-
}();
29-
30-
if (num < max_nums[1]) {
31-
return fmt::format("{:>6}", num);
32-
}
33-
34-
for (unsigned i = 1, e = max_nums.size() - 1; i != e; ++i) {
35-
if (num < max_nums[i + 1]) {
36-
return fmt::format("{:>5.3g}{}", num / static_cast<double>(max_nums[i]), suffixes[i]);
37-
}
38-
}
39-
40-
return "Invalid number";
41-
}
42-
4320
static void print_header()
4421
{
4522
fmt::print("\n");
@@ -50,68 +27,6 @@ static void print_header()
5027
" ta phr\n");
5128
}
5229

53-
static std::string float_to_string(float f, int digits, int field_width)
54-
{
55-
std::ostringstream os;
56-
int precision;
57-
58-
if (std::isnan(f) || std::abs(f) < 0.0001f) {
59-
f = 0.f;
60-
precision = digits - 1;
61-
} else {
62-
precision = digits - (int)(std::log10(std::abs(f + 0.0001f)) - 2 * DBL_EPSILON);
63-
}
64-
65-
precision = std::max(precision, 0);
66-
67-
os << std::setw(field_width) << std::fixed << std::setprecision(precision) << f;
68-
return os.str();
69-
}
70-
71-
static std::string float_to_eng_string(float f, int digits)
72-
{
73-
static char const* const prefixes[2][9] = {
74-
{
75-
"",
76-
"m",
77-
"u",
78-
"n",
79-
"p",
80-
"f",
81-
"a",
82-
"z",
83-
"y",
84-
},
85-
{
86-
"",
87-
"k",
88-
"M",
89-
"G",
90-
"T",
91-
"P",
92-
"E",
93-
"Z",
94-
"Y",
95-
},
96-
};
97-
98-
const int degree = (f == 0.f) ? 0 : std::lrint(std::floor(std::log10(std::abs(f)) / 3));
99-
100-
std::string factor;
101-
102-
if (std::abs(degree) < 9) {
103-
factor = prefixes[(degree < 0) ? 0 : 1][std::abs(degree)];
104-
} else {
105-
return "failed";
106-
}
107-
108-
const double scaled = f * std::pow(1000.0, -degree);
109-
if (degree != 0) {
110-
return float_to_string(scaled, digits, 5) + factor;
111-
}
112-
return " " + float_to_string(scaled, digits, 5 - factor.length()) + factor;
113-
}
114-
11530
void metrics_plotter_stdout::report_metrics(const scheduler_cell_metrics& metrics)
11631
{
11732
if (!print_metrics) {
@@ -143,7 +58,7 @@ void metrics_plotter_stdout::report_metrics(const scheduler_cell_metrics& metric
14358

14459
fmt::print(" {:>2}", int(ue.dl_mcs.to_uint()));
14560
if (ue.dl_brate_kbps > 0) {
146-
fmt::print(" {:>6.6}", float_to_eng_string(ue.dl_brate_kbps * 1e3, 1));
61+
fmt::print(" {:>6.6}", float_to_eng_string(ue.dl_brate_kbps * 1e3, 1, true));
14762
} else {
14863
fmt::print(" {:>6}", 0);
14964
}
@@ -155,7 +70,7 @@ void metrics_plotter_stdout::report_metrics(const scheduler_cell_metrics& metric
15570
} else {
15671
fmt::print(" {:>3}%", 0);
15772
}
158-
fmt::print(" {}", scaled_fmt_integer(ue.dl_bs));
73+
fmt::print(" {}", scaled_fmt_integer(ue.dl_bs, /*align_right=*/true));
15974

16075
fmt::print(" |");
16176

@@ -177,7 +92,7 @@ void metrics_plotter_stdout::report_metrics(const scheduler_cell_metrics& metric
17792

17893
fmt::print(" {:>2}", ue.ul_mcs.to_uint());
17994
if (ue.ul_brate_kbps > 0) {
180-
fmt::print(" {:>6.6}", float_to_eng_string(ue.ul_brate_kbps * 1e3, 1));
95+
fmt::print(" {:>6.6}", float_to_eng_string(ue.ul_brate_kbps * 1e3, 1, true));
18196
} else {
18297
fmt::print(" {:>6}", 0);
18398
}
@@ -190,9 +105,9 @@ void metrics_plotter_stdout::report_metrics(const scheduler_cell_metrics& metric
190105
} else {
191106
fmt::print(" {:>3}%", 0);
192107
}
193-
fmt::print(" {}", scaled_fmt_integer(ue.bsr));
108+
fmt::print(" {}", scaled_fmt_integer(ue.bsr, /*align_right=*/true));
194109
if (ue.last_ta.has_value()) {
195-
fmt::print(" {}", float_to_eng_string(ue.last_ta->to_seconds<float>(), 0));
110+
fmt::print(" {}", float_to_eng_string(ue.last_ta->to_seconds<float>(), 0, true));
196111
} else {
197112
fmt::print(" n/a");
198113
}

include/srsran/rlc/rlc_tx_metrics.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#pragma once
1212

1313
#include "srsran/rlc/rlc_config.h"
14+
#include "srsran/support/engineering_notation.h"
1415
#include "srsran/support/format_utils.h"
1516
#include "fmt/format.h"
1617

@@ -81,11 +82,11 @@ inline std::string format_rlc_tx_metrics(timer_duration metrics_period, const rl
8182
{
8283
fmt::memory_buffer buffer;
8384
fmt::format_to(buffer,
84-
"period={}ms num_sdus={} sdu_rate={}kbps, dropped_sdus={} discarded_sdus={} "
85+
"period={}ms num_sdus={} sdu_rate={}bps, dropped_sdus={} discarded_sdus={} "
8586
"num_pdus_no_segm={} pdu_rate_no_segm={}kbps",
8687
metrics_period.count(),
87-
m.num_sdus,
88-
(double)m.num_sdu_bytes * 8 / (double)metrics_period.count(),
88+
scaled_fmt_integer(m.num_sdus, /*right_align=*/false),
89+
float_to_eng_string((float)m.num_sdu_bytes * 8 * 1000 / (metrics_period.count()), 1, false),
8990
m.num_dropped_sdus,
9091
m.num_discarded_sdus,
9192
m.num_pdus_no_segmentation,

0 commit comments

Comments
 (0)