[lxc-devel] [lxcfs/master] virtualize the 'btime' field of /proc/stat
almostivan on Github
lxc-bot at linuxcontainers.org
Mon Jan 30 04:45:45 UTC 2017
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 507 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170130/35e73c0a/attachment.bin>
-------------- next part --------------
From 9ac264cfb0836a013af6fa31a4e4c01afe72255a Mon Sep 17 00:00:00 2001
From: Jason Baron <jbaron at akamai.com>
Date: Fri, 27 Jan 2017 16:57:54 -0500
Subject: [PATCH] virtualize the 'btime' field of /proc/stat
Currently, the 'btime' of /proc/stat reflects the boot time of the host.
We would like it to reflect when the guest boots, so use the start time of
init.
Signed-off-by: Jason Baron <jbaron at akamai.com>
---
bindings.c | 47 ++++++++++++++++++++++++++++++-----------------
1 file changed, 30 insertions(+), 17 deletions(-)
diff --git a/bindings.c b/bindings.c
index a23c349..8973df8 100644
--- a/bindings.c
+++ b/bindings.c
@@ -3450,6 +3450,27 @@ static int proc_cpuinfo_read(char *buf, size_t size, off_t offset,
return rv;
}
+static long int getreaperctime(pid_t pid)
+{
+ char fnam[100];
+ struct stat sb;
+ int ret;
+ pid_t qpid;
+
+ qpid = lookup_initpid_in_store(pid);
+ if (qpid <= 0)
+ return 0;
+
+ ret = snprintf(fnam, 100, "/proc/%d", qpid);
+ if (ret < 0 || ret >= 100)
+ return 0;
+
+ if (lstat(fnam, &sb) < 0)
+ return 0;
+
+ return sb.st_ctime;
+}
+
static int proc_stat_read(char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
@@ -3513,7 +3534,10 @@ static int proc_stat_read(char *buf, size_t size, off_t offset,
continue;
if (sscanf(line, "cpu%9[^ ]", cpu_char) != 1) {
/* not a ^cpuN line containing a number N, just print it */
- l = snprintf(cache, cache_size, "%s", line);
+ if (strncmp(line, "btime", 5) == 0)
+ l = snprintf(cache, cache_size, "btime %ld\n", getreaperctime(fc->pid));
+ else
+ l = snprintf(cache, cache_size, "%s", line);
if (l < 0) {
perror("Error writing to cache");
rv = 0;
@@ -3603,23 +3627,12 @@ static int proc_stat_read(char *buf, size_t size, off_t offset,
static long int getreaperage(pid_t pid)
{
- char fnam[100];
- struct stat sb;
- int ret;
- pid_t qpid;
-
- qpid = lookup_initpid_in_store(pid);
- if (qpid <= 0)
- return 0;
-
- ret = snprintf(fnam, 100, "/proc/%d", qpid);
- if (ret < 0 || ret >= 100)
- return 0;
-
- if (lstat(fnam, &sb) < 0)
- return 0;
+ long int ctime;
- return time(NULL) - sb.st_ctime;
+ ctime = getreaperctime(pid);
+ if (ctime)
+ return time(NULL) - ctime;
+ return ctime;
}
static unsigned long get_reaper_busy(pid_t task)
More information about the lxc-devel
mailing list