[v9,05/10] parse: add a helper to obtain an uptime

Submitted by Pavel Tikhomirov on April 3, 2018, 9:34 a.m.

Details

Message ID 20180403093426.10894-6-ptikhomirov@virtuozzo.com
State New
Series "don't use wrong pagemap (from other task) on pid reuse"
Headers show

Commit Message

Pavel Tikhomirov April 3, 2018, 9:34 a.m.
will be used in the next patch

https://jira.sw.ru/browse/PSBM-67502

note: man for /proc/uptime says that uptime is in seconds and for now
the format is "seconds.centiseconds", where ecentiseconds is 2 digits

v8: add length specifier to parse only centiseconds
v9: put uptime to u_int64_t directly, define CSEC_PER_SEC

Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
---
 criu/include/proc_parse.h |  1 +
 criu/proc_parse.c         | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

Patch hide | download patch | download mbox

diff --git a/criu/include/proc_parse.h b/criu/include/proc_parse.h
index 0f5e91056..13072bfd5 100644
--- a/criu/include/proc_parse.h
+++ b/criu/include/proc_parse.h
@@ -108,5 +108,6 @@  int parse_children(pid_t pid, pid_t **_c, int *_n);
 
 extern bool is_vma_range_fmt(char *line);
 extern void parse_vmflags(char *buf, u32 *flags, u64 *madv, int *io_pf);
+extern int parse_uptime(u_int64_t *upt);
 
 #endif /* __CR_PROC_PARSE_H__ */
diff --git a/criu/proc_parse.c b/criu/proc_parse.c
index 4ab11a31a..387a90323 100644
--- a/criu/proc_parse.c
+++ b/criu/proc_parse.c
@@ -2712,3 +2712,28 @@  int parse_children(pid_t pid, pid_t **_c, int *_n)
 	xfree(ch);
 	return -1;
 }
+
+#define CSEC_PER_SEC 100
+
+__maybe_unused int parse_uptime(u_int64_t *upt)
+{
+	unsigned long sec, csec;
+	FILE *f;
+
+	f = fopen("/proc/uptime", "r");
+	if (!f) {
+		pr_perror("Failed to fopen /proc/uptime");
+		return -1;
+	}
+
+	if (fscanf(f, "%lu.%2lu", &sec, &csec) != 2) {
+		pr_perror("Failed to parse /proc/uptime");
+		fclose(f);
+		return -1;
+	}
+
+	*upt = sec * USEC_PER_SEC + csec * (USEC_PER_SEC / CSEC_PER_SEC);
+
+	fclose(f);
+	return 0;
+}

Comments

Dmitry Safonov April 3, 2018, 1:11 p.m.
2018-04-03 10:34 GMT+01:00 Pavel Tikhomirov <ptikhomirov@virtuozzo.com>:
[..]
> +
> +#define CSEC_PER_SEC 100
> +
> +__maybe_unused int parse_uptime(u_int64_t *upt)

Nit: can we use uint64_t?
I see u_int64_t the first time and git says we've it only in one other place.

> +{
> +       unsigned long sec, csec;
> +       FILE *f;
> +
> +       f = fopen("/proc/uptime", "r");
> +       if (!f) {
> +               pr_perror("Failed to fopen /proc/uptime");
> +               return -1;
> +       }
> +
> +       if (fscanf(f, "%lu.%2lu", &sec, &csec) != 2) {
> +               pr_perror("Failed to parse /proc/uptime");
> +               fclose(f);
> +               return -1;
> +       }
> +
> +       *upt = sec * USEC_PER_SEC + csec * (USEC_PER_SEC / CSEC_PER_SEC);

It's not clear from the commit message, why do you save time in usec,
rather than in csec?

Thanks,
             Dmitry
Pavel Tikhomirov April 3, 2018, 1:24 p.m.
On 04/03/2018 04:11 PM, Dmitry Safonov wrote:
> 2018-04-03 10:34 GMT+01:00 Pavel Tikhomirov <ptikhomirov@virtuozzo.com>:
> [..]
>> +
>> +#define CSEC_PER_SEC 100
>> +
>> +__maybe_unused int parse_uptime(u_int64_t *upt)
> 
> Nit: can we use uint64_t?
> I see u_int64_t the first time and git says we've it only in one other place.

Oops, I brought it from encode_time - will use uint64_t instead, thanks!

> 
>> +{
>> +       unsigned long sec, csec;
>> +       FILE *f;
>> +
>> +       f = fopen("/proc/uptime", "r");
>> +       if (!f) {
>> +               pr_perror("Failed to fopen /proc/uptime");
>> +               return -1;
>> +       }
>> +
>> +       if (fscanf(f, "%lu.%2lu", &sec, &csec) != 2) {
>> +               pr_perror("Failed to parse /proc/uptime");
>> +               fclose(f);
>> +               return -1;
>> +       }
>> +
>> +       *upt = sec * USEC_PER_SEC + csec * (USEC_PER_SEC / CSEC_PER_SEC);
> 
> It's not clear from the commit message, why do you save time in usec,
> rather than in csec?

I thought it would be better to have usec in image, so when/if we have 
more precise uptime we can just reuse same variable in image and won't 
need adding another one.

> 
> Thanks,
>               Dmitry
>
Dmitry Safonov April 3, 2018, 1:29 p.m.
вт, 3 апр 2018 г., 14:24 Pavel Tikhomirov <ptikhomirov@virtuozzo.com>:

>
>
> On 04/03/2018 04:11 PM, Dmitry Safonov wrote:
> > 2018-04-03 10:34 GMT+01:00 Pavel Tikhomirov <ptikhomirov@virtuozzo.com>:
> > [..]
> >> +
> >> +#define CSEC_PER_SEC 100
> >> +
> >> +__maybe_unused int parse_uptime(u_int64_t *upt)
> >
> > Nit: can we use uint64_t?
> > I see u_int64_t the first time and git says we've it only in one other
> place.
>
> Oops, I brought it from encode_time - will use uint64_t instead, thanks!
>
> >
> >> +{
> >> +       unsigned long sec, csec;
> >> +       FILE *f;
> >> +
> >> +       f = fopen("/proc/uptime", "r");
> >> +       if (!f) {
> >> +               pr_perror("Failed to fopen /proc/uptime");
> >> +               return -1;
> >> +       }
> >> +
> >> +       if (fscanf(f, "%lu.%2lu", &sec, &csec) != 2) {
> >> +               pr_perror("Failed to parse /proc/uptime");
> >> +               fclose(f);
> >> +               return -1;
> >> +       }
> >> +
> >> +       *upt = sec * USEC_PER_SEC + csec * (USEC_PER_SEC /
> CSEC_PER_SEC);
> >
> > It's not clear from the commit message, why do you save time in usec,
> > rather than in csec?
>
> I thought it would be better to have usec in image, so when/if we have
> more precise uptime we can just reuse same variable in image and won't
> need adding another one.
>

I don't mind. As you'll resend anyway - could you mention
that in commit message, please? ;)

 Thanks,
              Dmitry
(answering from phone, sorry for html)
Pavel Tikhomirov April 3, 2018, 1:30 p.m.
On 04/03/2018 04:29 PM, Dmitry Safonov wrote:
> 
> 
> вт, 3 апр 2018 г., 14:24 Pavel Tikhomirov <ptikhomirov@virtuozzo.com 
> <mailto:ptikhomirov@virtuozzo.com>>:
> 
> 
> 
>     On 04/03/2018 04:11 PM, Dmitry Safonov wrote:
>      > 2018-04-03 10:34 GMT+01:00 Pavel Tikhomirov
>     <ptikhomirov@virtuozzo.com <mailto:ptikhomirov@virtuozzo.com>>:
>      > [..]
>      >> +
>      >> +#define CSEC_PER_SEC 100
>      >> +
>      >> +__maybe_unused int parse_uptime(u_int64_t *upt)
>      >
>      > Nit: can we use uint64_t?
>      > I see u_int64_t the first time and git says we've it only in one
>     other place.
> 
>     Oops, I brought it from encode_time - will use uint64_t instead, thanks!
> 
>      >
>      >> +{
>      >> +       unsigned long sec, csec;
>      >> +       FILE *f;
>      >> +
>      >> +       f = fopen("/proc/uptime", "r");
>      >> +       if (!f) {
>      >> +               pr_perror("Failed to fopen /proc/uptime");
>      >> +               return -1;
>      >> +       }
>      >> +
>      >> +       if (fscanf(f, "%lu.%2lu", &sec, &csec) != 2) {
>      >> +               pr_perror("Failed to parse /proc/uptime");
>      >> +               fclose(f);
>      >> +               return -1;
>      >> +       }
>      >> +
>      >> +       *upt = sec * USEC_PER_SEC + csec * (USEC_PER_SEC /
>     CSEC_PER_SEC);
>      >
>      > It's not clear from the commit message, why do you save time in usec,
>      > rather than in csec?
> 
>     I thought it would be better to have usec in image, so when/if we have
>     more precise uptime we can just reuse same variable in image and won't
>     need adding another one.
> 
> 
> I don't mind. As you'll resend anyway - could you mention
> that in commit message, please? ;)

Ok, sure!

> 
>   Thanks,
>                Dmitry
> (answering from phone, sorry for html)