Skip to content

Commit

Permalink
Add some perfomance metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
shermp committed Aug 10, 2024
1 parent 3e37e0c commit fcbe712
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ option(ENABLE_HCI_DUMP "Enable HCI dump." OFF)
option(ENABLE_UAC2_LOG "Enable USB logging" OFF)
option(ENABLE_PAIR_DEL "Delete all HA pairings" OFF)
option(ENABLE_USB_SERIAL "Enable USB serial support" OFF)
option(ENABLE_PERF_METRICS "Enable Performance metrics" OFF)

if(ENABLE_BLE_LOG_AUDIO)
list(APPEND pico_asha_defines "ASHA_LOG_AUDIO")
Expand Down Expand Up @@ -83,6 +84,10 @@ else()
pico_enable_stdio_uart(pico-asha 1)
endif()

if(ENABLE_PERF_METRICS)
list(APPEND pico_asha_defines "ASHA_PERF_METRICS")
endif()

include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-Wdeprecated-enum-enum-conversion HAVE_DEP_ENNUM_ENUM_CONV)

Expand Down
38 changes: 34 additions & 4 deletions src/asha_bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#ifdef ASHA_HCI_DUMP
#include "hci_dump_embedded_stdout.h"
#endif
#ifdef ASHA_PERF_METRICS
#include "perf_metrics.hpp"
#endif

#include "asha_logging.h"
#include "asha_uuid.hpp"
Expand Down Expand Up @@ -195,16 +198,25 @@ extern "C" void bt_main()

uint8_t seq_num = 0u;
uint32_t write_index = 0u;

#ifdef ASHA_PERF_METRICS
uint32_t tmp_w_index = 0u;
#endif
//uint32_t audio_read_index = 0u;
using enum HA::State;
// Flush the first audio packet
/* Main audio streaming loop */
while (1) {
// No need to do anything if no hearing aids are connected!
if (ha_mgr.hearing_aids.size() == 0) continue;

if (ha_mgr.hearing_aids.empty()) continue;
#ifdef ASHA_PERF_METRICS
tmp_w_index = audio_buff.get_write_index();
if (tmp_w_index != write_index) {
perf_metrics.add_index_changed(get_absolute_time());
}
write_index = tmp_w_index;
#else
write_index = audio_buff.get_write_index();
#endif
AudioBuffer::Volume vol = audio_buff.get_volume();
for (auto& ha : ha_mgr.hearing_aids) {
LOG_AUDIO("%s: Audio Index: %u\n", ha.side_str, read_index);
Expand All @@ -224,6 +236,15 @@ extern "C" void bt_main()
}
}
}
#ifdef ASHA_PERF_METRICS
if (perf_metrics.should_dump_metrics()) {
// Disconnect HA's
scan_state = ScanState::DumpMetrics;
for (auto& ha : ha_mgr.hearing_aids) {
gap_disconnect(ha.conn_handle);
}
}
#endif
}
}

Expand Down Expand Up @@ -353,7 +374,7 @@ static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *pa
uint8_t reason = hci_event_disconnection_complete_get_reason(packet);
LOG_INFO("Received disconnection event.\n");
// Expected disconnection, reenable scanning
if (scan_state == ScanState::Disconnecting) {
if (scan_state == ScanState::Disconnecting || scan_state == ScanState::DumpMetrics) {
LOG_INFO("Expected disconnection\n");
} else {
LOG_ERROR("Disconnected with reason: %d\n", static_cast<int>(reason));
Expand All @@ -364,6 +385,15 @@ static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *pa
LOG_INFO("%s device disconnected.\n", (ha.side() == HA::Side::Left) ? "Left" : "Right");
ha_mgr.remove_by_conn_handle(c);
}
#ifdef ASHA_PERF_METRICS
if (scan_state == ScanState::DumpMetrics) {
if (ha_mgr.hearing_aids.empty()) {
LOG_INFO("Dumping perf metrics\n");
perf_metrics.dump_metrics();
}
break;
}
#endif
scan_state = ScanState::Scan;
curr_scan.reset();
break;
Expand Down
1 change: 1 addition & 0 deletions src/asha_bt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum class ScanState {
ReadPSM,
Finalizing,
Complete,
DumpMetrics,
};

constexpr uint16_t buff_size_sdu = 161;
Expand Down
19 changes: 19 additions & 0 deletions src/hearing_aid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

#include "util.hpp"

#ifdef ASHA_PERF_METRICS
#include "pico/time.h"
#include "perf_metrics.hpp"
#endif

namespace asha
{

Expand Down Expand Up @@ -276,6 +281,13 @@ void HA::on_can_send_audio_packet_now()
{
if (state != State::AudioPacketSending) return;
LOG_AUDIO("%s: Sending audio packet. Seq Num: %d\n", side_str, (int)audio_packet[0]);
#ifdef ASHA_PERF_METRICS
if (side() == Side::Left) {
perf_metrics.add_send_left(get_absolute_time());
} else {
perf_metrics.add_send_right(get_absolute_time());
}
#endif
state = State::AudioPacketSent;
l2cap_send(cid, audio_packet, sdu_size_bytes);
// if (res != ERROR_CODE_SUCCESS) {
Expand All @@ -288,6 +300,13 @@ void HA::on_can_send_audio_packet_now()
void HA::on_audio_packet_sent()
{
if (state == State::AudioPacketSent) {
#ifdef ASHA_PERF_METRICS
if (side() == Side::Left) {
perf_metrics.add_sent_left(get_absolute_time());
} else {
perf_metrics.add_sent_right(get_absolute_time());
}
#endif
state = State::AudioPacketReady;
LOG_AUDIO("%s: Sent audio packet. State: %d. Seq Num: %d\n", side_str, static_cast<int>(state), (int)audio_packet[0]);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
#include "asha_unique_id.hpp"
#include "asha_audio.hpp"

#ifdef ASHA_PERF_METRICS
#include "perf_metrics.hpp"
#endif

namespace asha
{
#ifdef ASHA_PERF_METRICS
#include "perf_metrics.hpp"
PerfMetrics perf_metrics;
#endif

AudioBuffer audio_buff;

Expand Down
62 changes: 62 additions & 0 deletions src/perf_metrics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#ifdef ASHA_PERF_METRICS
#include <vector>
#include "pico/time.h"
#include "stdio.h"

namespace asha
{

constexpr ssize_t metrics_vector_size = 1 * 1024;
struct PerfMetrics {
std::vector<absolute_time_t> index_changed;
std::vector<absolute_time_t> send_left;
std::vector<absolute_time_t> sent_left;
std::vector<absolute_time_t> send_right;
std::vector<absolute_time_t> sent_right;

PerfMetrics()
{
index_changed.reserve(metrics_vector_size);
send_left.reserve(metrics_vector_size);
sent_left.reserve(metrics_vector_size);
send_right.reserve(metrics_vector_size);
sent_right.reserve(metrics_vector_size);
}

void add_index_changed(absolute_time_t t) {if (index_changed.size() < metrics_vector_size) index_changed.push_back(t);}
void add_send_left(absolute_time_t t) {if (send_left.size() < metrics_vector_size) send_left.push_back(t);}
void add_sent_left(absolute_time_t t) {if (sent_left.size() < metrics_vector_size) sent_left.push_back(t);}
void add_send_right(absolute_time_t t) {if (send_right.size() < metrics_vector_size) send_right.push_back(t);}
void add_sent_right(absolute_time_t t) {if (sent_right.size() < metrics_vector_size) sent_right.push_back(t);}

bool should_dump_metrics()
{
return index_changed.size() >= metrics_vector_size; //||
send_left.size() >= metrics_vector_size ||
sent_left.size() >= metrics_vector_size ||
send_right.size() >= metrics_vector_size ||
sent_right.size() >= metrics_vector_size;
}
void dump_metrics()
{
// for (auto t : index_changed) {
// printf("%llu,", t);
// }
// printf("\n");
printf("index_changed,send_left,sent_left,send_right,sent_right\n");
for (size_t i = 0; i < metrics_vector_size; ++i) {
absolute_time_t index = (i < index_changed.size()) ? index_changed[i] : 0;
absolute_time_t send_left_packet = (i < send_left.size()) ? send_left[i] : 0;
absolute_time_t sent_left_packet = (i < sent_left.size()) ? sent_left[i] : 0;
absolute_time_t send_right_packet = (i < send_right.size()) ? send_right[i] : 0;
absolute_time_t sent_rightpacket = (i < sent_right.size()) ? sent_right[i] : 0;
printf("%llu,%llu,%llu,%llu,%llu\n", index, send_left_packet, sent_left_packet, send_right_packet, sent_rightpacket);
}
}
};

extern PerfMetrics perf_metrics;
} // namespace asha
#endif

0 comments on commit fcbe712

Please sign in to comment.