Skip to contentSkip to conversation

histogram: add helper function to expose histograms to userspace

PATCH In review Axel Rasmussen 1 participant 1 message May 28, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 2/7 histogram: add helper function to expose histograms to userspace

This change introduces the histogram_print_buckets_rcu(). This API is used for implementing e.g. procfs or sysfs files exposing histograms. print_buckets can be combined with e.g. seq_file to provide a read interface for such a file.

This is a squashed, refactored, and modified version of a previously-internal implementation. Thanks to the following individuals for portions of the implementation:

Junaid Shahid <[email protected]> - Original implementation Yu Zhao <[email protected]> - Simplification

---
 include/linux/histogram.h | 23 ++++++++++++++++
 lib/histogram.c           | 55 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
include/linux/histogram.h +23 -0
index 137930ca933f..7a1dc33fff5e 100644
@@ -65,6 +65,12 @@
  * histogram_destroy_rcu(&hrcu);
  */
 
+/*
+ * Max number of digits in decimal needed to represent U64_MAX threshold,
+ * plus one space.
+ */
+#define HISTO_MAX_CHARS_PER_THRESHOLD (20 + 1)
+
 struct histogram {
 	struct rcu_head rcu;
 	u64 __percpu *buckets;
@@ -189,6 +195,23 @@ static inline void histogram_record_rcu(struct histogram_rcu *hrcu, u64 val,
 ssize_t histogram_read_rcu(const struct histogram_rcu *hrcu, u64 *buckets,
 			   u64 *thresholds, size_t nr_thresholds);
 
+/**
+ * histogram_print_buckets_rcu() - helper function to print histogram buckets
+ * @hrcu: histogram
+ * @buffer: output buffer to fill
+ * @len: length of the output buffer
+ *
+ * Reads buckets by calling histogram_read_rcu(), then fills the output buffer.
+ *
+ * Context: Performs allocation with GFP_ATOMIC.
+ *
+ * Returns: The number of characters written to @buffer, or a negative error
+ * code on failure. If the buffer isn't large enough to contain the output,
+ * -EINVAL is returned.
+ */
+int histogram_print_buckets_rcu(struct histogram_rcu *hrcu, char *buffer,
+				int len);
+
 /**
  * histogram_set_thresholds_rcu() - set RCU-protected histogram thresholds
  * @hrcu: histogram
lib/histogram.c +55 -0

67 lines not on this page · raw message

Signature · 1 line
2.27.0.rc0.183.gde8f92d652-goog