Skip to contentSkip to conversation

port of Lockmeter on i386 2.5.64 Patch

RFT Hanna Linder 4 participants 4 messages Mar 10, 2003

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

This is an early release of my port of the lockmeter tool written by John Hawkes ([email protected]). The patch is included below and available from sourceforge under the Linux Scalability Effort Project:
http://prdownloads.sourceforge.net/lse/lockmeter1.5-2.5.64-1.diff?download

Lockmeter will show you which functions are holding or spinning on which locks. This can be very useful in showing a particular lock is or is not a problem under certain workloads on SMP systems.

I know this patch is not complete but it works for i386. I will start porting the changes (mainly kconfig stuff and some minor locking changes) to the other architectures this week. If you see something wrong please let me know (patches gladly accepted ;).

You will also need the user tool lockstat to read the results. It can be found here:
ftp://oss.sgi.com/projects/lockmeter/download/lockstat-1.4.10.tar.gz

Hanna

-------

arch/i386/Kconfig +7 -0
diff -Nru -X dontdiff linux-2.5.64/arch/i386/Kconfig linux-lockmeter/arch/i386/Kconfig
@@ -1497,6 +1497,13 @@
 	  best used in conjunction with the NMI watchdog so that spinlock
 	  deadlocks are also debuggable.
 
+config LOCKMETER
+	bool "Kernel lock metering"
+	depends on SMP
+	help
+	  Say Y to enable kernel lock metering, which adds overhead to SMP locks, 
+	  but allows you to see various statistics using the lockstat command.
+
 config DEBUG_HIGHMEM
 	bool "Highmem debugging"
 	depends on DEBUG_KERNEL && HIGHMEM
arch/i386/lib/dec_and_lock.c +3 -0
diff -Nru -X dontdiff linux-2.5.64/arch/i386/lib/dec_and_lock.c linux-lockmeter/arch/i386/lib/dec_and_lock.c
@@ -10,6 +10,7 @@
 #include <linux/spinlock.h>
 #include <asm/atomic.h>
 
+#ifndef ATOMIC_DEC_AND_LOCK
 int atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock)
 {
 	int counter;
@@ -38,3 +39,5 @@
 	spin_unlock(lock);
 	return 0;
 }
+#endif
+
arch/sparc64/kernel/devices.c +4 -0
diff -Nru -X dontdiff linux-2.5.64/arch/sparc64/kernel/devices.c linux-lockmeter/arch/sparc64/kernel/devices.c
@@ -30,6 +30,8 @@
 extern void cpu_probe(void);
 extern void central_probe(void);
 
+unsigned long cpu_hz;
+
 void __init device_scan(void)
 {
 	char node_str[128];
@@ -68,6 +70,8 @@
 					prom_getproperty(scan, "portid",
 							 (char *) &thismid, sizeof(thismid));
 				}
+				if (!cpu_hz)
+					cpu_hz = prom_getint(scan, "clock-frequency");
 				linux_cpus[cpu_ctr].mid = thismid;
 				printk("Found CPU %d (node=%08x,mid=%d)\n",
 				       cpu_ctr, (unsigned) scan, thismid);
arch/sparc64/lib/rwlock.S +28 -0
diff -Nru -X dontdiff linux-2.5.64/arch/sparc64/lib/rwlock.S linux-lockmeter/arch/sparc64/lib/rwlock.S
@@ -63,5 +63,33 @@
 	be,pt		%icc, 99b
 	 membar		#StoreLoad | #StoreStore
 	ba,a,pt		%xcc, 1b
+ 
+	.globl	__read_trylock
+__read_trylock: /* %o0 = lock_ptr */
+	ldsw		[%o0], %g5
+	brlz,pn		%g5, 100f
+	 add		%g5, 1, %g7
+	cas		[%o0], %g5, %g7
+	cmp		%g5, %g7
+	bne,pn		%icc, __read_trylock
+	 membar		#StoreLoad | #StoreStore
+	retl
+	 mov		1, %o0
+
+	.globl		__write_trylock
+__write_trylock: /* %o0 = lock_ptr */
+	sethi		%hi(0x80000000), %g2
+1:	lduw		[%o0], %g5
+4:	brnz,pn		%g5, 100f
+	 or		%g5, %g2, %g7
+	cas		[%o0], %g5, %g7
+	cmp		%g5, %g7
+	bne,pn		%icc, 1b
+	 membar		#StoreLoad | #StoreStore
+	retl
+	 mov		1, %o0
+100:	retl
+	 mov		0, %o0
+
 rwlock_impl_end:
 
fs/proc/proc_misc.c +37 -0
diff -Nru -X dontdiff linux-2.5.64/fs/proc/proc_misc.c linux-lockmeter/fs/proc/proc_misc.c
@@ -535,6 +535,36 @@
 		entry->proc_fops = f;
 }
 
+#ifdef CONFIG_LOCKMETER
+extern ssize_t get_lockmeter_info(char *, size_t, loff_t *);
+extern ssize_t put_lockmeter_info(const char *, size_t);
+extern int get_lockmeter_info_size(void);
+
+/*
+ * This function accesses lock metering information. 
+ */
+static ssize_t read_lockmeter(struct file *file, char *buf,
+			      size_t count, loff_t *ppos)
+{
+	return get_lockmeter_info(buf, count, ppos);
+}
+
+/*
+ * Writing to /proc/lockmeter resets the counters
+ */
+static ssize_t write_lockmeter(struct file * file, const char * buf,
+			       size_t count, loff_t *ppos)
+{
+	return put_lockmeter_info(buf, count);
+}
+
+static struct file_operations proc_lockmeter_operations = {
+	NULL,           /* lseek */
+	read:		read_lockmeter,
+	write:		write_lockmeter,
+};
+#endif  /* CONFIG_LOCKMETER */
+
 void __init proc_misc_init(void)
 {
 	struct proc_dir_entry *entry;
@@ -596,6 +626,13 @@
 			entry->size = (1+prof_len) * sizeof(unsigned int);
 		}
 	}
+#ifdef CONFIG_LOCKMETER
+	entry = create_proc_entry("lockmeter", S_IWUSR | S_IRUGO, NULL);
+	if (entry) {
+		entry->proc_fops = &proc_lockmeter_operations;
+		entry->size = get_lockmeter_info_size();
+	}
+#endif
 #ifdef CONFIG_PPC32
 	{
 		extern struct file_operations ppc_htab_operations;
include/asm-alpha/lockmeter.h +90 -0

90 lines not on this page · raw message

include/asm-alpha/spinlock.h +50 -0

69 lines not on this page · raw message

include/asm-i386/lockmeter.h +127 -0

127 lines not on this page · raw message

include/asm-i386/spinlock.h +67 -0

88 lines not on this page · raw message

include/asm-ia64/lockmeter.h +72 -0

72 lines not on this page · raw message

include/asm-ia64/spinlock.h +73 -0

91 lines not on this page · raw message

include/asm-mips/lockmeter.h +126 -0

126 lines not on this page · raw message

include/asm-mips/spinlock.h +9 -0
diff -Nru -X dontdiff linux-2.5.64/include/asm-mips/spinlock.h linux-lockmeter/include/asm-mips/spinlock.h
@@ -74,9 +74,18 @@
 
 typedef struct {
 	volatile unsigned int lock;
+#if CONFIG_LOCKMETER
+	/* required for LOCKMETER since all bits in lock are used */
+	/* and we need this storage for CPU and lock INDEX        */
+	unsigned lockmeter_magic;
+#endif
 } rwlock_t;
 
+#ifdef CONFIG_LOCKMETER
+#define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0 }
+#else
 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
+#endif
 
 static inline void read_lock(rwlock_t *rw)
 {
include/asm-mips64/lockmeter.h +120 -0

120 lines not on this page · raw message

include/asm-mips64/pgtable.h +1 -1
diff -Nru -X dontdiff linux-2.5.64/include/asm-mips64/pgtable.h linux-lockmeter/include/asm-mips64/pgtable.h
@@ -129,7 +129,7 @@
 #define USER_PTRS_PER_PGD	(TASK_SIZE/PGDIR_SIZE)
 #define FIRST_USER_PGD_NR	0
 
-#define KPTBL_PAGE_ORDER  1
+#define KPTBL_PAGE_ORDER  2
 #define VMALLOC_START     XKSEG
 #define VMALLOC_VMADDR(x) ((unsigned long)(x))
 #define VMALLOC_END       \
include/asm-sparc64/lockmeter.h +47 -0

47 lines not on this page · raw message

include/asm-sparc64/spinlock.h +28 -8
diff -Nru -X dontdiff linux-2.5.64/include/asm-sparc64/spinlock.h linux-lockmeter/include/asm-sparc64/spinlock.h
@@ -30,15 +30,23 @@
 
 #ifndef CONFIG_DEBUG_SPINLOCK
 
-typedef unsigned char spinlock_t;
-#define SPIN_LOCK_UNLOCKED	0
+typedef struct {
+	unsigned char lock;
+	unsigned int  index;
+} spinlock_t;
 
-#define spin_lock_init(lock)	(*((unsigned char *)(lock)) = 0)
-#define spin_is_locked(lock)	(*((volatile unsigned char *)(lock)) != 0)
+#ifdef CONFIG_LOCKMETER
+#define SPIN_LOCK_UNLOCKED	(spinlock_t) {0, 0}
+#else
+#define SPIN_LOCK_UNLOCKED	(spinlock_t) { 0 }
+#endif
+
+#define spin_lock_init(__lock)	do { *(__lock) = SPIN_LOCK_UNLOCKED; } while(0)
+#define spin_is_locked(__lock)	(*((volatile unsigned char *)(&((__lock)->lock))) != 0)
 
-#define spin_unlock_wait(lock)	\
+#define spin_unlock_wait(__lock)	\
 do {	membar("#LoadLoad");	\
-} while(*((volatile unsigned char *)lock))
+} while(*((volatile unsigned char *)(&(((spinlock_t *)__lock)->lock))))
 
 static __inline__ void _raw_spin_lock(spinlock_t *lock)
 {
@@ -109,8 +117,20 @@
 
 #ifndef CONFIG_DEBUG_SPINLOCK
 
-typedef unsigned int rwlock_t;
-#define RW_LOCK_UNLOCKED	0
+#ifdef CONFIG_LOCKMETER
+typedef struct {
+	unsigned int lock;
+	unsigned int index;
+	unsigned int cpu;
+} rwlock_t;
+#define RW_LOCK_UNLOCKED       (rwlock_t) { 0, 0, 0xff }
+#else
+typedef struct {
+	unsigned int lock;
+} rwlock_t;
+#define RW_LOCK_UNLOCKED        (rwlock_t) { 0 }
+#endif
+
 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
 #define rwlock_is_locked(x) (*(x) != RW_LOCK_UNLOCKED)
 
include/linux/lockmeter.h +320 -0

320 lines not on this page · raw message

include/linux/spinlock.h +47 -7

107 lines not on this page · raw message

kernel/Makefile +1 -0
diff -Nru -X dontdiff linux-2.5.64/kernel/Makefile linux-lockmeter/kernel/Makefile
@@ -10,6 +10,7 @@
 
 obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o
 obj-$(CONFIG_SMP) += cpu.o
+obj-$(CONFIG_LOCKMETER) += lockmeter.o
 obj-$(CONFIG_UID16) += uid16.o
 obj-$(CONFIG_MODULES) += ksyms.o module.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
kernel/ksyms.c +10 -0
diff -Nru -X dontdiff linux-2.5.64/kernel/ksyms.c linux-lockmeter/kernel/ksyms.c
@@ -615,6 +615,16 @@
 EXPORT_SYMBOL(__per_cpu_offset);
 #endif
 
+#if defined(CONFIG_LOCKMETER)
+EXPORT_SYMBOL(_metered_spin_lock);
+EXPORT_SYMBOL(_metered_spin_unlock);
+EXPORT_SYMBOL(_metered_spin_trylock);
+EXPORT_SYMBOL(_metered_read_lock);
+EXPORT_SYMBOL(_metered_read_unlock);
+EXPORT_SYMBOL(_metered_write_lock);
+EXPORT_SYMBOL(_metered_write_unlock);
+#endif
+
 /* debug */
 EXPORT_SYMBOL(dump_stack);
 EXPORT_SYMBOL(ptrace_notify);
kernel/lockmeter.c +1088 -0

1,088 lines not on this page · raw message

#1 lore raw

include/linux/lockmeter.h line 47

Quoted 12 lines · Hanna Linder
include/linux/lockmeter.h +12 -0
+/*
+ * Currently, the mips64 and sparc64 kernels talk to a 32-bit lockstat, so we
+ * need to force compatibility in the inter-communication data structure.
+ */
+
+#if defined(CONFIG_MIPS32_COMPAT)
+#define TIME_T		uint32_t
+#elif defined(CONFIG_SPARC32_COMPAT)
+#define TIME_T		uint64_t
+#else
+#define TIME_T		time_t
+#endif

Hmm, is there a way to inherit this from arch code?

include/linux/lockmeter.h line 53

Quoted 5 lines · Hanna Linder
include/linux/lockmeter.h +5 -0
+#if defined(__KERNEL__) || (!defined(CONFIG_MIPS32_COMPAT) && !defined(CONFIG_SPARC32_COMPAT)) || (_MIPS_SZLONG==32)
+#define POINTER		void *
+#else
+#define	POINTER		int64_t
+#endif

What on earth? Shouldn't this be done some other way?

I really think 32-bit compatibility should be mostly ignored; userspace should just be rearranged to understand it's looking at a 64-bit kernel even when running in a 32-bit userspace.

include/linux/lockmeter.h line 185

Quoted 7 lines · Hanna Linder
include/linux/lockmeter.h +7 -0
+#ifdef USER_MODE_TESTING
+	rwlock_t    entry_lock;           /* lock for this read lock entry... */
+	                                  /* avoid having more than one rdr at*/
+	                                  /* needed for user space testing... */
+	                                  /* not needed for kernel 'cause it  */
+					  /* is non-preemptive. ............. */
+#endif

Hmm, USER_MODE_TESTING can probably just go.

include/linux/spinlock.h line 334

Quoted 7 lines · Hanna Linder

#define spin_unlock_irqrestore(lock, flags) \
do { \

include/linux/spinlock.h +1 -1
-	_raw_spin_unlock(lock); \
+	spin_unlock(lock); \
 	local_irq_restore(flags); \
 	preempt_enable(); \
 } while (0)

Erm, is this safe? there's a reason _raw_spin_unlock() is used here, for the CONFIG_PREEMPT stuff basically. It'd look less disturbing if the old names and semantics were preserved.

kernel/lockmeter.c line 881

Quoted 3 lines · Hanna Linder
kernel/lockmeter.c +3 -0
+#ifdef DEBUG_LOCKMETER
+		printk("put_lockmeter_info(cpu=%d): LSTAT_ON\n",THIS_CPU_NUMBER);
+#endif

DEBUG_LOCKMETER should probably go away for merging.

All in all I'm glad to have this back. It'd be a valuable addition to CONFIG_PROFILING once things are lined up with Linux conventions. Despite a little bit of crustiness inherited from before ppl were used to Linux conventions it's still pretty smooth.

-- wli

#2 lore raw
In reply to Hanna Linder

arch/i386/Kconfig line 1502

Quoted 3 lines · Hanna Linder
arch/i386/Kconfig +3 -0
+config LOCKMETER
+	bool "Kernel lock metering"
+	depends on SMP

Should that be 'depends on SMP || PREEMPT'? I remember the original 2.4 pre-empt patches had one to deal with instrumenting/breaking locks. Not sure if that logic still applies...

#3 lore raw
Quoted 7 lines · On Mon, 2003-03-10 at 00:33, [email protected] wrote:

arch/i386/Kconfig line 1502

Quoted 3 lines
arch/i386/Kconfig +3 -0
+config LOCKMETER
+	bool "Kernel lock metering"
+	depends on SMP

Should that be 'depends on SMP || PREEMPT'? I remember the original 2.4 pre-empt patches had one to deal with instrumenting/breaking locks. Not sure if that logic still applies...

No, it depends on SMP alone.

To do the metering, it needs the actual locks compiled into the kernel.

I believe what you are thinking of is the preempt-stats patch, which measures periods of non-preemptibility. Same idea but different.

Robert Love