Skip to contentSkip to conversation

Performance Stats: Kernel patch

PATCH Maxim Uvarov 7 participants 12 messages Apr 3, 2007 – Apr 10, 2007

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

Hello all,

I have idea to include to the user the following new per-process (thread) performance statistics:

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

What do you think about it? Patch is bellow.

Best regards,
Max Uvarov.

Description:
Patch adds Process Performance Statistics.

arch/i386/kernel/entry.S       |    3 +++
arch/mips/kernel/entry.S       |    3 +++
arch/powerpc/kernel/entry_32.S |    3 +++
arch/x86_64/kernel/entry.S     |    3 +++
fs/proc/array.c                |   13 +++++++++++++
include/linux/sched.h          |    3 +++
kernel/fork.c                  |    3 +++
kernel/sys.c                   |    8 ++++++++
lib/Kconfig.debug              |   10 ++++++++++
9 files changed, 49 insertions(+)
fs/proc/array.c +0 -0
Index: linux-2.6.18/fs/proc/array.c
===================================================================
@@ -295,6 +295,18 @@ static inline char *task_cap(struct task
                           cap_t(p->cap_effective));

}

+#ifdef THREAD_PERF_STAT
+static inline char *task_perf(struct task_struct *p, char *buffer) +{
+ return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"

+                           "Nivcsw:\t%lu\n"
+                           "Syscalls:\t%d\n",
+                           cap_t(p->nvcsw),
+                           cap_t(p->nivcsw),
+                           cap_t(p->sysc_cnt));

+}
+#endif /* THREAD_PERF_STAT */
+
#define get_blocked_on(t) (-1)

static char *show_blocked_on(struct task_struct *task, char *buffer) @@ -327,6 +339,7 @@ int proc_pid_status(struct task_struct *

       buffer = task_show_regs(task, buffer);
#endif
       buffer = show_blocked_on(task,buffer);
+       buffer = task_perf(task, buffer);
       return buffer - orig;
}
include/linux/sched.h +0 -0
Index: linux-2.6.18/include/linux/sched.h
===================================================================
@@ -1193,6 +1193,9 @@ struct task_struct {
       ltt_facility_t ltt_facilities[LTT_FAC_PER_PROCESS];

#endif //CONFIG_LTT_USERSPACE_GENERIC

+#ifdef THREAD_PERF_STAT
+ int sysc_cnt; /* Syscall counter */
+#endif /* THREAD_PERF_STAT */
/*
* task-specic data
* XXX: for a future release, MontaVista-only

kernel/sys.c +0 -0
Index: linux-2.6.18/kernel/sys.c
===================================================================
@@ -2071,3 +2071,11 @@ asmlinkage long sys_prctl(int option, un
       }
       return error;

}
+#ifdef THREAD_PERF_STAT
+asmlinkage void inc_syscallcnt(void)
+{
+ current->sysc_cnt += 1;
+ return;
+}
+#endif /* THREAD_PERF_STAT */
+

kernel/fork.c +3 -0
Index: linux-2.6.18/kernel/fork.c
===================================================================
@@ -1079,6 +1079,9 @@ static struct task_struct *copy_process(
       p->wchar = 0;           /* I/O counter: bytes written */
       p->syscr = 0;           /* I/O counter: read syscalls */
       p->syscw = 0;           /* I/O counter: write syscalls */
+#ifdef THREAD_PERF_STAT
+       p->sysc_cnt = 0;        /* Syscall counter: total numbers of syscalls */
+#endif /* THREAD_PERF_STAT */
       acct_clear_integrals(p);
 
       p->it_virt_expires = cputime_zero;
arch/i386/kernel/entry.S +0 -0
Index: linux-2.6.18/arch/i386/kernel/entry.S
===================================================================
@@ -394,6 +394,9 @@ syscall_exit:
       cli                             # make sure we don't miss an 

interrupt

                                       # setting need_resched or sigpending
                                       # between sampling and the iret
+#ifdef THREAD_PERF_STAT
+       call inc_syscallcnt             # Increment syscalls counter current->sysc_cnt
+#endif /* THREAD_PERF_STAT */
       TRACE_IRQS_OFF
       movl TI_flags(%ebp), %ecx
       testw $_TIF_ALLWORK_MASK, %cx   # current->work
Index: linux-2.6.18/arch/x86_64/kernel/entry.S
===================================================================
--- linux-2.6.18.orig/arch/x86_64/kernel/entry.S
+++ linux-2.6.18/arch/x86_64/kernel/entry.S
@@ -388,6 +388,9 @@ ENTRY(int_ret_from_sys_call)
       /* edi: mask to check */
int_with_check:
       GET_THREAD_INFO(%rcx)
+#ifdef THREAD_PERF_STAT
+       call inc_syscallcnt     # Increment syscalls counter current->sysc_cnt
+#endif /* THREAD_PERF_STAT */
       movl threadinfo_flags(%rcx),%edx
       andl %edi,%edx
       jnz   int_careful
Index: linux-2.6.18/arch/powerpc/kernel/entry_32.S
===================================================================
--- linux-2.6.18.orig/arch/powerpc/kernel/entry_32.S
+++ linux-2.6.18/arch/powerpc/kernel/entry_32.S
@@ -236,6 +236,9 @@ ret_from_syscall:
#ifdef SHOW_SYSCALLS
       bl      do_show_syscall_exit
#endif
+#ifdef THREAD_PERF_STAT
+       bl      inc_syscallcnt  /* Increment syscalls counter current->sysc_cnt */
+#endif /* THREAD_PERF_STAT */
#ifdef CONFIG_MICROSTATE
       /* FIXME: this is too early since this might not return to
          user space just yet because of syscall_exit_work */
Index: linux-2.6.18/arch/mips/kernel/entry.S
===================================================================
--- linux-2.6.18.orig/arch/mips/kernel/entry.S
+++ linux-2.6.18/arch/mips/kernel/entry.S
@@ -75,6 +75,9 @@ FEXPORT(syscall_exit)
       raw_local_irq_disable   # make sure need_resched and
                                       # signals dont change between
                                       # sampling and return
+#ifdef THREAD_PERF_STAT
+       jal     inc_syscallcnt          # Increment syscalls counter current->sysc_cnt
+#endif /* THREAD_PERF_STAT */
       LONG_L  a2, TI_FLAGS($28)       # current->work
       li      t0, _TIF_ALLWORK_MASK
       and     t0, a2, t0
Index: linux-2.6.18/lib/Kconfig.debug
===================================================================
--- linux-2.6.18.orig/lib/Kconfig.debug
+++ linux-2.6.18/lib/Kconfig.debug
@@ -539,4 +539,14 @@ config RCU_TORTURE_TEST
         Say M if you want the RCU torture tests to build as a module.
         Say N if you are unsure.

+config THREAD_PERF_STAT

+       bool "Per-process (thread) performance statistics"
+       depends on (X86 || PPC || MIPS)
+       help
+         Make available to the user the following new per-process (thread) performance statistics:
+           * Involuntary Context Switches
+           * Voluntary Context Switches
+           * Number of system calls

+ This information is available via /proc/PID/status.
+
source "lib/Kconfig.kgdb"

#1 lore raw
Quoted 1 line · Maxim Uvarov

What do you think about it? Patch is bellow.

Was this patch actually compile and run tested?

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

Quoted 9 lines

Index: linux-2.6.18/fs/proc/array.c
===================================================================

fs/proc/array.c +1 -0
@@ -295,6 +295,18 @@ static inline char *task_cap(struct task
                            cap_t(p->cap_effective));
 }
 
+#ifdef THREAD_PERF_STAT

This needs to be CONFIG_THREAD_PERF_STAT everyplace except the Kconfig stuff.

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

Quoted 9 lines
fs/proc/array.c +9 -0
+static inline char *task_perf(struct task_struct *p, char *buffer)
+{
+    return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"
+                           "Nivcsw:\t%lu\n"
+                           "Syscalls:\t%d\n",
+                           cap_t(p->nvcsw),
+                           cap_t(p->nivcsw),
+                           cap_t(p->sysc_cnt));
+}

You might want a #else that defines a null body for this inline, to avoid a problem a bit later.

fs/proc/array.c int proc_pid_status(struct task_struct * · line 342

Quoted 10 lines
fs/proc/array.c +3 -0
+#endif /* THREAD_PERF_STAT */
+
 #define get_blocked_on(t)      (-1)
 
 static char *show_blocked_on(struct task_struct *task, char *buffer)
@@ -327,6 +339,7 @@ int proc_pid_status(struct task_struct *
        buffer = task_show_regs(task, buffer);
 #endif
        buffer = show_blocked_on(task,buffer);
+       buffer = task_perf(task, buffer);

What will this call if you don't have THREAD_PERF_STAT defined?

Here's where you need the #else clause providing a null body for you....

I don't see how this could possibly have built cleanly, since the CONFIG_ issue above would have prevented you from defining a task_perf() function...

#2 lore raw

Hello again,

Please see my answers bellow:

Quoted 11 lines · [email protected] wrote:
Quoted 3 lines · Maxim Uvarov

What do you think about it? Patch is bellow.

Was this patch actually compile and run tested?

Yes I have tested it on PPC, X86, X86_64, Mips targets. It works.

Quoted 10 lines
Quoted 10 lines

Index: linux-2.6.18/fs/proc/array.c
===================================================================

fs/proc/array.c +0 -0
@@ -295,6 +295,18 @@ static inline char *task_cap(struct task
                           cap_t(p->cap_effective));
 }
 
Quoted 7 lines

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

Quoted 3 lines
fs/proc/array.c +1 -0
+#ifdef THREAD_PERF_STAT
 
 

This needs to be CONFIG_THREAD_PERF_STAT everyplace except the Kconfig stuff.

Yes you are right. I have not check it after adding ifdef :(

Quoted 34 lines

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

Quoted 13 lines
fs/proc/array.c +9 -0
+static inline char *task_perf(struct task_struct *p, char *buffer)
+{
+    return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"
+                           "Nivcsw:\t%lu\n"
+                           "Syscalls:\t%d\n",
+                           cap_t(p->nvcsw),
+                           cap_t(p->nivcsw),
+                           cap_t(p->sysc_cnt));
+}
 

You might want a #else that defines a null body for this inline, to avoid a problem a bit later.

fs/proc/array.c int proc_pid_status(struct task_struct * · line 344

Quoted 12 lines
fs/proc/array.c +2 -0
+#endif /* THREAD_PERF_STAT */
+

#define get_blocked_on(t) (-1)

static char *show_blocked_on(struct task_struct *task, char *buffer)

fs/proc/array.c +1 -0
@@ -327,6 +339,7 @@ int proc_pid_status(struct task_struct *
       buffer = task_show_regs(task, buffer);
 #endif
       buffer = show_blocked_on(task,buffer);
+       buffer = task_perf(task, buffer);
 
 

What will this call if you don't have THREAD_PERF_STAT defined?

Quoted 8 lines

Here's where you need the #else clause providing a null body for you....

I don't see how this could possibly have built cleanly, since the CONFIG_ issue above would have prevented you from defining a task_perf() function...

New version of this patch. Please flay it.

Description:
Patch adds Process Performance Statistics.

arch/i386/kernel/entry.S       |    3 +++
arch/mips/kernel/entry.S       |    3 +++
arch/powerpc/kernel/entry_32.S |    3 +++
arch/x86_64/kernel/entry.S     |    3 +++
fs/proc/array.c                |   17 +++++++++++++++++
include/linux/sched.h          |    3 +++
kernel/fork.c                  |    3 +++
kernel/sys.c                   |    8 ++++++++
lib/Kconfig.debug              |   17 +++++++++++++++++
9 files changed, 60 insertions(+)
fs/proc/array.c +0 -0
Index: linux-2.6.18/fs/proc/array.c
===================================================================
@@ -295,6 +295,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 THREAD_PERF_STAT_SYSC
+ buffer += sprintf(buffer, "Syscalls:\t%d\n", cap_t(p->sysc_cnt));
+#endif /* THREAD_PERF_STAT_SYSC */
+
+ return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"

+                           "Nivcsw:\t%lu\n",
+                           cap_t(p->nvcsw),
+                           cap_t(p->nivcsw));

+}
+#endif /* CONFIG_THREAD_PERF_STAT */
+
#define get_blocked_on(t) (-1)

static char *show_blocked_on(struct task_struct *task, char *buffer) @@ -327,6 +341,9 @@ int proc_pid_status(struct task_struct *

       buffer = task_show_regs(task, buffer);
#endif
       buffer = show_blocked_on(task,buffer);
+#ifdef CONFIG_THREAD_PERF_STAT
+       buffer = task_perf(task, buffer);
+#endif /* CONFIG_THREAD_PERF_STAT */
       return buffer - orig;
}
include/linux/sched.h +0 -0
Index: linux-2.6.18/include/linux/sched.h
===================================================================
@@ -1193,6 +1193,9 @@ struct task_struct {
       ltt_facility_t ltt_facilities[LTT_FAC_PER_PROCESS];

#endif //CONFIG_LTT_USERSPACE_GENERIC

+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+ int sysc_cnt; /* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
/*
* task-specic data
* XXX: for a future release, MontaVista-only

kernel/sys.c +0 -0
Index: linux-2.6.18/kernel/sys.c
===================================================================
@@ -2071,3 +2071,11 @@ asmlinkage long sys_prctl(int option, un
       }
       return error;

}
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+asmlinkage void inc_syscallcnt(void)
+{
+ current->sysc_cnt += 1;
+ return;
+}
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+

kernel/fork.c +3 -0
Index: linux-2.6.18/kernel/fork.c
===================================================================
@@ -1079,6 +1079,9 @@ static struct task_struct *copy_process(
       p->wchar = 0;           /* I/O counter: bytes written */
       p->syscr = 0;           /* I/O counter: read syscalls */
       p->syscw = 0;           /* I/O counter: write syscalls */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       p->sysc_cnt = 0;        /* Syscall counter: total numbers of syscalls */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
       acct_clear_integrals(p);
 
       p->it_virt_expires = cputime_zero;
arch/i386/kernel/entry.S +0 -0
Index: linux-2.6.18/arch/i386/kernel/entry.S
===================================================================
@@ -394,6 +394,9 @@ syscall_exit:
       cli                             # make sure we don't miss an 

interrupt

                                       # setting need_resched or sigpending
                                       # between sampling and the iret
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       call inc_syscallcnt             # Increment syscalls counter current->sysc_cnt
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
       TRACE_IRQS_OFF
       movl TI_flags(%ebp), %ecx
       testw $_TIF_ALLWORK_MASK, %cx   # current->work
Index: linux-2.6.18/arch/x86_64/kernel/entry.S
===================================================================
--- linux-2.6.18.orig/arch/x86_64/kernel/entry.S
+++ linux-2.6.18/arch/x86_64/kernel/entry.S
@@ -388,6 +388,9 @@ ENTRY(int_ret_from_sys_call)
       /* edi: mask to check */
int_with_check:
       GET_THREAD_INFO(%rcx)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       call inc_syscallcnt     # Increment syscalls counter current->sysc_cnt
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
       movl threadinfo_flags(%rcx),%edx
       andl %edi,%edx
       jnz   int_careful
Index: linux-2.6.18/arch/powerpc/kernel/entry_32.S
===================================================================
--- linux-2.6.18.orig/arch/powerpc/kernel/entry_32.S
+++ linux-2.6.18/arch/powerpc/kernel/entry_32.S
@@ -236,6 +236,9 @@ ret_from_syscall:
#ifdef SHOW_SYSCALLS
       bl      do_show_syscall_exit
#endif
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       bl      inc_syscallcnt  /* Increment syscalls counter current->sysc_cnt */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
#ifdef CONFIG_MICROSTATE
       /* FIXME: this is too early since this might not return to
          user space just yet because of syscall_exit_work */
Index: linux-2.6.18/arch/mips/kernel/entry.S
===================================================================
--- linux-2.6.18.orig/arch/mips/kernel/entry.S
+++ linux-2.6.18/arch/mips/kernel/entry.S
@@ -75,6 +75,9 @@ FEXPORT(syscall_exit)
       raw_local_irq_disable   # make sure need_resched and
                                       # signals dont change between
                                       # sampling and return
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       jal     inc_syscallcnt          # Increment syscalls counter current->sysc_cnt
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
       LONG_L  a2, TI_FLAGS($28)       # current->work
       li      t0, _TIF_ALLWORK_MASK
       and     t0, a2, t0
Index: linux-2.6.18/lib/Kconfig.debug
===================================================================
--- linux-2.6.18.orig/lib/Kconfig.debug
+++ linux-2.6.18/lib/Kconfig.debug
@@ -539,4 +539,21 @@ config RCU_TORTURE_TEST
         Say M if you want the RCU torture tests to build as a module.
         Say N if you are unsure.

+config THREAD_PERF_STAT

+       bool "Per-process (thread) performance statistics"
+       depends on (X86 || PPC || MIPS)
+       help
+         Make available to the user the following new per-process (thread) performance statistics:
+           * Involuntary Context Switches
+           * Voluntary Context Switches
+           * Number of system calls (option)
+         This information is available via /proc/PID/status.
+
+config THREAD_PERF_STAT_SYSC
+       bool "enable syscall counter"
+       depends on THREAD_PERF_STAT
+       help

+ This option add syscalls counter to /proc/PID/status.
+
+
source "lib/Kconfig.kgdb"

#3 lore raw
Quoted 10 lines · On Wed, 04 Apr 2007 17:15:43 +0400 Maxim Uvarov <[email protected]> wrote:
arch/i386/kernel/entry.S +0 -0
@@ -394,6 +394,9 @@ syscall_exit:
        cli                             # make sure we don't miss an 

interrupt

                                        # setting need_resched or sigpending
                                        # between sampling and the iret
arch/i386/kernel/entry.S +2 -0
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       call inc_syscallcnt             # Increment syscalls counter 
Quoted 2 lines

current->sysc_cnt
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */

Please dont call a function to do one increment !

You are touching one of the most critical part of the kernel...

Also, dont do this while interrupts are masked, this is not necessary.

Maybe better to place sysc_cnt in 'struct thread_info' because %ebp point to it.

Also you missed the fact that syscalls on i386 can use sysenter (around line 343)

Quoted 4 lines

TRACE_IRQS_OFF
movl TI_flags(%ebp), %ecx
testw $_TIF_ALLWORK_MASK, %cx # current->work

#7 lore raw
In reply to Maxim Uvarov
Quoted 2 lines · Maxim Uvarov

Hello again,

[snip]

Quoted 2 lines

New version of this patch. Please flay it.

A few small comments below.

Quoted 7 lines
(quoted diff) +5 -0
+config THREAD_PERF_STAT
+       bool "Per-process (thread) performance statistics"
+       depends on (X86 || PPC || MIPS)
+       help
+         Make available to the user the following new per-process

(thread) performance statistics:

Get rid of the word "new" - it won't continue to be a "new" feature forever :-)

Review

Quoted 3 lines
(quoted diff) +3 -0
+           * Involuntary Context Switches
+           * Voluntary Context Switches
+           * Number of system calls (option)

In the first two lines you Capitalize Each Word, but in the last line you don't - why?
Shouldn't the first two have "Number of" prepended?
Perhaps it's just me, but I would write "(optional)" instead of "(option)".

In short, I'd suggest this :

* Number of involuntary context switches
* Number of voluntary context switches
* Number of system calls (optional)

Quoted 1 line

+ This information is available via /proc/PID/status.

What about tools that currently parse /proc/PID/status ? Don't you
risk breaking userland stuff by changing this file? Wouldn't it be
better to use a new file?

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+config THREAD_PERF_STAT_SYSC
+       bool "enable syscall counter"

Capitalize the first word; "Enable syscall counter" .

Review

Quoted 3 lines
(quoted diff) +3 -0
+       depends on THREAD_PERF_STAT
+       help
+          This option add syscalls counter to  /proc/PID/status.

I'd probably have written "This option adds a syscall counter to /proc/PID/status."

Signature · 3 lines
Jesper Juhl <[email protected]>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html
#4 lore raw
In reply to Eric Dumazet

Hello Eric,

I changed patch according to your comments.
Could you please take a look at it?

Quoted 5 lines · Eric Dumazet

On Wed, 04 Apr 2007 17:15:43 +0400
Maxim Uvarov <[email protected]> wrote:

Quoted 17 lines
Quoted 9 lines
arch/i386/kernel/entry.S +0 -0
@@ -394,6 +394,9 @@ syscall_exit:
       cli                             # make sure we don't miss an 

interrupt

                                       # setting need_resched or sigpending
                                       # between sampling and the iret
arch/i386/kernel/entry.S +2 -0
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       call inc_syscallcnt             # Increment syscalls counter 
Quoted 4 lines

current->sysc_cnt
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */

Please dont call a function to do one increment !

Yes, you are right. I have changed it.

Quoted 18 lines

You are touching one of the most critical part of the kernel...

Also, dont do this while interrupts are masked, this is not necessary.

Maybe better to place sysc_cnt in 'struct thread_info' because %ebp point to it.

Also you missed the fact that syscalls on i386 can use sysenter (around line 343)

Quoted 6 lines

TRACE_IRQS_OFF
movl TI_flags(%ebp), %ecx
testw $_TIF_ALLWORK_MASK, %cx # current->work

I rewrote this patch for x86. And I have question. Should /proc/PID/status
be used for these counters and this option be selectable or it will better to create another file in /proc/PID/ directory?

I will include syscall counters for other arches later.

P.S.
Jesper Juhl, thank you.

Best regards,
Maxim.

Description:
Patch adds Process Performance Statistics.

arch/i386/kernel/asm-offsets.c |    3 +++
arch/i386/kernel/entry.S       |    6 ++++++
fs/proc/array.c                |   14 ++++++++++++++
include/asm/thread_info.h      |    3 +++
kernel/fork.c                  |    3 +++
lib/Kconfig.debug              |   16 ++++++++++++++++
6 files changed, 45 insertions(+)
fs/proc/array.c +0 -0
Index: linux-2.6.18/fs/proc/array.c
===================================================================
@@ -295,6 +295,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", cap_t(p->thread_info->sysc_cnt));
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
+
+ return buffer + sprintf(buffer, "Nvcsw:\t%lu\n"

+                           "Nivcsw:\t%lu\n",
+                           cap_t(p->nvcsw),
+                           cap_t(p->nivcsw));

+}
+#endif /* CONFIG_THREAD_PERF_STAT */
+
#define get_blocked_on(t) (-1)

static char *show_blocked_on(struct task_struct *task, char *buffer)

arch/i386/kernel/entry.S +3 -0
Index: linux-2.6.18/arch/i386/kernel/entry.S
===================================================================
@@ -334,6 +334,9 @@ sysenter_past_esp:
       jae syscall_badsys
       call *sys_call_table(,%eax,4)
       movl %eax,EAX(%esp)
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       incl    TI_sysc_cnt(%ebp)       # Increment syscalls counter
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */

#ifdef CONFIG_MICROSTATE
call msa_end_syscall
#endif
@@ -388,6 +391,9 @@ syscall_call:
call *sys_call_table(,%eax,4)
movl %eax,EAX(%esp) # store the return value
syscall_exit:
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+ incl TI_sysc_cnt(%ebp) # Increment syscalls counter current->sysc_cnt
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
#ifdef CONFIG_MICROSTATE
call msa_end_syscall
#endif

lib/Kconfig.debug +16 -0
Index: linux-2.6.18/lib/Kconfig.debug
===================================================================
@@ -539,4 +539,20 @@ config RCU_TORTURE_TEST
         Say M if you want the RCU torture tests to build as a module.
         Say N if you are unsure.
 
+config THREAD_PERF_STAT
+       bool "Per-process (thread) performance statistics"
+       depends on X86
+       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
+       help
+         This option adds a syscall counter to /proc/PID/status.
+

source "lib/Kconfig.kgdb"

include/asm/thread_info.h +3 -0
Index: linux-2.6.18/include/asm/thread_info.h
===================================================================
@@ -44,6 +44,9 @@ struct thread_info {
                                                  of nested (IRQ) stacks
                                               */
       __u8                    supervisor_stack[0];
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       unsigned long           sysc_cnt;           /* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */

};

#else /* !__ASSEMBLY__ */

kernel/fork.c +3 -0
Index: linux-2.6.18/kernel/fork.c
===================================================================
@@ -1079,6 +1079,9 @@ static struct task_struct *copy_process(
       p->wchar = 0;           /* I/O counter: bytes written */
       p->syscr = 0;           /* I/O counter: read syscalls */
       p->syscw = 0;           /* I/O counter: write syscalls */
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       p->thread_info->sysc_cnt = 0;   /* Syscall counter: total numbers of syscalls */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
       acct_clear_integrals(p);
 
       p->it_virt_expires = cputime_zero;
arch/i386/kernel/asm-offsets.c +3 -0
Index: linux-2.6.18/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();
 
#5 lore raw
Quoted 5 lines · On Wed, 04 Apr 2007 20:52:34 +0400 Maxim Uvarov <[email protected]> wrote:

Index: linux-2.6.18/include/asm/thread_info.h
===================================================================

include/asm/thread_info.h struct thread_info { · line 52

Quoted 9 lines
include/asm/thread_info.h +3 -0
@@ -44,6 +44,9 @@ struct thread_info {
                                                   of nested (IRQ) stacks
                                                */
        __u8                    supervisor_stack[0];
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       unsigned long           sysc_cnt;           /* Syscall counter */
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */
 };
 
 #else /* !__ASSEMBLY__ */

You probably meant include/asm-i386/thread_info.h, not include/asm/thread_info.h

Then, you should not place sysc_cnt after supervisor_stack[0], since supervisor_stack[0] should be the last element of struct thread_info.

Better to put sysc_cnt right after preempt_count for example.

Also, is there any reason your patches are against 2.6.18, and not current kernel ?

#8 lore raw
In reply to Maxim Uvarov
Quoted 10 lines · Maxim Uvarov

New version of this patch. Please flay it.

Index: linux-2.6.18/fs/proc/array.c
===================================================================

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

Quoted 7 lines
fs/proc/array.c +4 -0
@@ -295,6 +295,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 THREAD_PERF_STAT_SYSC

Missed the CONFIG_ here.

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

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

cap_t()??!? That's from include/linux/capability.h, and looks something like:

#ifdef STRICT_CAP_T_TYPECHECKS
#define cap_t(x) (x).cap
#else
#define cap_t(x) (x)
#endif

and you're probably picking up the second part, making it a no-op. And if you ever hit the first part of that ifdef, you'll throw a compile error.

Somebody else can comment on the use of #ifdef - we tend to frown on it inside open C code, but I'm not seeing a really brilliant way to avoid them entirely (the 'static inline task_perf' can probably move to a .h, but it's hard to find a clean way to avoid the ifdefs given that we have Kconfig variables to select it. array.c already has a CONFIG_S390 in it, anyhow. :)

There's a mostly-hypothetical race between inc_syscall() and the places that increment the context switch counters, and where we read the values - but at worst, we'll output a stale off-by-one-ish value. Certainly not worth grabbing a lock on the task struct for *this* usage, but the sort of thing you want to keep in mind as you write other code.

Other random comments:

1) You probably want to rebase against something more recent (2.6.21-rc<mumble> or the final .21 when it's released).

2) It arrived here with some line-wrapping damage, most likely to the fact that you posted it with Thunderbird. There's a mystic Thunderbird incantation to make it not do that, but I have no idea what it is - it's in the list archives someplace.

#6 lore raw
In reply to Eric Dumazet
Quoted 11 lines · Eric Dumazet
Quoted 4 lines · On Wed, 04 Apr 2007 17:15:43 +0400 Maxim Uvarov <[email protected]> wrote:
(quoted diff) +2 -0
+#ifdef CONFIG_THREAD_PERF_STAT_SYSC
+       call inc_syscallcnt             # Increment syscalls counter 

current->sysc_cnt
+#endif /* CONFIG_THREAD_PERF_STAT_SYSC */

Please dont call a function to do one increment !

You are touching one of the most critical part of the kernel...

Obviously Eric and I looked at this looking for totally different things. :)

Thanks for spotting that, I totally failed to notice that part....

#9 lore raw
In reply to [email protected]
Quoted 9 lines · On Wed, 04 Apr 2007 17:50:50 -0400 [email protected] wrote:

Other random comments:

1) You probably want to rebase against something more recent (2.6.21-rc<mumble> or the final .21 when it's released).

2) It arrived here with some line-wrapping damage, most likely to the fact that you posted it with Thunderbird. There's a mystic Thunderbird incantation to make it not do that, but I have no idea what it is - it's in the list archives someplace.

This may help (or not):
http://mbligh.org/linuxdocs/Email/Clients/Thunderbird

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#10 lore raw
In reply to [email protected]
Quoted 4 lines · [email protected] wrote:

2) It arrived here with some line-wrapping damage, most likely to the fact that you posted it with Thunderbird. There's a mystic Thunderbird incantation to make it not do that, but I have no idea what it is - it's in the list archives someplace.

I don't use TBird (seamonkey fan) but I assume the patch can just be attached rather than inlined. Some mailers are pretty arcane otherwise.

But I do like the idea, but the issue of things which parse /proc/PID/status hasn't had comments. A good parser would ignore what it didn't understand, or take everything, not everyone has a good parser. ;-)

Signature · 3 lines
Bill Davidsen <[email protected]>
  "We have more to fear from the bungling of the incompetent than from
the machinations of the wicked."  - from Slashdot
#11 lore raw
In reply to Maxim Uvarov

Hi!

Quoted 5 lines

Yes I have tested it on PPC, X86, X86_64, Mips targets. It works.

Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+                           cap_t(p->nvcsw),
+                           cap_t(p->nivcsw),

Nvcsw? W dn't s ncrptd dntfrs lk nvcsw n krnl. (:-)

Pavel

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