Find the answer to your Linux question:
Results 1 to 5 of 5
Hi guys, first message here. I'd like to retrieve the current cpu id where the thread is running. I know the getcpu() system call, but it requires kernel 2.6.19 while ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2012
    Posts
    2

    getcpu() - system call for kernel 2.6.18


    Hi guys,
    first message here. I'd like to retrieve the current cpu id where the thread is running.
    I know the getcpu() system call, but it requires kernel 2.6.19 while my kernel is 2.6.18 :oO. Isthere any alternative?

    regards
    -s.fox

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,454
    You might find something in /proc/<pid> where <pid> is the task's process id. As you noted, getcpu() is only valid for kernels from 2.6.19 and later. So, there is nothing there for RHEL 5 or other distributions stuck at the 2.6.18 or earlier kernel. Sorry I can't be more specific than that. I munged through my /proc and didn't come up with anything that is clearly the cpu associated with a specific process.

    I'm going to boot up my CentOS 5 VM and see if I can dig up more for you.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,454
    Ok. No joy in Mudville! Sorry, but I can't find anything in /proc for 2.6.18 that will help. There are some entries that I don't understand, so that doesn't mean the data isn't there, but I just don't know where it is!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,454
    So, after some more Googling and checking, here is a work around. You can use the command "ps -aeF | grep <pid>" and look at the PSR column to find the current cpu the task is running on. This definitely works for 2.6.18 kernels. Next step is to look at the source for ps to find how it determines which CPU is running the process. Once you do that, you should be able to create your own library function, or use the appropriate library function, to get the CPU where the process is running. Without spending a lot more time, that is as definitive an answer as I can get you. I hope this helps!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Sep 2012
    Posts
    2
    As you may have figured out, I am stuck with CentOS 5. This is a kind of workaround:
    Code:
    // for kernel < 2.6.19
    pid_t pid = syscall(SYS_getpid);
    std::stringstream stringbuffer;
    int cpu_id;
    stringbuffer << "cat /proc/" << pid << "/stat | awk '{print $39}'";
    FILE* file = popen(stringbuffer.str().c_str(), "r");
    if (fscanf(file, "%d", &cpu_id) == EOF){
        cpu_id = -1;
    }
     pclose(file);
    
    return cpu_id;
    Thanks for your help.
    Regards
    - sfox

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •