November 01, 2008

簡易測量 X round-trip time

前文 [非同步 Xlib 處理機制] 探討了鮮為人知的 X Window System 的非同步設計議題。無論 XOrg 實做上引入哪些新機制,基本上,典型的運作模式大致為,在 reply-receiving 函式呼叫前,client 端會預先保存 reply data,接著,server 端施行對於 client 要求而生的同步動作,而控制權從 server 重回到 client 的這段期間,稱為 "round-trip" time,可視作作繪圖指令 / 操作的往返。本文則提出一個簡易測量 X round-trip time 的途徑。

X protocol 的設計相當洗練且豐富,在 X client 這端,我們可透過 Xlib 中的 XNoOp 函式,對 protocol 傳送 "NoOperation" 的 request,一旦經過 X transport 處理後,即可得知大概一來一往 round-trip time,以下是簡單的實做: [xttl.c]
/* Measuring X round-trip time.
 * Copyright (c) 2008 Jim Huang <jserv.tw@gmail.com>, MIT X License
 *
 * Build:
 *   gcc -o xttl xttl.c -Wall `pkg-config --cflags --libs x11`
 */
#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <X11/Xlib.h>
#include <sys/time.h>

#define TRY_TIMES (10)
#define INTERVAL (1)
int main()
{
	Display *disp;
	struct timeval start = { 0UL, 0UL }, end = { 0UL, 0UL };
	char *disp_name;
	int ret = 0;
	int count = 0;

	disp_name = XDisplayName(NULL);
	if ((disp = XOpenDisplay(NULL)) == NULL) {
		errx(1, "cannot open display %s", disp_name);
	}

	printf("X server version is %i.%i\n", 
	       ProtocolVersion(disp), ProtocolRevision(disp));
	printf("X Vendor string: %s\n", ServerVendor(disp));
	printf("X Vendor release number: %i\n", XVendorRelease(disp));

	while (count++ < TRY_TIMES) {
		ret = gettimeofday(&start, NULL);
		if (ret < 0) {
			err(1, "gettimeofday");
		}

		/* The XNoOp function sends a NoOperation protocol request
		 * to the X server, thereby exercising the connection. */
		if (XNoOp(disp)) {
			struct timeval diff = { 0UL, 0UL };
			if ((ret = gettimeofday(&end, NULL)) < 0)
				err(1, "gettimeofday");

			if (end.tv_sec < start.tv_sec &&

			    end.tv_usec < start.tv_usec) {
				break;
			}

			diff.tv_sec = end.tv_sec - start.tv_sec;
			diff.tv_usec = end.tv_usec - start.tv_usec;

			printf(" reply from display %s: XNoOP_seq=%i time=%lu.%lu ms\n",
			       disp_name, count,
			       (diff.tv_sec * 1000 * INTERVAL), diff.tv_usec);
		}
		sleep(INTERVAL);
	}
	XCloseDisplay(disp);

	return 0;
}
參考輸出如下:
$ ./xttl 
X server version is 11.0
X Vendor string: The X.Org Foundation
X Vendor release number: 10502000
 reply from display :0: XNoOP_seq=1 time=0.4 ms
 reply from display :0: XNoOP_seq=2 time=0.5 ms
 reply from display :0: XNoOP_seq=3 time=0.6 ms
 reply from display :0: XNoOP_seq=4 time=0.7 ms
 reply from display :0: XNoOP_seq=5 time=0.9 ms
 reply from display :0: XNoOP_seq=6 time=0.5 ms
 reply from display :0: XNoOP_seq=7 time=0.6 ms
 reply from display :0: XNoOP_seq=8 time=0.5 ms
 reply from display :0: XNoOP_seq=9 time=0.6 ms
 reply from display :0: XNoOP_seq=10 time=0.5 ms

由 jserv 發表於 November 1, 2008 06:39 PM
迴響
發表迴響









記住我的資訊?