How to finding all runnable processes

[亡魂溺海] 提交于 2021-02-19 04:12:48

问题


I'm learning about the scheduler and trying to print all runnable proceeses. So I have written a kernel module that uses the for_each_process macro to iterate over all processes, and prints the ones at "runnable" state. But this seems like a stupid (and inefficient) way of doing this. So I thought about getting a reference to all running queues and use their Red-Black-Tree to go over the runnable processes, but couldn't find a way to do this.

I have found out that there is a list of sched_classs for each CPU which are stop_sched_class->rt_sched_class->fair_sched_class->idle_sched_class and each one of them has it's own running queue. But couldn't find a way to reach them all.

I have used the module that uses the tasks_timeline to find all runnable processes, to print the address of the running queues - seems I have 3 running queues (while having only two processors).

The module:

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */
#include <linux/sched.h>

MODULE_LICENSE("GPL");

struct cfs_rq {
        struct load_weight load;
        unsigned int nr_running, h_nr_running;
};

void printList(void){
    int count;
    struct task_struct * tsk;

    count = 0;
    for_each_process(tsk){
        if(tsk->state)
            continue;
        printk("pid: %d rq: %p (%d)\n", tsk->pid, tsk->se.cfs_rq, tsk->se.cfs_rq->nr_running);
        count++;
    }
    printk("count is: %d\n", count);
}

int init_module(void)
{
    printList();

    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world proc.\n");
}

The output:

[ 8215.627038] pid: 9147 ffff88007bbe9200 (3)
[ 8215.627043] pid: 9148 ffff8800369b0200 (2)
[ 8215.627045] pid: 9149 ffff8800369b0200 (2)
[ 8215.627047] pid: 9150 ffff88007bbe9200 (3)
[ 8215.627049] pid: 9151 ffff88007bbe9200 (3)
[ 8215.627051] pid: 9154 ffff8800a46d4600 (1)
[ 8215.627053] count is: 6
[ 8215.653741] Goodbye world proc.

About the computer:

$ uname -a
Linux k 3.13.0-39-generic #66-Ubuntu SMP Tue Oct 28 13:30:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ cat /proc/cpuinfo | grep 'processor' | wc -l
2

So my questions are:

  1. How can I print all runnable processes in a nicer way?
  2. How are running queues made and managed?
  3. Are the running queues somehow linked each other? (How?)

回答1:


$ps -A -l and find the instance where both the process state (R) and the Process Flags (1) are as mentioned.




回答2:


You can try this below cmd.

Sample output.

127:~$ ps -A -l | grep -e R -e D
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
1 S     0  1367     2  0  80   0 -     0 -      ?        00:00:01 SEPDRV_ABNORMAL
4 R  1000  2634  2569  2  80   0 - 794239 -     ?        00:25:06 Web Content
1 D     0 20091     2  0  80   0 -     0 -      ?        00:00:00 kworker/3:2
4 R  1000 21077  9361  0  80   0 -  7229 -      pts/17   00:00:00 ps


来源:https://stackoverflow.com/questions/26862357/how-to-finding-all-runnable-processes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!