Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 277d796e authored by Michael Sun's avatar Michael Sun Committed by Mingguang Xu
Browse files

btaa: attribute wakeup and wakelock duration over processed HCI buckets

Attach recorded BT wakeup to the first post-wakeup parsed HCI packet.
Distribute wakelock duration over bucketed HCI info based on
transmitted byte counts.

Tag: #feature
Bug: 177232907
BUG: 177228387
Test: verified locally BTAA aggregator updated with BT activities
BYPASS_LONG_LINES_REASON: consist with gd format

Change-Id: Idf93afcf13af9e76b4bc6f6ef30cf8424d8b4982
parent 6fb1e4fc
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include <android/binder_manager.h>

#include "btaa/attribution_processor.h"
#include "btaa/hci_processor.h"
#include "btaa/wakelock_processor.h"
#include "module.h"
#include "os/log.h"
@@ -100,7 +101,9 @@ struct ActivityAttribution::impl {
    }
  }

  void on_hci_packet(hal::HciPacket packet, hal::SnoopLogger::PacketType type, uint16_t length) {}
  void on_hci_packet(hal::HciPacket packet, hal::SnoopLogger::PacketType type, uint16_t length) {
    attribution_processor_.OnBtaaPackets(std::move(hci_processor_.OnHciPacket(std::move(packet), type, length)));
  }

  void on_wakelock_acquired() {
    wakelock_processor_.OnWakelockAcquired();
@@ -125,6 +128,7 @@ struct ActivityAttribution::impl {

  ActivityAttributionCallback* callback_;
  AttributionProcessor attribution_processor_;
  HciProcessor hci_processor_;
  WakelockProcessor wakelock_processor_;
};

+23 −0
Original line number Diff line number Diff line
@@ -17,17 +17,40 @@
#pragma once

#include <cstdint>
#include <unordered_map>

#include "hci_processor.h"

namespace bluetooth {
namespace activity_attribution {

struct AddressActivityKey {
  hci::Address address;
  Activity activity;

  bool operator==(const AddressActivityKey& other) const {
    return (address == other.address && activity == other.activity);
  }
};

struct AddressActivityKeyHasher {
  std::size_t operator()(const AddressActivityKey& key) const {
    return (
        (std::hash<std::string>()(key.address.ToString()) ^
         (std::hash<unsigned char>()(static_cast<unsigned char>(key.activity)))));
  }
};

class AttributionProcessor {
 public:
  void OnBtaaPackets(std::vector<BtaaHciPacket> btaa_packets);
  void OnWakelockReleased(uint32_t duration_ms);
  void OnWakeup();

 private:
  bool wakeup_ = false;
  std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> btaa_aggregator_;
  std::unordered_map<AddressActivityKey, BtaaAggregationEntry, AddressActivityKeyHasher> wakelock_duration_aggregator_;
};

}  // namespace activity_attribution
+44 −1
Original line number Diff line number Diff line
@@ -21,7 +21,50 @@
namespace bluetooth {
namespace activity_attribution {

void AttributionProcessor::OnWakelockReleased(uint32_t duration_ms) {}
void AttributionProcessor::OnBtaaPackets(std::vector<BtaaHciPacket> btaa_packets) {
  AddressActivityKey key;

  for (auto& btaa_packet : btaa_packets) {
    key.address = btaa_packet.address;
    key.activity = btaa_packet.activity;

    if (wakelock_duration_aggregator_.find(key) == wakelock_duration_aggregator_.end()) {
      wakelock_duration_aggregator_[key] = {};
    }
    wakelock_duration_aggregator_[key].byte_count += btaa_packet.byte_count;

    if (wakeup_) {
      wakelock_duration_aggregator_[key].wakeup_count += 1;
    }
  }
  wakeup_ = false;
}

void AttributionProcessor::OnWakelockReleased(uint32_t duration_ms) {
  uint32_t total_byte_count = 0;
  uint32_t ms_per_byte = 0;

  for (auto& it : wakelock_duration_aggregator_) {
    total_byte_count += it.second.byte_count;
  }

  if (total_byte_count == 0) {
    return;
  }

  ms_per_byte = duration_ms / total_byte_count;
  for (auto& it : wakelock_duration_aggregator_) {
    it.second.wakelock_duration = ms_per_byte * it.second.byte_count;
    if (btaa_aggregator_.find(it.first) == btaa_aggregator_.end()) {
      btaa_aggregator_[it.first] = {};
    }

    btaa_aggregator_[it.first].wakeup_count += it.second.wakeup_count;
    btaa_aggregator_[it.first].byte_count += it.second.byte_count;
    btaa_aggregator_[it.first].wakelock_duration += it.second.wakelock_duration;
  }
  wakelock_duration_aggregator_.clear();
}

void AttributionProcessor::OnWakeup() {
  if (wakeup_) {