]>
git.proxmox.com Git - mirror_lxcfs.git/blob - src/proc_cpuview.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #ifndef FUSE_USE_VERSION
8 #define FUSE_USE_VERSION 26
11 #define _FILE_OFFSET_BITS 64
13 #define __STDC_FORMAT_MACROS
31 #include <linux/magic.h>
32 #include <linux/sched.h>
33 #include <sys/epoll.h>
35 #include <sys/mount.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <sys/syscall.h>
39 #include <sys/sysinfo.h>
44 #include "cgroup_fuse.h"
45 #include "cpuset_parse.h"
46 #include "cgroups/cgroup.h"
47 #include "cgroups/cgroup_utils.h"
48 #include "memory_utils.h"
49 #include "proc_loadavg.h"
52 /* Data for CPU view */
55 struct cpuacct_usage
*usage
; // Real usage as read from the host's /proc/stat
56 struct cpuacct_usage
*view
; // Usage stats reported to the container
58 pthread_mutex_t lock
; // For node manipulation
59 struct cg_proc_stat
*next
;
62 struct cg_proc_stat_head
{
63 struct cg_proc_stat
*next
;
67 * For access to the list. Reading can be parallel, pruning is exclusive.
69 pthread_rwlock_t lock
;
72 #define CPUVIEW_HASH_SIZE 100
73 static struct cg_proc_stat_head
*proc_stat_history
[CPUVIEW_HASH_SIZE
];
75 static void reset_proc_stat_node(struct cg_proc_stat
*node
,
76 struct cpuacct_usage
*usage
, int cpu_count
)
78 lxcfs_debug("Resetting stat node for %s\n", node
->cg
);
79 memcpy(node
->usage
, usage
, sizeof(struct cpuacct_usage
) * cpu_count
);
81 for (int i
= 0; i
< cpu_count
; i
++) {
82 node
->view
[i
].user
= 0;
83 node
->view
[i
].system
= 0;
84 node
->view
[i
].idle
= 0;
87 node
->cpu_count
= cpu_count
;
90 static bool expand_proc_stat_node(struct cg_proc_stat
*node
, int cpu_count
)
92 __do_free
struct cpuacct_usage
*new_usage
= NULL
, *new_view
= NULL
;
94 /* Allocate new memory */
95 new_usage
= malloc(sizeof(struct cpuacct_usage
) * cpu_count
);
99 new_view
= malloc(sizeof(struct cpuacct_usage
) * cpu_count
);
103 /* Copy existing data & initialize new elements */
104 for (int i
= 0; i
< cpu_count
; i
++) {
105 if (i
< node
->cpu_count
) {
106 new_usage
[i
].user
= node
->usage
[i
].user
;
107 new_usage
[i
].system
= node
->usage
[i
].system
;
108 new_usage
[i
].idle
= node
->usage
[i
].idle
;
110 new_view
[i
].user
= node
->view
[i
].user
;
111 new_view
[i
].system
= node
->view
[i
].system
;
112 new_view
[i
].idle
= node
->view
[i
].idle
;
114 new_usage
[i
].user
= 0;
115 new_usage
[i
].system
= 0;
116 new_usage
[i
].idle
= 0;
118 new_view
[i
].user
= 0;
119 new_view
[i
].system
= 0;
120 new_view
[i
].idle
= 0;
125 node
->usage
= move_ptr(new_usage
);
128 node
->view
= move_ptr(new_view
);
129 node
->cpu_count
= cpu_count
;
134 static void free_proc_stat_node(struct cg_proc_stat
*node
)
136 pthread_mutex_destroy(&node
->lock
);
137 free_disarm(node
->cg
);
138 free_disarm(node
->usage
);
139 free_disarm(node
->view
);
143 static struct cg_proc_stat
*add_proc_stat_node(struct cg_proc_stat
*new_node
)
145 int hash
= calc_hash(new_node
->cg
) % CPUVIEW_HASH_SIZE
;
146 struct cg_proc_stat_head
*head
= proc_stat_history
[hash
];
147 struct cg_proc_stat
*node
, *rv
= new_node
;
149 pthread_rwlock_wrlock(&head
->lock
);
152 head
->next
= new_node
;
159 if (strcmp(node
->cg
, new_node
->cg
) == 0) {
160 /* The node is already present, return it */
161 free_proc_stat_node(new_node
);
171 node
->next
= new_node
;
176 pthread_rwlock_unlock(&head
->lock
);
180 static struct cg_proc_stat
*new_proc_stat_node(struct cpuacct_usage
*usage
, int cpu_count
, const char *cg
)
182 struct cg_proc_stat
*node
;
185 node
= malloc(sizeof(struct cg_proc_stat
));
193 node
->cg
= malloc(strlen(cg
) + 1);
197 strcpy(node
->cg
, cg
);
199 node
->usage
= malloc(sizeof(struct cpuacct_usage
) * cpu_count
);
203 memcpy(node
->usage
, usage
, sizeof(struct cpuacct_usage
) * cpu_count
);
205 node
->view
= malloc(sizeof(struct cpuacct_usage
) * cpu_count
);
209 node
->cpu_count
= cpu_count
;
212 if (pthread_mutex_init(&node
->lock
, NULL
) != 0)
213 log_error(goto err
, "Failed to initialize node lock");
215 for (i
= 0; i
< cpu_count
; i
++) {
216 node
->view
[i
].user
= 0;
217 node
->view
[i
].system
= 0;
218 node
->view
[i
].idle
= 0;
224 if (node
&& node
->cg
)
226 if (node
&& node
->usage
)
228 if (node
&& node
->view
)
236 static bool cgfs_param_exist(const char *controller
, const char *cgroup
,
239 __do_free
char *path
= NULL
;
242 cfd
= get_cgroup_fd(controller
);
246 path
= must_make_path(dot_or_empty(cgroup
), cgroup
, file
, NULL
);
247 return (faccessat(cfd
, path
, F_OK
, 0) == 0);
250 static struct cg_proc_stat
*prune_proc_stat_list(struct cg_proc_stat
*node
)
252 struct cg_proc_stat
*first
= NULL
;
254 for (struct cg_proc_stat
*prev
= NULL
; node
; ) {
255 if (!cgfs_param_exist("cpu", node
->cg
, "cpu.shares")) {
256 struct cg_proc_stat
*tmp
= node
;
258 lxcfs_debug("Removing stat node for %s\n", node
->cg
);
261 prev
->next
= node
->next
;
266 free_proc_stat_node(tmp
);
278 #define PROC_STAT_PRUNE_INTERVAL 10
279 static void prune_proc_stat_history(void)
281 time_t now
= time(NULL
);
283 for (int i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
284 pthread_rwlock_wrlock(&proc_stat_history
[i
]->lock
);
286 if ((proc_stat_history
[i
]->lastcheck
+ PROC_STAT_PRUNE_INTERVAL
) > now
) {
287 pthread_rwlock_unlock(&proc_stat_history
[i
]->lock
);
291 if (proc_stat_history
[i
]->next
) {
292 proc_stat_history
[i
]->next
= prune_proc_stat_list(proc_stat_history
[i
]->next
);
293 proc_stat_history
[i
]->lastcheck
= now
;
296 pthread_rwlock_unlock(&proc_stat_history
[i
]->lock
);
300 static struct cg_proc_stat
*find_proc_stat_node(struct cg_proc_stat_head
*head
,
303 struct cg_proc_stat
*node
;
305 pthread_rwlock_rdlock(&head
->lock
);
308 pthread_rwlock_unlock(&head
->lock
);
315 if (strcmp(cg
, node
->cg
) == 0)
317 } while ((node
= node
->next
));
322 pthread_rwlock_unlock(&head
->lock
);
323 prune_proc_stat_history();
327 static struct cg_proc_stat
*find_or_create_proc_stat_node(struct cpuacct_usage
*usage
, int cpu_count
, const char *cg
)
329 int hash
= calc_hash(cg
) % CPUVIEW_HASH_SIZE
;
330 struct cg_proc_stat_head
*head
= proc_stat_history
[hash
];
331 struct cg_proc_stat
*node
;
333 node
= find_proc_stat_node(head
, cg
);
335 node
= new_proc_stat_node(usage
, cpu_count
, cg
);
339 node
= add_proc_stat_node(node
);
340 lxcfs_debug("New stat node (%d) for %s\n", cpu_count
, cg
);
343 pthread_mutex_lock(&node
->lock
);
345 /* If additional CPUs on the host have been enabled, CPU usage counter
346 * arrays have to be expanded */
347 if (node
->cpu_count
< cpu_count
) {
348 lxcfs_debug("Expanding stat node %d->%d for %s\n",
349 node
->cpu_count
, cpu_count
, cg
);
351 if (!expand_proc_stat_node(node
, cpu_count
)) {
352 pthread_mutex_unlock(&node
->lock
);
353 return log_debug(NULL
, "Unable to expand stat node %d->%d for %s", node
->cpu_count
, cpu_count
, cg
);
360 static void add_cpu_usage(uint64_t *surplus
, struct cpuacct_usage
*usage
,
361 uint64_t *counter
, uint64_t threshold
)
363 unsigned long free_space
, to_add
;
365 free_space
= threshold
- usage
->user
- usage
->system
;
367 if (free_space
> usage
->idle
)
368 free_space
= usage
->idle
;
370 to_add
= free_space
> *surplus
? *surplus
: free_space
;
373 usage
->idle
-= to_add
;
377 static unsigned long diff_cpu_usage(struct cpuacct_usage
*older
,
378 struct cpuacct_usage
*newer
,
379 struct cpuacct_usage
*diff
, int cpu_count
)
381 unsigned long sum
= 0;
383 for (int i
= 0; i
< cpu_count
; i
++) {
384 if (!newer
[i
].online
)
388 * When cpuset is changed on the fly, the CPUs might get
389 * reordered. We could either reset all counters, or check
390 * that the substractions below will return expected results.
392 if (newer
[i
].user
> older
[i
].user
)
393 diff
[i
].user
= newer
[i
].user
- older
[i
].user
;
397 if (newer
[i
].system
> older
[i
].system
)
398 diff
[i
].system
= newer
[i
].system
- older
[i
].system
;
402 if (newer
[i
].idle
> older
[i
].idle
)
403 diff
[i
].idle
= newer
[i
].idle
- older
[i
].idle
;
408 sum
+= diff
[i
].system
;
416 * Read cgroup CPU quota parameters from `cpu.cfs_quota_us` or
417 * `cpu.cfs_period_us`, depending on `param`. Parameter value is returned
420 static bool read_cpu_cfs_param(const char *cg
, const char *param
, int64_t *value
)
422 __do_free
char *str
= NULL
;
423 char file
[11 + 6 + 1]; /* cpu.cfs__us + quota/period + \0 */
425 snprintf(file
, sizeof(file
), "cpu.cfs_%s_us", param
);
427 if (!cgroup_ops
->get(cgroup_ops
, "cpu", cg
, file
, &str
))
430 if (sscanf(str
, "%"PRId64
, value
) != 1)
437 * Return the exact number of visible CPUs based on CPU quotas.
438 * If there is no quota set, zero is returned.
440 static double exact_cpu_count(const char *cg
)
444 int64_t cfs_quota
, cfs_period
;
446 if (!read_cpu_cfs_param(cg
, "quota", &cfs_quota
))
449 if (!read_cpu_cfs_param(cg
, "period", &cfs_period
))
452 if (cfs_quota
<= 0 || cfs_period
<= 0)
455 rv
= (double)cfs_quota
/ (double)cfs_period
;
457 nprocs
= get_nprocs();
466 * Return the maximum number of visible CPUs based on CPU quotas.
467 * If there is no quota set, zero is returned.
469 int max_cpu_count(const char *cg
)
471 __do_free
char *cpuset
= NULL
;
473 int64_t cfs_quota
, cfs_period
;
474 int nr_cpus_in_cpuset
= 0;
476 if (!read_cpu_cfs_param(cg
, "quota", &cfs_quota
))
479 if (!read_cpu_cfs_param(cg
, "period", &cfs_period
))
482 cpuset
= get_cpuset(cg
);
484 nr_cpus_in_cpuset
= cpu_number_in_cpuset(cpuset
);
486 if (cfs_quota
<= 0 || cfs_period
<= 0){
487 if (nr_cpus_in_cpuset
> 0)
488 return nr_cpus_in_cpuset
;
493 rv
= cfs_quota
/ cfs_period
;
495 /* In case quota/period does not yield a whole number, add one CPU for
498 if ((cfs_quota
% cfs_period
) > 0)
501 nprocs
= get_nprocs();
505 /* use min value in cpu quota and cpuset */
506 if (nr_cpus_in_cpuset
> 0 && nr_cpus_in_cpuset
< rv
)
507 rv
= nr_cpus_in_cpuset
;
512 int cpuview_proc_stat(const char *cg
, const char *cpuset
,
513 struct cpuacct_usage
*cg_cpu_usage
, int cg_cpu_usage_size
,
514 FILE *f
, char *buf
, size_t buf_size
)
516 __do_free
char *line
= NULL
;
517 __do_free
struct cpuacct_usage
*diff
= NULL
;
518 size_t linelen
= 0, total_len
= 0, l
;
519 int curcpu
= -1; /* cpu numbering starts at 0 */
521 int max_cpus
= max_cpu_count(cg
), cpu_cnt
= 0;
522 uint64_t user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0,
523 softirq
= 0, steal
= 0, guest
= 0, guest_nice
= 0;
524 uint64_t user_sum
= 0, system_sum
= 0, idle_sum
= 0;
525 uint64_t user_surplus
= 0, system_surplus
= 0;
526 uint64_t total_sum
, threshold
;
527 struct cg_proc_stat
*stat_node
;
528 int nprocs
= get_nprocs_conf();
530 if (cg_cpu_usage_size
< nprocs
)
531 nprocs
= cg_cpu_usage_size
;
533 /* Read all CPU stats and stop when we've encountered other lines */
534 while (getline(&line
, &linelen
, f
) != -1) {
536 char cpu_char
[10]; /* That's a lot of cores */
537 uint64_t all_used
, cg_used
;
539 if (strlen(line
) == 0)
542 /* not a ^cpuN line containing a number N */
543 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1)
546 if (sscanf(cpu_char
, "%d", &physcpu
) != 1)
549 if (physcpu
>= cg_cpu_usage_size
)
555 if (!cpu_in_cpuset(physcpu
, cpuset
)) {
556 for (i
= curcpu
; i
<= physcpu
; i
++)
557 cg_cpu_usage
[i
].online
= false;
561 if (curcpu
< physcpu
) {
562 /* Some CPUs may be disabled */
563 for (i
= curcpu
; i
< physcpu
; i
++)
564 cg_cpu_usage
[i
].online
= false;
569 cg_cpu_usage
[curcpu
].online
= true;
571 ret
= sscanf(line
, "%*s %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
"lu",
585 all_used
= user
+ nice
+ system
+ iowait
+ irq
+ softirq
+ steal
+ guest
+ guest_nice
;
586 cg_used
= cg_cpu_usage
[curcpu
].user
+ cg_cpu_usage
[curcpu
].system
;
588 if (all_used
>= cg_used
) {
589 cg_cpu_usage
[curcpu
].idle
= idle
+ (all_used
- cg_used
);
592 lxcfs_error("cpu%d from %s has unexpected cpu time: %" PRIu64
" in /proc/stat, %" PRIu64
" in cpuacct.usage_all; unable to determine idle time",
593 curcpu
, cg
, all_used
, cg_used
);
594 cg_cpu_usage
[curcpu
].idle
= idle
;
598 /* Cannot use more CPUs than is available due to cpuset */
599 if (max_cpus
> cpu_cnt
)
602 stat_node
= find_or_create_proc_stat_node(cg_cpu_usage
, nprocs
, cg
);
604 return log_error(0, "Failed to find/create stat node for %s", cg
);
606 diff
= malloc(sizeof(struct cpuacct_usage
) * nprocs
);
611 * If the new values are LOWER than values stored in memory, it means
612 * the cgroup has been reset/recreated and we should reset too.
614 for (curcpu
= 0; curcpu
< nprocs
; curcpu
++) {
615 if (!cg_cpu_usage
[curcpu
].online
)
618 if (cg_cpu_usage
[curcpu
].user
< stat_node
->usage
[curcpu
].user
)
619 reset_proc_stat_node(stat_node
, cg_cpu_usage
, nprocs
);
624 total_sum
= diff_cpu_usage(stat_node
->usage
, cg_cpu_usage
, diff
, nprocs
);
626 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
627 stat_node
->usage
[curcpu
].online
= cg_cpu_usage
[curcpu
].online
;
629 if (!stat_node
->usage
[curcpu
].online
)
634 stat_node
->usage
[curcpu
].user
+= diff
[curcpu
].user
;
635 stat_node
->usage
[curcpu
].system
+= diff
[curcpu
].system
;
636 stat_node
->usage
[curcpu
].idle
+= diff
[curcpu
].idle
;
638 if (max_cpus
> 0 && i
>= max_cpus
) {
639 user_surplus
+= diff
[curcpu
].user
;
640 system_surplus
+= diff
[curcpu
].system
;
644 /* Calculate usage counters of visible CPUs */
646 uint64_t diff_user
= 0;
647 uint64_t diff_system
= 0;
648 uint64_t diff_idle
= 0;
649 uint64_t max_diff_idle
= 0;
650 uint64_t max_diff_idle_index
= 0;
653 /* threshold = maximum usage per cpu, including idle */
654 threshold
= total_sum
/ cpu_cnt
* max_cpus
;
656 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
657 if (!stat_node
->usage
[curcpu
].online
)
665 if (diff
[curcpu
].user
+ diff
[curcpu
].system
>= threshold
)
669 add_cpu_usage(&user_surplus
, &diff
[curcpu
],
670 &diff
[curcpu
].user
, threshold
);
672 if (diff
[curcpu
].user
+ diff
[curcpu
].system
>= threshold
)
675 /* If there is still room, add system */
676 add_cpu_usage(&system_surplus
, &diff
[curcpu
],
677 &diff
[curcpu
].system
, threshold
);
680 if (user_surplus
> 0)
681 lxcfs_debug("leftover user: %lu for %s\n", user_surplus
, cg
);
682 if (system_surplus
> 0)
683 lxcfs_debug("leftover system: %lu for %s\n", system_surplus
, cg
);
685 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
686 if (!stat_node
->usage
[curcpu
].online
)
694 stat_node
->view
[curcpu
].user
+= diff
[curcpu
].user
;
695 stat_node
->view
[curcpu
].system
+= diff
[curcpu
].system
;
696 stat_node
->view
[curcpu
].idle
+= diff
[curcpu
].idle
;
698 user_sum
+= stat_node
->view
[curcpu
].user
;
699 system_sum
+= stat_node
->view
[curcpu
].system
;
700 idle_sum
+= stat_node
->view
[curcpu
].idle
;
702 diff_user
+= diff
[curcpu
].user
;
703 diff_system
+= diff
[curcpu
].system
;
704 diff_idle
+= diff
[curcpu
].idle
;
705 if (diff
[curcpu
].idle
> max_diff_idle
) {
706 max_diff_idle
= diff
[curcpu
].idle
;
707 max_diff_idle_index
= curcpu
;
710 lxcfs_v("curcpu: %d, diff_user: %lu, diff_system: %lu, diff_idle: %lu\n", curcpu
, diff
[curcpu
].user
, diff
[curcpu
].system
, diff
[curcpu
].idle
);
712 lxcfs_v("total. diff_user: %lu, diff_system: %lu, diff_idle: %lu\n", diff_user
, diff_system
, diff_idle
);
714 /* revise cpu usage view to support partial cpu case. */
715 exact_cpus
= exact_cpu_count(cg
);
716 if (exact_cpus
< (double)max_cpus
){
717 unsigned long delta
= (unsigned long)((double)(diff_user
+ diff_system
+ diff_idle
) * (1 - exact_cpus
/ (double)max_cpus
));
719 lxcfs_v("revising cpu usage view to match the exact cpu count [%f]\n", exact_cpus
);
720 lxcfs_v("delta: %lu\n", delta
);
721 lxcfs_v("idle_sum before: %lu\n", idle_sum
);
722 idle_sum
= idle_sum
> delta
? idle_sum
- delta
: 0;
723 lxcfs_v("idle_sum after: %lu\n", idle_sum
);
725 curcpu
= max_diff_idle_index
;
726 lxcfs_v("curcpu: %d, idle before: %lu\n", curcpu
, stat_node
->view
[curcpu
].idle
);
727 stat_node
->view
[curcpu
].idle
= stat_node
->view
[curcpu
].idle
> delta
? stat_node
->view
[curcpu
].idle
- delta
: 0;
728 lxcfs_v("curcpu: %d, idle after: %lu\n", curcpu
, stat_node
->view
[curcpu
].idle
);
731 for (curcpu
= 0; curcpu
< nprocs
; curcpu
++) {
732 if (!stat_node
->usage
[curcpu
].online
)
735 stat_node
->view
[curcpu
].user
= stat_node
->usage
[curcpu
].user
;
736 stat_node
->view
[curcpu
].system
= stat_node
->usage
[curcpu
].system
;
737 stat_node
->view
[curcpu
].idle
= stat_node
->usage
[curcpu
].idle
;
739 user_sum
+= stat_node
->view
[curcpu
].user
;
740 system_sum
+= stat_node
->view
[curcpu
].system
;
741 idle_sum
+= stat_node
->view
[curcpu
].idle
;
745 /* Render the file */
747 l
= snprintf(buf
, buf_size
,
748 "cpu %" PRIu64
" 0 %" PRIu64
" %" PRIu64
" 0 0 0 0 0 0\n",
749 user_sum
, system_sum
, idle_sum
);
750 lxcfs_v("cpu-all: %s\n", buf
);
752 return log_error(0, "Failed to write cache");
754 return log_error(0, "Write to cache was truncated");
760 /* Render visible CPUs */
761 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
762 if (!stat_node
->usage
[curcpu
].online
)
767 if (max_cpus
> 0 && i
== max_cpus
)
770 l
= snprintf(buf
, buf_size
, "cpu%d %" PRIu64
" 0 %" PRIu64
" %" PRIu64
" 0 0 0 0 0 0\n",
772 stat_node
->view
[curcpu
].user
,
773 stat_node
->view
[curcpu
].system
,
774 stat_node
->view
[curcpu
].idle
);
775 lxcfs_v("cpu: %s\n", buf
);
777 return log_error(0, "Failed to write cache");
779 return log_error(0, "Write to cache was truncated");
786 /* Pass the rest of /proc/stat, start with the last line read */
787 l
= snprintf(buf
, buf_size
, "%s", line
);
789 return log_error(0, "Failed to write cache");
791 return log_error(0, "Write to cache was truncated");
797 /* Pass the rest of the host's /proc/stat */
798 while (getline(&line
, &linelen
, f
) != -1) {
799 l
= snprintf(buf
, buf_size
, "%s", line
);
801 return log_error(0, "Failed to write cache");
803 return log_error(0, "Write to cache was truncated");
811 pthread_mutex_unlock(&stat_node
->lock
);
817 * check whether this is a '^processor" line in /proc/cpuinfo
819 static inline bool is_processor_line(const char *line
)
822 return sscanf(line
, "processor : %d", &cpu
) == 1;
825 static inline bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
828 return sscanf(line
, "processor : %d", &cpu
) == 1;
831 int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
832 struct fuse_file_info
*fi
)
834 __do_free
char *cg
= NULL
, *cpuset
= NULL
, *line
= NULL
;
835 __do_free
void *fopen_cache
= NULL
;
836 __do_fclose
FILE *f
= NULL
;
837 struct fuse_context
*fc
= fuse_get_context();
838 struct lxcfs_opts
*opts
= (struct lxcfs_opts
*)fc
->private_data
;
839 struct file_info
*d
= INTTYPE_TO_PTR(fi
->fh
);
840 size_t linelen
= 0, total_len
= 0;
841 bool am_printing
= false, firstline
= true, is_s390x
= false;
842 int curcpu
= -1, cpu
, max_cpus
= 0;
844 char *cache
= d
->buf
;
845 size_t cache_size
= d
->buflen
;
850 if (offset
> d
->size
)
856 left
= d
->size
- offset
;
857 total_len
= left
> size
? size
: left
;
858 memcpy(buf
, cache
+ offset
, total_len
);
863 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
864 if (initpid
<= 1 || is_shared_pidns(initpid
))
867 cg
= get_pid_cgroup(initpid
, "cpuset");
869 return read_file_fuse("proc/cpuinfo", buf
, size
, d
);
870 prune_init_slice(cg
);
872 cpuset
= get_cpuset(cg
);
876 if (cgroup_ops
->can_use_cpuview(cgroup_ops
) && opts
&& opts
->use_cfs
)
881 max_cpus
= max_cpu_count(cg
);
883 f
= fopen_cached("/proc/cpuinfo", "re", &fopen_cache
);
887 while (getline(&line
, &linelen
, f
) != -1) {
891 if (strstr(line
, "IBM/S390") != NULL
) {
898 if (strncmp(line
, "# processors:", 12) == 0)
901 if (is_processor_line(line
)) {
902 if (use_view
&& max_cpus
> 0 && (curcpu
+1) == max_cpus
)
905 am_printing
= cpuline_in_cpuset(line
, cpuset
);
908 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
910 return log_error(0, "Failed to write cache");
912 return log_error(0, "Write to cache was truncated");
918 } else if (is_s390x
&& sscanf(line
, "processor %d:", &cpu
) == 1) {
921 if (use_view
&& max_cpus
> 0 && (curcpu
+1) == max_cpus
)
924 if (!cpu_in_cpuset(cpu
, cpuset
))
928 p
= strchr(line
, ':');
933 l
= snprintf(cache
, cache_size
, "processor %d:%s", curcpu
, p
);
935 return log_error(0, "Failed to write cache");
937 return log_error(0, "Write to cache was truncated");
946 l
= snprintf(cache
, cache_size
, "%s", line
);
948 return log_error(0, "Failed to write cache");
950 return log_error(0, "Write to cache was truncated");
959 __do_free
char *origcache
= d
->buf
;
962 d
->buf
= malloc(d
->buflen
);
964 d
->buf
= move_ptr(origcache
);
969 cache_size
= d
->buflen
;
971 l
= snprintf(cache
, cache_size
, "vendor_id : IBM/S390\n");
972 if (l
< 0 || l
>= cache_size
)
978 l
= snprintf(cache
, cache_size
, "# processors : %d\n", curcpu
+ 1);
979 if (l
< 0 || l
>= cache_size
)
985 l
= snprintf(cache
, cache_size
, "%s", origcache
);
986 if (l
< 0 || l
>= cache_size
)
993 if (total_len
> size
) total_len
= size
;
995 /* read from off 0 */
996 memcpy(buf
, d
->buf
, total_len
);
1001 * Returns 0 on success.
1002 * It is the caller's responsibility to free `return_usage`, unless this
1003 * function returns an error.
1005 int read_cpuacct_usage_all(char *cg
, char *cpuset
,
1006 struct cpuacct_usage
**return_usage
, int *size
)
1008 __do_free
char *usage_str
= NULL
;
1009 __do_free
struct cpuacct_usage
*cpu_usage
= NULL
;
1010 int cpucount
= get_nprocs_conf();
1011 int i
= 0, j
= 0, read_pos
= 0, read_cnt
= 0;
1014 uint64_t cg_user
, cg_system
;
1015 int64_t ticks_per_sec
;
1017 ticks_per_sec
= sysconf(_SC_CLK_TCK
);
1019 if (ticks_per_sec
< 0 && errno
== EINVAL
) {
1022 "read_cpuacct_usage_all failed to determine number of clock ticks "
1027 cpu_usage
= malloc(sizeof(struct cpuacct_usage
) * cpucount
);
1031 memset(cpu_usage
, 0, sizeof(struct cpuacct_usage
) * cpucount
);
1032 if (!cgroup_ops
->get(cgroup_ops
, "cpuacct", cg
, "cpuacct.usage_all", &usage_str
)) {
1034 size_t sz
= 0, asz
= 0;
1036 /* read cpuacct.usage_percpu instead. */
1037 lxcfs_v("failed to read cpuacct.usage_all. reading cpuacct.usage_percpu instead\n%s", "");
1038 if (!cgroup_ops
->get(cgroup_ops
, "cpuacct", cg
, "cpuacct.usage_percpu", &usage_str
))
1040 lxcfs_v("usage_str: %s\n", usage_str
);
1042 /* convert cpuacct.usage_percpu into cpuacct.usage_all. */
1043 lxcfs_v("converting cpuacct.usage_percpu into cpuacct.usage_all\n%s", "");
1045 must_strcat(&data
, &sz
, &asz
, "cpu user system\n");
1047 while (sscanf(usage_str
+ read_pos
, "%" PRIu64
" %n", &cg_user
, &read_cnt
) > 0) {
1048 lxcfs_debug("i: %d, cg_user: %" PRIu64
", read_pos: %d, read_cnt: %d\n", i
, cg_user
, read_pos
, read_cnt
);
1049 must_strcat(&data
, &sz
, &asz
, "%d %lu 0\n", i
, cg_user
);
1051 read_pos
+= read_cnt
;
1056 lxcfs_v("usage_str: %s\n", usage_str
);
1059 if (sscanf(usage_str
, "cpu user system\n%n", &read_cnt
) != 0)
1060 return log_error(-1, "read_cpuacct_usage_all reading first line from %s/cpuacct.usage_all failed", cg
);
1062 read_pos
+= read_cnt
;
1064 for (i
= 0, j
= 0; i
< cpucount
; i
++) {
1065 ret
= sscanf(usage_str
+ read_pos
,
1066 "%d %" PRIu64
" %" PRIu64
"\n%n", &cg_cpu
,
1067 &cg_user
, &cg_system
, &read_cnt
);
1073 return log_error(-1, "read_cpuacct_usage_all reading from %s/cpuacct.usage_all failed", cg
);
1075 read_pos
+= read_cnt
;
1077 /* Convert the time from nanoseconds to USER_HZ */
1078 cpu_usage
[j
].user
= cg_user
/ 1000.0 / 1000 / 1000 * ticks_per_sec
;
1079 cpu_usage
[j
].system
= cg_system
/ 1000.0 / 1000 / 1000 * ticks_per_sec
;
1083 *return_usage
= move_ptr(cpu_usage
);
1088 static bool cpuview_init_head(struct cg_proc_stat_head
**head
)
1090 *head
= malloc(sizeof(struct cg_proc_stat_head
));
1092 return log_error(false, "%s", strerror(errno
));
1094 (*head
)->lastcheck
= time(NULL
);
1095 (*head
)->next
= NULL
;
1097 if (pthread_rwlock_init(&(*head
)->lock
, NULL
) != 0) {
1099 return log_error(false, "Failed to initialize list lock");
1105 bool init_cpuview(void)
1109 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++)
1110 proc_stat_history
[i
] = NULL
;
1112 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
1113 if (!cpuview_init_head(&proc_stat_history
[i
]))
1120 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
1121 if (proc_stat_history
[i
])
1122 free_disarm(proc_stat_history
[i
]);
1128 static void cpuview_free_head(struct cg_proc_stat_head
*head
)
1130 struct cg_proc_stat
*node
, *tmp
;
1138 free_proc_stat_node(tmp
);
1145 pthread_rwlock_destroy(&head
->lock
);
1149 void free_cpuview(void)
1151 for (int i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++)
1152 if (proc_stat_history
[i
])
1153 cpuview_free_head(proc_stat_history
[i
]);