Skip to contentSkip to conversation

Performance Stats: Kernel patch

PATCH Maxim Uvarov 5 participants 16 messages Apr 9, 2007 – Apr 16, 2007

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

Hello collogues,

Patches make available to the user the following new per-process (thread) performance statistics:

* Involuntary Context Switches
* Voluntary Context Switches
* Number of system calls

This data is useful for detecting hyperactivity patterns between processes.

I have attached 3 patches perf_stat.patch, perf_stat_x86_64.patch and perf_stat_ppc.patch for linux-2.6.21-rc5 and corrected all notes which were before. Do you have any objections? I need your opinion again.

Best regards,
Maxim Uvarov.

Patch adds Process Performance Statistics.

 arch/i386/kernel/asm-offsets.c |    3 +++
 arch/i386/kernel/entry.S       |    6 ++++++
 fs/proc/array.c                |   17 +++++++++++++++++
 include/asm-i386/thread_info.h |    5 +++--
 kernel/fork.c                  |    4 ++++
 lib/Kconfig.debug              |   15 +++++++++++++++
 6 files changed, 48 insertions(+), 2 deletions(-)
fs/proc/array.c +17 -0
Index: linux-2.6.21-rc5/fs/proc/array.c
===================================================================
@@ -291,6 +291,20 @@ static inline char *task_cap(struct task
 			    cap_t(p->cap_effective));
 }
 
+#ifdef CONFIG_THREAD_PERF_STAT
+static inline char *task_perf(struct task_struct *p, char *buffer)
+{
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       buffer += sprintf(buffer, "Syscalls:\t%lu\n", p->thread_info->sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
+       return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"
+                           "Nivcsw:\t%lu\n",
+                           p->nvcsw,
+                           p->nivcsw);
+}
+#endif /* CONFIG_THREAD_PERF_STAT */
+
 int proc_pid_status(struct task_struct *task, char * buffer)
 {
 	char * orig = buffer;
@@ -309,6 +323,9 @@ int proc_pid_status(struct task_struct *
 #if defined(CONFIG_S390)
 	buffer = task_show_regs(task, buffer);
 #endif
+#ifdef CONFIG_THREAD_PERF_STAT
+	buffer = task_perf(task, buffer);
+#endif /* CONFIG_THREAD_PERF_STAT */
 	return buffer - orig;
 }
 
kernel/fork.c +4 -0
Index: linux-2.6.21-rc5/kernel/fork.c
===================================================================
@@ -1044,6 +1044,10 @@ static struct task_struct *copy_process(
 	p->syscr = 0;		/* I/O counter: read syscalls */
 	p->syscw = 0;		/* I/O counter: write syscalls */
 #endif
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        p->thread_info->sysc_cnt = 0;   /* Syscall counter: total numbers of syscalls */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
 	task_io_accounting_init(p);
 	acct_clear_integrals(p);
 
lib/Kconfig.debug +15 -0
Index: linux-2.6.21-rc5/lib/Kconfig.debug
===================================================================
@@ -446,3 +446,18 @@ config FAULT_INJECTION_STACKTRACE_FILTER
 	select FRAME_POINTER
 	help
 	  Provide stacktrace filter for fault-injection capabilities
+
+config THREAD_PERF_STAT
+       bool "Per-process (thread) performance statistics"
+       help
+         Make available to the user the following per-process (thread) performance statistics:
+            * Number of involuntary context switches
+            * Number of voluntary context switches
+            * Number of system calls (optional)
+         This information is available via /proc/PID/status.
+
+config THREAD_PERF_STAT_SYSC
+       bool "Enable syscall counter"
+       depends on THREAD_PERF_STAT && X86_32
+       help
+         This option adds a syscall counter to /proc/PID/status.
arch/i386/kernel/entry.S +6 -0
Index: linux-2.6.21-rc5/arch/i386/kernel/entry.S
===================================================================
@@ -342,6 +342,9 @@ sysenter_past_esp:
 	jae syscall_badsys
 	call *sys_call_table(,%eax,4)
 	movl %eax,PT_EAX(%esp)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        incl    TI_sysc_cnt(%ebp)       # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	DISABLE_INTERRUPTS(CLBR_ECX|CLBR_EDX)
 	TRACE_IRQS_OFF
 	movl TI_flags(%ebp), %ecx
@@ -385,6 +388,9 @@ syscall_call:
 	call *sys_call_table(,%eax,4)
 	movl %eax,PT_EAX(%esp)		# store the return value
 syscall_exit:
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        incl    TI_sysc_cnt(%ebp)       # Increment syscalls counter 
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	DISABLE_INTERRUPTS(CLBR_ANY)	# make sure we don't miss an interrupt
 					# setting need_resched or sigpending
 					# between sampling and the iret
arch/i386/kernel/asm-offsets.c +3 -0
Index: linux-2.6.21-rc5/arch/i386/kernel/asm-offsets.c
===================================================================
@@ -56,6 +56,9 @@ void foo(void)
 	OFFSET(TI_addr_limit, thread_info, addr_limit);
 	OFFSET(TI_restart_block, thread_info, restart_block);
 	OFFSET(TI_sysenter_return, thread_info, sysenter_return);
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        OFFSET(TI_sysc_cnt, thread_info, sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	BLANK();
 
 	OFFSET(GDS_size, Xgt_desc_struct, size);
include/asm-i386/thread_info.h +3 -2
Index: linux-2.6.21-rc5/include/asm-i386/thread_info.h
===================================================================
@@ -31,8 +31,9 @@ struct thread_info {
 	unsigned long		status;		/* thread-synchronous flags */
 	__u32			cpu;		/* current CPU */
 	int			preempt_count;	/* 0 => preemptable, <0 => BUG */
-
-
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long		sysc_cnt;	/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	mm_segment_t		addr_limit;	/* thread address space:
 					 	   0-0xBFFFFFFF for user-thead
 						   0-0xFFFFFFFF for kernel-thread
lib/Kconfig.debug +1 -1
Index: linux-2.6.21-rc5/lib/Kconfig.debug
===================================================================
@@ -458,6 +458,6 @@ config THREAD_PERF_STAT
 
 config THREAD_PERF_STAT_SYSC
        bool "Enable syscall counter"
-       depends on THREAD_PERF_STAT && X86_32
+       depends on THREAD_PERF_STAT && X86
        help
          This option adds a syscall counter to /proc/PID/status.
include/asm-x86_64/thread_info.h +3 -1
Index: linux-2.6.21-rc5/include/asm-x86_64/thread_info.h
===================================================================
@@ -30,7 +30,9 @@ struct thread_info {
 	__u32			status;		/* thread synchronous flags */
 	__u32			cpu;		/* current CPU */
 	int 			preempt_count;	/* 0 => preemptable, <0 => BUG */
-
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long		sysc_cnt;	/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	mm_segment_t		addr_limit;	
 	struct restart_block    restart_block;
 };
arch/x86_64/kernel/entry.S +9 -0
Index: linux-2.6.21-rc5/arch/x86_64/kernel/entry.S
===================================================================
@@ -236,6 +236,11 @@ ENTRY(system_call)
 	movq %r10,%rcx
 	call *sys_call_table(,%rax,8)  # XXX:	 rip relative
 	movq %rax,RAX-ARGOFFSET(%rsp)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	GET_THREAD_INFO(%rcx)
+	addq $1, threadinfo_sysc_cnt(%rcx)   # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
 /*
  * Syscall return path ending with SYSRET (fast path)
  * Has incomplete stack frame and undefined top of stack. 
@@ -319,6 +324,10 @@ tracesys:			 
 	call *sys_call_table(,%rax,8)
 1:	movq %rax,RAX-ARGOFFSET(%rsp)
 	/* Use IRET because user could have changed frame */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	GET_THREAD_INFO(%rcx)
+	addq $1, threadinfo_sysc_cnt(%rcx)   # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 		
 /* 
  * Syscall return path ending with IRET.
arch/x86_64/kernel/asm-offsets.c +3 -0
Index: linux-2.6.21-rc5/arch/x86_64/kernel/asm-offsets.c
===================================================================
@@ -35,6 +35,9 @@ int main(void)
 	ENTRY(addr_limit);
 	ENTRY(preempt_count);
 	ENTRY(status);
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC 
+	ENTRY(sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	BLANK();
 #undef ENTRY
 #define ENTRY(entry) DEFINE(pda_ ## entry, offsetof(struct x8664_pda, entry))
include/asm-powerpc/thread_info.h +3 -0
Index: linux-2.6.21-rc5/include/asm-powerpc/thread_info.h
===================================================================
@@ -35,6 +35,9 @@ struct thread_info {
 	int		cpu;			/* cpu we're on */
 	int		preempt_count;		/* 0 => preemptable,
 						   <0 => BUG */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long	sysc_cnt;		/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	struct restart_block restart_block;
 	unsigned long	local_flags;		/* private flags for thread */
 
arch/powerpc/kernel/asm-offsets.c +4 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/asm-offsets.c
===================================================================
@@ -94,6 +94,10 @@ int main(void)
 	DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
 	DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
 	DEFINE(TI_TASK, offsetof(struct thread_info, task));
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        DEFINE(TI_SYSC_CNT, offsetof(struct thread_info, sysc_cnt)); 
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+		
 #ifdef CONFIG_PPC32
 	DEFINE(TI_EXECDOMAIN, offsetof(struct thread_info, exec_domain));
 	DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
arch/powerpc/kernel/entry_32.S +5 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/entry_32.S
===================================================================
@@ -223,6 +223,11 @@ ret_from_syscall:
 #endif
 	mr	r6,r3
 	rlwinm	r12,r1,0,0,(31-THREAD_SHIFT)	/* current_thread_info() */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	lwz r9,TI_SYSC_CNT(r12)
+	addi r9,r9,1
+	stw r9,TI_SYSC_CNT(r12)
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	/* disable interrupts so current_thread_info()->flags can't change */
 	LOAD_MSR_KERNEL(r10,MSR_KERNEL)	/* doesn't include MSR_EE */
 	SYNC
lib/Kconfig.debug +1 -1
Index: linux-2.6.21-rc5/lib/Kconfig.debug
===================================================================
@@ -458,6 +458,6 @@ config THREAD_PERF_STAT
 
 config THREAD_PERF_STAT_SYSC
        bool "Enable syscall counter"
-       depends on THREAD_PERF_STAT && X86
+       depends on THREAD_PERF_STAT && (X86 || PPC32)
        help
          This option adds a syscall counter to /proc/PID/status.
#1 lore raw

arch/x86_64/kernel/entry.S ENTRY(system_call) · line 243

Quoted 11 lines · On Mon, 09 Apr 2007 18:22:22 +0400 Maxim Uvarov <[email protected]> wrote:
arch/x86_64/kernel/entry.S +5 -0
@@ -236,6 +236,11 @@ ENTRY(system_call)
 	movq %r10,%rcx
 	call *sys_call_table(,%rax,8)  # XXX:	 rip relative
 	movq %rax,RAX-ARGOFFSET(%rsp)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	GET_THREAD_INFO(%rcx)
+	addq $1, threadinfo_sysc_cnt(%rcx)   # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+

Please reorganize the code so that you dont duplicate GET_THREAD_INFO() stuff You can do this for example before calling the syscall, not after.

ja badsys
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+ incq threadinfo_sysc_cnt(%rcx) # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
movq %r10,%rcx
call *sys_call_table(,%rax,8) # XXX: rip relative

#2 lore raw

Eric Dumazet wrote:
[snip]

Quoted 1 line

Please reorganize the code so that you dont duplicate GET_THREAD_INFO() stuff

Thanks Eric, I's really better. I have done changes. Do you have any others objections now? All is in attached perf_stat.patch.

Best regards,
Maxim Uvarov.

Patch adds Process Performance Statistics.
It make available to the user the following
new per-process (thread) performance statistics:
* Involuntary Context Switches
* Voluntary Context Switches
* Number of system calls

This data is useful for detecting hyperactivity
patterns between processes.

 arch/i386/kernel/asm-offsets.c    |    3 +++
 arch/i386/kernel/entry.S          |    3 +++
 arch/powerpc/kernel/asm-offsets.c |    4 ++++
 arch/powerpc/kernel/entry_32.S    |    5 +++++
 arch/powerpc/kernel/entry_64.S    |    5 +++++
 arch/x86_64/kernel/asm-offsets.c  |    3 +++
 arch/x86_64/kernel/entry.S        |    3 +++
 fs/proc/array.c                   |   17 +++++++++++++++++
 include/asm-i386/thread_info.h    |    5 +++--
 include/asm-powerpc/thread_info.h |    3 +++
 include/asm-x86_64/thread_info.h  |    4 +++-
 kernel/fork.c                     |    4 ++++
 lib/Kconfig.debug                 |   15 +++++++++++++++
 13 files changed, 71 insertions(+), 3 deletions(-)
fs/proc/array.c +17 -0
Index: linux-2.6.21-rc5/fs/proc/array.c
===================================================================
@@ -291,6 +291,20 @@ static inline char *task_cap(struct task
 			    cap_t(p->cap_effective));
 }
 
+#ifdef CONFIG_THREAD_PERF_STAT
+static inline char *task_perf(struct task_struct *p, char *buffer)
+{
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       buffer += sprintf(buffer, "Syscalls:\t%lu\n", p->thread_info->sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
+       return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"
+                           "Nivcsw:\t%lu\n",
+                           p->nvcsw,
+                           p->nivcsw);
+}
+#endif /* CONFIG_THREAD_PERF_STAT */
+
 int proc_pid_status(struct task_struct *task, char * buffer)
 {
 	char * orig = buffer;
@@ -309,6 +323,9 @@ int proc_pid_status(struct task_struct *
 #if defined(CONFIG_S390)
 	buffer = task_show_regs(task, buffer);
 #endif
+#ifdef CONFIG_THREAD_PERF_STAT
+	buffer = task_perf(task, buffer);
+#endif /* CONFIG_THREAD_PERF_STAT */
 	return buffer - orig;
 }
 
kernel/fork.c +4 -0
Index: linux-2.6.21-rc5/kernel/fork.c
===================================================================
@@ -1044,6 +1044,10 @@ static struct task_struct *copy_process(
 	p->syscr = 0;		/* I/O counter: read syscalls */
 	p->syscw = 0;		/* I/O counter: write syscalls */
 #endif
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        p->thread_info->sysc_cnt = 0;   /* Syscall counter: total numbers of syscalls */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
 	task_io_accounting_init(p);
 	acct_clear_integrals(p);
 
lib/Kconfig.debug +15 -0
Index: linux-2.6.21-rc5/lib/Kconfig.debug
===================================================================
@@ -446,3 +446,18 @@ config FAULT_INJECTION_STACKTRACE_FILTER
 	select FRAME_POINTER
 	help
 	  Provide stacktrace filter for fault-injection capabilities
+
+config THREAD_PERF_STAT
+       bool "Per-process (thread) performance statistics"
+       help
+         Make available to the user the following per-process (thread) performance statistics:
+            * Number of involuntary context switches
+            * Number of voluntary context switches
+            * Number of system calls (optional)
+         This information is available via /proc/PID/status.
+
+config THREAD_PERF_STAT_SYSC
+       bool "Enable syscall counter"
+       depends on THREAD_PERF_STAT && (X86 || PPC)
+       help
+         This option adds a syscall counter to /proc/PID/status.
arch/i386/kernel/entry.S +3 -0
Index: linux-2.6.21-rc5/arch/i386/kernel/entry.S
===================================================================
@@ -334,6 +334,9 @@ sysenter_past_esp:
 	CFI_ADJUST_CFA_OFFSET 4
 	SAVE_ALL
 	GET_THREAD_INFO(%ebp)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        incl    TI_sysc_cnt(%ebp)       # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 
 	/* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
 	testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
arch/i386/kernel/asm-offsets.c +3 -0
Index: linux-2.6.21-rc5/arch/i386/kernel/asm-offsets.c
===================================================================
@@ -56,6 +56,9 @@ void foo(void)
 	OFFSET(TI_addr_limit, thread_info, addr_limit);
 	OFFSET(TI_restart_block, thread_info, restart_block);
 	OFFSET(TI_sysenter_return, thread_info, sysenter_return);
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        OFFSET(TI_sysc_cnt, thread_info, sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	BLANK();
 
 	OFFSET(GDS_size, Xgt_desc_struct, size);
include/asm-i386/thread_info.h +3 -2
Index: linux-2.6.21-rc5/include/asm-i386/thread_info.h
===================================================================
@@ -31,8 +31,9 @@ struct thread_info {
 	unsigned long		status;		/* thread-synchronous flags */
 	__u32			cpu;		/* current CPU */
 	int			preempt_count;	/* 0 => preemptable, <0 => BUG */
-
-
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long		sysc_cnt;	/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	mm_segment_t		addr_limit;	/* thread address space:
 					 	   0-0xBFFFFFFF for user-thead
 						   0-0xFFFFFFFF for kernel-thread
arch/powerpc/kernel/asm-offsets.c +4 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/asm-offsets.c
===================================================================
@@ -94,6 +94,10 @@ int main(void)
 	DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
 	DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
 	DEFINE(TI_TASK, offsetof(struct thread_info, task));
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        DEFINE(TI_SYSC_CNT, offsetof(struct thread_info, sysc_cnt));
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
 #ifdef CONFIG_PPC32
 	DEFINE(TI_EXECDOMAIN, offsetof(struct thread_info, exec_domain));
 	DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
arch/powerpc/kernel/entry_32.S +5 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/entry_32.S
===================================================================
@@ -202,6 +202,11 @@ _GLOBAL(DoSyscall)
 	bl	do_show_syscall
 #endif /* SHOW_SYSCALLS */
 	rlwinm	r10,r1,0,0,(31-THREAD_SHIFT)	/* current_thread_info() */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	lwz r11,TI_SYSC_CNT(r10)
+	addi r11,r11,1
+	stw r11,TI_SYSC_CNT(r10)
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	lwz	r11,TI_FLAGS(r10)
 	andi.	r11,r11,_TIF_SYSCALL_T_OR_A
 	bne-	syscall_dotrace
arch/powerpc/kernel/entry_64.S +5 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/entry_64.S
===================================================================
@@ -115,6 +115,11 @@ END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISER
 	addi	r9,r1,STACK_FRAME_OVERHEAD
 #endif
 	clrrdi	r11,r1,THREAD_SHIFT
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	lwz r10,TI_SYSC_CNT(r11)
+	addi r10,r10,1
+	stw r10,TI_SYSC_CNT(r11)
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	ld	r10,TI_FLAGS(r11)
 	andi.	r11,r10,_TIF_SYSCALL_T_OR_A
 	bne-	syscall_dotrace
arch/x86_64/kernel/asm-offsets.c +3 -0
Index: linux-2.6.21-rc5/arch/x86_64/kernel/asm-offsets.c
===================================================================
@@ -35,6 +35,9 @@ int main(void)
 	ENTRY(addr_limit);
 	ENTRY(preempt_count);
 	ENTRY(status);
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	ENTRY(sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	BLANK();
 #undef ENTRY
 #define ENTRY(entry) DEFINE(pda_ ## entry, offsetof(struct x8664_pda, entry))
arch/x86_64/kernel/entry.S +3 -0
Index: linux-2.6.21-rc5/arch/x86_64/kernel/entry.S
===================================================================
@@ -229,6 +229,9 @@ ENTRY(system_call)
 	movq  %rcx,RIP-ARGOFFSET(%rsp)
 	CFI_REL_OFFSET rip,RIP-ARGOFFSET
 	GET_THREAD_INFO(%rcx)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	addq $1, threadinfo_sysc_cnt(%rcx)   # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SECCOMP),threadinfo_flags(%rcx)
 	jnz tracesys
 	cmpq $__NR_syscall_max,%rax
include/asm-powerpc/thread_info.h +3 -0
Index: linux-2.6.21-rc5/include/asm-powerpc/thread_info.h
===================================================================
@@ -35,6 +35,9 @@ struct thread_info {
 	int		cpu;			/* cpu we're on */
 	int		preempt_count;		/* 0 => preemptable,
 						   <0 => BUG */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long	sysc_cnt;		/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	struct restart_block restart_block;
 	unsigned long	local_flags;		/* private flags for thread */
 
include/asm-x86_64/thread_info.h +3 -1
Index: linux-2.6.21-rc5/include/asm-x86_64/thread_info.h
===================================================================
@@ -30,7 +30,9 @@ struct thread_info {
 	__u32			status;		/* thread synchronous flags */
 	__u32			cpu;		/* current CPU */
 	int 			preempt_count;	/* 0 => preemptable, <0 => BUG */
-
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long		sysc_cnt;	/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	mm_segment_t		addr_limit;	
 	struct restart_block    restart_block;
 };
#3 lore raw
Quoted 3 lines · On Wed, 11 Apr 2007 15:59:16 +0400 Maxim Uvarov <[email protected]> wrote:

Thanks Eric, I's really better. I have done changes. Do you have any others objections now?
All is in attached perf_stat.patch.

Hi Maxim

I know *nothing* about powerpc assembly, but I think there is a problem :

arch/powerpc/kernel/entry_64.S +5 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/entry_64.S
===================================================================
@@ -115,6 +115,11 @@ END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISER
 	addi	r9,r1,STACK_FRAME_OVERHEAD
 #endif
 	clrrdi	r11,r1,THREAD_SHIFT
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	lwz r10,TI_SYSC_CNT(r11)
+	addi r10,r10,1
+	stw r10,TI_SYSC_CNT(r11)
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	ld	r10,TI_FLAGS(r11)
 	andi.	r11,r10,_TIF_SYSCALL_T_OR_A
 	bne-	syscall_dotrace

sysc_cnt being an 'unsigned long', its a 64 bits long integer... So you probably need ld/std instructions instead of lwz/stw

#4 lore raw
Quoted 6 lines · Eric Dumazet

sysc_cnt being an 'unsigned long', its a 64 bits long integer... So you probably need ld/std instructions instead of lwz/stw

Seems you are right again :(

#5 lore raw
In reply to Maxim Uvarov
Quoted 9 lines · On Wed, 11 Apr 2007 15:59:16 +0400 Maxim Uvarov <[email protected]> wrote:

Patch adds Process Performance Statistics.
It make available to the user the following
new per-process (thread) performance statistics:
* Involuntary Context Switches
* Voluntary Context Switches
* Number of system calls

This data is useful for detecting hyperactivity
patterns between processes.

Your description is not very clear about the semantic of your stats.

You currently returns stats only for thread(s) (not process as you claimed)

Please check kernel/sys.c:k_getrusage() to see how getrusage() has to sum *lot* of individual fields to get precise process numbers (even counting stats for dead threads)

#6 lore raw
Quoted 10 lines · Eric Dumazet
Quoted 6 lines · On Wed, 11 Apr 2007 15:59:16 +0400 Maxim Uvarov <[email protected]> wrote:

Patch adds Process Performance Statistics.
It make available to the user the following new per-process (thread) performance statistics:
* Involuntary Context Switches
* Voluntary Context Switches
* Number of system calls

Quoted 7 lines
Quoted 2 lines

This data is useful for detecting hyperactivity patterns between processes.

Your description is not very clear about the semantic of your stats.

You currently returns stats only for thread(s) (not process as you claimed)

I'm not sure if you were confused by his use of thread in parenthesis, but isn't the whole point of this to see which threads are doing what? Or am I misreading his result as intentional?

Quoted 3 lines

Please check kernel/sys.c:k_getrusage() to see how getrusage() has to sum *lot* of individual fields to get precise process numbers (even counting stats for dead threads)

Signature · 3 lines
bill davidsen <[email protected]>
 CTO TMR Associates, Inc
 Doing interesting things with small computers since 1979
#7 lore raw
Quoted 9 lines · Bill Davidsen

[snip]

Quoted 5 lines

Your description is not very clear about the semantic of your stats.

You currently returns stats only for thread(s) (not process as you claimed)

I'm not sure if you were confused by his use of thread in parenthesis, but isn't the whole point of this to see which threads are doing what? Or am I misreading his result as intentional?

Yes the point is to get some statistics from thread in other words to see which threads are doing what.

#8 lore raw
In reply to Eric Dumazet
Quoted 1 line · Eric Dumazet

Please check kernel/sys.c:k_getrusage() to see how getrusage() has to sum *lot* of individual fields to get precise process numbers (even counting stats for dead threads)

Thanks for helping me and for this link. But it is not enough clear for me what do you mean at this time. Inside of patch I am using 2 default counters task_struct->nivcsw and task_struct->nvcsw. And also one new syscall counter. And there is only one way to increment this counter, it is from entry.S.

If you are speaking about locks, in my point of view, they are not needed in this code. Because increment syscall counter is atomic for X86 (just one assembly instruction) and in case with PPC (3 instructions) there 1) nothing wrong will not happen in any case 2) only own thread can increase it's syscall counter. So here should be not any race conditions.

I've tested this patch on x86,x86_64,and ppc_32. And I should work now with ppc_64 (I didn't check). And also updated description.

Best regards,
Maxim Uvarov.

Patch adds Process Performance Statistics.
It make available to the user the following thread performance statistics:
* Involuntary Context Switches (task_struct->nivcsw)
* Voluntary Context Switches (task_struct->nvcsw)
* Number of system calls (added new counter

     thread_info->sysc_cnt)

Statistics information is available from
/proc/PID/status

This data is useful for detecting hyperactivity
patterns between processes.

 arch/i386/kernel/asm-offsets.c    |    3 +++
 arch/i386/kernel/entry.S          |    3 +++
 arch/powerpc/kernel/asm-offsets.c |    4 ++++
 arch/powerpc/kernel/entry_32.S    |    5 +++++
 arch/powerpc/kernel/entry_64.S    |    5 +++++
 arch/x86_64/kernel/asm-offsets.c  |    3 +++
 arch/x86_64/kernel/entry.S        |    3 +++
 fs/proc/array.c                   |   17 +++++++++++++++++
 include/asm-i386/thread_info.h    |    5 +++--
 include/asm-powerpc/thread_info.h |    3 +++
 include/asm-x86_64/thread_info.h  |    4 +++-
 kernel/fork.c                     |    4 ++++
 lib/Kconfig.debug                 |   15 +++++++++++++++
 13 files changed, 71 insertions(+), 3 deletions(-)
fs/proc/array.c +17 -0
Index: linux-2.6.21-rc5/fs/proc/array.c
===================================================================
@@ -291,6 +291,20 @@ static inline char *task_cap(struct task
 			    cap_t(p->cap_effective));
 }
 
+#ifdef CONFIG_THREAD_PERF_STAT
+static inline char *task_perf(struct task_struct *p, char *buffer)
+{
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       buffer += sprintf(buffer, "Syscalls:\t%lu\n", p->thread_info->sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
+       return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"
+                           "Nivcsw:\t%lu\n",
+                           p->nvcsw,
+                           p->nivcsw);
+}
+#endif /* CONFIG_THREAD_PERF_STAT */
+
 int proc_pid_status(struct task_struct *task, char * buffer)
 {
 	char * orig = buffer;
@@ -309,6 +323,9 @@ int proc_pid_status(struct task_struct *
 #if defined(CONFIG_S390)
 	buffer = task_show_regs(task, buffer);
 #endif
+#ifdef CONFIG_THREAD_PERF_STAT
+	buffer = task_perf(task, buffer);
+#endif /* CONFIG_THREAD_PERF_STAT */
 	return buffer - orig;
 }
 
kernel/fork.c +4 -0
Index: linux-2.6.21-rc5/kernel/fork.c
===================================================================
@@ -1044,6 +1044,10 @@ static struct task_struct *copy_process(
 	p->syscr = 0;		/* I/O counter: read syscalls */
 	p->syscw = 0;		/* I/O counter: write syscalls */
 #endif
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        p->thread_info->sysc_cnt = 0;   /* Syscall counter: total numbers of syscalls */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
 	task_io_accounting_init(p);
 	acct_clear_integrals(p);
 
lib/Kconfig.debug +15 -0
Index: linux-2.6.21-rc5/lib/Kconfig.debug
===================================================================
@@ -446,3 +446,18 @@ config FAULT_INJECTION_STACKTRACE_FILTER
 	select FRAME_POINTER
 	help
 	  Provide stacktrace filter for fault-injection capabilities
+
+config THREAD_PERF_STAT
+       bool "Per-process (thread) performance statistics"
+       help
+         Make available to the user the following per-process (thread) performance statistics:
+            * Number of involuntary context switches
+            * Number of voluntary context switches
+            * Number of system calls (optional)
+         This information is available via /proc/PID/status.
+
+config THREAD_PERF_STAT_SYSC
+       bool "Enable syscall counter"
+       depends on THREAD_PERF_STAT && (X86 || PPC)
+       help
+         This option adds a syscall counter to /proc/PID/status.
arch/i386/kernel/entry.S +3 -0
Index: linux-2.6.21-rc5/arch/i386/kernel/entry.S
===================================================================
@@ -334,6 +334,9 @@ sysenter_past_esp:
 	CFI_ADJUST_CFA_OFFSET 4
 	SAVE_ALL
 	GET_THREAD_INFO(%ebp)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        incl    TI_sysc_cnt(%ebp)       # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 
 	/* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
 	testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
arch/i386/kernel/asm-offsets.c +3 -0
Index: linux-2.6.21-rc5/arch/i386/kernel/asm-offsets.c
===================================================================
@@ -56,6 +56,9 @@ void foo(void)
 	OFFSET(TI_addr_limit, thread_info, addr_limit);
 	OFFSET(TI_restart_block, thread_info, restart_block);
 	OFFSET(TI_sysenter_return, thread_info, sysenter_return);
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        OFFSET(TI_sysc_cnt, thread_info, sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	BLANK();
 
 	OFFSET(GDS_size, Xgt_desc_struct, size);
include/asm-i386/thread_info.h +3 -2
Index: linux-2.6.21-rc5/include/asm-i386/thread_info.h
===================================================================
@@ -31,8 +31,9 @@ struct thread_info {
 	unsigned long		status;		/* thread-synchronous flags */
 	__u32			cpu;		/* current CPU */
 	int			preempt_count;	/* 0 => preemptable, <0 => BUG */
-
-
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long		sysc_cnt;	/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	mm_segment_t		addr_limit;	/* thread address space:
 					 	   0-0xBFFFFFFF for user-thead
 						   0-0xFFFFFFFF for kernel-thread
arch/powerpc/kernel/asm-offsets.c +4 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/asm-offsets.c
===================================================================
@@ -94,6 +94,10 @@ int main(void)
 	DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
 	DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
 	DEFINE(TI_TASK, offsetof(struct thread_info, task));
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        DEFINE(TI_SYSC_CNT, offsetof(struct thread_info, sysc_cnt));
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
 #ifdef CONFIG_PPC32
 	DEFINE(TI_EXECDOMAIN, offsetof(struct thread_info, exec_domain));
 	DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
arch/powerpc/kernel/entry_32.S +5 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/entry_32.S
===================================================================
@@ -202,6 +202,11 @@ _GLOBAL(DoSyscall)
 	bl	do_show_syscall
 #endif /* SHOW_SYSCALLS */
 	rlwinm	r10,r1,0,0,(31-THREAD_SHIFT)	/* current_thread_info() */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	lwz r11,TI_SYSC_CNT(r10)
+	addi r11,r11,1
+	stw r11,TI_SYSC_CNT(r10)
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	lwz	r11,TI_FLAGS(r10)
 	andi.	r11,r11,_TIF_SYSCALL_T_OR_A
 	bne-	syscall_dotrace
arch/powerpc/kernel/entry_64.S +5 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/entry_64.S
===================================================================
@@ -115,6 +115,11 @@ END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISER
 	addi	r9,r1,STACK_FRAME_OVERHEAD
 #endif
 	clrrdi	r11,r1,THREAD_SHIFT
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	ld r10,TI_SYSC_CNT(r11)
+	addi r10,r10,1
+	std r10,TI_SYSC_CNT(r11)
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	ld	r10,TI_FLAGS(r11)
 	andi.	r11,r10,_TIF_SYSCALL_T_OR_A
 	bne-	syscall_dotrace
arch/x86_64/kernel/asm-offsets.c +3 -0
Index: linux-2.6.21-rc5/arch/x86_64/kernel/asm-offsets.c
===================================================================
@@ -35,6 +35,9 @@ int main(void)
 	ENTRY(addr_limit);
 	ENTRY(preempt_count);
 	ENTRY(status);
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	ENTRY(sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	BLANK();
 #undef ENTRY
 #define ENTRY(entry) DEFINE(pda_ ## entry, offsetof(struct x8664_pda, entry))
arch/x86_64/kernel/entry.S +3 -0
Index: linux-2.6.21-rc5/arch/x86_64/kernel/entry.S
===================================================================
@@ -229,6 +229,9 @@ ENTRY(system_call)
 	movq  %rcx,RIP-ARGOFFSET(%rsp)
 	CFI_REL_OFFSET rip,RIP-ARGOFFSET
 	GET_THREAD_INFO(%rcx)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	addq $1, threadinfo_sysc_cnt(%rcx)   # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SECCOMP),threadinfo_flags(%rcx)
 	jnz tracesys
 	cmpq $__NR_syscall_max,%rax
include/asm-powerpc/thread_info.h +3 -0
Index: linux-2.6.21-rc5/include/asm-powerpc/thread_info.h
===================================================================
@@ -35,6 +35,9 @@ struct thread_info {
 	int		cpu;			/* cpu we're on */
 	int		preempt_count;		/* 0 => preemptable,
 						   <0 => BUG */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long	sysc_cnt;		/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	struct restart_block restart_block;
 	unsigned long	local_flags;		/* private flags for thread */
 
include/asm-x86_64/thread_info.h +3 -1
Index: linux-2.6.21-rc5/include/asm-x86_64/thread_info.h
===================================================================
@@ -30,7 +30,9 @@ struct thread_info {
 	__u32			status;		/* thread synchronous flags */
 	__u32			cpu;		/* current CPU */
 	int 			preempt_count;	/* 0 => preemptable, <0 => BUG */
-
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long		sysc_cnt;	/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	mm_segment_t		addr_limit;	
 	struct restart_block    restart_block;
 };
#9 lore raw
Quoted 11 lines · Maxim Uvarov
Quoted 1 line · Eric Dumazet

Please check kernel/sys.c:k_getrusage() to see how getrusage() has to

sum *lot* of individual fields to get precise process numbers (even counting stats for dead threads)

Thanks for helping me and for this link. But it is not enough clear for me what do you mean at this time. Inside of patch I am using 2 default counters task_struct->nivcsw and task_struct->nvcsw. And also one new syscall counter. And there is only one way to increment this counter, it is from entry.S.

If you are speaking about locks, in my point of view, they are not needed in this code. Because increment syscall counter is atomic for X86 (just one assembly instruction) and in case with PPC (3 instructions) there 1) nothing wrong will not happen in any case 2) only own thread can increase it's syscall counter. So here should be not any race conditions.

I was not speaking about locks.

Quoted 15 lines

I've tested this patch on x86,x86_64,and ppc_32. And I should work now with ppc_64 (I didn't check). And also updated description.

Best regards,
Maxim Uvarov.

------------------------------------------------------------------------

Patch adds Process Performance Statistics.
It make available to the user the following thread performance statistics:
* Involuntary Context Switches (task_struct->nivcsw)
* Voluntary Context Switches (task_struct->nvcsw)
* Number of system calls (added new counter

Quoted 7 lines
     thread_info->sysc_cnt)

Statistics information is available from
/proc/PID/status

This data is useful for detecting hyperactivity patterns between processes.

What I meant is : You falsely speak of 'PROCESS performance statistics'.

Your implementation only cares about threads, not processes. There is a slight difference, that getrusage() can do.

So if you do "cat /proc/PID/status", you'll get counters not for the PROCESS, only the main thread of the process.

If you want an analogy, imagine a "ps aux" that doesnt show the cpu time of all threads of a process, but only the cpu time of the main thread. Quite meaningless isnt it ?

So either :

1) You change all your description to mention 'thread' instead of 'process'.

2) You change your implementation to match your claim.

#10 lore raw
Quoted 8 lines · Eric Dumazet

[snip]
What I meant is : You falsely speak of 'PROCESS performance statistics'.

Your implementation only cares about threads, not processes. There is a slight difference, that getrusage() can do.

So if you do "cat /proc/PID/status", you'll get counters not for the PROCESS, only the main thread of the process.

Quoted 7 lines

If you want an analogy, imagine a "ps aux" that doesnt show the cpu time of all threads of a process, but only the cpu time of the main thread. Quite meaningless isnt it ?

So either :

1) You change all your description to mention 'thread' instead of 'process'.

2) You change your implementation to match your claim.

Thanks again Eric, very useful notes.

Important information is per-thread counters. So I changed only description. Seems no issues now?

Patch is attached.

Best regards,
Maxim.

Patch makes available to the user the following thread performance statistics:
* Involuntary Context Switches (task_struct->nivcsw)
* Voluntary Context Switches (task_struct->nvcsw)
* Number of system calls (added new counter

     thread_info->sysc_cnt)

Statistics information is available from
/proc/PID/status

This data is useful for detecting hyperactivity
patterns between processes.

 arch/i386/kernel/asm-offsets.c    |    3 +++
 arch/i386/kernel/entry.S          |    3 +++
 arch/powerpc/kernel/asm-offsets.c |    4 ++++
 arch/powerpc/kernel/entry_32.S    |    5 +++++
 arch/powerpc/kernel/entry_64.S    |    5 +++++
 arch/x86_64/kernel/asm-offsets.c  |    3 +++
 arch/x86_64/kernel/entry.S        |    3 +++
 fs/proc/array.c                   |   17 +++++++++++++++++
 include/asm-i386/thread_info.h    |    5 +++--
 include/asm-powerpc/thread_info.h |    3 +++
 include/asm-x86_64/thread_info.h  |    4 +++-
 kernel/fork.c                     |    4 ++++
 lib/Kconfig.debug                 |   15 +++++++++++++++
 13 files changed, 71 insertions(+), 3 deletions(-)
fs/proc/array.c +17 -0
Index: linux-2.6.21-rc5/fs/proc/array.c
===================================================================
@@ -291,6 +291,20 @@ static inline char *task_cap(struct task
 			    cap_t(p->cap_effective));
 }
 
+#ifdef CONFIG_THREAD_PERF_STAT
+static inline char *task_perf(struct task_struct *p, char *buffer)
+{
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       buffer += sprintf(buffer, "Syscalls:\t%lu\n", p->thread_info->sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
+       return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"
+                           "Nivcsw:\t%lu\n",
+                           p->nvcsw,
+                           p->nivcsw);
+}
+#endif /* CONFIG_THREAD_PERF_STAT */
+
 int proc_pid_status(struct task_struct *task, char * buffer)
 {
 	char * orig = buffer;
@@ -309,6 +323,9 @@ int proc_pid_status(struct task_struct *
 #if defined(CONFIG_S390)
 	buffer = task_show_regs(task, buffer);
 #endif
+#ifdef CONFIG_THREAD_PERF_STAT
+	buffer = task_perf(task, buffer);
+#endif /* CONFIG_THREAD_PERF_STAT */
 	return buffer - orig;
 }
 
kernel/fork.c +4 -0
Index: linux-2.6.21-rc5/kernel/fork.c
===================================================================
@@ -1044,6 +1044,10 @@ static struct task_struct *copy_process(
 	p->syscr = 0;		/* I/O counter: read syscalls */
 	p->syscw = 0;		/* I/O counter: write syscalls */
 #endif
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        p->thread_info->sysc_cnt = 0;   /* Syscall counter: total numbers of syscalls */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
 	task_io_accounting_init(p);
 	acct_clear_integrals(p);
 
lib/Kconfig.debug +15 -0
Index: linux-2.6.21-rc5/lib/Kconfig.debug
===================================================================
@@ -446,3 +446,18 @@ config FAULT_INJECTION_STACKTRACE_FILTER
 	select FRAME_POINTER
 	help
 	  Provide stacktrace filter for fault-injection capabilities
+
+config THREAD_PERF_STAT
+       bool "Thread performance statistics"
+       help
+         Make available to the user the following per-thread performance statistics:
+            * Number of involuntary context switches
+            * Number of voluntary context switches
+            * Number of system calls (optional)
+         This information is available via /proc/PID/status.
+
+config THREAD_PERF_STAT_SYSC
+       bool "Enable syscall counter"
+       depends on THREAD_PERF_STAT && (X86 || PPC)
+       help
+         This option adds a syscall counter to /proc/PID/status.
arch/i386/kernel/entry.S +3 -0
Index: linux-2.6.21-rc5/arch/i386/kernel/entry.S
===================================================================
@@ -334,6 +334,9 @@ sysenter_past_esp:
 	CFI_ADJUST_CFA_OFFSET 4
 	SAVE_ALL
 	GET_THREAD_INFO(%ebp)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        incl    TI_sysc_cnt(%ebp)       # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 
 	/* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
 	testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
arch/i386/kernel/asm-offsets.c +3 -0
Index: linux-2.6.21-rc5/arch/i386/kernel/asm-offsets.c
===================================================================
@@ -56,6 +56,9 @@ void foo(void)
 	OFFSET(TI_addr_limit, thread_info, addr_limit);
 	OFFSET(TI_restart_block, thread_info, restart_block);
 	OFFSET(TI_sysenter_return, thread_info, sysenter_return);
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        OFFSET(TI_sysc_cnt, thread_info, sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	BLANK();
 
 	OFFSET(GDS_size, Xgt_desc_struct, size);
include/asm-i386/thread_info.h +3 -2
Index: linux-2.6.21-rc5/include/asm-i386/thread_info.h
===================================================================
@@ -31,8 +31,9 @@ struct thread_info {
 	unsigned long		status;		/* thread-synchronous flags */
 	__u32			cpu;		/* current CPU */
 	int			preempt_count;	/* 0 => preemptable, <0 => BUG */
-
-
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long		sysc_cnt;	/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	mm_segment_t		addr_limit;	/* thread address space:
 					 	   0-0xBFFFFFFF for user-thead
 						   0-0xFFFFFFFF for kernel-thread
arch/powerpc/kernel/asm-offsets.c +4 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/asm-offsets.c
===================================================================
@@ -94,6 +94,10 @@ int main(void)
 	DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
 	DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
 	DEFINE(TI_TASK, offsetof(struct thread_info, task));
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+        DEFINE(TI_SYSC_CNT, offsetof(struct thread_info, sysc_cnt));
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
 #ifdef CONFIG_PPC32
 	DEFINE(TI_EXECDOMAIN, offsetof(struct thread_info, exec_domain));
 	DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
arch/powerpc/kernel/entry_32.S +5 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/entry_32.S
===================================================================
@@ -202,6 +202,11 @@ _GLOBAL(DoSyscall)
 	bl	do_show_syscall
 #endif /* SHOW_SYSCALLS */
 	rlwinm	r10,r1,0,0,(31-THREAD_SHIFT)	/* current_thread_info() */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	lwz r11,TI_SYSC_CNT(r10)
+	addi r11,r11,1
+	stw r11,TI_SYSC_CNT(r10)
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	lwz	r11,TI_FLAGS(r10)
 	andi.	r11,r11,_TIF_SYSCALL_T_OR_A
 	bne-	syscall_dotrace
arch/powerpc/kernel/entry_64.S +5 -0
Index: linux-2.6.21-rc5/arch/powerpc/kernel/entry_64.S
===================================================================
@@ -115,6 +115,11 @@ END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISER
 	addi	r9,r1,STACK_FRAME_OVERHEAD
 #endif
 	clrrdi	r11,r1,THREAD_SHIFT
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	ld r10,TI_SYSC_CNT(r11)
+	addi r10,r10,1
+	std r10,TI_SYSC_CNT(r11)
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	ld	r10,TI_FLAGS(r11)
 	andi.	r11,r10,_TIF_SYSCALL_T_OR_A
 	bne-	syscall_dotrace
arch/x86_64/kernel/asm-offsets.c +3 -0
Index: linux-2.6.21-rc5/arch/x86_64/kernel/asm-offsets.c
===================================================================
@@ -35,6 +35,9 @@ int main(void)
 	ENTRY(addr_limit);
 	ENTRY(preempt_count);
 	ENTRY(status);
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	ENTRY(sysc_cnt);
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	BLANK();
 #undef ENTRY
 #define ENTRY(entry) DEFINE(pda_ ## entry, offsetof(struct x8664_pda, entry))
arch/x86_64/kernel/entry.S +3 -0
Index: linux-2.6.21-rc5/arch/x86_64/kernel/entry.S
===================================================================
@@ -229,6 +229,9 @@ ENTRY(system_call)
 	movq  %rcx,RIP-ARGOFFSET(%rsp)
 	CFI_REL_OFFSET rip,RIP-ARGOFFSET
 	GET_THREAD_INFO(%rcx)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	addq $1, threadinfo_sysc_cnt(%rcx)   # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SECCOMP),threadinfo_flags(%rcx)
 	jnz tracesys
 	cmpq $__NR_syscall_max,%rax
include/asm-powerpc/thread_info.h +3 -0
Index: linux-2.6.21-rc5/include/asm-powerpc/thread_info.h
===================================================================
@@ -35,6 +35,9 @@ struct thread_info {
 	int		cpu;			/* cpu we're on */
 	int		preempt_count;		/* 0 => preemptable,
 						   <0 => BUG */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long	sysc_cnt;		/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	struct restart_block restart_block;
 	unsigned long	local_flags;		/* private flags for thread */
 
include/asm-x86_64/thread_info.h +3 -1
Index: linux-2.6.21-rc5/include/asm-x86_64/thread_info.h
===================================================================
@@ -30,7 +30,9 @@ struct thread_info {
 	__u32			status;		/* thread synchronous flags */
 	__u32			cpu;		/* current CPU */
 	int 			preempt_count;	/* 0 => preemptable, <0 => BUG */
-
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+	unsigned long		sysc_cnt;	/* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 	mm_segment_t		addr_limit;	
 	struct restart_block    restart_block;
 };
#11 lore raw
In reply to Maxim Uvarov

Hi!

fs/proc/array.c static inline char *task_cap(struct task · line 304

Quoted 4 lines
fs/proc/array.c +4 -0
+       return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"
+                           "Nivcsw:\t%lu\n",
+                           p->nvcsw,
+                           p->nivcsw);

We don't encrypt variable names like this.
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#12 lore raw

At some point in the past, someone's attribution was stripped from:

Quoted 4 lines
Quoted 4 lines
fs/proc/array.c +4 -0
+       return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"
+                           "Nivcsw:\t%lu\n",
+                           p->nvcsw,
+                           p->nivcsw);
Quoted 1 line · Pavel Machek

We don't encrypt variable names like this.

nvcsw and nivcsw are conventional variable names for these quantities.

-- wli

#13 lore raw

Hi!

Quoted 1 line · William Lee Irwin III

At some point in the past, someone's attribution was stripped from:

Maxim's.

Quoted 9 lines
Quoted 4 lines
Quoted 4 lines
fs/proc/array.c +4 -0
+       return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"
+                           "Nivcsw:\t%lu\n",
+                           p->nvcsw,
+                           p->nivcsw);
Quoted 1 line · Pavel Machek

We don't encrypt variable names like this.

nvcsw and nivcsw are conventional variable names for these quantities.

I can't decipher them and would not want users see them in /proc.

Would nonvoluntary_ctxt_switch be that much worse?

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#14 lore raw
Quoted 1 line · William Lee Irwin III
Quoted 1 line

nvcsw and nivcsw are conventional variable names for these quantities.

Quoted 2 lines · Pavel Machek

I can't decipher them and would not want users see them in /proc. Would nonvoluntary_ctxt_switch be that much worse?

I'm not too attached to a particular name. I just think that the traditional counter names should pass readability/etc. criteria on account of the very strong historical precedent. I'd consider more verbose affairs for the sake of putative legibility as equivalent in quality. Maybe that means others voting for the more verbose names should result in an aggregate decision in favor of verbosity.

-- wli

#15 lore raw
Quoted 23 lines · William Lee Irwin III
Quoted 3 lines · William Lee Irwin III
Quoted 3 lines

nvcsw and nivcsw are conventional variable names for these quantities.

Quoted 4 lines · Pavel Machek

I can't decipher them and would not want users see them in /proc. Would nonvoluntary_ctxt_switch be that much worse?

I'm not too attached to a particular name. I just think that the traditional counter names should pass readability/etc. criteria on account of the very strong historical precedent. I'd consider more verbose affairs for the sake of putative legibility as equivalent in quality. Maybe that means others voting for the more verbose names should result in an aggregate decision in favor of verbosity.

Quoted 3 lines

-- wli

The standard /proc output is:

[email protected]:~# cat /proc/$$/status
Name:   bash
State:  S (sleeping)
SleepAVG:       88%
Tgid:   836
Pid:    836
PPid:   1
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 256
Groups: 0 1 2 4
VmPeak:     2360 kB
VmSize:     2360 kB
VmLck:         0 kB
VmHWM:      1400 kB
VmRSS:      1400 kB
VmData:      196 kB
VmStk:        88 kB
VmExe:       564 kB
VmLib:      1412 kB
VmPTE:        12 kB
Threads:        1
SigQ:   0/1024
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000010000
SigIgn: 0000000000384004
SigCgt: 000000004b813efb
CapInh: 0000000000000000
CapPrm: 00000000fffffeff
CapEff: 00000000fffffeff

I can't say that "Nvcsw" and "Nivcsw" is something less verbose as output above. It is more usual so it is more clear for you. But if you need this counters you should know what are they and how to use them.

As for me "nonvoluntary_ctxt_switch" and "voluntary_ctxt_switch" is more verbose, and it is not big deal to change names. But I think that information about this counters and verbose name should be in user space tools, like ps.