/* SPDX-License-Identifier: LGPL-2.1+ */
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
-#ifndef FUSE_USE_VERSION
-#define FUSE_USE_VERSION 26
-#endif
-
-#define _FILE_OFFSET_BITS 64
+#include "config.h"
-#define __STDC_FORMAT_MACROS
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
-#include <fuse.h>
#include <inttypes.h>
#include <libgen.h>
#include <pthread.h>
#include <sys/sysinfo.h>
#include <sys/vfs.h>
+#include "proc_cpuview.h"
+
#include "bindings.h"
-#include "config.h"
#include "cgroup_fuse.h"
#include "cpuset_parse.h"
#include "cgroups/cgroup.h"
for (struct cg_proc_stat *prev = NULL; node; ) {
if (!cgroup_supports("cpu", node->cg, "cpu.shares")) {
- call_cleaner(free_proc_stat_node) struct cg_proc_stat *cur = node;
+ struct cg_proc_stat *cur = node;
if (prev)
prev->next = node->next;
first = node->next;
node = node->next;
- lxcfs_debug("Removing stat node for %s\n", cur->cg);
+ lxcfs_debug("Removing stat node for %s\n", cur);
+
+ free_proc_stat_node(cur);
} else {
if (!first)
first = node;
return node;
}
-static struct cg_proc_stat *find_or_create_proc_stat_node(struct cpuacct_usage *usage, int cpu_count, const char *cg)
+static struct cg_proc_stat *find_or_create_proc_stat_node(struct cpuacct_usage *usage,
+ int cpu_count, const char *cg)
{
int hash = calc_hash(cg) % CPUVIEW_HASH_SIZE;
struct cg_proc_stat_head *head = proc_stat_history[hash];
/*
* Read cgroup CPU quota parameters from `cpu.cfs_quota_us` or
* `cpu.cfs_period_us`, depending on `param`. Parameter value is returned
- * throuh `value`.
+ * through `value`.
*/
static bool read_cpu_cfs_param(const char *cg, const char *param, int64_t *value)
{
if (all_used >= cg_used) {
cg_cpu_usage[curcpu].idle = idle + (all_used - cg_used);
-
} else {
- lxcfs_error("cpu%d from %s has unexpected cpu time: %" PRIu64 " in /proc/stat, %" PRIu64 " in cpuacct.usage_all; unable to determine idle time",
- curcpu, cg, all_used, cg_used);
+ lxcfs_v("cpu%d from %s has unexpected cpu time: %" PRIu64 " in /proc/stat, %" PRIu64 " in cpuacct.usage_all; unable to determine idle time",
+ curcpu, cg, all_used, cg_used);
cg_cpu_usage[curcpu].idle = idle;
}
}
if (max_cpus > cpu_cnt || !max_cpus)
max_cpus = cpu_cnt;
+ /* takes lock pthread_mutex_lock(&node->lock) */
stat_node = find_or_create_proc_stat_node(cg_cpu_usage, nprocs, cg);
if (!stat_node)
return log_error(0, "Failed to find/create stat node for %s", cg);
diff = zalloc(sizeof(struct cpuacct_usage) * nprocs);
if (!diff)
- return 0;
+ goto out_pthread_mutex_unlock;
/*
* If the new values are LOWER than values stored in memory, it means
uint64_t max_diff_idle = 0;
uint64_t max_diff_idle_index = 0;
double exact_cpus;
-
/* threshold = maximum usage per cpu, including idle */
threshold = total_sum / cpu_cnt * max_cpus;
"cpu %" PRIu64 " 0 %" PRIu64 " %" PRIu64 " 0 0 0 0 0 0\n",
user_sum, system_sum, idle_sum);
lxcfs_v("cpu-all: %s\n", buf);
- if (l < 0)
- return log_error(0, "Failed to write cache");
- if (l >= buf_size)
- return log_error(0, "Write to cache was truncated");
+ if (l < 0) {
+ lxcfs_error("Failed to write cache");
+ total_len = 0;
+ goto out_pthread_mutex_unlock;
+ }
+ if ((size_t)l >= buf_size) {
+ lxcfs_error("Write to cache was truncated");
+ total_len = 0;
+ goto out_pthread_mutex_unlock;
+ }
buf += l;
buf_size -= l;
stat_node->view[curcpu].system,
stat_node->view[curcpu].idle);
lxcfs_v("cpu: %s\n", buf);
- if (l < 0)
- return log_error(0, "Failed to write cache");
- if (l >= buf_size)
- return log_error(0, "Write to cache was truncated");
+ if (l < 0) {
+ lxcfs_error("Failed to write cache");
+ total_len = 0;
+ goto out_pthread_mutex_unlock;
+ }
+ if ((size_t)l >= buf_size) {
+ lxcfs_error("Write to cache was truncated");
+ total_len = 0;
+ goto out_pthread_mutex_unlock;
+ }
buf += l;
buf_size -= l;
/* Pass the rest of /proc/stat, start with the last line read */
l = snprintf(buf, buf_size, "%s", line);
- if (l < 0)
- return log_error(0, "Failed to write cache");
- if (l >= buf_size)
- return log_error(0, "Write to cache was truncated");
+ if (l < 0) {
+ lxcfs_error("Failed to write cache");
+ total_len = 0;
+ goto out_pthread_mutex_unlock;
+ }
+ if ((size_t)l >= buf_size) {
+ lxcfs_error("Write to cache was truncated");
+ total_len = 0;
+ goto out_pthread_mutex_unlock;
+ }
buf += l;
buf_size -= l;
/* Pass the rest of the host's /proc/stat */
while (getline(&line, &linelen, f) != -1) {
l = snprintf(buf, buf_size, "%s", line);
- if (l < 0)
- return log_error(0, "Failed to write cache");
- if (l >= buf_size)
- return log_error(0, "Write to cache was truncated");
+ if (l < 0) {
+ lxcfs_error("Failed to write cache");
+ total_len = 0;
+ goto out_pthread_mutex_unlock;
+ }
+ if ((size_t)l >= buf_size) {
+ lxcfs_error("Write to cache was truncated");
+ total_len = 0;
+ goto out_pthread_mutex_unlock;
+ }
buf += l;
buf_size -= l;
total_len += l;
}
+out_pthread_mutex_unlock:
if (stat_node)
pthread_mutex_unlock(&stat_node->lock);
size_t cache_size = d->buflen;
if (offset) {
- int left;
+ size_t left;
if (offset > d->size)
return -EINVAL;
l = snprintf(cache, cache_size, "processor : %d\n", curcpu);
if (l < 0)
return log_error(0, "Failed to write cache");
- if (l >= cache_size)
+ if ((size_t)l >= cache_size)
return log_error(0, "Write to cache was truncated");
cache += l;
cache_size -= l;
l = snprintf(cache, cache_size, "processor %d:%s", curcpu, p);
if (l < 0)
return log_error(0, "Failed to write cache");
- if (l >= cache_size)
+ if ((size_t)l >= cache_size)
return log_error(0, "Write to cache was truncated");
cache += l;
l = snprintf(cache, cache_size, "%s", line);
if (l < 0)
return log_error(0, "Failed to write cache");
- if (l >= cache_size)
+ if ((size_t)l >= cache_size)
return log_error(0, "Write to cache was truncated");
cache += l;
cache_size = d->buflen;
total_len = 0;
l = snprintf(cache, cache_size, "vendor_id : IBM/S390\n");
- if (l < 0 || l >= cache_size)
+ if (l < 0 || (size_t)l >= cache_size)
return 0;
cache_size -= l;
cache += l;
total_len += l;
l = snprintf(cache, cache_size, "# processors : %d\n", curcpu + 1);
- if (l < 0 || l >= cache_size)
+ if (l < 0 || (size_t)l >= cache_size)
return 0;
cache_size -= l;
cache += l;
total_len += l;
l = snprintf(cache, cache_size, "%s", origcache);
- if (l < 0 || l >= cache_size)
+ if (l < 0 || (size_t)l >= cache_size)
return 0;
total_len += l;
}