]>
git.proxmox.com Git - mirror_lxcfs.git/blob - src/proc_cpuview.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #define __STDC_FORMAT_MACROS
26 #include <linux/magic.h>
27 #include <linux/sched.h>
28 #include <sys/epoll.h>
30 #include <sys/mount.h>
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <sys/syscall.h>
34 #include <sys/sysinfo.h>
37 #include "proc_cpuview.h"
40 #include "cgroup_fuse.h"
41 #include "cpuset_parse.h"
42 #include "cgroups/cgroup.h"
43 #include "cgroups/cgroup_utils.h"
44 #include "memory_utils.h"
45 #include "proc_loadavg.h"
48 /* Data for CPU view */
51 struct cpuacct_usage
*usage
; /* Real usage as read from the host's /proc/stat. */
52 struct cpuacct_usage
*view
; /* Usage stats reported to the container. */
54 pthread_mutex_t lock
; /* For node manipulation. */
55 struct cg_proc_stat
*next
;
58 struct cg_proc_stat_head
{
59 struct cg_proc_stat
*next
;
63 * For access to the list. Reading can be parallel, pruning is exclusive.
65 pthread_rwlock_t lock
;
68 #define CPUVIEW_HASH_SIZE 100
69 static struct cg_proc_stat_head
*proc_stat_history
[CPUVIEW_HASH_SIZE
];
71 static void reset_proc_stat_node(struct cg_proc_stat
*node
,
72 struct cpuacct_usage
*usage
, int cpu_count
)
74 lxcfs_debug("Resetting stat node for %s\n", node
->cg
);
75 memcpy(node
->usage
, usage
, sizeof(struct cpuacct_usage
) * cpu_count
);
77 for (int i
= 0; i
< cpu_count
; i
++) {
78 node
->view
[i
].user
= 0;
79 node
->view
[i
].system
= 0;
80 node
->view
[i
].idle
= 0;
83 node
->cpu_count
= cpu_count
;
86 static bool expand_proc_stat_node(struct cg_proc_stat
*node
, int cpu_count
)
88 __do_free
struct cpuacct_usage
*new_usage
= NULL
, *new_view
= NULL
;
90 /* Allocate new memory */
91 new_usage
= zalloc(sizeof(struct cpuacct_usage
) * cpu_count
);
95 new_view
= zalloc(sizeof(struct cpuacct_usage
) * cpu_count
);
99 /* Copy existing data & initialize new elements */
100 for (int i
= 0; i
< cpu_count
; i
++) {
101 if (i
< node
->cpu_count
) {
102 new_usage
[i
].user
= node
->usage
[i
].user
;
103 new_usage
[i
].system
= node
->usage
[i
].system
;
104 new_usage
[i
].idle
= node
->usage
[i
].idle
;
106 new_view
[i
].user
= node
->view
[i
].user
;
107 new_view
[i
].system
= node
->view
[i
].system
;
108 new_view
[i
].idle
= node
->view
[i
].idle
;
113 node
->usage
= move_ptr(new_usage
);
116 node
->view
= move_ptr(new_view
);
117 node
->cpu_count
= cpu_count
;
122 static void free_proc_stat_node(struct cg_proc_stat
*node
)
126 * We're abusing the usage pointer to indicate that
127 * pthread_mutex_init() was successful. Don't judge me.
130 pthread_mutex_destroy(&node
->lock
);
131 free_disarm(node
->cg
);
132 free_disarm(node
->usage
);
133 free_disarm(node
->view
);
138 define_cleanup_function(struct cg_proc_stat
*, free_proc_stat_node
);
140 static struct cg_proc_stat
*add_proc_stat_node(struct cg_proc_stat
*new_node
)
142 call_cleaner(free_proc_stat_node
) struct cg_proc_stat
*new = new_node
;
143 struct cg_proc_stat
*rv
= new_node
;
144 int hash
= calc_hash(new->cg
) % CPUVIEW_HASH_SIZE
;
145 struct cg_proc_stat_head
*head
= proc_stat_history
[hash
];
146 struct cg_proc_stat
*cur
;
148 pthread_rwlock_wrlock(&head
->lock
);
151 head
->next
= move_ptr(new);
152 goto out_rwlock_unlock
;
159 * The node to be added is already present in the list, so
160 * free the newly allocated one and return the one we found.
162 if (strcmp(cur
->cg
, new->cg
) == 0) {
164 goto out_rwlock_unlock
;
173 /* Add new node to end of list. */
174 cur
->next
= move_ptr(new);
175 goto out_rwlock_unlock
;
179 pthread_rwlock_unlock(&head
->lock
);
183 static struct cg_proc_stat
*new_proc_stat_node(struct cpuacct_usage
*usage
,
184 int cpu_count
, const char *cg
)
186 call_cleaner(free_proc_stat_node
) struct cg_proc_stat
*node
= NULL
;
187 __do_free
struct cpuacct_usage
*new_usage
= NULL
;
189 node
= zalloc(sizeof(struct cg_proc_stat
));
193 node
->cg
= strdup(cg
);
197 new_usage
= memdup(usage
, sizeof(struct cpuacct_usage
) * cpu_count
);
201 node
->view
= zalloc(sizeof(struct cpuacct_usage
) * cpu_count
);
205 node
->cpu_count
= cpu_count
;
207 if (pthread_mutex_init(&node
->lock
, NULL
))
210 * We're abusing the usage pointer to indicate that
211 * pthread_mutex_init() was successful. Don't judge me.
213 node
->usage
= move_ptr(new_usage
);
215 return move_ptr(node
);
218 static bool cgroup_supports(const char *controller
, const char *cgroup
,
221 __do_free
char *path
= NULL
;
224 cfd
= get_cgroup_fd(controller
);
228 path
= must_make_path_relative(cgroup
, file
, NULL
);
229 return faccessat(cfd
, path
, F_OK
, 0) == 0;
232 static struct cg_proc_stat
*prune_proc_stat_list(struct cg_proc_stat
*node
)
234 struct cg_proc_stat
*first
= NULL
;
236 for (struct cg_proc_stat
*prev
= NULL
; node
; ) {
237 if (!cgroup_supports("cpu", node
->cg
, "cpu.shares")) {
238 call_cleaner(free_proc_stat_node
) struct cg_proc_stat
*cur
= node
;
241 prev
->next
= node
->next
;
246 lxcfs_debug("Removing stat node for %s\n", cur
->cg
);
258 #define PROC_STAT_PRUNE_INTERVAL 10
259 static void prune_proc_stat_history(void)
261 time_t now
= time(NULL
);
263 for (int i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
264 pthread_rwlock_wrlock(&proc_stat_history
[i
]->lock
);
266 if ((proc_stat_history
[i
]->lastcheck
+ PROC_STAT_PRUNE_INTERVAL
) > now
) {
267 pthread_rwlock_unlock(&proc_stat_history
[i
]->lock
);
271 if (proc_stat_history
[i
]->next
) {
272 proc_stat_history
[i
]->next
= prune_proc_stat_list(proc_stat_history
[i
]->next
);
273 proc_stat_history
[i
]->lastcheck
= now
;
276 pthread_rwlock_unlock(&proc_stat_history
[i
]->lock
);
280 static struct cg_proc_stat
*find_proc_stat_node(struct cg_proc_stat_head
*head
,
283 struct cg_proc_stat
*node
;
285 pthread_rwlock_rdlock(&head
->lock
);
288 pthread_rwlock_unlock(&head
->lock
);
295 if (strcmp(cg
, node
->cg
) == 0)
297 } while ((node
= node
->next
));
302 pthread_rwlock_unlock(&head
->lock
);
303 prune_proc_stat_history();
307 static struct cg_proc_stat
*find_or_create_proc_stat_node(struct cpuacct_usage
*usage
,
308 int cpu_count
, const char *cg
)
310 int hash
= calc_hash(cg
) % CPUVIEW_HASH_SIZE
;
311 struct cg_proc_stat_head
*head
= proc_stat_history
[hash
];
312 struct cg_proc_stat
*node
;
314 node
= find_proc_stat_node(head
, cg
);
316 node
= new_proc_stat_node(usage
, cpu_count
, cg
);
320 node
= add_proc_stat_node(node
);
321 lxcfs_debug("New stat node (%d) for %s\n", cpu_count
, cg
);
324 pthread_mutex_lock(&node
->lock
);
327 * If additional CPUs on the host have been enabled, CPU usage counter
328 * arrays have to be expanded.
330 if (node
->cpu_count
< cpu_count
) {
331 lxcfs_debug("Expanding stat node %d->%d for %s\n",
332 node
->cpu_count
, cpu_count
, cg
);
334 if (!expand_proc_stat_node(node
, cpu_count
)) {
335 pthread_mutex_unlock(&node
->lock
);
336 return log_debug(NULL
, "Unable to expand stat node %d->%d for %s", node
->cpu_count
, cpu_count
, cg
);
343 static void add_cpu_usage(uint64_t *surplus
, struct cpuacct_usage
*usage
,
344 uint64_t *counter
, uint64_t threshold
)
346 uint64_t free_space
, to_add
;
348 free_space
= threshold
- usage
->user
- usage
->system
;
350 if (free_space
> usage
->idle
)
351 free_space
= usage
->idle
;
353 if (free_space
> *surplus
)
359 usage
->idle
-= to_add
;
363 static uint64_t diff_cpu_usage(struct cpuacct_usage
*older
,
364 struct cpuacct_usage
*newer
,
365 struct cpuacct_usage
*diff
, int cpu_count
)
369 for (int i
= 0; i
< cpu_count
; i
++) {
370 if (!newer
[i
].online
)
374 * When cpuset is changed on the fly, the CPUs might get
375 * reordered. We could either reset all counters, or check
376 * that the substractions below will return expected results.
378 if (newer
[i
].user
> older
[i
].user
)
379 diff
[i
].user
= newer
[i
].user
- older
[i
].user
;
383 if (newer
[i
].system
> older
[i
].system
)
384 diff
[i
].system
= newer
[i
].system
- older
[i
].system
;
388 if (newer
[i
].idle
> older
[i
].idle
)
389 diff
[i
].idle
= newer
[i
].idle
- older
[i
].idle
;
394 sum
+= diff
[i
].system
;
402 * Read cgroup CPU quota parameters from `cpu.cfs_quota_us` or
403 * `cpu.cfs_period_us`, depending on `param`. Parameter value is returned
406 static bool read_cpu_cfs_param(const char *cg
, const char *param
, int64_t *value
)
408 __do_free
char *str
= NULL
;
409 char file
[STRLITERALLEN("cpu.cfs_period_us") + 1];
413 if (pure_unified_layout(cgroup_ops
)) {
414 first
= !strcmp(param
, "quota");
415 ret
= snprintf(file
, sizeof(file
), "cpu.max");
417 ret
= snprintf(file
, sizeof(file
), "cpu.cfs_%s_us", param
);
419 if (ret
< 0 || (size_t)ret
>= sizeof(file
))
422 if (!cgroup_ops
->get(cgroup_ops
, "cpu", cg
, file
, &str
))
425 return sscanf(str
, first
? "%" PRId64
: "%*d %" PRId64
, value
) == 1;
429 * Return the exact number of visible CPUs based on CPU quotas.
430 * If there is no quota set, zero is returned.
432 static double exact_cpu_count(const char *cg
)
436 int64_t cfs_quota
, cfs_period
;
438 if (!read_cpu_cfs_param(cg
, "quota", &cfs_quota
))
441 if (!read_cpu_cfs_param(cg
, "period", &cfs_period
))
444 if (cfs_quota
<= 0 || cfs_period
<= 0)
447 rv
= (double)cfs_quota
/ (double)cfs_period
;
449 nprocs
= get_nprocs();
458 * Return the maximum number of visible CPUs based on CPU quotas.
459 * If there is no quota set, zero is returned.
461 int max_cpu_count(const char *cg
)
463 __do_free
char *cpuset
= NULL
;
465 int64_t cfs_quota
, cfs_period
;
466 int nr_cpus_in_cpuset
= 0;
468 if (!read_cpu_cfs_param(cg
, "quota", &cfs_quota
))
471 if (!read_cpu_cfs_param(cg
, "period", &cfs_period
))
474 cpuset
= get_cpuset(cg
);
476 nr_cpus_in_cpuset
= cpu_number_in_cpuset(cpuset
);
478 if (cfs_quota
<= 0 || cfs_period
<= 0) {
479 if (nr_cpus_in_cpuset
> 0)
480 return nr_cpus_in_cpuset
;
485 rv
= cfs_quota
/ cfs_period
;
488 * In case quota/period does not yield a whole number, add one CPU for
491 if ((cfs_quota
% cfs_period
) > 0)
494 nprocs
= get_nprocs();
498 /* Use min value in cpu quota and cpuset. */
499 if (nr_cpus_in_cpuset
> 0 && nr_cpus_in_cpuset
< rv
)
500 rv
= nr_cpus_in_cpuset
;
505 int cpuview_proc_stat(const char *cg
, const char *cpuset
,
506 struct cpuacct_usage
*cg_cpu_usage
, int cg_cpu_usage_size
,
507 FILE *f
, char *buf
, size_t buf_size
)
509 __do_free
char *line
= NULL
;
510 __do_free
struct cpuacct_usage
*diff
= NULL
;
511 size_t linelen
= 0, total_len
= 0;
512 int curcpu
= -1; /* cpu numbering starts at 0 */
515 uint64_t user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0,
516 softirq
= 0, steal
= 0, guest
= 0, guest_nice
= 0;
517 uint64_t user_sum
= 0, system_sum
= 0, idle_sum
= 0;
518 uint64_t user_surplus
= 0, system_surplus
= 0;
519 int nprocs
, max_cpus
;
521 uint64_t total_sum
, threshold
;
522 struct cg_proc_stat
*stat_node
;
524 nprocs
= get_nprocs_conf();
525 if (cg_cpu_usage_size
< nprocs
)
526 nprocs
= cg_cpu_usage_size
;
528 /* Read all CPU stats and stop when we've encountered other lines */
529 while (getline(&line
, &linelen
, f
) != -1) {
531 char cpu_char
[10]; /* That's a lot of cores */
532 uint64_t all_used
, cg_used
;
534 if (strlen(line
) == 0)
537 /* not a ^cpuN line containing a number N */
538 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1)
541 if (sscanf(cpu_char
, "%d", &physcpu
) != 1)
544 if (physcpu
>= cg_cpu_usage_size
)
550 if (!cpu_in_cpuset(physcpu
, cpuset
)) {
551 for (i
= curcpu
; i
<= physcpu
; i
++)
552 cg_cpu_usage
[i
].online
= false;
556 if (curcpu
< physcpu
) {
557 /* Some CPUs may be disabled */
558 for (i
= curcpu
; i
< physcpu
; i
++)
559 cg_cpu_usage
[i
].online
= false;
564 cg_cpu_usage
[curcpu
].online
= true;
566 ret
= sscanf(line
, "%*s %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
"lu",
580 all_used
= user
+ nice
+ system
+ iowait
+ irq
+ softirq
+ steal
+ guest
+ guest_nice
;
581 cg_used
= cg_cpu_usage
[curcpu
].user
+ cg_cpu_usage
[curcpu
].system
;
583 if (all_used
>= cg_used
) {
584 cg_cpu_usage
[curcpu
].idle
= idle
+ (all_used
- cg_used
);
587 lxcfs_error("cpu%d from %s has unexpected cpu time: %" PRIu64
" in /proc/stat, %" PRIu64
" in cpuacct.usage_all; unable to determine idle time",
588 curcpu
, cg
, all_used
, cg_used
);
589 cg_cpu_usage
[curcpu
].idle
= idle
;
593 /* Cannot use more CPUs than is available in cpuset. */
594 max_cpus
= max_cpu_count(cg
);
595 if (max_cpus
> cpu_cnt
|| !max_cpus
)
598 /* takes lock pthread_mutex_lock(&node->lock) */
599 stat_node
= find_or_create_proc_stat_node(cg_cpu_usage
, nprocs
, cg
);
601 return log_error(0, "Failed to find/create stat node for %s", cg
);
603 diff
= zalloc(sizeof(struct cpuacct_usage
) * nprocs
);
605 goto out_pthread_mutex_unlock
;
608 * If the new values are LOWER than values stored in memory, it means
609 * the cgroup has been reset/recreated and we should reset too.
611 for (curcpu
= 0; curcpu
< nprocs
; curcpu
++) {
612 if (!cg_cpu_usage
[curcpu
].online
)
615 if (cg_cpu_usage
[curcpu
].user
< stat_node
->usage
[curcpu
].user
)
616 reset_proc_stat_node(stat_node
, cg_cpu_usage
, nprocs
);
621 total_sum
= diff_cpu_usage(stat_node
->usage
, cg_cpu_usage
, diff
, nprocs
);
623 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
624 stat_node
->usage
[curcpu
].online
= cg_cpu_usage
[curcpu
].online
;
626 if (!stat_node
->usage
[curcpu
].online
)
631 stat_node
->usage
[curcpu
].user
+= diff
[curcpu
].user
;
632 stat_node
->usage
[curcpu
].system
+= diff
[curcpu
].system
;
633 stat_node
->usage
[curcpu
].idle
+= diff
[curcpu
].idle
;
635 if (max_cpus
> 0 && i
>= max_cpus
) {
636 user_surplus
+= diff
[curcpu
].user
;
637 system_surplus
+= diff
[curcpu
].system
;
641 /* Calculate usage counters of visible CPUs */
643 uint64_t diff_user
= 0;
644 uint64_t diff_system
= 0;
645 uint64_t diff_idle
= 0;
646 uint64_t max_diff_idle
= 0;
647 uint64_t max_diff_idle_index
= 0;
649 /* threshold = maximum usage per cpu, including idle */
650 threshold
= total_sum
/ cpu_cnt
* max_cpus
;
652 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
653 if (!stat_node
->usage
[curcpu
].online
)
661 if (diff
[curcpu
].user
+ diff
[curcpu
].system
>= threshold
)
665 add_cpu_usage(&user_surplus
, &diff
[curcpu
],
666 &diff
[curcpu
].user
, threshold
);
668 if (diff
[curcpu
].user
+ diff
[curcpu
].system
>= threshold
)
671 /* If there is still room, add system */
672 add_cpu_usage(&system_surplus
, &diff
[curcpu
],
673 &diff
[curcpu
].system
, threshold
);
676 if (user_surplus
> 0)
677 lxcfs_debug("leftover user: %lu for %s\n", user_surplus
, cg
);
678 if (system_surplus
> 0)
679 lxcfs_debug("leftover system: %lu for %s\n", system_surplus
, cg
);
681 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
682 if (!stat_node
->usage
[curcpu
].online
)
690 stat_node
->view
[curcpu
].user
+= diff
[curcpu
].user
;
691 stat_node
->view
[curcpu
].system
+= diff
[curcpu
].system
;
692 stat_node
->view
[curcpu
].idle
+= diff
[curcpu
].idle
;
694 user_sum
+= stat_node
->view
[curcpu
].user
;
695 system_sum
+= stat_node
->view
[curcpu
].system
;
696 idle_sum
+= stat_node
->view
[curcpu
].idle
;
698 diff_user
+= diff
[curcpu
].user
;
699 diff_system
+= diff
[curcpu
].system
;
700 diff_idle
+= diff
[curcpu
].idle
;
701 if (diff
[curcpu
].idle
> max_diff_idle
) {
702 max_diff_idle
= diff
[curcpu
].idle
;
703 max_diff_idle_index
= curcpu
;
706 lxcfs_v("curcpu: %d, diff_user: %lu, diff_system: %lu, diff_idle: %lu\n", curcpu
, diff
[curcpu
].user
, diff
[curcpu
].system
, diff
[curcpu
].idle
);
708 lxcfs_v("total. diff_user: %lu, diff_system: %lu, diff_idle: %lu\n", diff_user
, diff_system
, diff_idle
);
710 /* revise cpu usage view to support partial cpu case. */
711 exact_cpus
= exact_cpu_count(cg
);
712 if (exact_cpus
< (double)max_cpus
){
713 uint64_t delta
= (uint64_t)((double)(diff_user
+ diff_system
+ diff_idle
) * (1 - exact_cpus
/ (double)max_cpus
));
715 lxcfs_v("revising cpu usage view to match the exact cpu count [%f]\n", exact_cpus
);
716 lxcfs_v("delta: %lu\n", delta
);
717 lxcfs_v("idle_sum before: %lu\n", idle_sum
);
718 if (idle_sum
> delta
)
719 idle_sum
= idle_sum
- delta
;
722 lxcfs_v("idle_sum after: %lu\n", idle_sum
);
724 curcpu
= max_diff_idle_index
;
725 lxcfs_v("curcpu: %d, idle before: %lu\n", curcpu
, stat_node
->view
[curcpu
].idle
);
726 if (stat_node
->view
[curcpu
].idle
> delta
)
727 stat_node
->view
[curcpu
].idle
= stat_node
->view
[curcpu
].idle
- delta
;
729 stat_node
->view
[curcpu
].idle
= 0;
730 lxcfs_v("curcpu: %d, idle after: %lu\n", curcpu
, stat_node
->view
[curcpu
].idle
);
733 for (curcpu
= 0; curcpu
< nprocs
; curcpu
++) {
734 if (!stat_node
->usage
[curcpu
].online
)
737 stat_node
->view
[curcpu
].user
= stat_node
->usage
[curcpu
].user
;
738 stat_node
->view
[curcpu
].system
= stat_node
->usage
[curcpu
].system
;
739 stat_node
->view
[curcpu
].idle
= stat_node
->usage
[curcpu
].idle
;
741 user_sum
+= stat_node
->view
[curcpu
].user
;
742 system_sum
+= stat_node
->view
[curcpu
].system
;
743 idle_sum
+= stat_node
->view
[curcpu
].idle
;
747 /* Render the file */
749 l
= snprintf(buf
, buf_size
,
750 "cpu %" PRIu64
" 0 %" PRIu64
" %" PRIu64
" 0 0 0 0 0 0\n",
751 user_sum
, system_sum
, idle_sum
);
752 lxcfs_v("cpu-all: %s\n", buf
);
754 lxcfs_error("Failed to write cache");
756 goto out_pthread_mutex_unlock
;
759 lxcfs_error("Write to cache was truncated");
761 goto out_pthread_mutex_unlock
;
768 /* Render visible CPUs */
769 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
770 if (!stat_node
->usage
[curcpu
].online
)
775 if (max_cpus
> 0 && i
== max_cpus
)
778 l
= snprintf(buf
, buf_size
, "cpu%d %" PRIu64
" 0 %" PRIu64
" %" PRIu64
" 0 0 0 0 0 0\n",
780 stat_node
->view
[curcpu
].user
,
781 stat_node
->view
[curcpu
].system
,
782 stat_node
->view
[curcpu
].idle
);
783 lxcfs_v("cpu: %s\n", buf
);
785 lxcfs_error("Failed to write cache");
787 goto out_pthread_mutex_unlock
;
790 lxcfs_error("Write to cache was truncated");
792 goto out_pthread_mutex_unlock
;
800 /* Pass the rest of /proc/stat, start with the last line read */
801 l
= snprintf(buf
, buf_size
, "%s", line
);
803 lxcfs_error("Failed to write cache");
805 goto out_pthread_mutex_unlock
;
808 lxcfs_error("Write to cache was truncated");
810 goto out_pthread_mutex_unlock
;
817 /* Pass the rest of the host's /proc/stat */
818 while (getline(&line
, &linelen
, f
) != -1) {
819 l
= snprintf(buf
, buf_size
, "%s", line
);
821 lxcfs_error("Failed to write cache");
823 goto out_pthread_mutex_unlock
;
826 lxcfs_error("Write to cache was truncated");
828 goto out_pthread_mutex_unlock
;
836 out_pthread_mutex_unlock
:
838 pthread_mutex_unlock(&stat_node
->lock
);
844 * check whether this is a '^processor" line in /proc/cpuinfo
846 static inline bool is_processor_line(const char *line
)
849 return sscanf(line
, "processor : %d", &cpu
) == 1;
852 static inline bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
856 if (sscanf(line
, "processor : %d", &cpu
) == 1)
857 return cpu_in_cpuset(cpu
, cpuset
);
862 int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
863 struct fuse_file_info
*fi
)
865 __do_free
char *cg
= NULL
, *cpuset
= NULL
, *line
= NULL
;
866 __do_free
void *fopen_cache
= NULL
;
867 __do_fclose
FILE *f
= NULL
;
868 struct fuse_context
*fc
= fuse_get_context();
869 struct lxcfs_opts
*opts
= (struct lxcfs_opts
*)fc
->private_data
;
870 struct file_info
*d
= INTTYPE_TO_PTR(fi
->fh
);
871 size_t linelen
= 0, total_len
= 0;
872 bool am_printing
= false, firstline
= true, is_s390x
= false;
873 int curcpu
= -1, cpu
, max_cpus
= 0;
875 char *cache
= d
->buf
;
876 size_t cache_size
= d
->buflen
;
881 if (offset
> d
->size
)
887 left
= d
->size
- offset
;
888 total_len
= left
> size
? size
: left
;
889 memcpy(buf
, cache
+ offset
, total_len
);
894 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
895 if (initpid
<= 1 || is_shared_pidns(initpid
))
898 cg
= get_pid_cgroup(initpid
, "cpuset");
900 return read_file_fuse("proc/cpuinfo", buf
, size
, d
);
901 prune_init_slice(cg
);
903 cpuset
= get_cpuset(cg
);
907 if (cgroup_ops
->can_use_cpuview(cgroup_ops
) && opts
&& opts
->use_cfs
)
912 max_cpus
= max_cpu_count(cg
);
914 f
= fopen_cached("/proc/cpuinfo", "re", &fopen_cache
);
918 while (getline(&line
, &linelen
, f
) != -1) {
922 if (strstr(line
, "IBM/S390") != NULL
) {
929 if (strncmp(line
, "# processors:", 12) == 0)
932 if (is_processor_line(line
)) {
933 if (use_view
&& max_cpus
> 0 && (curcpu
+ 1) == max_cpus
)
936 am_printing
= cpuline_in_cpuset(line
, cpuset
);
939 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
941 return log_error(0, "Failed to write cache");
943 return log_error(0, "Write to cache was truncated");
949 } else if (is_s390x
&& sscanf(line
, "processor %d:", &cpu
) == 1) {
952 if (use_view
&& max_cpus
> 0 && (curcpu
+ 1) == max_cpus
)
955 if (!cpu_in_cpuset(cpu
, cpuset
))
959 p
= strchr(line
, ':');
964 l
= snprintf(cache
, cache_size
, "processor %d:%s", curcpu
, p
);
966 return log_error(0, "Failed to write cache");
968 return log_error(0, "Write to cache was truncated");
977 l
= snprintf(cache
, cache_size
, "%s", line
);
979 return log_error(0, "Failed to write cache");
981 return log_error(0, "Write to cache was truncated");
990 __do_free
char *origcache
= d
->buf
;
993 d
->buf
= malloc(d
->buflen
);
995 d
->buf
= move_ptr(origcache
);
1000 cache_size
= d
->buflen
;
1002 l
= snprintf(cache
, cache_size
, "vendor_id : IBM/S390\n");
1003 if (l
< 0 || l
>= cache_size
)
1009 l
= snprintf(cache
, cache_size
, "# processors : %d\n", curcpu
+ 1);
1010 if (l
< 0 || l
>= cache_size
)
1016 l
= snprintf(cache
, cache_size
, "%s", origcache
);
1017 if (l
< 0 || l
>= cache_size
)
1023 d
->size
= total_len
;
1024 if (total_len
> size
)
1027 /* read from off 0 */
1028 memcpy(buf
, d
->buf
, total_len
);
1034 * Returns 0 on success.
1035 * It is the caller's responsibility to free `return_usage`, unless this
1036 * function returns an error.
1038 int read_cpuacct_usage_all(char *cg
, char *cpuset
,
1039 struct cpuacct_usage
**return_usage
, int *size
)
1041 __do_free
char *usage_str
= NULL
;
1042 __do_free
struct cpuacct_usage
*cpu_usage
= NULL
;
1043 int i
= 0, j
= 0, read_pos
= 0, read_cnt
= 0;
1047 uint64_t cg_user
, cg_system
;
1048 int64_t ticks_per_sec
;
1050 ticks_per_sec
= sysconf(_SC_CLK_TCK
);
1051 if (ticks_per_sec
< 0 && errno
== EINVAL
) {
1052 lxcfs_debug("%m - Failed to determine number of ticks per second");
1056 cpucount
= get_nprocs_conf();
1057 cpu_usage
= malloc(sizeof(struct cpuacct_usage
) * cpucount
);
1061 memset(cpu_usage
, 0, sizeof(struct cpuacct_usage
) * cpucount
);
1062 if (!cgroup_ops
->get(cgroup_ops
, "cpuacct", cg
, "cpuacct.usage_all", &usage_str
)) {
1063 char *sep
= " \t\n";
1066 /* Read cpuacct.usage_percpu instead. */
1067 lxcfs_debug("Falling back to cpuacct.usage_percpu");
1068 if (!cgroup_ops
->get(cgroup_ops
, "cpuacct", cg
, "cpuacct.usage_percpu", &usage_str
))
1071 lxc_iterate_parts(tok
, usage_str
, sep
) {
1072 uint64_t percpu_user
;
1077 tok
= trim_whitespace_in_place(tok
);
1078 ret
= safe_uint64(tok
, &percpu_user
, 10);
1082 /* Convert the time from nanoseconds to USER_HZ */
1083 cpu_usage
[i
].user
= percpu_user
/ 1000.0 / 1000 / 1000 * ticks_per_sec
;
1084 cpu_usage
[i
].system
= cpu_usage
[i
].user
;
1086 lxcfs_debug("cpu%d with time %s", i
, tok
);
1089 if (sscanf(usage_str
, "cpu user system\n%n", &read_cnt
) != 0)
1090 return log_error(-1, "read_cpuacct_usage_all reading first line from %s/cpuacct.usage_all failed", cg
);
1092 read_pos
+= read_cnt
;
1094 for (i
= 0, j
= 0; i
< cpucount
; i
++) {
1095 ret
= sscanf(usage_str
+ read_pos
,
1096 "%d %" PRIu64
" %" PRIu64
"\n%n", &cg_cpu
,
1097 &cg_user
, &cg_system
, &read_cnt
);
1103 return log_error(-EINVAL
, "Failed to parse cpuacct.usage_all line %s from cgroup %s",
1104 usage_str
+ read_pos
, cg
);
1106 read_pos
+= read_cnt
;
1108 /* Convert the time from nanoseconds to USER_HZ */
1109 cpu_usage
[j
].user
= cg_user
/ 1000.0 / 1000 / 1000 * ticks_per_sec
;
1110 cpu_usage
[j
].system
= cg_system
/ 1000.0 / 1000 / 1000 * ticks_per_sec
;
1115 *return_usage
= move_ptr(cpu_usage
);
1120 static bool cpuview_init_head(struct cg_proc_stat_head
**head
)
1122 __do_free
struct cg_proc_stat_head
*h
;
1124 h
= zalloc(sizeof(struct cg_proc_stat_head
));
1128 if (pthread_rwlock_init(&h
->lock
, NULL
))
1131 h
->lastcheck
= time(NULL
);
1133 *head
= move_ptr(h
);
1137 bool init_cpuview(void)
1141 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++)
1142 proc_stat_history
[i
] = NULL
;
1144 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
1145 if (!cpuview_init_head(&proc_stat_history
[i
]))
1152 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
1153 if (proc_stat_history
[i
])
1154 free_disarm(proc_stat_history
[i
]);
1160 static void cpuview_free_head(struct cg_proc_stat_head
*head
)
1162 struct cg_proc_stat
*node
;
1168 struct cg_proc_stat
*cur
= node
;
1170 free_proc_stat_node(cur
);
1176 pthread_rwlock_destroy(&head
->lock
);
1180 void free_cpuview(void)
1182 for (int i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++)
1183 if (proc_stat_history
[i
])
1184 cpuview_free_head(proc_stat_history
[i
]);