2014年9月14日 星期日

Task_Struct


source:task_struct


1224 struct task_struct {
1225         volatile long state;    /* process執行的狀態,
                                       -1 不可執行, 0 執行中, >0 已停止 */
1226         void *stack;
1227         atomic_t usage;
1228         unsigned int flags;     
/* flag是process當前的狀態標籤,如下:
                            0x00000002表示process is created
                            0x00000004表示process正準備退出
                            0x00000040表示此process被fork出,但並沒有執行exec
                            0x00000400表示此process由其他process 發送相關信號而被殺死 */
1229         unsigned int ptrace;
1231#ifdef CONFIG_SMP
1232         struct llist_node wake_entry;
1233         int on_cpu;
1234         struct task_struct *last_wakee;
1235         unsigned long wakee_flips;
1236         unsigned long wakee_flip_decay_ts;
1238         int wake_cpu;
1239 #endif
1240         int on_rq;
1242         int prio, static_prio, normal_prio;
             /*prio=priority,表示此process的priority,prio表示動態優先級,static_prio是靜態優先級,在process建立時確定,範圍從-20~19,越小優先權越高*/
1243         unsigned int rt_priority;
1244         const struct sched_class *sched_class;
1245         struct sched_entity se;
1246         struct sched_rt_entity rt;
1247 #ifdef CONFIG_CGROUP_SCHED
1248         struct task_group *sched_task_group;
1249 #endif
1250         struct sched_dl_entity dl;
1251 
1252 #ifdef CONFIG_PREEMPT_NOTIFIERS       
1254         struct hlist_head preempt_notifiers;
            /* list of struct preempt_notifier(通知): */
1255 #endif
1256 
1257 #ifdef CONFIG_BLK_DEV_IO_TRACE
1258         unsigned int btrace_seq;
1259 #endif
1260
1261         unsigned int policy;
1262         int nr_cpus_allowed;
1263         cpumask_t cpus_allowed;
1265 #ifdef CONFIG_PREEMPT_RCU
1266         int rcu_read_lock_nesting;
1267         char rcu_read_unlock_special;
1268         struct list_head rcu_node_entry;
1269 #endif /* #ifdef CONFIG_PREEMPT_RCU */
1270 #ifdef CONFIG_TREE_PREEMPT_RCU
1271         struct rcu_node *rcu_blocked_node;
1272 #endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
1273 #ifdef CONFIG_RCU_BOOST
1274         struct rt_mutex *rcu_boost_mutex;
1275 #endif /* #ifdef CONFIG_RCU_BOOST */
1277 #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
1278         struct sched_info sched_info;
1279 #endif
1281         struct list_head tasks;
            /*使用list_head結構體*/
1282 #ifdef CONFIG_SMP
1283         struct plist_node pushable_tasks;
1284         struct rb_node pushable_dl_tasks;
1285 #endif
1287         struct mm_struct *mm, *active_mm;
            /*使用mm_struct結構體,描述process的定址空間*/
1288 #ifdef CONFIG_COMPAT_BRK
1289         unsigned brk_randomized:1;
1290 #endif
1291         /* per-thread vma caching */
1292         u32 vmacache_seqnum;
1293         struct vm_area_struct *vmacache[VMACACHE_SIZE];
1294 #if defined(SPLIT_RSS_COUNTING)
1295         struct task_rss_stat    rss_stat;
1296 #endif
1297 /* task state 參數 */
1298         int exit_state;
1299         int exit_code, exit_signal;/*程序的返回代碼及程序亦常中只產生的信號 由父子process輪流查詢*/
1300         int pdeath_signal;  /*  當parent死亡這個signal會發送 */
1301         unsigned int jobctl;    /* JOBCTL_*, siglock protected */
1303         /* 用於模擬以前LINUX版本的ABI行為 */
1304         unsigned int personality;
1306         unsigned in_execve:1;   /* Tell the LSMs that the process is doing an execve */
1308         unsigned in_iowait:1;
1310         /* task may not gain privileges */
1311         unsigned no_new_privs:1;
1313         /* Revert to default priority/policy when forking */
1314         unsigned sched_reset_on_fork:1;
1315         unsigned sched_contributes_to_load:1;
1317         pid_t pid;     /*這個是process number,為kernel自己維護,因為在Linux中thread是由process所實現的*/
1318         pid_t tgid;   /*這個為thread number,和thread組內的領頭process的pid一致,在用戶程序中getpid()其實是return tgid值*/
1320 #ifdef CONFIG_CC_STACKPROTECTOR
1321         /* Canary value for the -fstack-protector gcc feature */
1322         unsigned long stack_canary;
1323 #endif
1324         /*
1325          * pointers to (original) parent process, youngest child, younger sibling,
1326          * older sibling, respectively.  (p->father can be replaced with
1327          * p->real_parent->pid)
1328          */
1329         struct task_struct __rcu *real_parent; /* real parent process 親生父親*/
1330         struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports                                                該process的parent process不一定是親生的*/
1331         /*
1332          * children/sibling forms the list of my natural children
1333          */
1334         struct list_head children;      /* children指的是該process child的list,可以得到所有child process的描述,但需要使用list_for_each和list_entry,list_entry其實直接使用了constainer_of,參考*/
1335         struct list_head sibling;       /* 指的是該process的兄弟列表,也就是其parent的所有孩子列表 */
1336         struct task_struct *group_leader;       /* threadgroup leader,為thread的process描述,因為linux沒單獨實現thread的相關結構,所以利用一個process代替thread,然後做一些特殊處理 */

1343         struct list_head ptraced;
1344         struct list_head ptrace_entry;       /* ptraced 是一個list,該list記錄了使用ptrace的process,該List包含了childran和PTRACE_ATTACH targets。p->ptrace_entry is p's link on the p->parent->ptraced list*/
1347         struct pid_link pids[PIDTYPE_MAX]; /*連結PID的HASH table*/
1348         struct list_head thread_group; /*該process的所有thread列表 */
1349         struct list_head thread_node;
1351         struct completion *vfork_done;          /* for vfork() */
1352         int __user *set_child_tid;              /* CLONE_CHILD_SETTID */
1353         int __user *clear_child_tid;            /* CLONE_CHILD_CLEARTID */
1355         cputime_t utime, stime, utimescaled, stimescaled;  /*該process使用的CPU time message,utime為在user mode下執行的時間,stime為在kernel mode下執行的時間*/
1356         cputime_t gtime;
1357 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
1358         struct cputime prev_cputime;
1359 #endif
1360 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1361         seqlock_t vtime_seqlock;
1362         unsigned long long vtime_snap;
1363         enum {
1364                 VTIME_SLEEPING = 0,
1365                 VTIME_USER,
1366                 VTIME_SYS,
1367         } vtime_snap_whence;
1368 #endif
1369         unsigned long nvcsw, nivcsw; /* context switch counts */
1370         struct timespec start_time;             /* monotonic time 啟動的時間*/
1371         struct timespec real_start_time;        /* boot based time */
1372 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
1373         unsigned long min_flt, maj_flt;
1375         struct task_cputime cputime_expires;
1376         struct list_head cpu_timers[3];
1378 /* process credentials(憑據) */
1379         const struct cred __rcu *real_cred; /* objective(客觀) and real subjective task
1380                                          * credentials (COW) */
1381         const struct cred __rcu *cred;  /* effective(有效) (overridable) subjective task
1382                                          * credentials (COW) */
1383         char comm[TASK_COMM_LEN]; /* executable name excluding path
1384                                      - access with [gs]et_task_comm (which lock
1385                                        it with task_lock())
1386                                      - initialized normally by setup_new_exec */
             /*COMM為保存process newname的字數組,長度為15,TASK_COMM_LEN長度為16*/
1387 /* file system info */
1388         int link_count, total_link_count; /*filesystem訊息*/
1389 #ifdef CONFIG_SYSVIPC
1390 /* ipc stuff */
1391         struct sysv_sem sysvsem;
1392 #endif
1393 #ifdef CONFIG_DETECT_HUNG_TASK
1394 /* hung task detection */
1395         unsigned long last_switch_count;
1396 #endif
1398         struct thread_struct thread; /*該process在特定CPU下的狀態*/
1399 /* filesystem information */
1400         struct fs_struct *fs; /*filesystem相關message結構*/
1402         struct files_struct *files; /* open file information */
1403 /* namespaces */
1404         struct nsproxy *nsproxy;
1405 /* signal handlers */
1406         struct signal_struct *signal;
1407         struct sighand_struct *sighand;
1409         sigset_t blocked, real_blocked;
1410         sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
1411         struct sigpending pending;
1413         unsigned long sas_ss_sp;
1414         size_t sas_ss_size;
1415         int (*notifier)(void *priv);1416         void *notifier_data;
1417         sigset_t *notifier_mask;
1418         struct callback_head *task_works;
1420         struct audit_context *audit_context;
1421 #ifdef CONFIG_AUDITSYSCALL
1422         kuid_t loginuid;
1423         unsigned int sessionid;
1424 #endif1425         struct seccomp seccomp;
1427 /* Thread group tracking(追蹤) */
1428         u32 parent_exec_id;
1429         u32 self_exec_id;
1430 /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
1431  * mempolicy */
1432         spinlock_t alloc_lock;
1434        /* Protection of the PI data structures: */
1435         raw_spinlock_t pi_lock;
1437 #ifdef CONFIG_RT_MUTEXES
1438        /* PI waiters blocked on a rt_mutex held by this task(priority inheritance[優先繼承]簡稱PI)*/
1439         struct rb_root pi_waiters;
1440         struct rb_node *pi_waiters_leftmost;
1441         /* Deadlock偵測和處理優先繼承權 */
1442         struct rt_mutex_waiter *pi_blocked_on;
1443         /* Top pi_waiters task */
1444         struct task_struct *pi_top_task;
1445 #endif
1447 #ifdef CONFIG_DEBUG_MUTEXES
1448         /* mutex deadlock detection */
1449         struct mutex_waiter *blocked_on;
1450 #endif
1451 #ifdef CONFIG_TRACE_IRQFLAGS
1452         unsigned int irq_events;
1453         unsigned long hardirq_enable_ip;
1454         unsigned long hardirq_disable_ip;
1455         unsigned int hardirq_enable_event;
1456         unsigned int hardirq_disable_event;
1457         int hardirqs_enabled;
1458         int hardirq_context;
1459         unsigned long softirq_disable_ip;
1460         unsigned long softirq_enable_ip;
1461         unsigned int softirq_disable_event;
1462         unsigned int softirq_enable_event;
1463         int softirqs_enabled;
1464         int softirq_context;
1465 #endif
1466 #ifdef CONFIG_LOCKDEP
1467 # define MAX_LOCK_DEPTH 48UL
1468         u64 curr_chain_key;
1469         int lockdep_depth;
1470         unsigned int lockdep_recursion;
1471         struct held_lock held_locks[MAX_LOCK_DEPTH];
1472         gfp_t lockdep_reclaim_gfp;
1473 #endif
1475 /* journalling filesystem info */
1476         void *journal_info;
1478 /* stacked block device info */
1479         struct bio_list *bio_list;
1481 #ifdef CONFIG_BLOCK
1482 /* stack plugging */
1483         struct blk_plug *plug;
1484 #endif
1486 /* VM state */
1487         struct reclaim_state *reclaim_state;
1489         struct backing_dev_info *backing_dev_info;
1491         struct io_context *io_context;
1493         unsigned long ptrace_message;
1494         siginfo_t *last_siginfo; /* For ptrace use.  */
1495         struct task_io_accounting ioac;
1496 #if defined(CONFIG_TASK_XACCT)
1497    u64 acct_rss_mem1;      /* 累積了rss的使用 */
1498    u64 acct_vm_mem1;       /* 累積virtual memory的使用 */
1499    cputime_t acct_timexpd; /* stime + utime since last update */
1500 #endif
1501 #ifdef CONFIG_CPUSETS
1502         nodemask_t mems_allowed;        /* Protected by alloc_lock */
1503         seqcount_t mems_allowed_seq;    /* Sequence no to catch updates */
1504         int cpuset_mem_spread_rotor;
1505         int cpuset_slab_spread_rotor;
1506 #endif
1507 #ifdef CONFIG_CGROUPS
1508         /* Control Group info protected by css_set_lock */
1509         struct css_set __rcu *cgroups;
1510         /* cg_list protected by css_set_lock and tsk->alloc_lock */
1511         struct list_head cg_list;
1512 #endif
1513 #ifdef CONFIG_FUTEX
1514         struct robust_list_head __user *robust_list;
1515 #ifdef CONFIG_COMPAT
1516         struct compat_robust_list_head __user *compat_robust_list;
1517 #endif
1518         struct list_head pi_state_list;
1519         struct futex_pi_state *pi_state_cache;
1520 #endif
1521 #ifdef CONFIG_PERF_EVENTS
1522         struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
1523         struct mutex perf_event_mutex;
1524         struct list_head perf_event_list;
1525 #endif
1526 #ifdef CONFIG_DEBUG_PREEMPT
1527         unsigned long preempt_disable_ip;
1528 #endif
1529 #ifdef CONFIG_NUMA
1530         struct mempolicy *mempolicy;    /* Protected by alloc_lock */
1531         short il_next;
1532         short pref_node_fork;
1533 #endif
1534 #ifdef CONFIG_NUMA_BALANCING(configure NUMA balancing)配置非一致性記憶體存取平衡
1535         int numa_scan_seq;
1536         unsigned int numa_scan_period;
1537         unsigned int numa_scan_period_max;
1538         int numa_preferred_nid;
1539         unsigned long numa_migrate_retry;
1540         u64 node_stamp;                 /* migration(簽移) stamp  */
1541         u64 last_task_numa_placement;
1542         u64 last_sum_exec_runtime;
1543         struct callback_head numa_work;
1545         struct list_head numa_entry;
1546         struct numa_group *numa_group;
1549          /* 每個節點上的故障導致平均指數衰減,這些指數決定了排程安置的基礎,該數值保持不變當PTE                持續不斷的掃描*/
1553         unsigned long *numa_faults_memory;
1554         unsigned long total_numa_faults;
1556         /*numa_faults_buffer記錄了每個節點的fault當正確的掃描時,當掃描完成後,count會在numa              _faults_memory中衰減 and these values are copied.*/
1561         unsigned long *numa_faults_buffer_memory;
1563         /* process執行時,當NUMA提示錯誤發生時,會追蹤結點*/
1567         unsigned long *numa_faults_cpu;
1568         unsigned long *numa_faults_buffer_cpu;
1570         /* 當最後掃描時在遠端/本地記錄到錯誤,numa_faults_locality會進行追蹤,process掃描期間                當他們為共用錯誤或私人錯誤會根據不同的權重改編內部錯誤。*/
1576         unsigned long numa_faults_locality[2];
1578         unsigned long numa_pages_migrated;
1579 #endif /* CONFIG_NUMA_BALANCING */
1581         struct rcu_head rcu;
1583         /*
1584          * cache last used pipe for splice
1585          */
1586         struct pipe_inode_info *splice_pipe;
1588         struct page_frag task_frag;
1590 #ifdef  CONFIG_TASK_DELAY_ACCT
1591         struct task_delay_info *delays;
1592 #endif
1593 #ifdef CONFIG_FAULT_INJECTION
1594         int make_it_fail;
1595 #endif
1596         /*當(nr_dirtied >= nr_dirtied_pause),會立即呼叫balance_dirty_pages() for some dirty throttling pause*/
1600         int nr_dirtied;
1601         int nr_dirtied_pause;
1602         unsigned long dirty_paused_when; /* 開始write-and-pause期間 */
1604 #ifdef CONFIG_LATENCYTOP
1605         int latency_record_count;
1606         struct latency_record latency_record[LT_SAVECOUNT];
1607 #endif
1608         /*time slack values; 他們循環使用poll()and select()等時間數值,單位為nanoseconds*/
1612         unsigned long timer_slack_ns;
1613         unsigned long default_timer_slack_ns;
1615 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1616         /* Index of current stored address in ret_stack */
1617         int curr_ret_stack;
1618         /* Stack of return addresses for return function tracing */
1619         struct ftrace_ret_stack *ret_stack;
1620         /* time stamp for last schedule */
1621         unsigned long long ftrace_timestamp;
1622         /*
1623          * Number of functions that haven't been traced
1624          * because of depth overrun.
1625          */
1626         atomic_t trace_overrun;
1627         /* Pause for the tracing */
1628         atomic_t tracing_graph_pause;
1629 #endif
1630 #ifdef CONFIG_TRACING
1631         /* state flags for use by tracers */
1632         unsigned long trace;
1633         /* bitmask and counter of trace recursion */
1634         unsigned long trace_recursion;
1635 #endif /* CONFIG_TRACING */
1636 #ifdef CONFIG_MEMCG /* memcg uses this to do batch job */
1637         struct memcg_batch_info {
1638                 int do_batch;   /* incremented when batch uncharge started */
1639                 struct mem_cgroup *memcg; /* target memcg of uncharge */
1640                 unsigned long nr_pages; /* uncharged usage */
1641                 unsigned long memsw_nr_pages; /* uncharged mem+swap usage */
1642         } memcg_batch;
1643         unsigned int memcg_kmem_skip_account;
1644         struct memcg_oom_info {
1645                 struct mem_cgroup *memcg;
1646                 gfp_t gfp_mask;
1647                 int order;
1648                 unsigned int may_oom:1;
1649         } memcg_oom;
1650 #endif
1651 #ifdef CONFIG_UPROBES
1652         struct uprobe_task *utask;
1653 #endif
1654 #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
1655         unsigned int    sequential_io;
1656         unsigned int    sequential_io_avg;
1657 #endif
1658 };

沒有留言:

張貼留言