dahdi_tools 分析(三)dahdi_test

dahdi_tools 分析(三)dahdi_test

dahdi_test 的使用

# dahdi_test -h
Usage: dahdi_test [-c COUNT] [-v]
    Valid options are:
  -c COUNT    Run just COUNT cycles (otherwise: forever).
  -v          More verbose output.
  -h          This help text.

示例:

# dahdi_test 
Opened pseudo dahdi interface, measuring accuracy...
97.888% 97.881% 97.891% 97.884% 97.883% 97.878% 97.895% 97.884% 
97.878% ^C
--- Results after 9 passes ---
Best: 97.895% -- Worst: 97.878% -- Average: 97.884602%
Cummulative Accuracy (not per pass): 97.885

代码分析

static double calculate_accuracy(double count, double ms)
{
    
    
	return ((count - _fmin(count, fabs(count - ms))) / count) * 100.0;
}

int main(int argc, char *argv[])
{
    
    
	int fd;
	int res;
	int c;
	int count = 0;
	int seconds = 0;
	int curarg = 1;
	char buf[8192];
	float ms;
	struct timeval start, now;
	fd = open("/dev/dahdi/pseudo", O_RDWR);   // 打开/dev/dahdi/pseudo
	if (fd < 0) {
    
    
		fprintf(stderr, "Unable to open dahdi interface: %s\n", strerror(errno));
		exit(1);
	}
	
	for (;;) {
    
    
		if (count == 0)
			ms = 0;
		gettimeofday(&start, NULL);          // 计时开始
		res = read(fd, buf, sizeof(buf));    // 读取 8192 字节数据
		if (res < 0) {
    
    
			fprintf(stderr, "Failed to read from pseudo interface: %s\n", strerror(errno));
			exit(1);
		}
		count += res;
		gettimeofday(&now, NULL);           // 计时结束
		ms += (now.tv_sec - start.tv_sec) * 8000;
		ms += (now.tv_usec - start.tv_usec) / 125.0;
		if (count >= SIZE) {
    
    
			const double percent = calculate_accuracy(count, ms);
			if (verbose) {
    
    
				printf("\n%d samples in %0.3f system clock sample intervals (%.3f%%)", 
						count, ms, percent);
			} else if (pass > 0 && (pass % 8) == 0) {
    
    
				printf("\n");
			}
			if (percent > best)
				best = percent;
			if (percent < worst)
				worst = percent;
			if (!verbose)
				printf("%.3f%% ", percent);
			total += percent;
			fflush(stdout);
			total_count += count;
			total_time += ms;
			count = 0;
			pass++;
		}
	}

代码大意是,每次读取从 /dev/dahdi/pseudo 中读取 8192 字节数据,并记录花了多长时间假设 x 秒,然后计算 x 时间里真正产生的数据 y = 采样率 8000 * x 。

计算 8192 / y 的百分比值。比值越接近于 100% 说明,系统性能越好。

思考

1.为什么不能达到100% ?

个人理解是 read 的过程还涉及系统调用,系统调用花费额外的时间,所以再强大的cpu 也不可能达到100%。

猜你喜欢

转载自blog.csdn.net/agave7/article/details/107341046