13

According to proc manual:

/proc/[pid]/stack (since Linux 2.6.29)

This file provides a symbolic trace of the function calls in this process's kernel stack. This file is provided only if the kernel was built with the CONFIG_STACKTRACE configuration option.

So I write a program to test:

#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>

void *thread_func(void *p_arg)
{
        pid_t pid = fork();
        if (pid > 0) {
            wait(NULL);
            return 0;
        } else if (pid == 0) {
            sleep(1000);
            return 0;
        }
        return NULL;
}
int main(void)
{
        pthread_t t1, t2;

        pthread_create(&t1, NULL, thread_func, "Thread 1");
        pthread_create(&t2, NULL, thread_func, "Thread 2");

        sleep(1000);
        return 0;
}

After running, use pstack to check the threads of progress:

linux-uibj:~ # pstack 24976
Thread 3 (Thread 0x7fd6e4ed5700 (LWP 24977)):
#0  0x00007fd6e528d3f4 in wait () from /lib64/libpthread.so.0
#1  0x0000000000400744 in thread_func ()
#2  0x00007fd6e52860a4 in start_thread () from /lib64/libpthread.so.0
#3  0x00007fd6e4fbb7fd in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7fd6e46d4700 (LWP 24978)):
#0  0x00007fd6e528d3f4 in wait () from /lib64/libpthread.so.0
#1  0x0000000000400744 in thread_func ()
#2  0x00007fd6e52860a4 in start_thread () from /lib64/libpthread.so.0
#3  0x00007fd6e4fbb7fd in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7fd6e569f700 (LWP 24976)):
#0  0x00007fd6e4f8d6cd in nanosleep () from /lib64/libc.so.6
#1  0x00007fd6e4f8d564 in sleep () from /lib64/libc.so.6
#2  0x00000000004007b1 in main ()

At the same time, check /proc/24976/stack:

linux-uibj:~ # cat /proc/24976/stack
[<ffffffff804ba1a7>] system_call_fastpath+0x16/0x1b
[<00007fd6e4f8d6cd>] 0x7fd6e4f8d6cd
[<ffffffffffffffff>] 0xffffffffffffffff

The 24976 process has 3 threads, and they all block on system call(nanosleep and wait), so all 3 threads now work in kernel space, and turn into kernel threads now, right? If this is true, there should be 3 stacks in /proc/[pid]/stack file. But it seems there is only 1 stack in /proc/[pid]/stack file.

How should I understand /proc/[pid]/stack?

Sam Protsenko
  • 14,045
  • 4
  • 59
  • 75
Nan Xiao
  • 16,671
  • 18
  • 103
  • 164

3 Answers3

6

How should I understand /proc/[pid]/stack ?

Taken from the man pages for proc:

There are additional helpful pseudo-paths:

[stack] The initial process's (also known as the main thread's) stack.

Just below this, you can find:

[stack:[tid]] (since Linux 3.4)

A thread's stack (where the [tid] is a thread ID). It corresponds to the /proc/[pid]/task/[tid]/path.

Which seems to be what you are looking for.

Joe Watkins
  • 17,032
  • 5
  • 41
  • 62
  • 5
    After your hints, I get the answer: on Linux, thread is actually a process, so `/proc/[tid]/stack` will get the thread's kernel stack info, or use `/proc/[pid]/task/[tid]/stack`. – Nan Xiao Oct 30 '15 at 07:40
1

Nan Xiao is right.
Thread kernel mode stack is under /proc/[PID]/task/[TID]/stack.

you are checking /proc/[PID]/stack, that's the main thread stack so you have only 1. Others are under task folder.

hguo
  • 11
  • 1
-1

That is for sleep locks. You might also look at perf -g to see spin locks including high system time.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
Dave
  • 1