[Crash-utility] log -T option should not be support after system suspend

2020-05-07 Thread 赵乾利
Hello,list


I notice a new option about "log -T",which translates the leading timestamp 
value of each message into human readable format.​i have checked the code,the 
implementation mainly consists of two steps:

1.use "kt->boot_date.tv_sec=kt->date.tv_sec - uptime_sec" to get system bootup 
time,uptime_sec is come from jiffies

2.use "kt->boot_date.tv_sec + nanos" to translates readable timestamp in every 
log entry.


This change does not take into account the system sleep,once system suspend 
this translation will make error,printk timestamp and jiffies won't be update 
during suspend,and system suspend is a common feature,so i think change is a 
bug.


As far as i know​ we can’t get the correct conversion time once the system 
sleeps.so we have to restrict​ this option only work in non-suspend state.

patch is attached.

wrong log:​
[Thu May  7 03:27:38 2020] PM: suspend entry 2020-05-06 19:22:40.481371186 UTC
[Thu May  7 03:27:38 2020] PM: suspend exit 2020-05-06 19:22:49.168938838 UTC
...
[Thu May  7 03:29:02 2020] PM: suspend entry 2020-05-06 19:28:54.366349876 UTC
[Thu May  7 03:29:03 2020] PM: suspend exit 2020-05-06 19:29:03.162471747 UTC


timestamp=utc+8


Thanks

#/**本邮件及其附件含有小米公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
 This e-mail and its attachments contain confidential information from XIAOMI, 
which is intended only for the person or entity whose address is listed above. 
Any use of the information contained herein in any way (including, but not 
limited to, total or partial disclosure, reproduction, or dissemination) by 
persons other than the intended recipient(s) is prohibited. If you receive this 
e-mail in error, please notify the sender by phone or email immediately and 
delete it!**/#
From 3b5702fd00b31481635575b39f90cd440e389e77 Mon Sep 17 00:00:00 2001
From: zhaoqianli 
Date: Thu, 7 May 2020 19:58:30 +0800
Subject: [PATCH] bugfix for "log -T",this command only supports the case
 without suspend

Signed-off-by: zhaoqianli 
---
 defs.h   |  2 ++
 kernel.c | 40 +++-
 2 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/defs.h b/defs.h
index aba58ff..2464afc 100644
--- a/defs.h
+++ b/defs.h
@@ -2089,6 +2089,8 @@ struct offset_table {/* stash of commonly-used offsets */
 	long size_class_size;
 	long gendisk_private_data;
 	long zram_table_entry;
+	long timekeeper_offs_boot;
+	long timekeeper_sleep_time;
 };
 
 struct size_table { /* stash of commonly-used sizes */
diff --git a/kernel.c b/kernel.c
index 5ed6021..0042101 100644
--- a/kernel.c
+++ b/kernel.c
@@ -95,6 +95,7 @@ static ulong __dump_audit(char *);
 static void dump_audit(void);
 static char *vmcoreinfo_read_string(const char *);
 static void check_vmcoreinfo(void);
+static uint64_t get_sleep_time(void);
 
 
 /*
@@ -222,6 +223,8 @@ kernel_init()
 	
 	MEMBER_OFFSET_INIT(timekeeper_xtime, "timekeeper", "xtime");
 	MEMBER_OFFSET_INIT(timekeeper_xtime_sec, "timekeeper", "xtime_sec");
+	MEMBER_OFFSET_INIT(timekeeper_offs_boot, "timekeeper", "offs_boot");
+	MEMBER_OFFSET_INIT(timekeeper_sleep_time, "timekeeper", "total_sleep_time");
 	get_xtime(>date);
 	if (CRASHDEBUG(1))
 		fprintf(fp, "xtime timespec.tv_sec: %lx: %s\n", 
@@ -4941,11 +4944,16 @@ cmd_log(void)
 
 	if (kt->boot_date.tv_sec == 0) {
 		ulonglong uptime_jiffies;
-		ulong  uptime_sec;
-		get_uptime(NULL, _jiffies);
-		uptime_sec = (uptime_jiffies)/(ulonglong)machdep->hz;
-		kt->boot_date.tv_sec = kt->date.tv_sec - uptime_sec;
-		kt->boot_date.tv_nsec = 0;
+		ulong uptime_sec;
+		if (!get_sleep_time()) {
+			get_uptime(NULL, _jiffies);
+			uptime_sec = (uptime_jiffies)/(ulonglong)machdep->hz;
+			kt->boot_date.tv_sec = kt->date.tv_sec - uptime_sec;
+			kt->boot_date.tv_nsec = 0;
+		} else if (msg_flags & SHOW_LOG_CTIME) {
+			option_not_supported('T');
+			return;
+		}
 	}
 
 	if (msg_flags & SHOW_LOG_AUDIT) {
@@ -11476,3 +11484,25 @@ check_vmcoreinfo(void)
 		}
 	}
 }
+
+static uint64_t
+get_sleep_time(void)
+{
+	struct syment *sp;
+	struct timespec sleep_time;
+	uint64_t sleep_nsec;
+	if (VALID_MEMBER(timekeeper_offs_boot) &&
+		(sp = kernel_symbol_search("shadow_timekeeper"))) {
+			readmem(sp->value + OFFSET(timekeeper_offs_boot), KVADDR,
+_nsec, sizeof(ulong), "shadow_timekeeper offs_boot", RETURN_ON_ERROR);
+	} else if (VALID_MEMBER(timekeeper_sleep_time) &&
+		(sp = kernel_symbol_search("timekeeper"))) {
+			readmem(sp->value + OFFSET(timekeeper_sleep_time), KVADDR,
+_time, sizeof(struct timespec), "timekeeper total_sleep_time", RETURN_ON_ERROR);
+			sleep_nsec = (10ULL*sleep_time.tv_sec) + sleep_time.tv_nsec;
+	} else if (kernel_symbol_exists("total_sleep_time")) {
+			get_symbol_data("total_sleep_time", sizeof(struct timespec), _time);
+			sleep_nsec = (10ULL*sleep_time.tv_sec) + sleep_time.tv_nsec;
+	}
+	return sleep_nsec;
+}
\ No newline at end of file
-- 
2.7.4

--
Crash-utility mailing 

Re: [Crash-utility] log -T option should not be support after system suspend

2020-05-07 Thread Dave Anderson


- Original Message -
> 赵乾利 wrote on Thu, May 07, 2020:
> > This change does not take into account the system sleep,once system
> > suspend this translation will make error,printk timestamp and jiffies
> > won't be update during suspend,and system suspend is a common
> > feature,so i think change is a bug.
> 
> This is how the regular unix command `dmesg -T` works, so I think it's
> worth having as is: timestamp will be mostly correct until the first
> sleep and then off by sleep amount.
> 
> This option isn't reliable anyway (drift depends on system but it's not
> unusual to be off by a few minutes on most systems with more than a week
> of uptime -- it drifts faster when cpu clock varies often), and it's not
> like there's any harm in this.. At most print a warning that times after
> sleep are wrong if you want to.
> 
> This is obviously just my opinion but I think for tools like crash, if a
> user wants to shoot themselves in the foot, we should let them to... I'm
> always annoyed when system tools know better and I need to waste time
> patching them to bypass checks...

I agree completely with Dominique.  Your patch displays "log: -T option not 
supported"
and bails out, which is arguably more misleading than just showing the 
timestamps.

The dmesg man page shows this:

   -T, --ctime
  Print human readable timestamps.  The timestamp could be 
inaccurate!

  The time source used for the logs is not updated after system 
SUSPEND/RESUME.

This kind of warning could be added to the log command's help page, or could
be displayed as a preface to the dump of the log.  Or things could be just left
as they are.

> 
> PS. Didn't want to send a mail "just" for it but thank you for all these
> years Dave, hope you keep in touch a bit when you feel bored :)
> And congratulation? to new maintainers!
> --
> Dominique

Thanks Dominique, I will still be watching the list from my personal email.

Dave


--
Crash-utility mailing list
Crash-utility@redhat.com
https://www.redhat.com/mailman/listinfo/crash-utility

Re: [Crash-utility] log -T option should not be support after system suspend

2020-05-07 Thread Dominique Martinet
赵乾利 wrote on Thu, May 07, 2020:
> This change does not take into account the system sleep,once system
> suspend this translation will make error,printk timestamp and jiffies
> won't be update during suspend,and system suspend is a common
> feature,so i think change is a bug.

This is how the regular unix command `dmesg -T` works, so I think it's
worth having as is: timestamp will be mostly correct until the first
sleep and then off by sleep amount.

This option isn't reliable anyway (drift depends on system but it's not
unusual to be off by a few minutes on most systems with more than a week
of uptime -- it drifts faster when cpu clock varies often), and it's not
like there's any harm in this.. At most print a warning that times after
sleep are wrong if you want to.


This is obviously just my opinion but I think for tools like crash, if a
user wants to shoot themselves in the foot, we should let them to... I'm
always annoyed when system tools know better and I need to waste time
patching them to bypass checks...


PS. Didn't want to send a mail "just" for it but thank you for all these
years Dave, hope you keep in touch a bit when you feel bored :)
And congratulation? to new maintainers!
-- 
Dominique


--
Crash-utility mailing list
Crash-utility@redhat.com
https://www.redhat.com/mailman/listinfo/crash-utility