LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Ian Pilcher <arequipeno@gmail.com>
To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org
Cc: axboe@kernel.dk, pavel@ucw.cz, kabel@kernel.org,
	linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org
Subject: [RFC PATCH v3 05/18] ledtrig-blkdev: Periodically check devices for activity & blink LEDs
Date: Wed, 18 Aug 2021 21:50:40 -0500	[thread overview]
Message-ID: <20210819025053.222710-6-arequipeno@gmail.com> (raw)
In-Reply-To: <20210819025053.222710-1-arequipeno@gmail.com>

Use a delayed workqueue to periodically check configured block devices for
activity since the last check.  Blink LEDs associated with devices on which
the configured type of activity (read/write) has occurred.

Signed-off-by: Ian Pilcher <arequipeno@gmail.com>
---
 drivers/leds/trigger/ledtrig-blkdev.c | 87 +++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)

diff --git a/drivers/leds/trigger/ledtrig-blkdev.c b/drivers/leds/trigger/ledtrig-blkdev.c
index fcae7ce63b92..e9c23824c33c 100644
--- a/drivers/leds/trigger/ledtrig-blkdev.c
+++ b/drivers/leds/trigger/ledtrig-blkdev.c
@@ -7,9 +7,11 @@
  */
 
 #include <linux/ctype.h>
+#include <linux/genhd.h>
 #include <linux/leds.h>
 #include <linux/list.h>
 #include <linux/mutex.h>
+#include <linux/part_stat.h>
 
 /* Default blink time & polling interval (milliseconds) */
 #define LEDTRIG_BLKDEV_BLINK_MSEC	75
@@ -66,6 +68,9 @@ static unsigned int ledtrig_blkdev_count;
 /* How often to check for drive activity - in jiffies */
 static unsigned int ledtrig_blkdev_interval;
 
+static void blkdev_process(struct work_struct *const work);
+static DECLARE_DELAYED_WORK(ledtrig_blkdev_work, blkdev_process);
+
 
 /*
  *
@@ -138,3 +143,85 @@ static bool blkdev_write_mode(const enum ledtrig_blkdev_mode mode)
 {
 	return mode != LEDTRIG_BLKDEV_MODE_RO;
 }
+
+
+/*
+ *
+ *	Periodically check for device acitivity and blink LEDs
+ *
+ */
+
+static void blkdev_blink(const struct ledtrig_blkdev_led *const led)
+{
+	unsigned long delay_on = READ_ONCE(led->blink_msec);
+	unsigned long delay_off = 1;	/* 0 leaves LED turned on */
+
+	led_blink_set_oneshot(led->led_dev, &delay_on, &delay_off, 0);
+}
+
+static void blkdev_update_disk(struct ledtrig_blkdev_disk *const disk,
+			       const unsigned int generation)
+{
+	const struct block_device *const part0 = disk->gd->part0;
+	const unsigned long read_ios = part_stat_read(part0, ios[STAT_READ]);
+	const unsigned long write_ios = part_stat_read(part0, ios[STAT_WRITE])
+				+ part_stat_read(part0, ios[STAT_DISCARD])
+				+ part_stat_read(part0, ios[STAT_FLUSH]);
+
+	if (disk->read_ios != read_ios) {
+		disk->read_act = true;
+		disk->read_ios = read_ios;
+	} else {
+		disk->read_act = false;
+	}
+
+	if (disk->write_ios != write_ios) {
+		disk->write_act = true;
+		disk->write_ios = write_ios;
+	} else {
+		disk->write_act = false;
+	}
+
+	disk->generation = generation;
+}
+
+static void blkdev_process(struct work_struct *const work)
+{
+	static unsigned int generation;
+
+	struct ledtrig_blkdev_led *led;
+	struct ledtrig_blkdev_link *link;
+	unsigned long delay;
+
+	if (!mutex_trylock(&ledtrig_blkdev_mutex))
+		goto exit_reschedule;
+
+	hlist_for_each_entry(led, &ledtrig_blkdev_leds, leds_node) {
+
+		hlist_for_each_entry(link, &led->disks, led_disks_node) {
+
+			struct ledtrig_blkdev_disk *const disk = link->disk;
+
+			if (disk->generation != generation)
+				blkdev_update_disk(disk, generation);
+
+			if (disk->read_act && blkdev_read_mode(led->mode)) {
+				blkdev_blink(led);
+				break;
+			}
+
+			if (disk->write_act && blkdev_write_mode(led->mode)) {
+				blkdev_blink(led);
+				break;
+			}
+		}
+	}
+
+	++generation;
+
+	mutex_unlock(&ledtrig_blkdev_mutex);
+
+exit_reschedule:
+	delay = READ_ONCE(ledtrig_blkdev_interval);
+	WARN_ON_ONCE(!schedule_delayed_work(&ledtrig_blkdev_work, delay));
+}
-- 
2.31.1


  parent reply	other threads:[~2021-08-19  2:51 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-19  2:50 [RFC PATCH v3 00/18] Add block device LED trigger Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 01/18] docs: Add block device (blkdev) LED trigger documentation Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 02/18] block: Add get_disk_by_name() for use by blkdev LED trigger Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 03/18] ledtrig-blkdev: Add file (ledtrig-blkdev.c) for block device " Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 04/18] ledtrig-blkdev: Add misc. helper functions to blkdev " Ian Pilcher
2021-08-19  2:50 ` Ian Pilcher [this message]
2021-08-19  2:50 ` [RFC PATCH v3 06/18] block: Add LED trigger pointer to struct gendisk Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 07/18] ledtrig-blkdev: Add function to initialize gendisk ledtrig member Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 08/18] ledtrig-blkdev: Add function to remove LED/device association Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 09/18] ledtrig-blkdev: Add function to disassociate a device from all LEDs Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 10/18] block: Call LED trigger init/cleanup functions Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 11/18] ledtrig-blkdev: Add function to associate a device with an LED Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 12/18] ledtrig-blkdev: Add sysfs attributes to [dis]associate LEDs & devices Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 13/18] ledtrig-blkdev: Add blink_time & interval sysfs attributes Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 14/18] ledtrig-blkdev: Add mode (read/write/rw) sysfs attributue Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 15/18] ledtrig-blkdev: Add function to associate blkdev trigger with LED Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 16/18] ledtrig-blkdev: Add function to disassociate an LED from the trigger Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 17/18] ledtrig-blkdev: Add initialization function Ian Pilcher
2021-08-19  2:50 ` [RFC PATCH v3 18/18] ledtrig-blkdev: Add config option to enable the trigger Ian Pilcher

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210819025053.222710-6-arequipeno@gmail.com \
    --to=arequipeno@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=kabel@kernel.org \
    --cc=kernelnewbies@kernelnewbies.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=pavel@ucw.cz \
    --subject='Re: [RFC PATCH v3 05/18] ledtrig-blkdev: Periodically check devices for activity & blink LEDs' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).