[Concept,04/11] bootstage: Add a way to read the time from a record

Message ID 20251023094308.3406453-5-sjg@u-boot.org
State New
Headers
Series Bootstage and script enhancements |

Commit Message

Simon Glass Oct. 23, 2025, 9:42 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

Add a function which returns the time given a record ID.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/bootstage.c      | 14 ++++++++++++++
 include/bootstage.h     | 13 +++++++++++++
 test/common/bootstage.c |  2 ++
 3 files changed, 29 insertions(+)
  

Patch

diff --git a/common/bootstage.c b/common/bootstage.c
index e0298991fa9..567b084668b 100644
--- a/common/bootstage.c
+++ b/common/bootstage.c
@@ -249,6 +249,20 @@  void bootstage_set_rec_count(uint count)
 	data->rec_count = count;
 }
 
+ulong bootstage_get_time(enum bootstage_id id)
+{
+	struct bootstage_data *data = gd->bootstage;
+	struct bootstage_record *rec;
+
+	if (!data)
+		return 0;
+	rec = find_id(data, id);
+	if (!rec)
+		return 0;
+
+	return rec->time_us;
+}
+
 /**
  * Get a record name as a printable string
  *
diff --git a/include/bootstage.h b/include/bootstage.h
index adc7b8f0a35..a0f687a9a4b 100644
--- a/include/bootstage.h
+++ b/include/bootstage.h
@@ -378,6 +378,14 @@  const struct bootstage_record *bootstage_get_rec(uint index);
  */
 void bootstage_set_rec_count(uint count);
 
+/*
+ * bootstage_get_time() - Get the timestamp for a bootstage ID
+ *
+ * @id: Bootstage id to look up
+ * Return: timestamp in us for that stage, or 0 if not found
+ */
+ulong bootstage_get_time(enum bootstage_id id);
+
 /* Print a report about boot time */
 void bootstage_report(void);
 
@@ -478,6 +486,11 @@  static inline uint32_t bootstage_accum(enum bootstage_id id)
 	return 0;
 }
 
+static inline ulong bootstage_get_time(enum bootstage_id id)
+{
+	return 0;
+}
+
 static inline void bootstage_report(void)
 {
 }
diff --git a/test/common/bootstage.c b/test/common/bootstage.c
index 6e97c3e3a72..d8f1d1d32f9 100644
--- a/test/common/bootstage.c
+++ b/test/common/bootstage.c
@@ -35,6 +35,7 @@  static int test_bootstage_mark(struct unit_test_state *uts)
 	ut_asserteq_str("test_stage_mark", rec->name);
 	ut_asserteq(time, rec->time_us);
 	ut_asserteq(0, rec->flags);
+	ut_asserteq(time, bootstage_get_time(BOOTSTAGE_ID_USER + 50));
 
 	/* Restore the original count */
 	bootstage_set_rec_count(count);
@@ -110,6 +111,7 @@  static int test_bootstage_accum(struct unit_test_state *uts)
 	/* Check the total time accumulated */
 	rec = bootstage_get_rec(index);
 	ut_asserteq(rec->time_us, elapsed1 + elapsed2);
+	ut_asserteq(rec->time_us, bootstage_get_time(id));
 
 	/* Restore the original count */
 	bootstage_set_rec_count(count);