默认错误分配模型 (dbu_error)
本节介绍占用总线链路收发器管道第四阶段的默认模型的功能和实现。该模型名为 dbu_error。源代码位于 <reldir>/models/std/links 目录下的文件 dbu_error.ps.c 中。
调用上下文
在总线链路上传输的数据包可能受到比特错误的随机应用。比特错误和碰撞通常是总线链路上的两个传输失败源。收发器管道的第四阶段用于为每个传输的数据包分配错误。安装在此阶段的程序在数据包接收完成时调用(即数据包的最后一个比特被接收时)。仿真内核期望此阶段计算影响数据包的比特错误数,并将此整数值返回到数据包的 OPC_TDA_BU_NUM_ERRORS 传输数据属性中。
错误分配算法
dbu_error 是安装在总线链路收发器管道第四阶段的默认管道模型。比特错误的分配基于分配给总线链路的 ber 属性。给定此错误速率和数据包中的比特数,dbu_error 确定数据包中存在的比特错误总数。在解释用于评估此数量的机制之前,值得一提的是一种简单但低效的替代方法。
确定给定数据包中比特错误数量的最真实模型将模拟接收器的行为,接收器必须根据比特错误的概率决定每个比特的正确性。这样的模型将遍历数据包中的每个比特,并根据均匀分布随机数的值决定比特是正确的还是错误的。该基本算法如下图所示。
图 7:错误分配的一种可能实现
brute_force_err_alloc (num_bits, bit_err_rate)
int num_bits;
double bit_err_rate;
{
int err_accum;
/* compute the number of bit errors in a segment of num_bits length */
/* affected by a probability of bit error bit_err_rate */
FIN
/* reset accumulator for bit errors */
err_accum = 0;
/* test each bit in the segment */
for (i = 0; i < num_bits ; i++)
{
/* obtain a uniformly distributed random */
/* number between 0.0 and 1.0 */
r = random_number (0.0, 1.0);
/* compare the number against the probability of bit error */
/* to evaluate the correctness of the bit */
if (r < bit_err_rate)
{
/* augment the bit error accumulator */
err_accum++;
}
}
/* return the total number of bit errors */
FRET ( err_accum);
}
由于该算法测试每个比特,因此非常精确。此外,除了计算数据包中的比特错误总数外,它还能够提供有关受影响比特位置的信息。然而,显然执行该算法所需的时间随数据包长度线性增长。此外,即使对于比特错误率较低的系统,仍然必须测试每个比特。因此,该算法对于一大类建模系统而言效率不够高,因而不是合适的默认模型。
dbu_error 采用的方法是避免顺序遍历数据包中的所有比特,从而不生成关于比特错误的位置信息,但仍然准确计算比特错误的总数。这应该用最少的计算量完成,以利于仿真效率。
dbu_error 用于计算数据包中比特错误数量的算法基于在长度为 N 的段中恰好发生 k 个比特错误的概率表达式。这个概率表示为 Pk。不发生错误的概率对应于概率为 (1-p) 的 N 个连续事件,由以下表达式给出:

恰好发生一个错误的概率对应于段中比特的不同排列数量,如下所示:

一般来说:

dbu_error 用于计算数据包中比特错误数量的算法利用上述公式。算法首先生成一个介于 0 和 1 之间的随机数,用于测试不同数量比特错误发生的概率。然后算法开始迭代。首先根据上述公式计算发生 0 个比特错误的概率,并与随机数进行比较。如果随机数低于此概率,则数据包中的比特错误数为 0。否则,计算发生 1 个或更少比特错误的概率。如果随机数小于此概率但高于前一个比特错误数的发生概率,则分配给数据包的比特错误数为 1。算法继续以这种方式迭代,直到找到 k 的值,使得发生 k 个或更少错误的概率大于初始随机数。然后分配给数据包的错误数为 k。
错误模型实现
dbu_error 根据总线的比特错误率和数据包的长度确定数据包的比特错误数量。因此,错误模型必须首先检索比特错误率和数据包长度。比特错误率由总线链路的 ber 属性给出,在获取总线链路的对象 ID 后确定。数据包的比特长度(以比特为单位)使用 op_pk_total_size_get() 内核过程获取。
在进入错误分配算法主体之前,会区分两种特殊情况。第一种情况涉及将产生确定性数量错误的比特错误率(即 0.0 或 1.0 的速率)。如果比特错误率为 0.0,则直接分配错误数为 0,而不进入算法主体。同样,如果比特错误率为 1.0,则直接分配错误数为数据包中的比特数。
剩余的情况是最一般的情况,由上述迭代算法处理。该模型的源代码如下图所示。
图 8:管道过程 dbu_error 的源代码
/* dbu_error.ps.c */
/* Default error allocation model for bus link transceiver pipeline */
/**********************************/
/* Copyright (c) 2013 */
/* Riverbed Technology, Inc. */
/* All Rights Reserved. */
/**********************************/
#include "opnet.h"
#include <math.h>
/* Define a convenient macro for computing */
/* factorials using the gamma function. */
#define log_factorial(n) lgamma ((double) n + 1.0)
extern double lgamma (double);
#if defined (__cplusplus)
extern "C"
#endif
void
dbu_error (Packet * pkptr)
{
Objid link_objid;
double pe, r, p_accum, p_exact;
double log_p1, log_p2, log_arrange;
int invert_errors = OPC_FALSE;
OpT_Packet_Size seg_size, num_errs;
/** Compute the number of errors assigned to the given packet **/
/** based on its length and the bit error probability. **/
FIN (dbu_error (pkptr));
/* Obtain object id of bus link carrying transmission. */
link_objid = op_td_get_int (pkptr, OPC_TDA_BU_LINK_OBJID);
/* Obtain the bit error probability of the channel. */
if (op_ima_obj_attr_get (link_objid, "ber", &pe) == OPC_COMPCODE_FAILURE)
op_sim_end ("Error in bus error computation pipeline stage (dbu_error):",
"Unable to get bit error probability from bus attribute.",
OPC_NIL, OPC_NIL);
/* Obtain the length of the packet. */
seg_size = op_pk_total_size_get (pkptr);
/* Case 1: if the bit error rate is zero, so is the number of errors. */
if (pe == 0.0 || seg_size == 0)
num_errs = 0;
/* Case 2: if the bit error rate is 1.0, then all the bits are in error. */
/* (note however, that bit error rates should not normally exceed 0.5). */
else if (pe >= 1.0)
num_errs = seg_size;
/* Case 3: The bit-error rate is not zero or one. */
else
{
/* If the bit error rate is greater than 0.5 and less than 1.0, */
/* invert the problem to find instead the number of bits that are */
/* not in error in order to accelerate the performance of the */
/* algorithm. Set a flag to indicate that the result will then */
/* have to be inverted. */
if (pe > 0.5)
{
pe = 1.0 - pe;
invert_errors = OPC_TRUE;
}
/* The error count can be obtained by mapping a uniform random */
/* number in [0, 1[ via the inverse of the cumulative mass function */
/* (CMF) for the bit error count distribution. */
/* Obtain a uniform random number in [0, 1[ to represent */
/* the value of the CMF at the outcome that will be produced. */
r = op_dist_uniform (1.0);
/* Integrate probability mass over possible outcomes until r is */
/* exceeded. The loop iteratively corresponds to "inverting" the CMF */
/* because it finds the bit error count at which the CMF first meets */
/* or exceeds the value r. */
for (p_accum = 0.0, num_errs = 0; num_errs <= seg_size; num_errs++)
{
/* Compute the probability of exactly 'num_errs' */
/* bit errors occurring. */
/* The probability that the first 'num_errs' bits will be in */
/* error is given by pow (pe, num_errs). Here it is obtained */
/* in logarithmic form to avoid underflow for small 'pe' or large */
/* 'num_errs'. */
log_p1 = (double) num_errs * log (pe);
/* Similarly, obtain the probability that the remaining bits */
/* will not be in error. The combination of these two events */
/* represents one possible configuration of bits yielding a total */
/* of 'num_errs' errors. */
log_p2 = (double) (seg_size - num_errs) * log (1.0 - pe);
/* Compute the number of arrangements that are possible with the */
/* same number of bits in error as the particular case above. Again*/
/* obtain this number in logarithmic form (to avoid overflow in */
/* this case). This result is expressed as the logarithmic form of */
/* the formula for the number N of combinations of k items from n: */
/* N = n!/(n-k)!k! */
log_arrange = log_factorial (seg_size) -
log_factorial (num_errs) -
log_factorial (seg_size - num_errs);
/* Compute the probability that exactly 'num_errs' are present */
/* in the segment of bits, in any arrangement. */
p_exact = exp (log_arrange + log_p1 + log_p2);
/* Add this to the probability mass accumulated so far for */
/* previously tested outcomes to obtain the value of the CMF at */
/* outcome = num_errs. */
p_accum += p_exact;
/*'num_errs' is the outcome for this trial if the CMF meets */
/* or exceeds the uniform random value selected earlier. */
if (p_accum >= r)
break;
}
/* If the bit error rate was inverted to compute correct bits instead, */
/* then reinvert the result to obtain the number of bits in error. */
if (invert_errors == OPC_TRUE)
num_errs = seg_size - num_errs;
}
/* Set number of bit errors in packet transmission data attribute. */
/* Limit to 32-bit value. */
op_td_set_int (pkptr, OPC_TDA_BU_NUM_ERRORS, ((int)num_errs));
FOUT
}