プロセス管理

主要な構造体

ランキュー 構造体(sys/sys/runq.h)

#define     RQ_NQS  (howmany(RQ_MAX_PRIO + 1, RQ_PPQ)) /* Number of run queues. */

TAILQ_HEAD(rq_queue, thread);

struct runq {
    struct rq_status        rq_status;
    struct rq_queue         rq_queues[RQ_NQS];
};

sys/sys/queue.c

#define TAILQ_HEAD(name, type)                              \
struct name {                                               \
    struct type *tqh_first; /* first element */             \
    struct type **tqh_last; /* addr of last next element */ \
    TRACEBUF                                                \
}

struct qm_trace {
    unsigned long    lastline;
    unsigned long    prevline;
    const char      *lastfile;
    const char      *prevfile;
};

#ifdef QUEUE_MACRO_DEBUG_TRACE
#define TRACEBUF    struct qm_trace trace;
#else
#define TRACEBUF
#endif      /* QUEUE_MACRO_DEBUG_TRACE */

プロセス 構造体(sys/sys/proc.h)

/*
 * Process structure.
 */
struct proc {
    LIST_ENTRY(proc) p_list;        /* (d) List of all processes. */
    TAILQ_HEAD(, thread) p_threads; /* (c) all threads. */
    struct mtx      p_slock;        /* process spin lock */
    struct ucred    *p_ucred;       /* (c) Process owner's identity. */
    struct filedesc *p_fd;          /* (b) Open files. */
    struct filedesc_to_leader *p_fdtol; /* (b) Tracking node */
    struct pwddesc  *p_pd;          /* (b) Cwd, chroot, jail, umask */
    struct pstats   *p_stats;       /* (b) Accounting/statistics (CPU). */
    struct plimit   *p_limit;       /* (c) Resource limits. */
    struct callout  p_limco;        /* (c) Limit callout handle */
    struct sigacts  *p_sigacts;     /* (x) Signal actions, state (CPU). */

    int             p_flag;         /* (c) P_* flags. */
    int             p_flag2;        /* (c) P2_* flags. */
    enum p_states {
        PRS_NEW = 0,                /* In creation */
        PRS_NORMAL,         /* threads can be run. */
        PRS_ZOMBIE
    } p_state;                      /* (j/c) Process status. */
    pid_t           p_pid;          /* (b) Process identifier. */
    LIST_ENTRY(proc) p_hash;        /* (d) Hash chain. */
    LIST_ENTRY(proc) p_pglist;      /* (g + e) List of processes in pgrp. */
    struct proc     *p_pptr;        /* (c + e) Pointer to parent process. */
    LIST_ENTRY(proc) p_sibling;     /* (e) List of sibling processes. */
    LIST_HEAD(, proc) p_children;   /* (e) Pointer to list of children. */
    struct proc     *p_reaper;      /* (e) My reaper. */
    LIST_HEAD(, proc) p_reaplist;   /* (e) List of my descendants
                        (if I am reaper). */
    LIST_ENTRY(proc) p_reapsibling; /* (e) List of siblings - descendants of
                        the same reaper. */
    struct mtx      p_mtx;          /* (n) Lock for this struct. */
    struct mtx      p_statmtx;      /* Lock for the stats */
    struct mtx      p_itimmtx;      /* Lock for the virt/prof timers */
    struct mtx      p_profmtx;      /* Lock for the profiling */
    struct ksiginfo *p_ksi; /* Locked by parent proc lock */
    sigqueue_t      p_sigqueue;     /* (c) Sigs not delivered to a td. */
#define p_siglist   p_sigqueue.sq_signals
    pid_t           p_oppid;        /* (c + e) Real parent pid. */

/* The following fields are all zeroed upon creation in fork. */
#define     p_startzero     p_vmspace
    struct vmspace  *p_vmspace;     /* (b) Address space. */
    u_int           p_swtick;       /* (c) Tick when swapped in or out. */
    u_int           p_cowgen;       /* (c) Generation of COW pointers. */
    struct itimerval p_realtimer;   /* (c) Alarm timer. */
    struct rusage   p_ru;           /* (a) Exit information. */
    struct rusage_ext p_rux;        /* (cu) Internal resource usage. */
    struct rusage_ext p_crux;       /* (c) Internal child resource usage. */
    int             p_profthreads;  /* (c) Num threads in addupc_task. */
    volatile int    p_exitthreads;  /* (j) Number of threads exiting */
    int             p_traceflag;    /* (o) Kernel trace points. */
    struct ktr_io_params    *p_ktrioparms;  /* (c + o) Params for ktrace. */
    struct vnode    *p_textvp;      /* (b) Vnode of executable. */
    struct vnode    *p_textdvp;     /* (b) Dir containing textvp. */
    char            *p_binname;     /* (b) Binary hardlink name. */
    u_int           p_lock;         /* (c) Prevent exit. */
    struct sigiolst p_sigiolst;     /* (c) List of sigio sources. */
    int             p_sigparent;    /* (c) Signal to parent on exit. */
    int             p_sig;          /* (n) For core dump/debugger XXX. */
    u_int           p_ptevents;     /* (c + e) ptrace() event mask. */
    struct kaioinfo *p_aioinfo;     /* (y) ASYNC I/O info. */
    struct thread   *p_singlethread;/* (c + j) If single threading this is it */
    int             p_suspcount;    /* (j) Num threads in suspended mode. */
    struct thread   *p_xthread;     /* (c) Trap thread */
    int             p_boundary_count;/* (j) Num threads at user boundary */
    int             p_pendingcnt;   /* (c) how many signals are pending */
    struct itimers  *p_itimers;     /* (c) POSIX interval timers. */
    struct procdesc *p_procdesc;    /* (e) Process descriptor, if any. */
    u_int           p_treeflag;     /* (e) P_TREE flags */
    int             p_pendingexits; /* (c) Count of pending thread exits. */
    struct filemon  *p_filemon;     /* (c) filemon-specific data. */
    int             p_pdeathsig;    /* (c) Signal from parent on exit. */
/* End area that is zeroed on creation. */
#define     p_endzero       p_magic

/* The following fields are all copied upon creation in fork. */
#define     p_startcopy     p_endzero
    u_int           p_magic;        /* (b) Magic number. */
    int             p_osrel;        /* (x) osreldate for the
                        binary (from ELF note, if any) */
    uint32_t        p_fctl0;        /* (x) ABI feature control, ELF note */
    char            p_comm[MAXCOMLEN + 1];  /* (x) Process name. */
    struct sysentvec *p_sysent;     /* (b) Syscall dispatch info. */
    struct pargs    *p_args;        /* (c) Process arguments. */
    rlim_t          p_cpulimit;     /* (c) Current CPU limit in seconds. */
    signed char     p_nice;         /* (c) Process "nice" value. */
    int             p_fibnum;       /* in this routing domain XXX MRT */
    pid_t           p_reapsubtree;  /* (e) Pid of the direct child of the
                        reaper which spawned
                        our subtree. */
    uint64_t        p_elf_flags;    /* (x) ELF flags */
    const void      *p_elf_brandinfo; /* (x) Elf_Brandinfo, NULL for
                        non ELF binaries. */
    sbintime_t      p_umtx_min_timeout;
/* End area that is copied on creation. */
#define     p_endcopy       p_xexit

    u_int           p_xexit;        /* (c) Exit code. */
    u_int           p_xsig;         /* (c) Stop/kill sig. */
    struct pgrp     *p_pgrp;        /* (c + e) Pointer to process group. */
    struct knlist   *p_klist;       /* (c) Knotes attached to this proc. */
    int             p_numthreads;   /* (c) Number of threads. */
    struct mdproc   p_md;           /* Any machine-dependent fields. */
    struct callout  p_itcallout;    /* (h + c) Interval timer callout. */
    u_short         p_acflag;       /* (c) Accounting flags. */
    struct proc     *p_peers;       /* (r) */
    struct proc     *p_leader;      /* (b) */
    void            *p_emuldata;    /* (c) Emulator state data. */
    struct label    *p_label;       /* (*) Proc (not subject) MAC label. */
    STAILQ_HEAD(, ktr_request)      p_ktr;  /* (o) KTR event queue. */
    LIST_HEAD(, mqueue_notifier)    p_mqnotifier; /* (c) mqueue notifiers.*/
    struct kdtrace_proc     *p_dtrace; /* (*) DTrace-specific data. */
    struct cv       p_pwait;        /* (*) wait cv for exit/exec. */
    struct racct    *p_racct;       /* (b) Resource accounting. */
    int             p_throttled;    /* (c) Flag for racct pcpu throttling */
    /*
    * An orphan is the child that has been re-parented to the
    * debugger as a result of attaching to it.  Need to keep
    * track of them for parent to be able to collect the exit
    * status of what used to be children.
    */
    LIST_ENTRY(proc) p_orphan;      /* (e) List of orphan processes. */
    LIST_HEAD(, proc) p_orphans;    /* (e) Pointer to list of orphans. */

    TAILQ_HEAD(, kq_timer_cb_data)  p_kqtim_stop;   /* (c) */
    LIST_ENTRY(proc) p_jaillist;    /* (d) Jail process linkage. */
};

スレッド 構造体(sys/sys/proc.h)

/*
 * Kernel runnable context (thread).
 * This is what is put to sleep and reactivated.
 * Thread context.  Processes may have multiple threads.
 */
struct thread {
    struct mtx      *volatile td_lock; /* replaces sched lock */
    struct proc     *td_proc;       /* (*) Associated process. */
    TAILQ_ENTRY(thread) td_plist;   /* (*) All threads in this proc. */
    TAILQ_ENTRY(thread) td_runq;    /* (t) Run queue. */
    union   {
        TAILQ_ENTRY(thread) td_slpq;        /* (t) Sleep queue. */
        struct thread *td_zombie; /* Zombie list linkage */
    };
    TAILQ_ENTRY(thread) td_lockq;   /* (t) Lock queue. */
    LIST_ENTRY(thread) td_hash;     /* (d) Hash chain. */
    struct cpuset   *td_cpuset;     /* (t) CPU affinity mask. */
    struct domainset_ref td_domain; /* (a) NUMA policy */
    struct seltd    *td_sel;        /* Select queue/channel. */
    struct sleepqueue *td_sleepqueue; /* (k) Associated sleep queue. */
    struct turnstile *td_turnstile; /* (k) Associated turnstile. */
    void            *td_pad1;       /* Available */
    struct umtx_q   *td_umtxq;      /* (c?) Link for when we're blocked. */
    lwpid_t         td_tid;         /* (b) Thread ID. */
    sigqueue_t      td_sigqueue;    /* (c) Sigs arrived, not delivered. */
#define     td_siglist      td_sigqueue.sq_signals
    u_char          td_lend_user_pri; /* (t) Lend user pri. */
    u_char          td_allocdomain; /* (b) NUMA domain backing this struct thread. */
    u_char          td_base_ithread_pri; /* (t) Base ithread pri */
    struct kmsan_td *td_kmsan;      /* (k) KMSAN state */

/* Cleared during fork1(), thread_create(), or kthread_add(). */
#define     td_startzero td_flags
    int             td_flags;       /* (t) TDF_* flags. */
    int             td_ast;         /* (t) TDA_* indicators */
    int             td_inhibitors;  /* (t) Why can not run. */
    int             td_pflags;      /* (k) Private thread (TDP_*) flags. */
    int             td_pflags2;     /* (k) Private thread (TDP2_*) flags. */
    int             td_dupfd;       /* (k) Ret value from fdopen. XXX */
    int             td_sqqueue;     /* (t) Sleepqueue queue blocked on. */
    const void      *td_wchan;      /* (t) Sleep address. */
    const char      *td_wmesg;      /* (t) Reason for sleep. */
    volatile u_char td_owepreempt;  /* (k*) Preempt on last critical_exit */
    u_char          td_tsqueue;     /* (t) Turnstile queue blocked on. */
    u_char          _td_pad0[2];    /* Available. */
    int             td_locks;       /* (k) Debug: count of non-spin locks */
    int             td_rw_rlocks;   /* (k) Count of rwlock read locks. */
    int             td_sx_slocks;   /* (k) Count of sx shared locks. */
    int             td_lk_slocks;   /* (k) Count of lockmgr shared locks. */
    struct lock_object *td_wantedlock; /* (k) Lock we are contending on */
    struct turnstile *td_blocked;   /* (t) Lock thread is blocked on. */
    const char      *td_lockname;   /* (t) Name of lock blocked on. */
    LIST_HEAD(, turnstile) td_contested;    /* (q) Contested locks. */
    struct lock_list_entry *td_sleeplocks; /* (k) Held sleep locks. */
    int             td_intr_nesting_level; /* (k) Interrupt recursion. */
    int             td_pinned;      /* (k) Temporary cpu pin count. */
    struct ucred    *td_realucred;  /* (k) Reference to credentials. */
    struct ucred    *td_ucred;      /* (k) Used credentials, temporarily switchable. */
    struct plimit   *td_limit;      /* (k) Resource limits. */
    int             td_slptick;     /* (t) Time at sleep. */
    int             td_blktick;     /* (t) Time spent blocked. */
    int             td_swvoltick;   /* (t) Time at last SW_VOL switch. */
    int             td_swinvoltick; /* (t) Time at last SW_INVOL switch. */
    u_int           td_cow;         /* (*) Number of copy-on-write faults */
    struct rusage   td_ru;          /* (t) rusage information. */
    struct rusage_ext td_rux;       /* (t) Internal rusage information. */
    uint64_t        td_incruntime;  /* (t) Cpu ticks to transfer to proc. */
    uint64_t        td_runtime;     /* (t) How many cpu ticks we've run. */
    u_int           td_pticks;      /* (t) Statclock hits for profiling */
    u_int           td_sticks;      /* (t) Statclock hits in system mode. */
    u_int           td_iticks;      /* (t) Statclock hits in intr mode. */
    u_int           td_uticks;      /* (t) Statclock hits in user mode. */
    int             td_intrval;     /* (t) Return value for sleepq. */
    sigset_t        td_oldsigmask;  /* (k) Saved mask from pre sigpause. */
    volatile u_int  td_generation;  /* (k) For detection of preemption */
    stack_t         td_sigstk;      /* (k) Stack ptr and on-stack flag. */
    int             td_xsig;        /* (c) Signal for ptrace */
    u_long          td_profil_addr; /* (k) Temporary addr until AST. */
    u_int           td_profil_ticks; /* (k) Temporary ticks until AST. */
    char            td_name[MAXCOMLEN + 1]; /* (*) Thread name. */
    struct file     *td_fpop;       /* (k) file referencing cdev under op */
    int             td_dbgflags;    /* (c) Userland debugger flags */
    siginfo_t       td_si;          /* (c) For debugger or core file */
    int             td_ng_outbound; /* (k) Thread entered ng from above. */
    struct osd      td_osd;         /* (k) Object specific data. */
    struct vm_map_entry *td_map_def_user; /* (k) Deferred entries. */
    pid_t           td_dbg_forked;  /* (c) Child pid for debugger. */
    u_int           td_no_sleeping; /* (k) Sleeping disabled count. */
    struct vnode    *td_vp_reserved;/* (k) Preallocated vnode. */
    void            *td_su;         /* (k) FFS SU private */
    sbintime_t      td_sleeptimo;   /* (t) Sleep timeout. */
    int             td_rtcgen;      /* (s) rtc_generation of abs. sleep */
    int             td_errno;       /* (k) Error from last syscall. */
    size_t          td_vslock_sz;   /* (k) amount of vslock-ed space */
    struct kcov_info *td_kcov_info; /* (*) Kernel code coverage data */
    long            td_ucredref;    /* (k) references on td_realucred */
    struct kexterr  td_kexterr;
#define     td_endzero td_sigmask

/* Copied during fork1(), thread_create(), or kthread_add(). */
#define     td_startcopy td_endzero
    sigset_t        td_sigmask;     /* (c) Current signal mask. */
    u_char          td_rqindex;     /* (t) Run queue index. */
    u_char          td_base_pri;    /* (t) Thread base kernel priority. */
    u_char          td_priority;    /* (t) Thread active priority. */
    u_char          td_pri_class;   /* (t) Scheduling class. */
    u_char          td_user_pri;    /* (t) User pri from estcpu and nice. */
    u_char          td_base_user_pri; /* (t) Base user pri */
    uintptr_t       td_rb_list;     /* (k) Robust list head. */
    uintptr_t       td_rbp_list;    /* (k) Robust priv list head. */
    uintptr_t       td_rb_inact;    /* (k) Current in-action mutex loc. */
    struct syscall_args td_sa;      /* (kx) Syscall parameters. Copied on
                    fork for child tracing. */
    void            *td_sigblock_ptr; /* (k) uptr for fast sigblock. */
    uint32_t        td_sigblock_val;  /* (k) fast sigblock value read at
                        td_sigblock_ptr on kern entry */
    void            *td_exterr_ptr;
#define     td_endcopy td_pcb

/*
* Fields that must be manually set in fork1(), thread_create(), kthread_add(),
* or already have been set in the allocator, constructor, etc.
*/
    struct pcb      *td_pcb;        /* (k) Kernel VA of pcb and kstack. */
    enum td_states {
        TDS_INACTIVE = 0x0,
        TDS_INHIBITED,
        TDS_CAN_RUN,
        TDS_RUNQ,
        TDS_RUNNING
    } td_state;                     /* (t) thread state */
    /* Note: td_state must be accessed using TD_{GET,SET}_STATE(). */
    union {
        syscallarg_t        tdu_retval[2];
        off_t               tdu_off;
    } td_uretoff;                   /* (k) Syscall aux returns. */
#define td_retval   td_uretoff.tdu_retval
    u_int           td_cowgen;      /* (k) Generation of COW pointers. */
    /* LP64 hole */
    struct callout  td_slpcallout;  /* (h) Callout for sleep. */
    struct trapframe *td_frame;     /* (k) */
    vm_offset_t     td_kstack;      /* (a) Kernel VA of kstack. */
    u_short td_kstack_pages;        /* (a) Size of the kstack. */
    u_short td_kstack_domain;               /* (a) Domain backing kstack KVA. */
    volatile u_int  td_critnest;    /* (k*) Critical section nest level. */
    struct mdthread td_md;          /* (k) Any machine-dependent fields. */
    struct kaudit_record    *td_ar; /* (k) Active audit record, if any. */
    struct lpohead  td_lprof[2];    /* (a) lock profiling objects. */
    struct kdtrace_thread   *td_dtrace; /* (*) DTrace-specific data. */
    struct vnet     *td_vnet;       /* (k) Effective vnet. */
    const char      *td_vnet_lpush; /* (k) Debugging vnet push / pop. */
    struct trapframe *td_intr_frame;/* (k) Frame of the current irq */
    struct proc     *td_rfppwait_p; /* (k) The vforked child */
    struct vm_page  **td_ma;        /* (k) uio pages held */
    int             td_ma_cnt;      /* (k) size of *td_ma */
    /* LP64 hole */
    void            *td_emuldata;   /* Emulator state data */
    int             td_lastcpu;     /* (t) Last cpu we were on. */
    int             td_oncpu;       /* (t) Which cpu we are on. */
    void            *td_lkpi_task;  /* LinuxKPI task struct pointer */
    int             td_pmcpend;
    void            *td_remotereq;  /* (c) dbg remote request. */
    off_t           td_ktr_io_lim;  /* (k) limit for ktrace file size */
#ifdef EPOCH_TRACE
    SLIST_HEAD(, epoch_tracker) td_epochs;
#endif
};

主要な関数