---
title: "Linux嵌入式实时操作系统开发与设计（十一）"
author: "mmdev"
date: "2004-08-19T14:29:00Z"
tags: []
slug: "linux"
draft: false
lastmod: "2004-08-19T14:29:00Z"
id: 1009472
url: "https://mmdev.iteye.com/blog/1009472"
---

附录A &lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;

**中断延迟测试代码：**

这是一个补丁(patch)文件，在我的Linux-2.2.12版本下编译通过。通过下面的命令粘贴到Linux源文件中：

**# cd /usr/src/linux-2.2.12**

**# patch –p1 &lt; /usr/src/linux-2.2.12/interrupt-latency-2.2.12-patch**

粘贴完成后编译Linux内核：

**# cd /usr/src/linux-2.2.12**

**# make config # 配置内核**

**# make dep**

**# make bzImage # 编译内核**

**# make modules # 编译模块**

**# make modules\_install # 装载模块**

**# cp arch/i386/boot/bzImage /boot/interrupt-lattency**

配置LILO，编辑/etc/lilo.conf，把下面几行添加进：

**image=/boot/interrupt-latency**

**label=interrupt-lattency**

**read-only**

**root=/dev/hda1**

重新装LILO：

**# /sbin/lilo**

重启后在LILO提示符下输入：

**LILO: interrupt-latency**

则可以监控系统调用开/关中断的内核就起动完成，还需要运行一个用户空间程序(view.c, 见下面)用于输入命令和打印测试结果。

**interrupt-latency-2.2.12-patch文件如下：**

diff --exclude=version.h --exclude=config.h -Nru linux-2.2.12/arch/i386/kernel/Makefile linux-2.2.12-interrupt/arch/i386/kernel/Makefile

--- linux-2.2.12/arch/i386/kernel/Makefile Wed Jan 20 &lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;time hour="10" minute="18"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; LETTER-SPACING: -0.4pt; mso-bidi-font-family: 'Times New Roman'"&gt;10:18:53&lt;/span&gt;&lt;/time&gt; 1999

+++ linux-2.2.12-interrupt/arch/i386/kernel/Makefile Mon Mar 6 &lt;time hour="11" minute="4"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; LETTER-SPACING: -0.4pt; mso-bidi-font-family: 'Times New Roman'"&gt;11:04:25&lt;/span&gt;&lt;/time&gt; 2000

@@ -14,7 +14,9 @@

O\_TARGET := kernel.o

O\_OBJS := process.o signal.o entry.o traps.o irq.o vm86.o \\

\- ptrace.o ioport.o ldt.o setup.o time.o sys\_i386.o

\+ ptrace.o ioport.o ldt.o setup.o time.o sys\_i386.o \\

\+ intr\_blocking.o

\+

OX\_OBJS := i386\_ksyms.o

MX\_OBJS :=

diff --exclude=version.h --exclude=config.h -Nru linux-2.2.12/arch/i386/kernel/entry.S linux-2.2.12-interrupt/arch/i386/kernel/entry.S

--- linux-2.2.12/arch/i386/kernel/entry.S Fri Apr 30 &lt;time hour="8" minute="13"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; LETTER-SPACING: -0.4pt; mso-bidi-font-family: 'Times New Roman'"&gt;08:13:37&lt;/span&gt;&lt;/time&gt; 1999

+++ linux-2.2.12-interrupt/arch/i386/kernel/entry.S Mon Mar 6 &lt;time hour="11" minute="5"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; LETTER-SPACING: -0.4pt; mso-bidi-font-family: 'Times New Roman'"&gt;11:05:10&lt;/span&gt;&lt;/time&gt; 2000

@@ -96,6 +96,7 @@

movl %dx,%es;

#define RESTORE\_ALL \\

\+ incl intrData; \\

popl %ebx; \\

popl %ecx; \\

popl %edx; \\

@@ -562,6 +563,8 @@

.long SYMBOL\_NAME(sys\_ni\_syscall) /* streams1 \*/

.long SYMBOL\_NAME(sys\_ni\_syscall) /* streams2 \*/

.long SYMBOL\_NAME(sys\_vfork) /* 190 \*/

\+

\+ .long SYMBOL\_NAME(sys\_get\_intrData) /* 191 \*/

/*

\* NOTE!! This doesn't have to be exact - we just have

diff --exclude=version.h --exclude=config.h -Nru linux-2.2.12/arch/i386/kernel/head.S linux-2.2.12-interrupt/arch/i386/kernel/head.S

--- linux-2.2.12/arch/i386/kernel/head.S Thu Jan 14 &lt;time hour="22" minute="57"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; LETTER-SPACING: -0.4pt; mso-bidi-font-family: 'Times New Roman'"&gt;22:57:25&lt;/span&gt;&lt;/time&gt; 1999

+++ linux-2.2.12-interrupt/arch/i386/kernel/head.S Mon Mar 6 &lt;time hour="11" minute="14"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; LETTER-SPACING: -0.4pt; mso-bidi-font-family: 'Times New Roman'"&gt;11:14:26&lt;/span&gt;&lt;/time&gt; 2000

@@ -316,6 +316,7 @@

movl %ax,%es

pushl $int\_msg

call SYMBOL\_NAME(printk)

\+ incl intrData

popl %eax

popl %ds

popl %es

diff --exclude=version.h --exclude=config.h -Nru linux-2.2.12/arch/i386/kernel/intr\_blocking.c linux-2.2.12-interrupt/arch/i386/kernel/intr\_blocking.c

--- linux-2.2.12/arch/i386/kernel/intr\_blocking.c Wed Dec 31 &lt;time hour="16" minute="0"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; LETTER-SPACING: -0.4pt; mso-bidi-font-family: 'Times New Roman'"&gt;16:00:00&lt;/span&gt;&lt;/time&gt; 1969

+++ linux-2.2.12-interrupt/arch/i386/kernel/intr\_blocking.c Mon Mar 6 &lt;time hour="11" minute="41"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; LETTER-SPACING: -0.4pt; mso-bidi-font-family: 'Times New Roman'"&gt;11:41:50&lt;/span&gt;&lt;/time&gt; 2000

@@ -0,0 +1,322 @@

+#include &lt;asm/system.h&gt;

\+

\+

+/\*\*\** platform \*\*\*\*/

+#define readclock(low) \\

\+ \_\_asm\__ \_\_volatile\__ ("rdtsc" : "=a" (low) : : "edx")

\+

+/\*\*\** configure \*\*\*\*/

+#define NUM\_LOG\_ENTRY 4

+#define INTR\_IENABLE 0x200

\+

+/\*\*\** data structure \*\*\*\*/

+struct IntrData {

\+ /* count interrupt and iret \*/

\+ int breakCount;

\+

\+ /* the test name \*/

\+ const char * testName;

\+

\+ /* flag to control logging \*/

\+ unsigned logFlag; /* 0 - no logging; 1 - logging \*/

\+

\+ /* panic flag - set to 1 if something is realy wrong \*/

\+ unsigned panicFlag;

\+

\+ /* for synchro between start and end \*/

\+ unsigned syncFlag;

\+

\+ /* we only log interrupts within certain range \*/

\+ unsigned rangeLow;

\+ unsigned rangeHigh;

\+

\+ /* count the total number interrupts and intrs in range\*/

\+ unsigned numIntrs;

\+ unsigned numInRangeIntrs;

\+

\+

\+ /* error accounting \*/

\+ unsigned skipSti;

\+ unsigned skipCli;

\+ unsigned syncStiError;

\+ unsigned syncCliError;

\+ unsigned stiBreakError;

\+ unsigned restoreSti;

\+ unsigned restoreCli;

\+

\+ struct {

\+ /* worst blocking time \*/

\+ unsigned blockingTime;

\+

\+ const char * startFileName;

\+ unsigned startFileLine;

\+ unsigned startCount;

\+

\+ const char \*endFileName;

\+ unsigned endFileLine;

\+ unsigned endCount;

\+ } count\[NUM\_LOG\_ENTRY];

+};

\+

+struct IntrData intrData = {

\+ 0,

\+ "interrupt latency test (4 distinctive entries)",

\+ 0,

\+ 0,

\+ 0,

\+

\+ 1,

\+ 0xffffffff,

\+

\+ 0,

\+ 0,

\+

\+ 0,

\+ 0,

\+ 0,

\+ 0,

\+ 0,

\+ 0,

\+ 0

+};

\+

\+

+/\*\*\** functions \*\*\*\*/

+#if 0

+void intr\_check\_int(int x)

+{

\+

\+ unsigned flag;

\+ \_\_intr\_save\_flags(flag);

\+

\+ if ((flag & INTR\_IENABLE) != 0) {

\+ switch(x) {

\+ case 0 :

\+ intrData.count\[0].blockingTime ++;

\+ break;

\+ case 1 :

\+ intrData.count\[0].startFileLine ++;

\+ break;

\+ case 2 :

\+ intrData.count\[0].startCount ++;

\+ break;

\+ case 3 :

\+ intrData.count\[0].endFileLine ++;

\+ break;

\+ case 4 :

\+ intrData.count\[0].endCount ++;

\+ break;

\+ default :

\+ intrData.count\[0].startFileName = "Wrong check number";

\+ break;

\+ }

\+ } else {

\+ switch(x) {

\+ case 0 :

\+ intrData.count\[1].blockingTime ++;

\+ break;

\+ case 1 :

\+ intrData.count\[1].startFileLine ++;

\+ break;

\+ case 2 :

\+ intrData.count\[1].startCount ++;

\+ break;

\+ case 3 :

\+ intrData.count\[1].endFileLine ++;

\+ break;

\+ case 4 :

\+ intrData.count\[1].endCount ++;

\+ break;

\+ default :

\+ intrData.count\[1].startFileName = "Wrong check number";

\+ break;

\+ }

\+ }

+}

\+

+#endif

\+

\+

+static inline void intr\_SetPanic(unsigned x, const char \*fname, unsigned l)

+{

\+ if (intrData.panicFlag != 0) {

\+ /* double error; impossible \*/

\+ intrData.panicFlag = 99;

\+ return;

\+ }

\+ intrData.panicFlag = x;

\+ intrData.count\[0].startFileName = fname;

\+ intrData.count\[0].startFileLine = l;

+}

\+

+static const char \*intrStartFileName;

+static unsigned intrStartFileLine;

+static unsigned intrStartCount;

\+

+/* strategy :

\+ * if it is true "cli", i.e., clearing the IF, we remember

\+ * everything, and clear breakCount.

\+ \*/

+void intr\_cli(const char \*fname, unsigned lineno)

+{

\+ unsigned flag;

\+ \_\_intr\_save\_flags(flag);

\+

\+ \_\_intr\_cli();

\+

\+ /* if we are not logging or we have an error, do nothing \*/

\+ if ((intrData.logFlag == 0) || ( intrData.panicFlag != 0)) {

\+ return;

\+ }

\+

\+ /* do nothing we had IF cleared before we call this function \*/

\+ if ((flag & INTR\_IENABLE) == 0) {

\+ intrData.skipCli ++;

\+ return;

\+ }

\+

\+ /* debug \*/

\+ if (intrData.syncFlag == 1) {

\+ intrData.syncCliError ++;

\+ }

\+ intrData.syncFlag = 1;

\+

\+ intrData.breakCount = 0;

\+

\+ /* Read the Time Stamp Counter \*/

\+ intrStartFileName = fname;

\+ intrStartFileLine = lineno;

\+ readclock(intrStartCount);

+}

\+

\+

+/* strategy:

\+ * we do a count only if

\+ * 1. syncFlag is 1 (a valid cli() was called)

\+ * 2. breakCount is 0 (no iret is called between cli() and this sti()

\+ \*/

+void intr\_sti(const char \*fname, unsigned lineno)

+{

\+ unsigned flag;

\+ unsigned endCount;

\+ unsigned diff;

\+ int i;

\+

\+ \_\_intr\_save\_flags(flag);

\+

\+ /* if we are not logging or we have an error, do nothing \*/

\+ if ((intrData.logFlag == 0) || ( intrData.panicFlag != 0)) {

\+ \_\_intr\_sti();

\+ return;

\+ }

\+

\+

\+ /* check if this is a real sti() \*/

\+ if ((flag & INTR\_IENABLE) != 0) {

\+ intrData.skipSti ++;

\+ \_\_intr\_sti();

\+ return;

\+ }

\+

\+ /* check 1\*/

\+ if (intrData.syncFlag != 1) {

\+ intrData.syncStiError ++;

\+ \_\_intr\_sti();

\+ return;

\+ }

\+

\+ /* check 2 \*/

\+ if (intrData.breakCount != 0) {

\+ intrData.stiBreakError ++;

\+ \_\_intr\_sti();

\+ return;

\+ }

\+

\+ /* read count again \*/

\+ readclock(endCount);

\+

\+ intrData.syncFlag = 0;

\+

\+ diff = endCount - intrStartCount;

\+

\+ if ((diff &gt;= intrData.rangeLow) && (diff &lt;= intrData.rangeHigh)) {

\+ unsigned lowest=0xffffffff;

\+ unsigned lowestIndex;

\+ unsigned sameIndex = 0xffffffff;

\+

\+ intrData.numInRangeIntrs++;

\+

\+ /* check if we need to log this event \*/

\+ for (i=0; i&lt; NUM\_LOG\_ENTRY; i++) {

\+

\+ if (lowest &gt; intrData.count\[i].blockingTime) {

\+ lowest = intrData.count\[i].blockingTime;

\+ lowestIndex = i;

\+ }

\+

\+ if ( (lineno == intrData.count\[i].endFileLine) &&

\+ (intrStartFileLine == intrData.count\[i].startFileLine) &&

\+ (fname\[0] == intrData.count\[i].endFileName\[0]) &&

\+ (intrStartFileName\[0] == intrData.count\[i].startFileName\[0]) ) {

\+ /* if the line numbers are same, the first chars in

\+ * both file names are same, we consider it is the same

\+ * entry. \*/

\+ sameIndex = i;

\+ }

\+ }

\+

\+ if (sameIndex == 0xffffffff) {

\+ i = lowestIndex;

\+ } else {

\+ i = sameIndex;

\+ }

\+

\+ if (diff &gt; intrData.count\[i].blockingTime) {

\+ intrData.count\[i].blockingTime = diff;

\+ intrData.count\[i].endFileName = fname;

\+ intrData.count\[i].endFileLine = lineno;

\+ intrData.count\[i].endCount = endCount;

\+ intrData.count\[i].startFileName = intrStartFileName;

\+ intrData.count\[i].startFileLine = intrStartFileLine;

\+ intrData.count\[i].startCount = intrStartCount;

\+ }

\+ }

\+

\+ intrData.numIntrs++;

\+ \_\_intr\_sti();

+}

\+

\+

+void intr\_restore\_flags(const char \*fname, unsigned lineno, unsigned x)

+{

\+ unsigned flag;

\+

\+ /* if we are not logging or we have an error, do nothing \*/

\+ if ((intrData.logFlag == 0) || ( intrData.panicFlag != 0)) {

\+ \_\_intr\_restore\_flags(x);

\+ return;

\+ }

\+

\+ \_\_intr\_save\_flags(flag);

\+

\+ if (((flag & INTR\_IENABLE) == 0) &&

\+ ((x & INTR\_IENABLE) != 0) ) {

\+ intrData.restoreSti ++;

\+ intr\_sti(fname, lineno);

\+ }

\+

\+ if ( ((flag & INTR\_IENABLE) != 0) &&

\+ ((x & INTR\_IENABLE) == 0) ) {

\+ intrData.restoreCli ++;

\+ intr\_cli(fname, lineno);

\+ }

\+

\+ \_\_intr\_restore\_flags(x);

+}

\+

+#include &lt;asm/uaccess.h&gt;

\+

+asmlinkage int sys\_get\_intrData(void \** ptr)

+{

\+ return put\_user(&intrData, ptr);

+}

diff --exclude=version.h --exclude=config.h -Nru linux-2.2.12/include/asm-i386/system.h linux-2.2.12-interrupt/include/asm-i386/system.h

--- linux-2.2.12/include/asm-i386/system.h Mon Oct 11 &lt;time hour="21" minute="28"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; LETTER-SPACING: -0.4pt; mso-bidi-font-family: 'Times New Roman'"&gt;21:28:12&lt;/span&gt;&lt;/time&gt; 1999

+++ linux-2.2.12-interrupt/include/asm-i386/system.h Mon Mar 6 &lt;time hour="11" minute="8"&gt;&lt;span lang="EN-US" style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; LETTER-SPACING: -0.4pt; mso-bidi-font-family: 'Times New Roman'"&gt;11:08:02&lt;/span&gt;&lt;/time&gt; 2000

@@ -174,13 +174,33 @@

#define wmb() \_\_asm\__ \_\_volatile\__ ("": : :"memory")

/* interrupt control.. \*/

+#if 0

#define \_\_sti() \_\_asm\__ \_\_volatile\__ ("sti": : :"memory")

#define \_\_cli() \_\_asm\__ \_\_volatile\__ ("cli": : :"memory")

#define \_\_save\_flags(x) \\

\_\_asm\__ \_\_volatile\_\_("pushfl ; popl %0":"=g" (x): /* no input \*/ :"memory")

#define \_\_restore\_flags(x) \\

\_\_asm\__ \_\_volatile\_\_("pushl %0 ; popfl": /* no output \*/ :"g" (x):"memory")

+#endif

+#define \_\_intr\_sti() \_\_asm\__ \_\_volatile\__ ("sti": : :"memory")

+#define \_\_intr\_cli() \_\_asm\__ \_\_volatile\__ ("cli": : :"memory")

+#define \_\_intr\_save\_flags(x) \\

+\_\_asm\__ \_\_volatile\_\_("pushfl ; popl %0":"=g" (x): /* no input \*/ :"memory")

+#define \_\_intr\_restore\_flags(x) \\

+\_\_asm\__ \_\_volatile\_\_("pushl %0 ; popfl": /* no output \*/ :"g" (x):"memory")

\+

+/* jsun \*/

+extern void intr\_cli(const char \*, unsigned);

+extern void intr\_sti(const char \*, unsigned);

+extern void intr\_restore\_flags(const char \*, unsigned, unsigned);

+extern void intr\_sync\_flag(const char \*, unsigned lineno);

\+

+#define \_\_cli() intr\_cli(\_\_FILE\_\_, \_\_LINE\_\_)

+#define \_\_sti() intr\_sti(\_\_FILE\_\_, \_\_LINE\_\_)

+#define \_\_save\_flags(x) \\

+\_\_asm\__ \_\_volatile\_\_("pushfl ; popl %0":"=g" (x): /* no input \*/ :"memory")

+#define \_\_restore\_flags(x) intr\_restore\_flags(\_\_FILE\_\_, \_\_LINE\_\_, x)

#ifdef \_\_SMP\__

@@ -197,9 +217,8 @@

#define cli() \_\_cli()

#define sti() \_\_sti()

-#define save\_flags(x) \_\_save\_flags(x)

+#define save\_flags(x) \_\_save\_flags(x)

#define restore\_flags(x) \_\_restore\_flags(x)

\-

#endif

/*

**用户空间程序view.c如下：**

#include &lt;sys/types.h&gt;

#include &lt;sys/stat.h&gt;

#include &lt;unistd.h&gt;

#include &lt;fcntl.h&gt;

#include &lt;assert.h&gt;

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** CONFIG \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

#define