# 2.7.2 负载追踪 - 数据结构

与负载相关的字段封装在数据结构 `sched_avg` 中：

```
/* file: include/linux/sched.h */
struct sched_avg {
    /* 上一次更新 load 的时间点，用来计算时间间隔 */
    u64 last_update_time;
    /* 基于可运行时间（runnable）的负载总和。
     * runnable状态指任务处于可运行状态的时间，其中包括在rq里等待的时间，以及在CPU上执行的时间
     */
    u64 load_sum;
    /* 在rq里等待的时间计算出来的负载总和 */
    u64 runnable_sum;
    /* 在cpu上执行的时间计算出来的负载总和 */
    u32 util_sum;
    /* 表示最近一次更新负载时，最近一个周期内的那部分时间，
     * 即accumulate_sum()函数上注释中标记为d3的时间
     */
    u32 period_contrib;
    /* 以下字段是平均值，同样基于上述的几个维度分别计算 */
    unsigned long load_avg;
    unsigned long runnable_avg;
    unsigned long util_avg;
    struct util_est util_est;
} ____cacheline_aligned;
```

当内核开启 `CONFIG_SMP=y` 时，cfsrq 与 schedentity 中都会包含跟踪负载的字段：

```
/* file: include/linux/sched.h */
struct sched_entity {
#ifdef CONFIG_SMP
    struct sched_avg avg;
#endif
};

/* file: kernel/sched/sched.h */
struct cfs_rq {
#ifdef CONFIG_SMP
    /* CFS load tracking */
    struct sched_avg avg;

    struct {
        raw_spinlock_t lock ____cacheline_aligned;
        int nr;
        unsigned long load_avg;
        unsigned long util_avg;
        unsigned long runnable_avg;
    } removed;

#endif /* CONFIG_SMP */
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://s3.shizhz.me/linux-sched/load-trace/load-trace-data-structure.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
