Commit b610f28a authored by Pavel Tikhomirov's avatar Pavel Tikhomirov Committed by Pavel Emelyanov

parse: add a helper to obtain an uptime

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

note: now uptime is in csec but I prefer saving it in usec, that allows
us to be reuse these image field when/if we have more accurate value.

v8: add length specifier to parse only centiseconds
v9: put uptime to u_int64_t directly, define CSEC_PER_SEC
v10: switch to uint64_t from u_int64_t, comment about usec in image
Signed-off-by: 's avatarPavel Tikhomirov <ptikhomirov@virtuozzo.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 57ab74c2
......@@ -107,5 +107,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(uint64_t *upt);
#endif /* __CR_PROC_PARSE_H__ */
......@@ -2629,3 +2629,28 @@ err:
xfree(ch);
return -1;
}
#define CSEC_PER_SEC 100
__maybe_unused int parse_uptime(uint64_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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment