Agora  1.2.0
Agora project
mac_thread_client.h
Go to the documentation of this file.
1 
5 #ifndef MAC_THREAD_H_
6 #define MAC_THREAD_H_
7 
8 #include <queue>
9 
10 #include "concurrentqueue.h"
11 #include "config.h"
12 #include "crc.h"
13 #include "gettime.h"
14 #include "message.h"
15 #include "ran_config.h"
16 #include "symbols.h"
17 #include "udp_comm.h"
18 #include "udp_server.h"
19 
29  public:
30  // Default log file for MAC layer outputs
31  static constexpr char kDefaultLogFilename[] = "data/mac_log_client";
32 
33  // Maximum number of outstanding UDP packets per UE that we allocate recv()
34  // buffer space for
35  static constexpr size_t kMaxPktsPerUE = 64;
36 
37  // Length of SNR moving average window
38  // TODO: map this to time?
39  static constexpr size_t kSNRWindowSize = 100;
40 
42  Config* const cfg, size_t core_offset,
44  Table<int8_t>* ul_bits_buffer, Table<int8_t>* ul_bits_buffer_status,
47  const std::string& log_filename = "");
48 
50 
51  // The main MAC thread event loop. It receives uplink data bits from the
52  // master thread and sends them to remote applications.
53  void RunEventLoop();
54 
55  private:
56  // Receive events from Agora PHY master thread. Forwards
57  // to appropriate function in MAC.
58  void ProcessRxFromPhy();
59 
60  // Receive decoded codeblocks from the PHY master thread. Send
61  // fully-received frames for UE #i to kRemoteHostname::(kBaseRemotePort + i)
63 
64  // Receive SNR report from PHY master thread. Use for RB scheduling.
65  // TODO: process CQI report here as well.
67 
68  // At client, process control information received from control
69  // channel and forward to PHY UE, so it transmits data in the scheduled
70  // time slots.
72 
73  // Receive user data bits (downlink bits at the MAC thread running at the
74  // server, uplink bits at the MAC thread running at the client) and forward
75  // them to the PHY.
77  void ProcessUdpPacketsFromAppsClient(const char* payload, RBIndicator ri);
78 
79  // If Mode::kServer, this thread is running at the Agora server. Else at
80  // the client.
81  // const Mode mode_;
82  Config* const cfg_;
83 
84  const double freq_ghz_; // RDTSC frequency in GHz
85  // We check for new MAC packets from applications every [tsc_delta_]
86  // clock ticks
87  const size_t tsc_delta_;
88 
89  const size_t core_offset_; // The CPU core on which this thread runs
90 
91  FILE* log_file_; // Log file used to store MAC layer outputs
92  std::string log_filename_;
93 
94  // UDP endpoint used for sending messages
95  std::unique_ptr<UDPComm> udp_comm_;
96 
97  // UDP endpoint for receiving control channel messages
98  std::unique_ptr<UDPServer> udp_control_channel_;
99 
100  // A preallocated buffer to store UDP packets received via recv()
101  std::vector<std::byte> udp_pkt_buf_;
102 
103  // A preallocated buffer to store UDP control information
104  // received via recv()
105  std::vector<std::byte> udp_control_buf_;
106 
107  // The timestamp at which we last received a UDP packet from an application
109 
110  // The frame ID of the next MAC packet we'll hand over to the PHY
111  size_t next_tx_frame_id_ = 0;
112 
113  // The radio ID of the next MAC packet we'll hand over to the PHY
114  size_t next_radio_id_ = 0;
115 
116  // The frame ID of the next TTI that the scheduler plans for
118 
120 
121  // Server-only members
122  struct {
123  // Staging buffers to accumulate decoded uplink code blocks for each UE
124  std::array<std::vector<std::byte>, kMaxUEs> frame_data_;
125 
126  // n_filled_in_frame_[i] is the number of bytes received in the current
127  // frame for UE #i
128  std::array<size_t, kMaxUEs> n_filled_in_frame_;
129 
130  // snr_[i] contains a moving window of SNR measurement for UE #i
131  std::array<std::queue<float>, kMaxUEs> snr_;
132 
133  // Placing at the end because it is variable size based on configuration
134  std::vector<std::vector<size_t>> data_size_;
135  } server_;
136 
137  // TODO: decoded_buffer_ is used by only the server, so it should be moved
138  // to server_ for clarity.
140 
141  struct {
142  // ul_bits_buffer_id_[i] is the index of the uplink data bits buffer to
143  // next use for radio #i
144  std::array<size_t, kMaxUEs> ul_bits_buffer_id_;
145 
148  } client_;
149 
150  // FIFO queue for receiving messages from the master thread
152 
153  // FIFO queue for sending messages to the master thread
155 
156  // CRC
157  std::unique_ptr<DoCRC> crc_obj_;
158 };
159 
160 #endif // MAC_THREAD_H_
MacThreadClient::fast_rand_
FastRand fast_rand_
Definition: mac_thread_client.h:119
udp_comm.h
Declaration file for the UDPComm class. This class is used to send messages and receive messages from...
kMacRemoteHostname
static constexpr char kMacRemoteHostname[]
Definition: symbols.h:324
FrameStats::GetDLSymbolIdx
size_t GetDLSymbolIdx(size_t symbol_number) const
Definition: framestats.cc:152
Config::Running
void Running(bool value)
Definition: config.h:33
Config::UeAntNum
size_t UeAntNum() const
Definition: config.h:41
PinToCoreWithOffset
void PinToCoreWithOffset(ThreadType thread_type, size_t core_offset, size_t thread_id, bool allow_reuse, bool verbose)
Definition: utils.cc:157
MacThreadClient::ul_bits_buffer_id_
std::array< size_t, kMaxUEs > ul_bits_buffer_id_
Definition: mac_thread_client.h:144
MacThreadClient::ProcessRxFromPhy
void ProcessRxFromPhy()
Definition: mac_thread_client.cc:87
Config::MacPacketsPerframe
size_t MacPacketsPerframe(Direction dir) const
Definition: config.h:276
MacThreadClient::core_offset_
const size_t core_offset_
Definition: mac_thread_client.h:89
MacThreadClient::decoded_buffer_
PtrCube< kFrameWnd, kMaxSymbols, kMaxUEs, int8_t > & decoded_buffer_
Definition: mac_thread_client.h:139
gen_tag_t::symbol_id_
uint16_t symbol_id_
Definition: message.h:33
kFrameWnd
static constexpr size_t kFrameWnd
Definition: symbols.h:18
EventType::kPacketFromMac
@ kPacketFromMac
MacPacketPacked::Ue
uint16_t Ue() const
Definition: message.h:280
MacThreadClient::MacThreadClient
MacThreadClient(Config *const cfg, size_t core_offset, PtrCube< kFrameWnd, kMaxSymbols, kMaxUEs, int8_t > &decoded_buffer, Table< int8_t > *ul_bits_buffer, Table< int8_t > *ul_bits_buffer_status, moodycamel::ConcurrentQueue< EventData > *rx_queue, moodycamel::ConcurrentQueue< EventData > *tx_queue, const std::string &log_filename="")
Definition: mac_thread_client.cc:14
MacThreadClient::kMaxPktsPerUE
static constexpr size_t kMaxPktsPerUE
Definition: mac_thread_client.h:35
MacThreadClient::server_
struct MacThreadClient::@9 server_
kUdpRxBufferPadding
static constexpr size_t kUdpRxBufferPadding
Definition: mac_thread_client.cc:12
MacThreadClient::freq_ghz_
const double freq_ghz_
Definition: mac_thread_client.h:84
RBIndicator
The packet that contains the control information (DCI) that tells each UE which uplink resource block...
Definition: ran_config.h:30
FrameStats::GetULSymbolIdx
size_t GetULSymbolIdx(size_t symbol_number) const
Definition: framestats.cc:156
crc.h
MacThreadClient::ProcessUdpPacketsFromAppsClient
void ProcessUdpPacketsFromAppsClient(const char *payload, RBIndicator ri)
Definition: mac_thread_client.cc:378
Config::MacDataBytesNumPerframe
size_t MacDataBytesNumPerframe(Direction dir) const
Definition: config.h:259
udp_server.h
Provides the UDPServer functions from the UDPComm class. Receiver only support.
fclose
fclose(fileID)
MacThreadClient
The MAC thread that runs alongside the PHY processing at the Agora server or client.
Definition: mac_thread_client.h:28
MacThreadClient::snr_
std::array< std::queue< float >, kMaxUEs > snr_
Definition: mac_thread_client.h:131
EventData::tags_
std::array< size_t, kMaxTags > tags_
Definition: message.h:146
AGORA_LOG_ERROR
#define AGORA_LOG_ERROR(...)
Definition: logger.h:39
Direction::kUplink
@ kUplink
Config::UeMacRxPort
size_t UeMacRxPort() const
Definition: config.h:323
Config::Frame
const FrameStats & Frame() const
Definition: config.h:340
AGORA_LOG_TRACE
#define AGORA_LOG_TRACE(...)
Definition: logger.h:92
gen_tag_t::ue_id_
uint16_t ue_id_
Definition: message.h:37
EventData::event_type_
EventType event_type_
Definition: message.h:144
Table< int8_t >
snr
snr
Definition: inspect_agora_results.m:118
gen_tag_t
Definition: message.h:22
MacPacketPacked::kHeaderSize
static constexpr size_t kHeaderSize
Definition: message.h:276
EventType::kSNRReport
@ kSNRReport
MacThreadClient::udp_comm_
std::unique_ptr< UDPComm > udp_comm_
Definition: mac_thread_client.h:95
EventType::kPacketToMac
@ kPacketToMac
MacThreadClient::frame_data_
std::array< std::vector< std::byte >, kMaxUEs > frame_data_
Definition: mac_thread_client.h:124
MacThreadClient::next_radio_id_
size_t next_radio_id_
Definition: mac_thread_client.h:114
EventData
Definition: message.h:142
MacThreadClient::scheduler_next_frame_id_
size_t scheduler_next_frame_id_
Definition: mac_thread_client.h:117
message.h
Self defined functions for message storage and passing.
MacThreadClient::log_file_
FILE * log_file_
Definition: mac_thread_client.h:91
MacThreadClient::RunEventLoop
void RunEventLoop()
Definition: mac_thread_client.cc:523
MacPacketHeaderPacked
Definition: message.h:244
MacThreadClient::rx_queue_
moodycamel::ConcurrentQueue< EventData > * rx_queue_
Definition: mac_thread_client.h:151
MacPacketPacked::Set
void Set(const uint16_t &f, const uint16_t &s, const uint16_t &u, const uint16_t &data_size)
Definition: message.h:286
FrameStats::ClientUlPilotSymbols
size_t ClientUlPilotSymbols() const
Definition: framestats.h:73
MacThreadClient::ProcessCodeblocksFromPhy
void ProcessCodeblocksFromPhy(EventData event)
Definition: mac_thread_client.cc:113
i
for i
Definition: generate_data.m:107
GetTime
Definition: gettime.h:11
Config::MacPayloadMaxLength
size_t MacPayloadMaxLength(Direction dir) const
Definition: config.h:272
FrameStats::NumDlDataSyms
size_t NumDlDataSyms() const
Definition: framestats.h:83
moodycamel::ConcurrentQueue::enqueue
bool enqueue(T const &item)
Definition: concurrentqueue.h:974
MacThreadClient::ProcessUdpPacketsFromApps
void ProcessUdpPacketsFromApps(RBIndicator ri)
Definition: mac_thread_client.cc:276
u
Plot Rx waveform for u
Definition: inspect_single_frame.m:108
fmt::v8::fprintf
auto fprintf(std::FILE *f, const S &fmt, const T &... args) -> int
Definition: printf.h:607
ThreadType::kWorkerMacTXRX
@ kWorkerMacTXRX
Direction::kDownlink
@ kDownlink
Config::GetFrameDurationSec
double GetFrameDurationSec() const
Return the frame duration in seconds.
Definition: config.h:464
MacThreadClient::udp_control_channel_
std::unique_ptr< UDPServer > udp_control_channel_
Definition: mac_thread_client.h:98
Config::FramesToTest
size_t FramesToTest() const
Definition: config.h:309
FrameStats::GetDLSymbol
size_t GetDLSymbol(size_t location) const
Definition: framestats.cc:106
MacThreadClient::ul_bits_buffer_status_
Table< int8_t > * ul_bits_buffer_status_
Definition: mac_thread_client.h:147
MacThreadClient::last_mac_pkt_rx_tsc_
size_t last_mac_pkt_rx_tsc_
Definition: mac_thread_client.h:108
MacThreadClient::tx_queue_
moodycamel::ConcurrentQueue< EventData > * tx_queue_
Definition: mac_thread_client.h:154
Config::MacPacketLength
size_t MacPacketLength(Direction dir) const
Definition: config.h:268
MacThreadClient::log_filename_
std::string log_filename_
Definition: mac_thread_client.h:92
symbols.h
ran_config.h
FrameStats::ClientDlPilotSymbols
size_t ClientDlPilotSymbols() const
Definition: framestats.h:76
MacThreadClient::kDefaultLogFilename
static constexpr char kDefaultLogFilename[]
Definition: mac_thread_client.h:31
Config::UeMacTxPort
size_t UeMacTxPort() const
Definition: config.h:324
moodycamel::ConcurrentQueue< EventData >
Config::UeAntTotal
size_t UeAntTotal() const
Definition: config.h:43
MacThreadClient::ProcessControlInformation
void ProcessControlInformation()
Definition: mac_thread_client.cc:258
GetTime::MeasureRdtscFreq
static double MeasureRdtscFreq()
Definition: gettime.h:51
MacThreadClient::cfg_
Config *const cfg_
Definition: mac_thread_client.h:82
MacThreadClient::ProcessSnrReportFromPhy
void ProcessSnrReportFromPhy(EventData event)
Definition: mac_thread_client.cc:102
MacThreadClient::n_filled_in_frame_
std::array< size_t, kMaxUEs > n_filled_in_frame_
Definition: mac_thread_client.h:128
AGORA_LOG_SYMBOL
#define AGORA_LOG_SYMBOL(...)
Definition: logger.h:82
MacThreadClient::ul_bits_buffer_
Table< int8_t > * ul_bits_buffer_
Definition: mac_thread_client.h:146
MacThreadClient::data_size_
std::vector< std::vector< size_t > > data_size_
Definition: mac_thread_client.h:134
MacThreadClient::next_tx_frame_id_
size_t next_tx_frame_id_
Definition: mac_thread_client.h:111
Config
Definition: config.h:26
FastRand
Definition: utils.h:179
AGORA_LOG_INFO
#define AGORA_LOG_INFO(...)
Definition: logger.h:62
FrameStats::GetDLSymbolLast
size_t GetDLSymbolLast() const
Definition: framestats.h:37
MacThreadClient::client_
struct MacThreadClient::@10 client_
AGORA_LOG_FRAME
#define AGORA_LOG_FRAME(...)
Definition: logger.h:72
MacPacketPacked::PayloadLength
uint16_t PayloadLength() const
Definition: message.h:282
RtAssert
static void RtAssert(bool condition, const char *throw_str)
Definition: utils.h:104
MacThreadClient::tsc_delta_
const size_t tsc_delta_
Definition: mac_thread_client.h:87
kLogMacPackets
static constexpr bool kLogMacPackets
Definition: symbols.h:211
to_string
std::string to_string() const
Definition: eth_common.h:64
MacThreadClient::udp_pkt_buf_
std::vector< std::byte > udp_pkt_buf_
Definition: mac_thread_client.h:101
config.h
Declaration file for the configuration class which importants json configuration values into class va...
moodycamel::ConcurrentQueue::try_dequeue
bool try_dequeue(U &item)
Definition: concurrentqueue.h:1104
MacPacketPacked
Definition: message.h:274
MacThreadClient::~MacThreadClient
~MacThreadClient()
Definition: mac_thread_client.cc:82
PtrCube< kFrameWnd, kMaxSymbols, kMaxUEs, int8_t >
gettime.h
kMaxUEs
static constexpr size_t kMaxUEs
Definition: symbols.h:289
MacThreadClient::kSNRWindowSize
static constexpr size_t kSNRWindowSize
Definition: mac_thread_client.h:39
gen_tag_t::frame_id_
uint32_t frame_id_
Definition: message.h:32
rx_mac_tag_t
Definition: message.h:303
mac_thread_client.h
concurrentqueue.h
kMacBaseClientPort
static constexpr size_t kMacBaseClientPort
Definition: symbols.h:337
Config::UeServerAddr
std::string UeServerAddr() const
Definition: config.h:298
utils_ldpc.h
MacThreadClient::crc_obj_
std::unique_ptr< DoCRC > crc_obj_
Definition: mac_thread_client.h:157
MacThreadClient::udp_control_buf_
std::vector< std::byte > udp_control_buf_
Definition: mac_thread_client.h:105