]>
git.proxmox.com Git - mirror_lxcfs.git/blob - src/proc_cpuview.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #ifndef FUSE_USE_VERSION
11 #define FUSE_USE_VERSION 30
14 #ifndef FUSE_USE_VERSION
15 #define FUSE_USE_VERSION 26
19 #define _FILE_OFFSET_BITS 64
21 #define __STDC_FORMAT_MACROS
39 #include <linux/magic.h>
40 #include <linux/sched.h>
41 #include <sys/epoll.h>
43 #include <sys/mount.h>
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <sys/syscall.h>
47 #include <sys/sysinfo.h>
51 #include "cgroup_fuse.h"
52 #include "cpuset_parse.h"
53 #include "cgroups/cgroup.h"
54 #include "cgroups/cgroup_utils.h"
55 #include "memory_utils.h"
56 #include "proc_loadavg.h"
59 /* Data for CPU view */
62 struct cpuacct_usage
*usage
; /* Real usage as read from the host's /proc/stat. */
63 struct cpuacct_usage
*view
; /* Usage stats reported to the container. */
65 pthread_mutex_t lock
; /* For node manipulation. */
66 struct cg_proc_stat
*next
;
69 struct cg_proc_stat_head
{
70 struct cg_proc_stat
*next
;
74 * For access to the list. Reading can be parallel, pruning is exclusive.
76 pthread_rwlock_t lock
;
79 #define CPUVIEW_HASH_SIZE 100
80 static struct cg_proc_stat_head
*proc_stat_history
[CPUVIEW_HASH_SIZE
];
82 static void reset_proc_stat_node(struct cg_proc_stat
*node
,
83 struct cpuacct_usage
*usage
, int cpu_count
)
85 lxcfs_debug("Resetting stat node for %s\n", node
->cg
);
86 memcpy(node
->usage
, usage
, sizeof(struct cpuacct_usage
) * cpu_count
);
88 for (int i
= 0; i
< cpu_count
; i
++) {
89 node
->view
[i
].user
= 0;
90 node
->view
[i
].system
= 0;
91 node
->view
[i
].idle
= 0;
94 node
->cpu_count
= cpu_count
;
97 static bool expand_proc_stat_node(struct cg_proc_stat
*node
, int cpu_count
)
99 __do_free
struct cpuacct_usage
*new_usage
= NULL
, *new_view
= NULL
;
101 /* Allocate new memory */
102 new_usage
= zalloc(sizeof(struct cpuacct_usage
) * cpu_count
);
106 new_view
= zalloc(sizeof(struct cpuacct_usage
) * cpu_count
);
110 /* Copy existing data & initialize new elements */
111 for (int i
= 0; i
< cpu_count
; i
++) {
112 if (i
< node
->cpu_count
) {
113 new_usage
[i
].user
= node
->usage
[i
].user
;
114 new_usage
[i
].system
= node
->usage
[i
].system
;
115 new_usage
[i
].idle
= node
->usage
[i
].idle
;
117 new_view
[i
].user
= node
->view
[i
].user
;
118 new_view
[i
].system
= node
->view
[i
].system
;
119 new_view
[i
].idle
= node
->view
[i
].idle
;
124 node
->usage
= move_ptr(new_usage
);
127 node
->view
= move_ptr(new_view
);
128 node
->cpu_count
= cpu_count
;
133 static void free_proc_stat_node(struct cg_proc_stat
*node
)
137 * We're abusing the usage pointer to indicate that
138 * pthread_mutex_init() was successful. Don't judge me.
141 pthread_mutex_destroy(&node
->lock
);
142 free_disarm(node
->cg
);
143 free_disarm(node
->usage
);
144 free_disarm(node
->view
);
149 define_cleanup_function(struct cg_proc_stat
*, free_proc_stat_node
);
151 static struct cg_proc_stat
*add_proc_stat_node(struct cg_proc_stat
*new_node
)
153 call_cleaner(free_proc_stat_node
) struct cg_proc_stat
*new = new_node
;
154 struct cg_proc_stat
*rv
= new_node
;
155 int hash
= calc_hash(new->cg
) % CPUVIEW_HASH_SIZE
;
156 struct cg_proc_stat_head
*head
= proc_stat_history
[hash
];
157 struct cg_proc_stat
*cur
;
159 pthread_rwlock_wrlock(&head
->lock
);
162 head
->next
= move_ptr(new);
163 goto out_rwlock_unlock
;
170 * The node to be added is already present in the list, so
171 * free the newly allocated one and return the one we found.
173 if (strcmp(cur
->cg
, new->cg
) == 0) {
175 goto out_rwlock_unlock
;
184 /* Add new node to end of list. */
185 cur
->next
= move_ptr(new);
186 goto out_rwlock_unlock
;
190 pthread_rwlock_unlock(&head
->lock
);
194 static struct cg_proc_stat
*new_proc_stat_node(struct cpuacct_usage
*usage
,
195 int cpu_count
, const char *cg
)
197 call_cleaner(free_proc_stat_node
) struct cg_proc_stat
*node
= NULL
;
198 __do_free
struct cpuacct_usage
*new_usage
= NULL
;
200 node
= zalloc(sizeof(struct cg_proc_stat
));
204 node
->cg
= strdup(cg
);
208 new_usage
= memdup(usage
, sizeof(struct cpuacct_usage
) * cpu_count
);
212 node
->view
= zalloc(sizeof(struct cpuacct_usage
) * cpu_count
);
216 node
->cpu_count
= cpu_count
;
218 if (pthread_mutex_init(&node
->lock
, NULL
))
221 * We're abusing the usage pointer to indicate that
222 * pthread_mutex_init() was successful. Don't judge me.
224 node
->usage
= move_ptr(new_usage
);
226 return move_ptr(node
);
229 static bool cgroup_supports(const char *controller
, const char *cgroup
,
232 __do_free
char *path
= NULL
;
235 cfd
= get_cgroup_fd(controller
);
239 path
= must_make_path_relative(cgroup
, file
, NULL
);
240 return faccessat(cfd
, path
, F_OK
, 0) == 0;
243 static struct cg_proc_stat
*prune_proc_stat_list(struct cg_proc_stat
*node
)
245 struct cg_proc_stat
*first
= NULL
;
247 for (struct cg_proc_stat
*prev
= NULL
; node
; ) {
248 if (!cgroup_supports("cpu", node
->cg
, "cpu.shares")) {
249 call_cleaner(free_proc_stat_node
) struct cg_proc_stat
*cur
= node
;
252 prev
->next
= node
->next
;
257 lxcfs_debug("Removing stat node for %s\n", cur
->cg
);
269 #define PROC_STAT_PRUNE_INTERVAL 10
270 static void prune_proc_stat_history(void)
272 time_t now
= time(NULL
);
274 for (int i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
275 pthread_rwlock_wrlock(&proc_stat_history
[i
]->lock
);
277 if ((proc_stat_history
[i
]->lastcheck
+ PROC_STAT_PRUNE_INTERVAL
) > now
) {
278 pthread_rwlock_unlock(&proc_stat_history
[i
]->lock
);
282 if (proc_stat_history
[i
]->next
) {
283 proc_stat_history
[i
]->next
= prune_proc_stat_list(proc_stat_history
[i
]->next
);
284 proc_stat_history
[i
]->lastcheck
= now
;
287 pthread_rwlock_unlock(&proc_stat_history
[i
]->lock
);
291 static struct cg_proc_stat
*find_proc_stat_node(struct cg_proc_stat_head
*head
,
294 struct cg_proc_stat
*node
;
296 pthread_rwlock_rdlock(&head
->lock
);
299 pthread_rwlock_unlock(&head
->lock
);
306 if (strcmp(cg
, node
->cg
) == 0)
308 } while ((node
= node
->next
));
313 pthread_rwlock_unlock(&head
->lock
);
314 prune_proc_stat_history();
318 static struct cg_proc_stat
*find_or_create_proc_stat_node(struct cpuacct_usage
*usage
, int cpu_count
, const char *cg
)
320 int hash
= calc_hash(cg
) % CPUVIEW_HASH_SIZE
;
321 struct cg_proc_stat_head
*head
= proc_stat_history
[hash
];
322 struct cg_proc_stat
*node
;
324 node
= find_proc_stat_node(head
, cg
);
326 node
= new_proc_stat_node(usage
, cpu_count
, cg
);
330 node
= add_proc_stat_node(node
);
331 lxcfs_debug("New stat node (%d) for %s\n", cpu_count
, cg
);
334 pthread_mutex_lock(&node
->lock
);
337 * If additional CPUs on the host have been enabled, CPU usage counter
338 * arrays have to be expanded.
340 if (node
->cpu_count
< cpu_count
) {
341 lxcfs_debug("Expanding stat node %d->%d for %s\n",
342 node
->cpu_count
, cpu_count
, cg
);
344 if (!expand_proc_stat_node(node
, cpu_count
)) {
345 pthread_mutex_unlock(&node
->lock
);
346 return log_debug(NULL
, "Unable to expand stat node %d->%d for %s", node
->cpu_count
, cpu_count
, cg
);
353 static void add_cpu_usage(uint64_t *surplus
, struct cpuacct_usage
*usage
,
354 uint64_t *counter
, uint64_t threshold
)
356 uint64_t free_space
, to_add
;
358 free_space
= threshold
- usage
->user
- usage
->system
;
360 if (free_space
> usage
->idle
)
361 free_space
= usage
->idle
;
363 if (free_space
> *surplus
)
369 usage
->idle
-= to_add
;
373 static uint64_t diff_cpu_usage(struct cpuacct_usage
*older
,
374 struct cpuacct_usage
*newer
,
375 struct cpuacct_usage
*diff
, int cpu_count
)
379 for (int i
= 0; i
< cpu_count
; i
++) {
380 if (!newer
[i
].online
)
384 * When cpuset is changed on the fly, the CPUs might get
385 * reordered. We could either reset all counters, or check
386 * that the substractions below will return expected results.
388 if (newer
[i
].user
> older
[i
].user
)
389 diff
[i
].user
= newer
[i
].user
- older
[i
].user
;
393 if (newer
[i
].system
> older
[i
].system
)
394 diff
[i
].system
= newer
[i
].system
- older
[i
].system
;
398 if (newer
[i
].idle
> older
[i
].idle
)
399 diff
[i
].idle
= newer
[i
].idle
- older
[i
].idle
;
404 sum
+= diff
[i
].system
;
412 * Read cgroup CPU quota parameters from `cpu.cfs_quota_us` or
413 * `cpu.cfs_period_us`, depending on `param`. Parameter value is returned
416 static bool read_cpu_cfs_param(const char *cg
, const char *param
, int64_t *value
)
418 __do_free
char *str
= NULL
;
419 char file
[STRLITERALLEN("cpu.cfs_period_us") + 1];
423 if (pure_unified_layout(cgroup_ops
)) {
424 first
= !strcmp(param
, "quota");
425 ret
= snprintf(file
, sizeof(file
), "cpu.max");
427 ret
= snprintf(file
, sizeof(file
), "cpu.cfs_%s_us", param
);
429 if (ret
< 0 || (size_t)ret
>= sizeof(file
))
432 if (!cgroup_ops
->get(cgroup_ops
, "cpu", cg
, file
, &str
))
435 return sscanf(str
, first
? "%" PRId64
: "%*d %" PRId64
, value
) == 1;
439 * Return the exact number of visible CPUs based on CPU quotas.
440 * If there is no quota set, zero is returned.
442 static double exact_cpu_count(const char *cg
)
446 int64_t cfs_quota
, cfs_period
;
448 if (!read_cpu_cfs_param(cg
, "quota", &cfs_quota
))
451 if (!read_cpu_cfs_param(cg
, "period", &cfs_period
))
454 if (cfs_quota
<= 0 || cfs_period
<= 0)
457 rv
= (double)cfs_quota
/ (double)cfs_period
;
459 nprocs
= get_nprocs();
468 * Return the maximum number of visible CPUs based on CPU quotas.
469 * If there is no quota set, zero is returned.
471 int max_cpu_count(const char *cg
)
473 __do_free
char *cpuset
= NULL
;
475 int64_t cfs_quota
, cfs_period
;
476 int nr_cpus_in_cpuset
= 0;
478 if (!read_cpu_cfs_param(cg
, "quota", &cfs_quota
))
481 if (!read_cpu_cfs_param(cg
, "period", &cfs_period
))
484 cpuset
= get_cpuset(cg
);
486 nr_cpus_in_cpuset
= cpu_number_in_cpuset(cpuset
);
488 if (cfs_quota
<= 0 || cfs_period
<= 0) {
489 if (nr_cpus_in_cpuset
> 0)
490 return nr_cpus_in_cpuset
;
495 rv
= cfs_quota
/ cfs_period
;
498 * In case quota/period does not yield a whole number, add one CPU for
501 if ((cfs_quota
% cfs_period
) > 0)
504 nprocs
= get_nprocs();
508 /* Use min value in cpu quota and cpuset. */
509 if (nr_cpus_in_cpuset
> 0 && nr_cpus_in_cpuset
< rv
)
510 rv
= nr_cpus_in_cpuset
;
515 int cpuview_proc_stat(const char *cg
, const char *cpuset
,
516 struct cpuacct_usage
*cg_cpu_usage
, int cg_cpu_usage_size
,
517 FILE *f
, char *buf
, size_t buf_size
)
519 __do_free
char *line
= NULL
;
520 __do_free
struct cpuacct_usage
*diff
= NULL
;
521 size_t linelen
= 0, total_len
= 0;
522 int curcpu
= -1; /* cpu numbering starts at 0 */
525 uint64_t user
= 0, nice
= 0, system
= 0, idle
= 0, iowait
= 0, irq
= 0,
526 softirq
= 0, steal
= 0, guest
= 0, guest_nice
= 0;
527 uint64_t user_sum
= 0, system_sum
= 0, idle_sum
= 0;
528 uint64_t user_surplus
= 0, system_surplus
= 0;
529 int nprocs
, max_cpus
;
531 uint64_t total_sum
, threshold
;
532 struct cg_proc_stat
*stat_node
;
534 nprocs
= get_nprocs_conf();
535 if (cg_cpu_usage_size
< nprocs
)
536 nprocs
= cg_cpu_usage_size
;
538 /* Read all CPU stats and stop when we've encountered other lines */
539 while (getline(&line
, &linelen
, f
) != -1) {
541 char cpu_char
[10]; /* That's a lot of cores */
542 uint64_t all_used
, cg_used
;
544 if (strlen(line
) == 0)
547 /* not a ^cpuN line containing a number N */
548 if (sscanf(line
, "cpu%9[^ ]", cpu_char
) != 1)
551 if (sscanf(cpu_char
, "%d", &physcpu
) != 1)
554 if (physcpu
>= cg_cpu_usage_size
)
560 if (!cpu_in_cpuset(physcpu
, cpuset
)) {
561 for (i
= curcpu
; i
<= physcpu
; i
++)
562 cg_cpu_usage
[i
].online
= false;
566 if (curcpu
< physcpu
) {
567 /* Some CPUs may be disabled */
568 for (i
= curcpu
; i
< physcpu
; i
++)
569 cg_cpu_usage
[i
].online
= false;
574 cg_cpu_usage
[curcpu
].online
= true;
576 ret
= sscanf(line
, "%*s %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
" %" PRIu64
"lu",
590 all_used
= user
+ nice
+ system
+ iowait
+ irq
+ softirq
+ steal
+ guest
+ guest_nice
;
591 cg_used
= cg_cpu_usage
[curcpu
].user
+ cg_cpu_usage
[curcpu
].system
;
593 if (all_used
>= cg_used
) {
594 cg_cpu_usage
[curcpu
].idle
= idle
+ (all_used
- cg_used
);
597 lxcfs_error("cpu%d from %s has unexpected cpu time: %" PRIu64
" in /proc/stat, %" PRIu64
" in cpuacct.usage_all; unable to determine idle time",
598 curcpu
, cg
, all_used
, cg_used
);
599 cg_cpu_usage
[curcpu
].idle
= idle
;
603 /* Cannot use more CPUs than is available in cpuset. */
604 max_cpus
= max_cpu_count(cg
);
605 if (max_cpus
> cpu_cnt
|| !max_cpus
)
608 stat_node
= find_or_create_proc_stat_node(cg_cpu_usage
, nprocs
, cg
);
610 return log_error(0, "Failed to find/create stat node for %s", cg
);
612 diff
= zalloc(sizeof(struct cpuacct_usage
) * nprocs
);
617 * If the new values are LOWER than values stored in memory, it means
618 * the cgroup has been reset/recreated and we should reset too.
620 for (curcpu
= 0; curcpu
< nprocs
; curcpu
++) {
621 if (!cg_cpu_usage
[curcpu
].online
)
624 if (cg_cpu_usage
[curcpu
].user
< stat_node
->usage
[curcpu
].user
)
625 reset_proc_stat_node(stat_node
, cg_cpu_usage
, nprocs
);
630 total_sum
= diff_cpu_usage(stat_node
->usage
, cg_cpu_usage
, diff
, nprocs
);
632 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
633 stat_node
->usage
[curcpu
].online
= cg_cpu_usage
[curcpu
].online
;
635 if (!stat_node
->usage
[curcpu
].online
)
640 stat_node
->usage
[curcpu
].user
+= diff
[curcpu
].user
;
641 stat_node
->usage
[curcpu
].system
+= diff
[curcpu
].system
;
642 stat_node
->usage
[curcpu
].idle
+= diff
[curcpu
].idle
;
644 if (max_cpus
> 0 && i
>= max_cpus
) {
645 user_surplus
+= diff
[curcpu
].user
;
646 system_surplus
+= diff
[curcpu
].system
;
650 /* Calculate usage counters of visible CPUs */
652 uint64_t diff_user
= 0;
653 uint64_t diff_system
= 0;
654 uint64_t diff_idle
= 0;
655 uint64_t max_diff_idle
= 0;
656 uint64_t max_diff_idle_index
= 0;
659 /* threshold = maximum usage per cpu, including idle */
660 threshold
= total_sum
/ cpu_cnt
* max_cpus
;
662 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
663 if (!stat_node
->usage
[curcpu
].online
)
671 if (diff
[curcpu
].user
+ diff
[curcpu
].system
>= threshold
)
675 add_cpu_usage(&user_surplus
, &diff
[curcpu
],
676 &diff
[curcpu
].user
, threshold
);
678 if (diff
[curcpu
].user
+ diff
[curcpu
].system
>= threshold
)
681 /* If there is still room, add system */
682 add_cpu_usage(&system_surplus
, &diff
[curcpu
],
683 &diff
[curcpu
].system
, threshold
);
686 if (user_surplus
> 0)
687 lxcfs_debug("leftover user: %lu for %s\n", user_surplus
, cg
);
688 if (system_surplus
> 0)
689 lxcfs_debug("leftover system: %lu for %s\n", system_surplus
, cg
);
691 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
692 if (!stat_node
->usage
[curcpu
].online
)
700 stat_node
->view
[curcpu
].user
+= diff
[curcpu
].user
;
701 stat_node
->view
[curcpu
].system
+= diff
[curcpu
].system
;
702 stat_node
->view
[curcpu
].idle
+= diff
[curcpu
].idle
;
704 user_sum
+= stat_node
->view
[curcpu
].user
;
705 system_sum
+= stat_node
->view
[curcpu
].system
;
706 idle_sum
+= stat_node
->view
[curcpu
].idle
;
708 diff_user
+= diff
[curcpu
].user
;
709 diff_system
+= diff
[curcpu
].system
;
710 diff_idle
+= diff
[curcpu
].idle
;
711 if (diff
[curcpu
].idle
> max_diff_idle
) {
712 max_diff_idle
= diff
[curcpu
].idle
;
713 max_diff_idle_index
= curcpu
;
716 lxcfs_v("curcpu: %d, diff_user: %lu, diff_system: %lu, diff_idle: %lu\n", curcpu
, diff
[curcpu
].user
, diff
[curcpu
].system
, diff
[curcpu
].idle
);
718 lxcfs_v("total. diff_user: %lu, diff_system: %lu, diff_idle: %lu\n", diff_user
, diff_system
, diff_idle
);
720 /* revise cpu usage view to support partial cpu case. */
721 exact_cpus
= exact_cpu_count(cg
);
722 if (exact_cpus
< (double)max_cpus
){
723 uint64_t delta
= (uint64_t)((double)(diff_user
+ diff_system
+ diff_idle
) * (1 - exact_cpus
/ (double)max_cpus
));
725 lxcfs_v("revising cpu usage view to match the exact cpu count [%f]\n", exact_cpus
);
726 lxcfs_v("delta: %lu\n", delta
);
727 lxcfs_v("idle_sum before: %lu\n", idle_sum
);
728 if (idle_sum
> delta
)
729 idle_sum
= idle_sum
- delta
;
732 lxcfs_v("idle_sum after: %lu\n", idle_sum
);
734 curcpu
= max_diff_idle_index
;
735 lxcfs_v("curcpu: %d, idle before: %lu\n", curcpu
, stat_node
->view
[curcpu
].idle
);
736 if (stat_node
->view
[curcpu
].idle
> delta
)
737 stat_node
->view
[curcpu
].idle
= stat_node
->view
[curcpu
].idle
- delta
;
739 stat_node
->view
[curcpu
].idle
= 0;
740 lxcfs_v("curcpu: %d, idle after: %lu\n", curcpu
, stat_node
->view
[curcpu
].idle
);
743 for (curcpu
= 0; curcpu
< nprocs
; curcpu
++) {
744 if (!stat_node
->usage
[curcpu
].online
)
747 stat_node
->view
[curcpu
].user
= stat_node
->usage
[curcpu
].user
;
748 stat_node
->view
[curcpu
].system
= stat_node
->usage
[curcpu
].system
;
749 stat_node
->view
[curcpu
].idle
= stat_node
->usage
[curcpu
].idle
;
751 user_sum
+= stat_node
->view
[curcpu
].user
;
752 system_sum
+= stat_node
->view
[curcpu
].system
;
753 idle_sum
+= stat_node
->view
[curcpu
].idle
;
757 /* Render the file */
759 l
= snprintf(buf
, buf_size
,
760 "cpu %" PRIu64
" 0 %" PRIu64
" %" PRIu64
" 0 0 0 0 0 0\n",
761 user_sum
, system_sum
, idle_sum
);
762 lxcfs_v("cpu-all: %s\n", buf
);
764 return log_error(0, "Failed to write cache");
766 return log_error(0, "Write to cache was truncated");
772 /* Render visible CPUs */
773 for (curcpu
= 0, i
= -1; curcpu
< nprocs
; curcpu
++) {
774 if (!stat_node
->usage
[curcpu
].online
)
779 if (max_cpus
> 0 && i
== max_cpus
)
782 l
= snprintf(buf
, buf_size
, "cpu%d %" PRIu64
" 0 %" PRIu64
" %" PRIu64
" 0 0 0 0 0 0\n",
784 stat_node
->view
[curcpu
].user
,
785 stat_node
->view
[curcpu
].system
,
786 stat_node
->view
[curcpu
].idle
);
787 lxcfs_v("cpu: %s\n", buf
);
789 return log_error(0, "Failed to write cache");
791 return log_error(0, "Write to cache was truncated");
798 /* Pass the rest of /proc/stat, start with the last line read */
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");
809 /* Pass the rest of the host's /proc/stat */
810 while (getline(&line
, &linelen
, f
) != -1) {
811 l
= snprintf(buf
, buf_size
, "%s", line
);
813 return log_error(0, "Failed to write cache");
815 return log_error(0, "Write to cache was truncated");
823 pthread_mutex_unlock(&stat_node
->lock
);
829 * check whether this is a '^processor" line in /proc/cpuinfo
831 static inline bool is_processor_line(const char *line
)
834 return sscanf(line
, "processor : %d", &cpu
) == 1;
837 static inline bool cpuline_in_cpuset(const char *line
, const char *cpuset
)
841 if (sscanf(line
, "processor : %d", &cpu
) == 1)
842 return cpu_in_cpuset(cpu
, cpuset
);
847 int proc_cpuinfo_read(char *buf
, size_t size
, off_t offset
,
848 struct fuse_file_info
*fi
)
850 __do_free
char *cg
= NULL
, *cpuset
= NULL
, *line
= NULL
;
851 __do_free
void *fopen_cache
= NULL
;
852 __do_fclose
FILE *f
= NULL
;
853 struct fuse_context
*fc
= fuse_get_context();
854 struct lxcfs_opts
*opts
= (struct lxcfs_opts
*)fc
->private_data
;
855 struct file_info
*d
= INTTYPE_TO_PTR(fi
->fh
);
856 size_t linelen
= 0, total_len
= 0;
857 bool am_printing
= false, firstline
= true, is_s390x
= false;
858 int curcpu
= -1, cpu
, max_cpus
= 0;
860 char *cache
= d
->buf
;
861 size_t cache_size
= d
->buflen
;
866 if (offset
> d
->size
)
872 left
= d
->size
- offset
;
873 total_len
= left
> size
? size
: left
;
874 memcpy(buf
, cache
+ offset
, total_len
);
879 pid_t initpid
= lookup_initpid_in_store(fc
->pid
);
880 if (initpid
<= 1 || is_shared_pidns(initpid
))
883 cg
= get_pid_cgroup(initpid
, "cpuset");
885 return read_file_fuse("proc/cpuinfo", buf
, size
, d
);
886 prune_init_slice(cg
);
888 cpuset
= get_cpuset(cg
);
892 if (cgroup_ops
->can_use_cpuview(cgroup_ops
) && opts
&& opts
->use_cfs
)
897 max_cpus
= max_cpu_count(cg
);
899 f
= fopen_cached("/proc/cpuinfo", "re", &fopen_cache
);
903 while (getline(&line
, &linelen
, f
) != -1) {
907 if (strstr(line
, "IBM/S390") != NULL
) {
914 if (strncmp(line
, "# processors:", 12) == 0)
917 if (is_processor_line(line
)) {
918 if (use_view
&& max_cpus
> 0 && (curcpu
+ 1) == max_cpus
)
921 am_printing
= cpuline_in_cpuset(line
, cpuset
);
924 l
= snprintf(cache
, cache_size
, "processor : %d\n", curcpu
);
926 return log_error(0, "Failed to write cache");
928 return log_error(0, "Write to cache was truncated");
934 } else if (is_s390x
&& sscanf(line
, "processor %d:", &cpu
) == 1) {
937 if (use_view
&& max_cpus
> 0 && (curcpu
+ 1) == max_cpus
)
940 if (!cpu_in_cpuset(cpu
, cpuset
))
944 p
= strchr(line
, ':');
949 l
= snprintf(cache
, cache_size
, "processor %d:%s", curcpu
, p
);
951 return log_error(0, "Failed to write cache");
953 return log_error(0, "Write to cache was truncated");
962 l
= snprintf(cache
, cache_size
, "%s", line
);
964 return log_error(0, "Failed to write cache");
966 return log_error(0, "Write to cache was truncated");
975 __do_free
char *origcache
= d
->buf
;
978 d
->buf
= malloc(d
->buflen
);
980 d
->buf
= move_ptr(origcache
);
985 cache_size
= d
->buflen
;
987 l
= snprintf(cache
, cache_size
, "vendor_id : IBM/S390\n");
988 if (l
< 0 || l
>= cache_size
)
994 l
= snprintf(cache
, cache_size
, "# processors : %d\n", curcpu
+ 1);
995 if (l
< 0 || l
>= cache_size
)
1001 l
= snprintf(cache
, cache_size
, "%s", origcache
);
1002 if (l
< 0 || l
>= cache_size
)
1008 d
->size
= total_len
;
1009 if (total_len
> size
)
1012 /* read from off 0 */
1013 memcpy(buf
, d
->buf
, total_len
);
1019 * Returns 0 on success.
1020 * It is the caller's responsibility to free `return_usage`, unless this
1021 * function returns an error.
1023 int read_cpuacct_usage_all(char *cg
, char *cpuset
,
1024 struct cpuacct_usage
**return_usage
, int *size
)
1026 __do_free
char *usage_str
= NULL
;
1027 __do_free
struct cpuacct_usage
*cpu_usage
= NULL
;
1028 int i
= 0, j
= 0, read_pos
= 0, read_cnt
= 0;
1032 uint64_t cg_user
, cg_system
;
1033 int64_t ticks_per_sec
;
1035 ticks_per_sec
= sysconf(_SC_CLK_TCK
);
1036 if (ticks_per_sec
< 0 && errno
== EINVAL
) {
1037 lxcfs_debug("%m - Failed to determine number of ticks per second");
1041 cpucount
= get_nprocs_conf();
1042 cpu_usage
= malloc(sizeof(struct cpuacct_usage
) * cpucount
);
1046 memset(cpu_usage
, 0, sizeof(struct cpuacct_usage
) * cpucount
);
1047 if (!cgroup_ops
->get(cgroup_ops
, "cpuacct", cg
, "cpuacct.usage_all", &usage_str
)) {
1048 char *sep
= " \t\n";
1051 /* Read cpuacct.usage_percpu instead. */
1052 lxcfs_debug("Falling back to cpuacct.usage_percpu");
1053 if (!cgroup_ops
->get(cgroup_ops
, "cpuacct", cg
, "cpuacct.usage_percpu", &usage_str
))
1056 lxc_iterate_parts(tok
, usage_str
, sep
) {
1057 uint64_t percpu_user
;
1062 tok
= trim_whitespace_in_place(tok
);
1063 ret
= safe_uint64(tok
, &percpu_user
, 10);
1067 /* Convert the time from nanoseconds to USER_HZ */
1068 cpu_usage
[i
].user
= percpu_user
/ 1000.0 / 1000 / 1000 * ticks_per_sec
;
1069 cpu_usage
[i
].system
= cpu_usage
[i
].user
;
1071 lxcfs_debug("cpu%d with time %s", i
, tok
);
1074 if (sscanf(usage_str
, "cpu user system\n%n", &read_cnt
) != 0)
1075 return log_error(-1, "read_cpuacct_usage_all reading first line from %s/cpuacct.usage_all failed", cg
);
1077 read_pos
+= read_cnt
;
1079 for (i
= 0, j
= 0; i
< cpucount
; i
++) {
1080 ret
= sscanf(usage_str
+ read_pos
,
1081 "%d %" PRIu64
" %" PRIu64
"\n%n", &cg_cpu
,
1082 &cg_user
, &cg_system
, &read_cnt
);
1088 return log_error(-EINVAL
, "Failed to parse cpuacct.usage_all line %s from cgroup %s",
1089 usage_str
+ read_pos
, cg
);
1091 read_pos
+= read_cnt
;
1093 /* Convert the time from nanoseconds to USER_HZ */
1094 cpu_usage
[j
].user
= cg_user
/ 1000.0 / 1000 / 1000 * ticks_per_sec
;
1095 cpu_usage
[j
].system
= cg_system
/ 1000.0 / 1000 / 1000 * ticks_per_sec
;
1100 *return_usage
= move_ptr(cpu_usage
);
1105 static bool cpuview_init_head(struct cg_proc_stat_head
**head
)
1107 __do_free
struct cg_proc_stat_head
*h
;
1109 h
= zalloc(sizeof(struct cg_proc_stat_head
));
1113 if (pthread_rwlock_init(&h
->lock
, NULL
))
1116 h
->lastcheck
= time(NULL
);
1118 *head
= move_ptr(h
);
1122 bool init_cpuview(void)
1126 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++)
1127 proc_stat_history
[i
] = NULL
;
1129 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
1130 if (!cpuview_init_head(&proc_stat_history
[i
]))
1137 for (i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++) {
1138 if (proc_stat_history
[i
])
1139 free_disarm(proc_stat_history
[i
]);
1145 static void cpuview_free_head(struct cg_proc_stat_head
*head
)
1147 struct cg_proc_stat
*node
;
1153 struct cg_proc_stat
*cur
= node
;
1155 free_proc_stat_node(cur
);
1161 pthread_rwlock_destroy(&head
->lock
);
1165 void free_cpuview(void)
1167 for (int i
= 0; i
< CPUVIEW_HASH_SIZE
; i
++)
1168 if (proc_stat_history
[i
])
1169 cpuview_free_head(proc_stat_history
[i
]);