From patchwork Wed Sep 14 20:47:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 12976539 X-Patchwork-Delegate: vishal.l.verma@intel.com 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 CE9E5ECAAD3 for ; Wed, 14 Sep 2022 20:47:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229541AbiINUr7 (ORCPT ); Wed, 14 Sep 2022 16:47:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229503AbiINUr6 (ORCPT ); Wed, 14 Sep 2022 16:47:58 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAAE66AA25 for ; Wed, 14 Sep 2022 13:47:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663188476; x=1694724476; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wxHvp86GJD8bYcGle+ijwYCUQIn4oAnCr/tWh5NO3/Q=; b=K5qfRFGeywtkjdcVq6nKvV176WoHYIXH1HQM/4kSqgkbdGotsZ3+PLr2 vNdcdYqTvAxJFHnR9W2qV7HVVUzYODxBdR/elKLrL8lLaxuPYuCCuYtmF F4KUTDMg+MV1NGk8MIU5FWWALl7GNxJyoEa0+ezeyUifGgF4AAtZpUMiO KjN24vnGukgBOI4Zn1RB0dL/kLzyU/FKYLA/Mm4+hQZOhO3YlHTVKVWIJ gKrik7KlaGPKVyUi8buDJeuowXWmKgpDH36dGbeqv0aEI2Ydm7a1eI98P laPRxexBkNFNaDZABrMawn7mt4wL2rmG0zZyFb3tbYXR0GMpuglIR2PLm g==; X-IronPort-AV: E=McAfee;i="6500,9779,10470"; a="299897306" X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="299897306" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:47:56 -0700 X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="742685971" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:47:55 -0700 Subject: [PATCH 1/7] ndctl: cxl: add helper function to parse trace event to json object From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: alison.schofield@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, bwidawsk@kernel.org, dan.j.williams@intel.com Date: Wed, 14 Sep 2022 13:47:55 -0700 Message-ID: <166318847529.3087953.18405653106756321818.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166318836385.3087953.6809315284050736931.stgit@djiang5-desk3.ch.intel.com> References: <166318836385.3087953.6809315284050736931.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. Signed-off-by: Dave Jiang Tested-by: Alison Schofield --- cxl/event_trace.c | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++ cxl/event_trace.h | 14 ++++ cxl/meson.build | 2 + meson.build | 1 4 files changed, 183 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..ffa2a9b9b036 --- /dev/null +++ b/cxl/event_trace.c @@ -0,0 +1,166 @@ +// 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 size) +{ + if (size <= 4) + return json_object_new_int(*(int *)num); + + return util_json_object_hex(*(unsigned long long *)num, 0); +} + +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; + char *tmp; + + tmp = strcasestr(f->type, "char[]"); + if (tmp) { /* event field is a 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->arraylen) { /* data array */ + unsigned char *data; + int chunks; + + 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->name, "uuid"); + if (tmp && f->arraylen == 16) { + 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; + } + + 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); + 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 */ + char *data; + + data = tep_get_field_raw(NULL, event, f->name, record, &len, 0); + if (!data) + continue; + + jobj = num_to_json(data, f->elementsize); + 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 Wed Sep 14 20:48:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 12976540 X-Patchwork-Delegate: vishal.l.verma@intel.com 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 C7B66C6FA82 for ; Wed, 14 Sep 2022 20:48:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229669AbiINUsE (ORCPT ); Wed, 14 Sep 2022 16:48:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229503AbiINUsD (ORCPT ); Wed, 14 Sep 2022 16:48:03 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60A4E6AA25 for ; Wed, 14 Sep 2022 13:48:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663188482; x=1694724482; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DwIldEzUVI0O6ficbQRXaBTNXjY6xo/z8V+glpZgRv4=; b=DniWETvY2le62qL1npqXLCTLsksOt3PsflHOJNOqMOLh/7N/4Ub1n3Cx F7gtrj99N4QuMbH/LV/PxcECIERX6sTE7/wTJuwbVqit0XInq5jqROeTT qh/N0SVHenLhgPsKL6REdY/1AbNRkyI7Q6nC+I48J26cfEtDxkXie8Iug 5HVoeThOGQQncMK6sMqj/AbbntQvh0T0Wlqw1EMIktA8t8ZQBZ3sOOOcT 2gNGVFwWI623A8JaRtqLwz8ysIFiT9Hss+13gzdjE5udjXRHeXV0R0u3R eR5sCMaSVrIGljRbFc7aryotoLuZVfuYbhacV2qrPOrbB+00WQKDodgIE Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10470"; a="362498940" X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="362498940" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:01 -0700 X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="759360388" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:01 -0700 Subject: [PATCH 2/7] ndctl: cxl: add helper to parse through all current events From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: alison.schofield@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, bwidawsk@kernel.org, dan.j.williams@intel.com Date: Wed, 14 Sep 2022 13:48:01 -0700 Message-ID: <166318848131.3087953.17609964424559300800.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166318836385.3087953.6809315284050736931.stgit@djiang5-desk3.ch.intel.com> References: <166318836385.3087953.6809315284050736931.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. Signed-off-by: Dave Jiang --- cxl/event_trace.c | 35 +++++++++++++++++++++++++++++++++++ cxl/event_trace.h | 7 +++++++ cxl/meson.build | 1 + meson.build | 2 ++ 4 files changed, 45 insertions(+) diff --git a/cxl/event_trace.c b/cxl/event_trace.c index ffa2a9b9b036..d5e67b55a4d8 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" @@ -164,3 +165,37 @@ 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; + int rc, i; + + /* Filter out all the events that the caller isn't interested in. */ + for (i = 0; event_ctx->systems[i]; i++) { + if (strcmp(event->system, event_ctx->systems[i]) != 0) + continue; + + rc = cxl_event_to_json_callback(event, record, &event_ctx->jlist_head); + if (rc < 0) + return rc; + } + + return 0; +} + +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..98cfbb4602e1 100644 --- a/cxl/event_trace.h +++ b/cxl/event_trace.h @@ -11,4 +11,11 @@ struct jlist_node { struct list_node list; }; +struct event_ctx { + const char **systems; + struct list_head jlist_head; +}; + +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 Wed Sep 14 20:48:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 12976541 X-Patchwork-Delegate: vishal.l.verma@intel.com 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 5D105ECAAD3 for ; Wed, 14 Sep 2022 20:48:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229521AbiINUsJ (ORCPT ); Wed, 14 Sep 2022 16:48:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229617AbiINUsI (ORCPT ); Wed, 14 Sep 2022 16:48:08 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0772F6AA25 for ; Wed, 14 Sep 2022 13:48:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663188488; x=1694724488; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ei9H7tMIrz+qBeCy4wxIj4jWlpbcQf9Wmr1EXirsk74=; b=X9ou+trmnTFLmij9BLKsolCm5YOECFaB9HRh1/p9EAvFlirQFT45ME5W t2uKXxC9J1CGaGjPC2AxuaS4MV7rOF+rgti1NCtWMu7heSFKaFEX17Yb6 rGkzZSz6Bw4A6YmIuG7JIYLSad4LTzyhXGXGgnZQ+OCVNnUNTmBnhqOCx e792FV6rE6YF/EyGGVb/nnUY6uUSfjHLSZ37nSMT4zEYwkqlF0evnK7kP GmuPI8HmbC2RqZc+zPU4aPEFwAgk+twQJEjGe9/yECIhZ/Zmi7qzjT8Ka qA1goB4B31irC8SMnTLZxkCq0owii308NirVSksl0V1fRJ3uLeanAsIhN w==; X-IronPort-AV: E=McAfee;i="6500,9779,10470"; a="281570322" X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="281570322" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:07 -0700 X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="720719115" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:07 -0700 Subject: [PATCH 3/7] ndctl: cxl: add common function to enable event trace From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: alison.schofield@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, bwidawsk@kernel.org, dan.j.williams@intel.com Date: Wed, 14 Sep 2022 13:48:06 -0700 Message-ID: <166318848691.3087953.726606131511690601.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166318836385.3087953.6809315284050736931.stgit@djiang5-desk3.ch.intel.com> References: <166318836385.3087953.6809315284050736931.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 | 27 +++++++++++++++++++++++++++ cxl/event_trace.h | 1 + 2 files changed, 28 insertions(+) diff --git a/cxl/event_trace.c b/cxl/event_trace.c index d5e67b55a4d8..39ca5a9c49be 100644 --- a/cxl/event_trace.c +++ b/cxl/event_trace.c @@ -199,3 +199,30 @@ 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 **systems) +{ + int rc, i, en_count = 0; + + for (i = 0; systems[i]; i++) { + char *path; + + rc = asprintf(&path, "events/%s/enable", systems[i]); + if (rc == -1) + continue; + rc = tracefs_instance_file_write(inst, path, "1"); + free(path); + if (rc == -1) + continue; + en_count++; + } + + if (!en_count) + return -ENOENT; + + 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 98cfbb4602e1..b4453c393e7a 100644 --- a/cxl/event_trace.h +++ b/cxl/event_trace.h @@ -17,5 +17,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 **systems); #endif From patchwork Wed Sep 14 20:48:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 12976542 X-Patchwork-Delegate: vishal.l.verma@intel.com 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 19DB0C6FA82 for ; Wed, 14 Sep 2022 20:48:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229617AbiINUsP (ORCPT ); Wed, 14 Sep 2022 16:48:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229503AbiINUsO (ORCPT ); Wed, 14 Sep 2022 16:48:14 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D1416BD62 for ; Wed, 14 Sep 2022 13:48:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663188494; x=1694724494; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qqFd9z0Ay6h2dsUpWd6B4jHhDdYbPVJj/DQK6umx3Vg=; b=C4le4Ewxvl3tBp9R9/qfKb+GkGg3/O/vTBvsVjucRF+M5kZiKhRAvfAw C8vp4UMtpueiokIM+vkKwVlgapXRB3ec8+/A7iS8KWMQlthiZP8CHm4+5 E36vx4trazG69c7DqNOqM2DkPMnOYTEvW2Z0wD39vHSFMpQIIlyuYjjaJ axUz8tmLFZF0h3n+x9IQllTg+627ane4oYfzL6pEwa0NZN/sN5ul1dJPM 7FhLJcxPYr5N/LYRRPzgPxVWTeqX2yo9qXcnwnlm/8a3Qu3UX6XGH8Q0F 2qoptpO60/eQPeAyNljYRsBYcuAA3dG/IDnjGrWr3dn0aH+4c6RW/4T3N A==; X-IronPort-AV: E=McAfee;i="6500,9779,10470"; a="297266817" X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="297266817" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:13 -0700 X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="617007441" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:13 -0700 Subject: [PATCH 4/7] ndctl: cxl: add common function to disable event trace From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: alison.schofield@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, bwidawsk@kernel.org, dan.j.williams@intel.com Date: Wed, 14 Sep 2022 13:48:12 -0700 Message-ID: <166318849259.3087953.6822321103087684015.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166318836385.3087953.6809315284050736931.stgit@djiang5-desk3.ch.intel.com> References: <166318836385.3087953.6809315284050736931.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 | 8 ++++++++ cxl/event_trace.h | 1 + 2 files changed, 9 insertions(+) diff --git a/cxl/event_trace.c b/cxl/event_trace.c index 39ca5a9c49be..50f9cc428aa7 100644 --- a/cxl/event_trace.c +++ b/cxl/event_trace.c @@ -226,3 +226,11 @@ int cxl_event_tracing_enable(struct tracefs_instance *inst, const char **systems tracefs_trace_on(inst); return 0; } + +int cxl_event_tracing_disable(struct tracefs_instance *inst) +{ + if (!tracefs_trace_is_on(inst)) + return 0; + + return tracefs_trace_off(inst); +} diff --git a/cxl/event_trace.h b/cxl/event_trace.h index b4453c393e7a..9931fb32b245 100644 --- a/cxl/event_trace.h +++ b/cxl/event_trace.h @@ -18,5 +18,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 **systems); +int cxl_event_tracing_disable(struct tracefs_instance *inst); #endif From patchwork Wed Sep 14 20:48:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 12976543 X-Patchwork-Delegate: vishal.l.verma@intel.com 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 2CD18ECAAD3 for ; Wed, 14 Sep 2022 20:48:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229503AbiINUsW (ORCPT ); Wed, 14 Sep 2022 16:48:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229779AbiINUsV (ORCPT ); Wed, 14 Sep 2022 16:48:21 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A9576C752 for ; Wed, 14 Sep 2022 13:48:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663188500; x=1694724500; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=j+GshgjHQ7ZOCuXbydQkOP8RRGIoynm8g2kOJC9UeqQ=; b=CxTh6Em3lMebqRs07mdZrr0cHcH+qmzRkX97vJzCCRbHS7+NVb3WyzzL xKEMkYzWuSUBHgynyTw/beTJvz18YPA/tEs+rIsci4Bqe8wGHBFkQ5qx+ P3onjofEhqQkGsMVb3bTFlm2AcSQVQJBNoXbhD3yNMjf/RzEH9MpWqBDy Z6KGWo+iLj2cjzgEU4rEi+O8eF+yG93ecneDoZZ8L9E0/ryELwpiU4nqp 5Oy0R7MGv4E4/FtPm8+iGEix98fIzNUrK4LItX1ot4EGbLtymn0X38vko 2oRvkAeXNKdtBufJDfI+2wGiWifLIIMGKozZayrR8v5UnzNOFwaXlbrVr A==; X-IronPort-AV: E=McAfee;i="6500,9779,10470"; a="296127360" X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="296127360" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:19 -0700 X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="617007454" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:19 -0700 Subject: [PATCH 5/7] ndctl: cxl: add monitor function for event trace events From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: alison.schofield@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, bwidawsk@kernel.org, dan.j.williams@intel.com Date: Wed, 14 Sep 2022 13:48:18 -0700 Message-ID: <166318849861.3087953.11165630490819410564.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166318836385.3087953.6809315284050736931.stgit@djiang5-desk3.ch.intel.com> References: <166318836385.3087953.6809315284050736931.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 | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 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..2ab2e249f575 --- /dev/null +++ b/cxl/monitor.c @@ -0,0 +1,139 @@ +// 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" + +const char *systems[] = { "cxl_events", "cxl_aer", NULL }; + +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_crate( 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, systems); + if (rc < 0) { + err(&monitor, "cxl_trace_event_enable() failed: %d\n", rc); + goto event_en_err; + } + + ectx.systems = systems; + 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 Wed Sep 14 20:48:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 12976544 X-Patchwork-Delegate: vishal.l.verma@intel.com 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 95396C6FA82 for ; Wed, 14 Sep 2022 20:48:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229770AbiINUs0 (ORCPT ); Wed, 14 Sep 2022 16:48:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229779AbiINUs0 (ORCPT ); Wed, 14 Sep 2022 16:48:26 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB7EA6C752 for ; Wed, 14 Sep 2022 13:48:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663188505; x=1694724505; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+O0ELCZLuUf6nwv6swT3F6dH4yOXGMoTMRu8Sm+IUmQ=; b=IxZtoqXdf+5eEUIhu1d3NPiOXlHBiCSEXR8rm27bjjs0udEW8L1tRFYH wlJJG1wEob7FBvgwwcTS+o+BUX8bbdH7TmgFuVICsfZuzSIM6KB0+x/JW dAlDvKC0ZrkVNHCq9xfe5ufdJi3sS+P0+EnINSYO//rSTxMzH0SIDFuXm tf6ofx7Q05SHU8bRR+ChVzhe+0wwVQ8WrZJ5SDvD1s29gSw0peOUh0KzM 3rFE+1fp0sl8wRxAetAtSP1gxjxJIKkmlMofeRPXWlLrRhBEBLhWmGmli Pi2Y5XYcipHCex3FMYAi3h3ImHUD2cFoDpRFEVxtJev36jVaW4aP1M0k8 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10470"; a="278266160" X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="278266160" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:25 -0700 X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="594538625" Received: from djiang5-desk3.ch.intel.com ([143.182.136.137]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:24 -0700 Subject: [PATCH 6/7] ndctl: cxl: add logging functions for monitor From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: alison.schofield@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, bwidawsk@kernel.org, dan.j.williams@intel.com Date: Wed, 14 Sep 2022 13:48:24 -0700 Message-ID: <166318850459.3087953.851045527272435004.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166318836385.3087953.6809315284050736931.stgit@djiang5-desk3.ch.intel.com> References: <166318836385.3087953.6809315284050736931.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 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cxl/monitor.c b/cxl/monitor.c index 2ab2e249f575..e030542a3fe8 100644 --- a/cxl/monitor.c +++ b/cxl/monitor.c @@ -39,6 +39,32 @@ 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); + } else + vfprintf(f, format, args); + + fflush(f); +} + static int monitor_event(struct cxl_ctx *ctx) { int fd, epollfd, rc = 0, timeout = -1; From patchwork Wed Sep 14 20:48:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 12976545 X-Patchwork-Delegate: vishal.l.verma@intel.com 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 CBEFCC6FA82 for ; Wed, 14 Sep 2022 20:48:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229801AbiINUse (ORCPT ); Wed, 14 Sep 2022 16:48:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229779AbiINUsc (ORCPT ); Wed, 14 Sep 2022 16:48:32 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B506B6D55D for ; Wed, 14 Sep 2022 13:48:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663188511; x=1694724511; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=v8mPJn8gFZzMEnNWpvc6BDFuNYCMsTNXHqtEXGLSSMk=; b=AB85Rt3A17PzORakQGwwfgebXzBzXv+COH85BDq9k+zIThThgreT/XhN UUbUXdxHcpupJsAVOEsgpQKaXchVwsESpd/Nrv7H3sEdoT1TCacTMdSGR rQNeL8jZ7H21ivQZhFZTI/7zBau5Z/0n0yk+63Cx9GTeA5My83NCor13t ns5Fl8cLlrCVT2WW9xR8grY+nvXa0rt04DXGNDtxPFkMGGSF4wm+5T9xs DZucXfQP1Kbg+EPIJ/PrMLcEPF9fmalKXi/dn1hEM1ZMQECS8VDiQR+KH tkQ3KzDwwsx2CkCUZXwHy42EdcITF4IOOENYPlbzRb6kq9//dUzsdwC5x g==; X-IronPort-AV: E=McAfee;i="6500,9779,10470"; a="360278068" X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="360278068" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Sep 2022 13:48:31 -0700 X-IronPort-AV: E=Sophos;i="5.93,315,1654585200"; d="scan'208";a="685449289" 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; 14 Sep 2022 13:48:30 -0700 Subject: [PATCH 7/7] ndctl: cxl: add monitor command to cxl From: Dave Jiang To: linux-cxl@vger.kernel.org Cc: alison.schofield@intel.com, vishal.l.verma@intel.com, ira.weiny@intel.com, bwidawsk@kernel.org, dan.j.williams@intel.com Date: Wed, 14 Sep 2022 13:48:30 -0700 Message-ID: <166318851033.3087953.1487642256782771767.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <166318836385.3087953.6809315284050736931.stgit@djiang5-desk3.ch.intel.com> References: <166318836385.3087953.6809315284050736931.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 | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 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 e030542a3fe8..e185d15254ec 100644 --- a/cxl/monitor.c +++ b/cxl/monitor.c @@ -34,9 +34,12 @@ const char *systems[] = { "cxl_events", "cxl_aer", NULL }; 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 +166,70 @@ 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.ctx.log_fn = log_standard; + } else { + monitor.log_file = fopen(monitor.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; +}