From patchwork Tue Nov 8 18:29:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 13036696 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E9139C4332F for ; Tue, 8 Nov 2022 18:29:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229882AbiKHS36 (ORCPT ); Tue, 8 Nov 2022 13:29:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229861AbiKHS34 (ORCPT ); Tue, 8 Nov 2022 13:29:56 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18F7C15735 for ; Tue, 8 Nov 2022 10:29:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667932195; x=1699468195; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=C5ktDag9K9FWp0zPwFOuS+0FNry24XJ9fIX8/PhOyvA=; b=Ak10IpDJV/HJhAXbMFCLxVx5O2D2X3T4bCorSLojLIJvtKSNJvpTU2dC k/bVBEG6lDRkFu0a2NPrflekyp8sovJpv4WiJ8QNORksnaN/WwhKnUmiB YQe0W7agoBkXZUZstKDJ7igclvJGPIlW+WGqEBKJoaMVMUBw+iCkfZGX7 d/fITJRgEBry24ciMkaYw3aXzK33ZumbosNb0dbVcjv26m2Gmvv3FMcVl zSeCEHxlJoDfvLaTPqyIzLATqlrFMmuLJPKtzVtEUnTXbs5r5E6+0icZo Gl7EjA1gBu+INq47l3lreo6AeHwSGGMOJEXMoCMoHkUFSuSr9Q6SBzkRQ Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="290491125" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="290491125" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:29:54 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="669650001" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="669650001" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:29:54 -0800 Subject: [PATCH v4 01/10] cxl: add helper function to parse trace event to json object From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: dan.j.williams@intel.com, ira.weiny@intel.com, vishal.l.verma@intel.com, alison.schofield@intel.com, rostedt@goodmis.org Date: Tue, 08 Nov 2022 11:29:53 -0700 Message-ID: <166793219376.3768752.15370223291855764073.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> References: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org Add the helper function that parses a trace event captured by libtraceevent in a tep handle. All the parsed fields are added to a json object. The json object is added to the provided list in the input parameter. Tested-by: Alison Schofield Signed-off-by: Dave Jiang --- cxl/event_trace.c | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++ cxl/event_trace.h | 14 ++++ cxl/meson.build | 2 + meson.build | 1 4 files changed, 218 insertions(+) create mode 100644 cxl/event_trace.c create mode 100644 cxl/event_trace.h diff --git a/cxl/event_trace.c b/cxl/event_trace.c new file mode 100644 index 000000000000..1b1b037e48bf --- /dev/null +++ b/cxl/event_trace.c @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2022, Intel Corp. All rights reserved. +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "json.h" +#include "event_trace.h" + +#define _GNU_SOURCE +#include + +static struct json_object *num_to_json(void *num, int elem_size, unsigned long flags) +{ + bool sign = flags & TEP_FIELD_IS_SIGNED; + int64_t val = 0; + + /* special case 64 bit as the call depends on sign */ + if (elem_size == 8) { + if (sign) + return json_object_new_int64(*(int64_t *)num); + else + return json_object_new_uint64(*(uint64_t *)num); + } + + /* All others fit in a signed 64 bit */ + switch (elem_size) { + case 1: + if (sign) + val = *(int8_t *)num; + else + val = *(uint8_t *)num; + break; + case 2: + if (sign) + val = *(int16_t *)num; + else + val = *(uint16_t *)num; + break; + case 4: + if (sign) + val = *(int32_t *)num; + else + val = *(uint32_t *)num; + break; + default: + /* + * Odd sizes are converted in the kernel to one of the above. + * It is an error to see them here. + */ + return NULL; + } + + return json_object_new_int64(val); +} + +static int cxl_event_to_json_callback(struct tep_event *event, + struct tep_record *record, struct list_head *jlist_head) +{ + struct tep_format_field **fields; + struct json_object *jevent, *jobj, *jarray; + struct jlist_node *jnode; + int i, j, rc = 0; + + jnode = malloc(sizeof(*jnode)); + if (!jnode) + return -ENOMEM; + + jevent = json_object_new_object(); + if (!jevent) { + rc = -ENOMEM; + goto err_jevent; + } + jnode->jobj = jevent; + + fields = tep_event_fields(event); + if (!fields) { + rc = -ENOENT; + goto err; + } + + jobj = json_object_new_string(event->system); + if (!jobj) { + rc = -ENOMEM; + goto err; + } + json_object_object_add(jevent, "system", jobj); + + jobj = json_object_new_string(event->name); + if (!jobj) { + rc = -ENOMEM; + goto err; + } + json_object_object_add(jevent, "event", jobj); + + jobj = json_object_new_uint64(record->ts); + if (!jobj) { + rc = -ENOMEM; + goto err; + } + json_object_object_add(jevent, "timestamp", jobj); + + for (i = 0; fields[i]; i++) { + struct tep_format_field *f = fields[i]; + int len; + + if (f->flags & TEP_FIELD_IS_STRING) { + char *str; + + str = tep_get_field_raw(NULL, event, f->name, record, &len, 0); + if (!str) + continue; + + jobj = json_object_new_string(str); + if (!jobj) { + rc = -ENOMEM; + goto err; + } + + json_object_object_add(jevent, f->name, jobj); + } else if (f->flags & TEP_FIELD_IS_ARRAY) { + unsigned char *data; + int chunks; + + data = tep_get_field_raw(NULL, event, f->name, record, &len, 0); + if (!data) + continue; + + jarray = json_object_new_array(); + if (!jarray) { + rc = -ENOMEM; + goto err; + } + + chunks = f->size / f->elementsize; + for (j = 0; j < chunks; j++) { + jobj = num_to_json(data, f->elementsize, f->flags); + if (!jobj) { + json_object_put(jarray); + return -ENOMEM; + } + json_object_array_add(jarray, jobj); + data += f->elementsize; + } + + json_object_object_add(jevent, f->name, jarray); + } else { /* single number */ + unsigned char *data; + char *tmp; + + data = tep_get_field_raw(NULL, event, f->name, record, &len, 0); + if (!data) + continue; + + /* check to see if we have a UUID */ + tmp = strcasestr(f->type, "uuid_t"); + if (tmp) { + char uuid[SYSFS_ATTR_SIZE]; + + uuid_unparse(data, uuid); + jobj = json_object_new_string(uuid); + if (!jobj) { + rc = -ENOMEM; + goto err; + } + + json_object_object_add(jevent, f->name, jobj); + continue; + } + + jobj = num_to_json(data, f->elementsize, f->flags); + if (!jobj) { + rc = -ENOMEM; + goto err; + } + + json_object_object_add(jevent, f->name, jobj); + } + } + + list_add_tail(jlist_head, &jnode->list); + return 0; + +err: + json_object_put(jevent); +err_jevent: + free(jnode); + return rc; +} diff --git a/cxl/event_trace.h b/cxl/event_trace.h new file mode 100644 index 000000000000..00975a0b5680 --- /dev/null +++ b/cxl/event_trace.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2022 Intel Corporation. All rights reserved. */ +#ifndef __CXL_EVENT_TRACE_H__ +#define __CXL_EVENT_TRACE_H__ + +#include +#include + +struct jlist_node { + struct json_object *jobj; + struct list_node list; +}; + +#endif diff --git a/cxl/meson.build b/cxl/meson.build index f2474aaa6e2e..8c7733431613 100644 --- a/cxl/meson.build +++ b/cxl/meson.build @@ -7,6 +7,7 @@ cxl_src = [ 'memdev.c', 'json.c', 'filter.c', + 'event_trace.c', ] cxl_tool = executable('cxl', @@ -19,6 +20,7 @@ cxl_tool = executable('cxl', kmod, json, versiondep, + traceevent, ], install : true, install_dir : rootbindir, diff --git a/meson.build b/meson.build index 20a646d135c7..f611e0bdd7f3 100644 --- a/meson.build +++ b/meson.build @@ -142,6 +142,7 @@ kmod = dependency('libkmod') libudev = dependency('libudev') uuid = dependency('uuid') json = dependency('json-c') +traceevent = dependency('libtraceevent') if get_option('docs').enabled() if get_option('asciidoctor').enabled() asciidoc = find_program('asciidoctor', required : true) From patchwork Tue Nov 8 18:29:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 13036705 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 11BFCC4332F for ; Tue, 8 Nov 2022 18:30:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230340AbiKHSaw (ORCPT ); Tue, 8 Nov 2022 13:30:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230001AbiKHSav (ORCPT ); Tue, 8 Nov 2022 13:30:51 -0500 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3391654B36 for ; Tue, 8 Nov 2022 10:30:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667932251; x=1699468251; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IUDO3qN6CnzVve4e8IjJOpZdaKU3qswJTV/Ln6jCQfA=; b=T/eobY7ooyvFJhWOGa68ufxAbB1Gn2BdNMWwKRPgWq5AuOX5KNg/oxoI Mqlik9NaTUoL91T7ODgs1Wb+PjsW0QuMTpa2aNJdmsNQmuFJepup5rBU9 ONPNCYSHbSZBAAu1moeMEMXK+KDC5OiDBOtDakKE5Xh3mobU4MF+/aAcV OuC1LXl7M0Sh7FMMz3KuZ2sdiiVWquYDTolijyL7uHXGgGrfZEHt9RKKB Tcq5Bu1TLT0CwkyCTSRAdeldnb4z8qFibcV3bqMK4j1a3ZVx8/YevCaTo Z79D3LsEnXqzlM3oO8vQX0sbe7lLutsurqqy9nuS6DCEgaq5FUDb5cWqB A==; X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="372916011" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="372916011" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:00 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="669650018" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="669650018" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:29:59 -0800 Subject: [PATCH v4 02/10] cxl: add helper to parse through all current events From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: dan.j.williams@intel.com, ira.weiny@intel.com, vishal.l.verma@intel.com, alison.schofield@intel.com, rostedt@goodmis.org Date: Tue, 08 Nov 2022 11:29:59 -0700 Message-ID: <166793219956.3768752.10060411389279195687.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> References: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org Add common function to iterate through and extract the events in the current trace buffer. The function uses tracefs_iterate_raw_events() from libtracefs to go through all the events loaded into a tep_handle. A callback is provided to the API call in order to parse the event. For cxl monitor, an array of interested "systems" is provided in order to filter for the interested events. Tested-by: Alison Schofield Signed-off-by: Dave Jiang --- cxl/event_trace.c | 36 ++++++++++++++++++++++++++++++++++++ cxl/event_trace.h | 10 ++++++++++ cxl/meson.build | 1 + meson.build | 2 ++ 4 files changed, 49 insertions(+) diff --git a/cxl/event_trace.c b/cxl/event_trace.c index 1b1b037e48bf..4667044e4a66 100644 --- a/cxl/event_trace.c +++ b/cxl/event_trace.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "json.h" #include "event_trace.h" @@ -199,3 +200,38 @@ err_jevent: free(jnode); return rc; } + +static int cxl_event_parse_cb(struct tep_event *event, struct tep_record *record, + int cpu, void *ctx) +{ + struct event_ctx *event_ctx = (struct event_ctx *)ctx; + + /* Filter out all the events that the caller isn't interested in. */ + if (strcmp(event->system, event_ctx->system) != 0) + return 0; + + if (event_ctx->event_name) { + if (strcmp(event->name, event_ctx->event_name) != 0) + return 0; + } + + if (event_ctx->parse_event) + return event_ctx->parse_event(event, record, &event_ctx->jlist_head); + + return cxl_event_to_json_callback(event, record, &event_ctx->jlist_head); +} + +int cxl_parse_events(struct tracefs_instance *inst, struct event_ctx *ectx) +{ + struct tep_handle *tep; + int rc; + + tep = tracefs_local_events(NULL); + if (!tep) + return -ENOMEM; + + rc = tracefs_iterate_raw_events(tep, inst, NULL, 0, + cxl_event_parse_cb, ectx); + tep_free(tep); + return rc; +} diff --git a/cxl/event_trace.h b/cxl/event_trace.h index 00975a0b5680..582882c1eb35 100644 --- a/cxl/event_trace.h +++ b/cxl/event_trace.h @@ -11,4 +11,14 @@ struct jlist_node { struct list_node list; }; +struct event_ctx { + const char *system; + struct list_head jlist_head; + const char *event_name; /* optional */ + int (*parse_event)(struct tep_event *event, struct tep_record *record, + struct list_head *jlist_head); /* optional */ +}; + +int cxl_parse_events(struct tracefs_instance *inst, struct event_ctx *ectx); + #endif diff --git a/cxl/meson.build b/cxl/meson.build index 8c7733431613..c59876262e76 100644 --- a/cxl/meson.build +++ b/cxl/meson.build @@ -21,6 +21,7 @@ cxl_tool = executable('cxl', json, versiondep, traceevent, + tracefs, ], install : true, install_dir : rootbindir, diff --git a/meson.build b/meson.build index f611e0bdd7f3..c204c8ac52de 100644 --- a/meson.build +++ b/meson.build @@ -143,6 +143,8 @@ libudev = dependency('libudev') uuid = dependency('uuid') json = dependency('json-c') traceevent = dependency('libtraceevent') +tracefs = dependency('libtracefs') + if get_option('docs').enabled() if get_option('asciidoctor').enabled() asciidoc = find_program('asciidoctor', required : true) From patchwork Tue Nov 8 18:30:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 13036697 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEAC0C433FE for ; Tue, 8 Nov 2022 18:30:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229861AbiKHSaI (ORCPT ); Tue, 8 Nov 2022 13:30:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230079AbiKHSaH (ORCPT ); Tue, 8 Nov 2022 13:30:07 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95F7C26CF for ; Tue, 8 Nov 2022 10:30:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667932206; x=1699468206; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IKg83VT9VshQ06FCtL9b/WzizLNhG1qurjA4Zy0CMBM=; b=JuK+mppiWk5u9X8ErcPZepkOC0YDlMJdP+mEsFQIQMT7NLBPLoH5a3AF QHKtQz+R/+Igry5pqOn/B55J7K2hIvWEBDKqY+MYKa5IVg5u+/Y4YgOSp J4M+ohoi48KlGddqptJ2SBEIKxyIWWFGhxcN5any8x2UVtEwHY7UqQUHV Xayagon1UHIYRnrMkI5AxuoZVkmekTMLb5VScJyuSLbmrDCPbC4jo/xXk UiUhaCZSKClOGoHi0s/PdUusL4iPXb8HpMFMb7P2S102jUXki0PtyUjqB jgI0V8pz3muH0gDlrRwuwR2q64ocYIJ0ttDx2R22f5fRINLMbIWJSbKT1 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="290491175" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="290491175" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:06 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="669650060" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="669650060" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:05 -0800 Subject: [PATCH v4 03/10] cxl: add common function to enable event trace From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: dan.j.williams@intel.com, ira.weiny@intel.com, vishal.l.verma@intel.com, alison.schofield@intel.com, rostedt@goodmis.org Date: Tue, 08 Nov 2022 11:30:05 -0700 Message-ID: <166793220535.3768752.9190750068767610122.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> References: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org Add a common function for cxl command to enable event tracing for the instance created. The interested "systems" will be enabled for tracing as well. Signed-off-by: Dave Jiang --- cxl/event_trace.c | 16 ++++++++++++++++ cxl/event_trace.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/cxl/event_trace.c b/cxl/event_trace.c index 4667044e4a66..95b0ce071166 100644 --- a/cxl/event_trace.c +++ b/cxl/event_trace.c @@ -235,3 +235,19 @@ int cxl_parse_events(struct tracefs_instance *inst, struct event_ctx *ectx) tep_free(tep); return rc; } + +int cxl_event_tracing_enable(struct tracefs_instance *inst, const char *system, + const char *event) +{ + int rc; + + rc = tracefs_event_enable(inst, system, event); + if (rc == -1) + return -errno; + + if (tracefs_trace_is_on(inst)) + return 0; + + tracefs_trace_on(inst); + return 0; +} diff --git a/cxl/event_trace.h b/cxl/event_trace.h index 582882c1eb35..aecf9aa96187 100644 --- a/cxl/event_trace.h +++ b/cxl/event_trace.h @@ -20,5 +20,7 @@ struct event_ctx { }; int cxl_parse_events(struct tracefs_instance *inst, struct event_ctx *ectx); +int cxl_event_tracing_enable(struct tracefs_instance *inst, const char *system, + const char *event); #endif From patchwork Tue Nov 8 18:30:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 13036698 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66C8EC4332F for ; Tue, 8 Nov 2022 18:30:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230107AbiKHSaO (ORCPT ); Tue, 8 Nov 2022 13:30:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230079AbiKHSaN (ORCPT ); Tue, 8 Nov 2022 13:30:13 -0500 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51B3B26CF for ; Tue, 8 Nov 2022 10:30:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667932212; x=1699468212; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EzEGlRQ6ptosn4px7u15Y1lZqnWs1uJ4iJuKGMoeyPY=; b=IghMAkiS46UrOaG27tYeuQsQyCgl4rzcTVv4A0V4yNK9xdF09j6OvW1R lheK/EuA/6BqPbRt+PJU45UEye4UQc9mzzfe7P1tj80jc+EBMA4HdGyq5 12AzFLj6ZkqFH6XdpRv5BtFVtFPEpkDbDqtktJRQhhL/mtqX1R4A3G74j huJt74jk2/tZyio3b7mhciXfJcDFUnuTMn2/UON+dRrJTFpqgpW65J6+K SSj9QbDAMivZ/WIbmTXsRrzD4LWveGgbXoCjKmLKKiqV0WUuCUdkZAX6i bwxo1gShOQRQME3kUca8bAEXGflfzQKZEaPaHpBQc5dbJsbqOJLfmu331 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="309480428" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="309480428" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:12 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="669650095" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="669650095" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:11 -0800 Subject: [PATCH v4 04/10] cxl: add common function to disable event trace From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: dan.j.williams@intel.com, ira.weiny@intel.com, vishal.l.verma@intel.com, alison.schofield@intel.com, rostedt@goodmis.org Date: Tue, 08 Nov 2022 11:30:11 -0700 Message-ID: <166793221113.3768752.2422093575535918509.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> References: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org Add a common function for cxl command that disables the event trace for the instance created. Signed-off-by: Dave Jiang --- cxl/event_trace.c | 5 +++++ cxl/event_trace.h | 1 + 2 files changed, 6 insertions(+) diff --git a/cxl/event_trace.c b/cxl/event_trace.c index 95b0ce071166..490c30e1dbfc 100644 --- a/cxl/event_trace.c +++ b/cxl/event_trace.c @@ -251,3 +251,8 @@ int cxl_event_tracing_enable(struct tracefs_instance *inst, const char *system, tracefs_trace_on(inst); return 0; } + +int cxl_event_tracing_disable(struct tracefs_instance *inst) +{ + return tracefs_trace_off(inst); +} diff --git a/cxl/event_trace.h b/cxl/event_trace.h index aecf9aa96187..89e98cafb320 100644 --- a/cxl/event_trace.h +++ b/cxl/event_trace.h @@ -22,5 +22,6 @@ struct event_ctx { int cxl_parse_events(struct tracefs_instance *inst, struct event_ctx *ectx); int cxl_event_tracing_enable(struct tracefs_instance *inst, const char *system, const char *event); +int cxl_event_tracing_disable(struct tracefs_instance *inst); #endif From patchwork Tue Nov 8 18:30:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 13036699 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 806ACC433FE for ; Tue, 8 Nov 2022 18:30:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230109AbiKHSaU (ORCPT ); Tue, 8 Nov 2022 13:30:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230079AbiKHSaT (ORCPT ); Tue, 8 Nov 2022 13:30:19 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9436526CF for ; Tue, 8 Nov 2022 10:30:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667932218; x=1699468218; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DhucDPoPZco3IJng6uR4kcYHNC1k1NwMtPRheRVs150=; b=WoKCSUcb9UX5Q8wLMLI4MquAeJIh3Y2z+joWuv3zDPnvyB1lhyk+DDMv qVtO58fdhlBnc4E/H0UzW1CGuirHC1q7qlDwuJrIj3MKQxO1P8duaY0mM WOpIuXDrxDEy4Zgq0EPzC9NLFd8jlXzYqsb75XmLvctn8tyLNroKJMMNW 2J4kVc16BHq6SxK/7UfGZy2Nbx9gZ9tExubitilkrv3njEZ/0/uHNHm+i Q3FxkAnoDy6058nR55c/ict/WxNyP2K+dJFJNiaLn8J9Vq8ABJ/AvqDVd g/Xw2Sc3n5YmNoj2E0eCF/ejqpTcxq/KoPiSuDuciM6/oKnM2HL8WemFU A==; X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="375046161" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="375046161" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:18 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="669650153" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="669650153" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:17 -0800 Subject: [PATCH v4 05/10] cxl: add monitor function for event trace events From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: dan.j.williams@intel.com, ira.weiny@intel.com, vishal.l.verma@intel.com, alison.schofield@intel.com, rostedt@goodmis.org Date: Tue, 08 Nov 2022 11:30:16 -0700 Message-ID: <166793221692.3768752.18351228388707416243.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> References: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org Add function that creates an event trace instance and utilize the cxl event trace common functions to extract interested events from the trace buffer. The monitoring function will pend on an epoll fd and wait for new events to appear in the trace buffer. Signed-off-by: Dave Jiang --- cxl/meson.build | 1 cxl/monitor.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 cxl/monitor.c diff --git a/cxl/meson.build b/cxl/meson.build index c59876262e76..eb8b2b1070ed 100644 --- a/cxl/meson.build +++ b/cxl/meson.build @@ -8,6 +8,7 @@ cxl_src = [ 'json.c', 'filter.c', 'event_trace.c', + 'monitor.c', ] cxl_tool = executable('cxl', diff --git a/cxl/monitor.c b/cxl/monitor.c new file mode 100644 index 000000000000..c3f7e3639ec0 --- /dev/null +++ b/cxl/monitor.c @@ -0,0 +1,140 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2022, Intel Corp. All rights reserved. +/* Some bits copied from ndctl monitor code */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* reuse the core log helpers for the monitor logger */ +#ifndef ENABLE_LOGGING +#define ENABLE_LOGGING +#endif +#ifndef ENABLE_DEBUG +#define ENABLE_DEBUG +#endif +#include + +#include "event_trace.h" + +static const char *cxl_system = "cxl"; + +static struct monitor { + struct log_ctx ctx; + FILE *log_file; + bool human; +} monitor; + +static int monitor_event(struct cxl_ctx *ctx) +{ + int fd, epollfd, rc = 0, timeout = -1; + struct epoll_event ev, *events; + struct tracefs_instance *inst; + struct event_ctx ectx; + int jflag; + + events = calloc(1, sizeof(struct epoll_event)); + if (!events) { + err(&monitor, "alloc for events error\n"); + return -ENOMEM; + } + + epollfd = epoll_create1(0); + if (epollfd == -1) { + rc = -errno; + err(&monitor, "epoll_create1() error: %d\n", rc); + goto epoll_err; + } + + inst = tracefs_instance_create("cxl_monitor"); + if (!inst) { + rc = -errno; + err(&monitor, "tracefs_instance_create( failed: %d\n", rc); + goto inst_err; + } + + fd = tracefs_instance_file_open(inst, "trace_pipe", -1); + if (fd < 0) { + rc = fd; + err(&monitor, "tracefs_instance_file_open() err: %d\n", rc); + goto inst_file_err; + } + + memset(&ev, 0, sizeof(ev)); + ev.events = EPOLLIN; + ev.data.fd = fd; + + if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) != 0) { + rc = -errno; + err(&monitor, "epoll_ctl() error: %d\n", rc); + goto epoll_ctl_err; + } + + rc = cxl_event_tracing_enable(inst, cxl_system, NULL); + if (rc < 0) { + err(&monitor, "cxl_trace_event_enable() failed: %d\n", rc); + goto event_en_err; + } + + memset(&ectx, 0, sizeof(ectx)); + ectx.system = cxl_system; + if (monitor.human) + jflag = JSON_C_TO_STRING_PRETTY; + else + jflag = JSON_C_TO_STRING_PLAIN; + + while (1) { + struct jlist_node *jnode, *next; + + rc = epoll_wait(epollfd, events, 1, timeout); + if (rc < 0) { + rc = -errno; + if (errno != EINTR) + err(&monitor, "epoll_wait error: %d\n", -errno); + break; + } + + list_head_init(&ectx.jlist_head); + rc = cxl_parse_events(inst, &ectx); + if (rc < 0) + goto parse_err; + + if (list_empty(&ectx.jlist_head)) + continue; + + list_for_each_safe(&ectx.jlist_head, jnode, next, list) { + notice(&monitor, "%s\n", + json_object_to_json_string_ext(jnode->jobj, jflag)); + list_del(&jnode->list); + json_object_put(jnode->jobj); + free(jnode); + } + } + +parse_err: + rc = cxl_event_tracing_disable(inst); +event_en_err: +epoll_ctl_err: + close(fd); +inst_file_err: + tracefs_instance_free(inst); +inst_err: + close(epollfd); +epoll_err: + free(events); + return rc; +} From patchwork Tue Nov 8 18:30:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 13036700 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3B2BBC43217 for ; Tue, 8 Nov 2022 18:30:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230112AbiKHSaZ (ORCPT ); Tue, 8 Nov 2022 13:30:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230142AbiKHSaY (ORCPT ); Tue, 8 Nov 2022 13:30:24 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D811F117D for ; Tue, 8 Nov 2022 10:30:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667932223; x=1699468223; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=T+RwdEaG7CAzsk3DbJ08hToav07BCFc/S567d0iVkG4=; b=gAB4SNG60AZApiiOZtu1zmKxldolnpOE9oF8Y1dTkqa8oxMdNbmfZ0At IBfz8qfCHRIVw0NFkssgK6wd6geUdS6NXk981aV2xAVbwgeD351IX5Zmh RkNhSvW272jDWi8QyI95iHDCfhQg5ORsazNhDQiWEBIQvwedExxbUmyHM r6sI9ZsOOr2/+/OdSfcIqZtbpym8TP/rzam4faSQVEiLUN60GcvhenCgG osW10y7ZJ1YmqNNEM/7lTFFpXV8Zdi9Y9pQBl7ZeiiD4OF7k78x4SsQtG /oDmZy3/1Zuyr+Efwd9zIMkF2SoU0Jfy1WLWNVkh0BPq63UXpnZB3i4xe A==; X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="375046171" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="375046171" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:23 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="705400091" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="705400091" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:23 -0800 Subject: [PATCH v4 06/10] cxl: add logging functions for monitor From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: dan.j.williams@intel.com, ira.weiny@intel.com, vishal.l.verma@intel.com, alison.schofield@intel.com, rostedt@goodmis.org Date: Tue, 08 Nov 2022 11:30:22 -0700 Message-ID: <166793222281.3768752.3333516400569686550.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> References: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org Duplicate log functions from ndctl/monitor to use for stdout and file logging. Signed-off-by: Dave Jiang --- cxl/monitor.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cxl/monitor.c b/cxl/monitor.c index c3f7e3639ec0..518d8142e40d 100644 --- a/cxl/monitor.c +++ b/cxl/monitor.c @@ -39,6 +39,31 @@ static struct monitor { bool human; } monitor; +static void log_standard(struct log_ctx *ctx, int priority, const char *file, + int line, const char *fn, const char *format, va_list args) +{ + if (priority == 6) + vfprintf(stdout, format, args); + else + vfprintf(stderr, format, args); +} + +static void log_file(struct log_ctx *ctx, int priority, const char *file, + int line, const char *fn, const char *format, va_list args) +{ + FILE *f = monitor.log_file; + + if (priority != LOG_NOTICE) { + struct timespec ts; + + clock_gettime(CLOCK_REALTIME, &ts); + fprintf(f, "[%10ld.%09ld] [%d] ", ts.tv_sec, ts.tv_nsec, getpid()); + } + + vfprintf(f, format, args); + fflush(f); +} + static int monitor_event(struct cxl_ctx *ctx) { int fd, epollfd, rc = 0, timeout = -1; From patchwork Tue Nov 8 18:30:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 13036701 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DE69C433FE for ; Tue, 8 Nov 2022 18:30:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230079AbiKHSaa (ORCPT ); Tue, 8 Nov 2022 13:30:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230001AbiKHSaa (ORCPT ); Tue, 8 Nov 2022 13:30:30 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ABC51E72F for ; Tue, 8 Nov 2022 10:30:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667932229; x=1699468229; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=geA5FvpKwrTuUY42W1nUPqEFsiD3A9hLebU6OboB/Qc=; b=ht2V4PIr3Ofxo+JqJhYPaAbb1Nhm0y9HxrD/rbv8UvwX0uHf/Z62Ffwv D01JIJuNGurXBWprtnZ4eDwbr77XKKd6tK2lbHS53uChLzgvdV0PSN42V ilNBN+cC3GKaZRwIc17A+iba4JMdHsFRNCnX8AN2VbI8Ai/gvbktj8Whu qSvBscYtCy6XoCOwIB+XWw8iin4r/Nbyp9qiOKPLpt4Xh4C+vxJ1VdJDU uKjdcDmmKY2ivW7oAXZ38v4H3D+JpbVabLstogO1Zr5Hh67afrgozc5SF TBtIs3IzgiQLU3HT50r+Rvf8MWiYJ9EzMV9QbhDX7LDDvzG+BgOPthh4H g==; X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="291159797" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="291159797" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:29 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="705400112" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="705400112" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:28 -0800 Subject: [PATCH v4 07/10] cxl: add monitor command to cxl From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: dan.j.williams@intel.com, ira.weiny@intel.com, vishal.l.verma@intel.com, alison.schofield@intel.com, rostedt@goodmis.org Date: Tue, 08 Nov 2022 11:30:28 -0700 Message-ID: <166793222841.3768752.2390044444606875734.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> References: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org Connect the monitoring functionality to the cxl monitor command. Add basic functionality to the cxl monitor command where it can be launched as a daemon and logging can be designated to stdout or a file. Signed-off-by: Dave Jiang --- cxl/builtin.h | 1 + cxl/cxl.c | 1 + cxl/monitor.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/cxl/builtin.h b/cxl/builtin.h index b28c2213993b..34c5cfb49051 100644 --- a/cxl/builtin.h +++ b/cxl/builtin.h @@ -22,4 +22,5 @@ int cmd_create_region(int argc, const char **argv, struct cxl_ctx *ctx); int cmd_enable_region(int argc, const char **argv, struct cxl_ctx *ctx); int cmd_disable_region(int argc, const char **argv, struct cxl_ctx *ctx); int cmd_destroy_region(int argc, const char **argv, struct cxl_ctx *ctx); +int cmd_monitor(int argc, const char **argv, struct cxl_ctx *ctx); #endif /* _CXL_BUILTIN_H_ */ diff --git a/cxl/cxl.c b/cxl/cxl.c index dd1be7a054a1..3be7026f43d3 100644 --- a/cxl/cxl.c +++ b/cxl/cxl.c @@ -76,6 +76,7 @@ static struct cmd_struct commands[] = { { "enable-region", .c_fn = cmd_enable_region }, { "disable-region", .c_fn = cmd_disable_region }, { "destroy-region", .c_fn = cmd_destroy_region }, + { "monitor", .c_fn = cmd_monitor }, }; int main(int argc, const char **argv) diff --git a/cxl/monitor.c b/cxl/monitor.c index 518d8142e40d..d8e498392ed7 100644 --- a/cxl/monitor.c +++ b/cxl/monitor.c @@ -32,11 +32,15 @@ #include "event_trace.h" static const char *cxl_system = "cxl"; +const char *default_log = "/var/log/cxl-monitor.log"; static struct monitor { + const char *log; struct log_ctx ctx; FILE *log_file; bool human; + bool verbose; + bool daemon; } monitor; static void log_standard(struct log_ctx *ctx, int priority, const char *file, @@ -163,3 +167,74 @@ epoll_err: free(events); return rc; } + +int cmd_monitor(int argc, const char **argv, struct cxl_ctx *ctx) +{ + const struct option options[] = { + OPT_FILENAME('l', "log", &monitor.log, + " | standard", + "where to output the monitor's notification"), + OPT_BOOLEAN('\0', "daemon", &monitor.daemon, + "run cxl monitor as a daemon"), + OPT_BOOLEAN('u', "human", &monitor.human, + "use human friendly output formats"), + OPT_BOOLEAN('v', "verbose", &monitor.verbose, + "emit extra debug messages to log"), + OPT_END(), + }; + const char * const u[] = { + "cxl monitor []", + NULL + }; + const char *prefix ="./"; + int rc = 0, i; + + argc = parse_options_prefix(argc, argv, prefix, options, u, 0); + for (i = 0; i < argc; i++) + error("unknown parameter \"%s\"\n", argv[i]); + if (argc) + usage_with_options(u, options); + + log_init(&monitor.ctx, "cxl/monitor", "CXL_MONITOR_LOG"); + monitor.ctx.log_fn = log_standard; + + if (monitor.verbose) + monitor.ctx.log_priority = LOG_DEBUG; + else + monitor.ctx.log_priority = LOG_INFO; + + if (monitor.log) { + if (strncmp(monitor.log, "./", 2) != 0) + fix_filename(prefix, (const char **)&monitor.log); + if (strncmp(monitor.log, "./standard", 10) == 0 && !monitor.daemon) { + monitor.ctx.log_fn = log_standard; + } else { + const char *log = monitor.log; + + if (!monitor.log) + log = default_log; + monitor.log_file = fopen(log, "a+"); + if (!monitor.log_file) { + rc = -errno; + error("open %s failed: %d\n", monitor.log, rc); + goto out; + } + monitor.ctx.log_fn = log_file; + } + } + + if (monitor.daemon) { + if (daemon(0, 0) != 0) { + err(&monitor, "daemon start failed\n"); + goto out; + } + info(&monitor, "cxl monitor daemon started.\n"); + } + + rc = monitor_event(ctx); + +out: + if (monitor.log_file) + fclose(monitor.log_file); + return rc; +} From patchwork Tue Nov 8 18:30:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 13036702 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5E8EC4332F for ; Tue, 8 Nov 2022 18:30:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230142AbiKHSah (ORCPT ); Tue, 8 Nov 2022 13:30:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230001AbiKHSag (ORCPT ); Tue, 8 Nov 2022 13:30:36 -0500 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B1353F05E for ; Tue, 8 Nov 2022 10:30:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667932235; x=1699468235; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Av6IyuHvGJr8brfWMPwuS8wabZmI7DOSQJTHBSYC2l0=; b=XRBFkobl2K4mbiUfw68JMlfVn/JSwxDOw3VEQ4xY02VWWytCT3bbjSq0 z1TcSMW0LK0SgnedhN6IO7ZOj/P0AljU+ywjUNuNjKah0stBtL6SNf7d/ 7jUHEln9lEszL4oklcr8LjJgxj00f0y3NFl7H5WqllRrQXhcb0yBVCk9a r7tbDZrTfnex7RJDEKrbmvZknw7gEh9oK4+UUHpp/T5nMoI+cb1W1l1ca BZdSE3+oU6QdUtWdJeyJhnXY96o9KNOzQ0X1IHrBZrerRp4spAW5TZwrF MA9W8dLju8i4tl+r/5Y6sGdZDRy/ApxI14UwCgHqUr5pQZw6HSsl0c7rm A==; X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="294143783" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="294143783" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:35 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="705400144" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="705400144" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:34 -0800 Subject: [PATCH v4 08/10] cxl: add an optional pid check to event parsing From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: dan.j.williams@intel.com, ira.weiny@intel.com, vishal.l.verma@intel.com, alison.schofield@intel.com, rostedt@goodmis.org Date: Tue, 08 Nov 2022 11:30:34 -0700 Message-ID: <166793223408.3768752.4466685754838805704.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> References: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org From: Alison Schofield When parsing CXL events, callers may only be interested in events that originate from the current process. Introduce an optional argument to the event trace context: event_pid. When event_pid is present, only include events with a matching pid in the returned JSON list. It is not a failure to see other, non matching results. Simply skip those. The initial use case for this is the listing of media errors, where only the media-errors requested by this process are wanted. Signed-off-by: Alison Schofield Signed-off-by: Dave Jiang --- cxl/event_trace.c | 5 +++++ cxl/event_trace.h | 1 + 2 files changed, 6 insertions(+) diff --git a/cxl/event_trace.c b/cxl/event_trace.c index 490c30e1dbfc..89c9c2db26a3 100644 --- a/cxl/event_trace.c +++ b/cxl/event_trace.c @@ -215,6 +215,11 @@ static int cxl_event_parse_cb(struct tep_event *event, struct tep_record *record return 0; } + if (event_ctx->event_pid) { + if (event_ctx->event_pid != tep_data_pid(event->tep, record)) + return 0; + } + if (event_ctx->parse_event) return event_ctx->parse_event(event, record, &event_ctx->jlist_head); diff --git a/cxl/event_trace.h b/cxl/event_trace.h index 89e98cafb320..e72b4347175e 100644 --- a/cxl/event_trace.h +++ b/cxl/event_trace.h @@ -15,6 +15,7 @@ struct event_ctx { const char *system; struct list_head jlist_head; const char *event_name; /* optional */ + int event_pid; /* optional */ int (*parse_event)(struct tep_event *event, struct tep_record *record, struct list_head *jlist_head); /* optional */ }; From patchwork Tue Nov 8 18:30:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 13036703 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8FCE9C433FE for ; Tue, 8 Nov 2022 18:30:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230434AbiKHSas (ORCPT ); Tue, 8 Nov 2022 13:30:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230187AbiKHSal (ORCPT ); Tue, 8 Nov 2022 13:30:41 -0500 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 034A0554F1 for ; Tue, 8 Nov 2022 10:30:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667932241; x=1699468241; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DeqOUaGy2SP0BpG6nmA3vfDOX3XIVNbyvfzpqIRcQuk=; b=kTuEiBeyv1/7m/5xuyaZlkUQe+ihiBuTuyuRWPmq2HlASB+opZTZjVYo LB/9/wdRmy5RivsVJZel/875dr9AW2EwET0/gyCRn0eJj0pziJZBnBSeG Ut1Chl1P4TlaMpWnkpZXEhy4g10FtOOxUCRa2BBTGtQsTcpFCwuWPHYAk j5E+//lDMGvbecHffLMqyYaYmRDBLBWw0JvVatGHcfRE39vPQoOKWOxds OOrNfjCvtAFjuutp591bk9B/rHcM9sDgrNAJiLl7+UsyRXgJD7hy5fsZh nCRi30LD4U/FNSRySJGjs5oSKowgxa5efVcMGNFG8oYfmRA9Zk+635sjj A==; X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="298290970" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="298290970" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:40 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="705400184" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="705400184" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:40 -0800 Subject: [PATCH v4 09/10] cxl: add systemd service for monitor From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: dan.j.williams@intel.com, ira.weiny@intel.com, vishal.l.verma@intel.com, alison.schofield@intel.com, rostedt@goodmis.org Date: Tue, 08 Nov 2022 11:30:39 -0700 Message-ID: <166793223975.3768752.11846694721780323639.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> References: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org Add a systemd service file for cxl monitor to start the monitoring service on boot initialization. Add the installation setup for the service file. Signed-off-by: Dave Jiang --- cxl/cxl-monitor.service | 9 +++++++++ cxl/meson.build | 4 ++++ ndctl.spec.in | 1 + 3 files changed, 14 insertions(+) create mode 100644 cxl/cxl-monitor.service diff --git a/cxl/cxl-monitor.service b/cxl/cxl-monitor.service new file mode 100644 index 000000000000..87c842b6f595 --- /dev/null +++ b/cxl/cxl-monitor.service @@ -0,0 +1,9 @@ +[Unit] +Description=Cxl Monitor Daemon + +[Service] +Type=simple +ExecStart=/usr/bin/cxl monitor + +[Install] +WantedBy=multi-user.target diff --git a/cxl/meson.build b/cxl/meson.build index eb8b2b1070ed..fc2e946707a8 100644 --- a/cxl/meson.build +++ b/cxl/meson.build @@ -11,6 +11,10 @@ cxl_src = [ 'monitor.c', ] +if get_option('systemd').enabled() + install_data('cxl-monitor.service', install_dir : systemdunitdir) +endif + cxl_tool = executable('cxl', cxl_src, include_directories : root_inc, diff --git a/ndctl.spec.in b/ndctl.spec.in index cfcafa2ba816..c883317c5ce7 100644 --- a/ndctl.spec.in +++ b/ndctl.spec.in @@ -194,6 +194,7 @@ fi %{_bindir}/cxl %{_mandir}/man1/cxl* %{bashcompdir}/cxl +%{_unitdir}/cxl-monitor.service %files -n LNAME %defattr(-,root,root) From patchwork Tue Nov 8 18:30:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 13036704 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1AE1C4332F for ; Tue, 8 Nov 2022 18:30:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230287AbiKHSav (ORCPT ); Tue, 8 Nov 2022 13:30:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230425AbiKHSas (ORCPT ); Tue, 8 Nov 2022 13:30:48 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1E0554B36 for ; Tue, 8 Nov 2022 10:30:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667932246; x=1699468246; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KSFZB3ntka+bSSvolTA/Uf4WdX3s1HtcDYCRMCLVZIU=; b=TeR/hs7NFTVdD0boDbenxfRwDQskdjyQzBfLXyIv65hUeU+BFFpCB0xt mzZu9HcKaMDSXP991u9RkfC9J5ie3qHv3VyIPoVhsAOSvyc9Y77bN4BTp JyCq7MX0JW7osii3iZrmDsNvWc8Q4f3cBfIGIZBDR/xrEmgfJItfTxpyg YM+nDbkYJO4g2P2+yQB9LwofyAwv89aVtimefmo7k66hBHhb3F5sKgpCw oKU/6jB61PB8E5vKVo7o2wyrhOUv2r67el3+04upaOQHvlG9MKRHFVDHd xOF1oMCd0EsEKOKYtxAqOzIDWT2MfuiJo/6MG3P5lQTMCxYPCmkPwfq1J w==; X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="375046233" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="375046233" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:46 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10525"; a="705400251" X-IronPort-AV: E=Sophos;i="5.96,148,1665471600"; d="scan'208";a="705400251" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2022 10:30:45 -0800 Subject: [PATCH v4 10/10] cxl: add man page documentation for monitor From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: dan.j.williams@intel.com, ira.weiny@intel.com, vishal.l.verma@intel.com, alison.schofield@intel.com, rostedt@goodmis.org Date: Tue, 08 Nov 2022 11:30:45 -0700 Message-ID: <166793224539.3768752.16520384828313781321.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> References: <166793212765.3768752.4370741471494095451.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/1.4 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cxl@vger.kernel.org Add man page documentation to explain the usage of cxl monitor. Signed-off-by: Dave Jiang --- Documentation/cxl/cxl-monitor.txt | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Documentation/cxl/cxl-monitor.txt diff --git a/Documentation/cxl/cxl-monitor.txt b/Documentation/cxl/cxl-monitor.txt new file mode 100644 index 000000000000..43c2ece72220 --- /dev/null +++ b/Documentation/cxl/cxl-monitor.txt @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 + +cxl-monitor(1) +================ + +NAME +---- +cxl-monitor - Monitor the CXL kernel trace events + +SYNOPSIS +-------- +[verse] +'cxl monitor' [] + +DESCRIPTION +----------- +Cxl monitor is used for monitoring the CXL trace events emitted by +the kernel and convert them to json objects and dumping the json format +notifications to standard output or a logfile. + +Both, the values in configuration file and in options will work. If +there is a conflict, the values in options will override the values in +the configuration file. Any updated values in the configuration file will +take effect only after the monitor process is restarted. + +EXAMPLES +-------- + +Run a monitor as a daemon to monitor events and output to a log file. +[verse] +cxl monitor --daemon --log=/var/log/cxl-monitor.log + +Run a monitor as a one-shot command and output the notifications to stdio. +[verse] +cxl monitor + +Run a monitor daemon as a system service +[verse] +systemctl start cxl-monitor.service + +OPTIONS +------- +-l:: +--log=:: + Send log messages to the specified destination. + - "": + Send log messages to specified . When fopen() is not able + to open , log messages will be forwarded to syslog. + - "standard": + Send messages to standard output. + +The default log destination is '/var/log/cxl-monitor.log' if "--daemon" is specified, +otherwise 'standard'. Note that standard and relative path for +will not work if "--daemon" is specified. + +--daemon:: + Run a monitor as a daemon. + +-u:: +--human:: + Output monitor notification as human friendly json format instead + of the default machine friendly json format. + +-v:: +--verbose:: + Emit extra debug messages to log. + +COPYRIGHT +--------- +Copyright (c) 2022, Intel Corp. License GPLv2: GNU GPL version 2 +. This is free software: you are +free to change and redistribute it. There is NO WARRANTY, to the +extent permitted by law. + +SEE ALSO +-------- +linkcxl:cxl-list[1]