History

sched/kcpustat: Introduce vtime-aware kcpustat accessor for CPUTIME_SYSTEM

Kcpustat is not correctly supported on nohz_full CPUs. The tick doesn't fire and the cputime therefore doesn't move forward. The issue has shown up after the vanishing of the remaining 1Hz which has made the stall visible. We are solving that with checking the task running on a CPU through RCU and reading its vtime delta that we add to the raw kcpustat values. We make sure that we fetch a coherent raw-kcpustat/vtime-delta couple sequence while checking that the CPU referred by the target vtime is the correct one, under the locked vtime seqcount. Only CPUTIME_SYSTEM is handled here as a start because it's the trivial case. User and guest time will require more preparation work to correctly handle niceness.

Frederic Weisbecker committed 7 years ago

Commit 64eea631 parent 023e9de2 files · +93 −0Browse files

Unified · Split

2 files
  1. include/linux/kernel_stat.h +11 −0
  2. kernel/sched/cputime.c +82 −0
include/linux/kernel_stat.h+11−0
@@ -78,6 +78,17 @@ static inline unsigned int kstat_cpu_irqs_sum(unsigned int cpu) · 77 unmodified lines
7878 return kstat_cpu(cpu).irqs_sum;
7979 }
8080
81+#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
82+extern u64 kcpustat_field(struct kernel_cpustat *kcpustat,
83+ enum cpu_usage_stat usage, int cpu);
84+#else
85+static inline u64 kcpustat_field(struct kernel_cpustat *kcpustat,
86+ enum cpu_usage_stat usage, int cpu)
87+{
88+ return kcpustat->cpustat[usage];
89+}
90+#endif
91+
8192 extern void account_user_time(struct task_struct *, u64);
8293 extern void account_guest_time(struct task_struct *, u64);
8394 extern void account_system_time(struct task_struct *, int, u64);
kernel/sched/cputime.c+82−0
@@ -911,4 +911,86 @@ void task_cputime(struct task_struct *t, u64 *utime, u64 *stime) · 910 unmodified lines
911911 *utime += vtime->utime + delta;
912912 } while (read_seqcount_retry(&vtime->seqcount, seq));
913913 }
914+
915+static int kcpustat_field_vtime(u64 *cpustat,
916+ struct vtime *vtime,
917+ enum cpu_usage_stat usage,
918+ int cpu, u64 *val)
919+{
920+ unsigned int seq;
921+ int err;
922+
923+ do {
924+ seq = read_seqcount_begin(&vtime->seqcount);
925+
926+ /*
927+ * We raced against context switch, fetch the
928+ * kcpustat task again.
929+ */
930+ if (vtime->cpu != cpu && vtime->cpu != -1)
931+ return -EAGAIN;
932+
933+ /*
934+ * Two possible things here:
935+ * 1) We are seeing the scheduling out task (prev) or any past one.
936+ * 2) We are seeing the scheduling in task (next) but it hasn't
937+ * passed though vtime_task_switch() yet so the pending
938+ * cputime of the prev task may not be flushed yet.
939+ *
940+ * Case 1) is ok but 2) is not. So wait for a safe VTIME state.
941+ */
942+ if (vtime->state == VTIME_INACTIVE)
943+ return -EAGAIN;
944+
945+ err = 0;
946+
947+ *val = cpustat[usage];
948+
949+ if (vtime->state == VTIME_SYS)
950+ *val += vtime->stime + vtime_delta(vtime);
951+
952+ } while (read_seqcount_retry(&vtime->seqcount, seq));
953+
954+ return 0;
955+}
956+
957+u64 kcpustat_field(struct kernel_cpustat *kcpustat,
958+ enum cpu_usage_stat usage, int cpu)
959+{
960+ u64 *cpustat = kcpustat->cpustat;
961+ struct rq *rq;
962+ u64 val;
963+ int err;
964+
965+ if (!vtime_accounting_enabled_cpu(cpu))
966+ return cpustat[usage];
967+
968+ /* Only support sys vtime for now */
969+ if (usage != CPUTIME_SYSTEM)
970+ return cpustat[usage];
971+
972+ rq = cpu_rq(cpu);
973+
974+ for (;;) {
975+ struct task_struct *curr;
976+ struct vtime *vtime;
977+
978+ rcu_read_lock();
979+ curr = rcu_dereference(rq->curr);
980+ if (WARN_ON_ONCE(!curr)) {
981+ rcu_read_unlock();
982+ return cpustat[usage];
983+ }
984+
985+ vtime = &curr->vtime;
986+ err = kcpustat_field_vtime(cpustat, vtime, usage, cpu, &val);
987+ rcu_read_unlock();
988+
989+ if (!err)
990+ return val;
991+
992+ cpu_relax();
993+ }
994+}
995+EXPORT_SYMBOL_GPL(kcpustat_field);
914996 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t