diff mbox series

[ndctl,RFC-PATCH,3/4] papr_scm: Implement parsing and clean-up for fetched dimm stats

Message ID 20200518112023.147139-4-vaibhav@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series Add support for reporting PAPR NVDIMM Statistics | expand

Commit Message

Vaibhav Jain May 18, 2020, 11:20 a.m. UTC
Previous commit added functionality to fetch the dimm stats from
kernel 'papr_scm' module and store them into dimm private
struct. However these fetched values may contain stats which are
currently unknown to libndctl.

Hence this patch implements mechanism to parse and clean-up these
fetched dimm stats in a new function update_read_perf_stats(). Also a
new array of known stat-ids and their corresponding names and types is
introduced with a lookup function get_dimm_stat_desc().

Also since libndctl tends to deallocate the cmd->iter.total_buf on its
own when ndctl_cmd is freed, update_read_perf_stats() set it to NULL
to prevent this from happening.

Signed-off-by: Vaibhav Jain <vaibhav@linux.ibm.com>
---
 ndctl/lib/papr_scm.c | 95 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 94 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/ndctl/lib/papr_scm.c b/ndctl/lib/papr_scm.c
index 14fb6d48c12a..d27bacaa7d2c 100644
--- a/ndctl/lib/papr_scm.c
+++ b/ndctl/lib/papr_scm.c
@@ -44,6 +44,13 @@ 
 /* Number of bytes to transffer in each ioctl for pdsm READ_PERF_STATS */
 #define GET_PERF_STAT_XFER_SIZE 16
 
+/* Structure that holds single dimm stat retrived from kernel */
+struct dimm_stat {
+	u64 id;
+	const char *name;
+	int type;
+};
+
 /* Per dimm data. Holds per-dimm data parsed from the cmd_pkgs */
 struct dimm_priv {
 
@@ -56,6 +63,40 @@  struct dimm_priv {
 	struct nd_pdsm_perf_stat *perf_stats;
 };
 
+/* List of all known dimm_stat descriptors */
+static const struct dimm_stat dimm_stats[] = {
+	{ 0x74437365526c7443ULL, "Controller Reset Count", STAT_TYPE_INT },
+	{ 0x6D547365526C7443ULL, "Controller Reset Elapsed Time", STAT_TYPE_INT},
+	{ 0x20736365536e6f50ULL, "Power-on Seconds", STAT_TYPE_INT64 },
+	{ 0x206566694c6d654dULL, "Life Remaining", STAT_TYPE_PERCENT },
+	{ 0x5563735274697243ULL, "Critical Resource Utilization", STAT_TYPE_PERCENT },
+	{ 0x746e434c74736f48ULL, "Host Load Count", STAT_TYPE_INT64 },
+	{ 0x746e435374736f48ULL, "Host Store Count", STAT_TYPE_INT64 },
+	{ 0x7275444c74736f48ULL, "Host Load Duration", STAT_TYPE_INT64 },
+	{ 0x7275445374736f48ULL, "Host Store Duration", STAT_TYPE_INT64 },
+	{ 0x20746e435264654dULL, "Media Read Count", STAT_TYPE_INT64 },
+	{ 0x20746e435764654dULL, "Media Write Count", STAT_TYPE_INT64 },
+	{ 0x207275445264654dULL, "Media Read Duration", STAT_TYPE_INT64 },
+	{ 0x207275445764654dULL, "Media Write Duration", STAT_TYPE_INT64 },
+	{ 0x746e434852686343ULL, "Cache Read Hit Count", STAT_TYPE_INT64 },
+	{ 0x746e434857686343ULL, "Cache Write Hit Count", STAT_TYPE_INT64 },
+	{ 0x746e435774736146ULL, "Fast Write Count", STAT_TYPE_INT64 },
+};
+
+/* Given a stat-id find the corrosponding descriptor */
+static const struct dimm_stat *get_dimm_stat_desc(u64 id)
+{
+	unsigned index;
+	if (id) {
+		for (index = 0; index < ARRAY_SIZE(dimm_stats); ++index) {
+			if (dimm_stats[index].id == id)
+				return &dimm_stats[index];
+		}
+	}
+
+	return NULL;
+}
+
 static bool papr_cmd_is_supported(struct ndctl_dimm *dimm, int cmd)
 {
 	/* Handle this separately to support monitor mode */
@@ -184,6 +225,47 @@  static int update_perf_stat_size(struct ndctl_dimm *dimm, struct ndctl_cmd *cmd)
 	return 0;
 }
 
+/* Parse the pdsm READ_PERF_STATS command package */
+static int update_read_perf_stats(struct ndctl_dimm *dimm, struct ndctl_cmd *cmd)
+{
+	struct nd_pdsm_cmd_pkg *pcmd = nd_to_pdsm_cmd_pkg(cmd->pkg);
+	struct dimm_priv *p = dimm->dimm_user_data;
+	struct nd_pdsm_perf_stat *stat_src, *stat_dest;
+
+	/* Prevent libndctl from freeing pointer to cmd->iter.total_buf */
+	cmd->iter.total_buf = NULL;
+
+	if (pcmd->cmd_status) {
+		/* Indicate no stats are available */
+		p->count_perf_stats = 0;
+		return pcmd->cmd_status;
+	}
+
+	/* is it an unknown version ? */
+	if (pcmd->payload_version != 1) {
+		papr_err(dimm, "Unknown payload version for perf stats\n");
+		return -EBADE;
+	}
+
+	/* count number of stats available and remove any unknown stats */
+	for(stat_dest = stat_src = p->perf_stats;
+	    (void *)stat_src < (void *)p->perf_stats + p->len_perf_stats;
+	    stat_src++) {
+
+		if (stat_src != stat_dest)
+			*stat_dest = *stat_src;
+
+		/* Skip unknown dimm stats */
+		if (get_dimm_stat_desc(stat_dest->id) != NULL)
+			stat_dest++;
+	}
+
+	/* Update the stats count that we received */
+	p->count_perf_stats = (stat_dest - p->perf_stats);
+
+	return 0;
+}
+
 /* Parse a command payload and update dimm flags/private data */
 static int update_dimm_stats(struct ndctl_dimm *dimm, struct ndctl_cmd *cmd)
 {
@@ -213,6 +295,8 @@  static int update_dimm_stats(struct ndctl_dimm *dimm, struct ndctl_cmd *cmd)
 		return update_dimm_health(dimm, cmd);
 	case PAPR_SCM_PDSM_FETCH_PERF_STATS:
 		return update_perf_stat_size(dimm, cmd);
+	case PAPR_SCM_PDSM_READ_PERF_STATS:
+		return update_read_perf_stats(dimm, cmd);
 	default:
 		papr_err(dimm, "Unhandled pdsm-request 0x%016llx\n",
 			 pcmd_to_pdsm(pcmd));
@@ -301,11 +385,20 @@  static unsigned int papr_smart_get_shutdown_state(struct ndctl_cmd *cmd)
 
 static unsigned int papr_smart_get_flags(struct ndctl_cmd *cmd)
 {
+	struct nd_pdsm_cmd_pkg *pcmd = nd_to_pdsm_cmd_pkg(cmd->pkg);
+
 	/* In case of error return empty flags * */
 	if (update_dimm_stats(cmd->dimm, cmd))
 		return 0;
 
-	return ND_SMART_HEALTH_VALID | ND_SMART_SHUTDOWN_VALID;
+	switch (pcmd_to_pdsm(pcmd)) {
+	case PAPR_SCM_PDSM_HEALTH:
+		return ND_SMART_HEALTH_VALID | ND_SMART_SHUTDOWN_VALID;
+	case PAPR_SCM_PDSM_READ_PERF_STATS:
+		return ND_SMART_STATS_VALID;
+	default:
+		return 0;
+	}
 }
 
 static int papr_dimm_init(struct ndctl_dimm *dimm)