From b8989aab9a0815d44014f993127390200ecd23c7 Mon Sep 17 00:00:00 2001
From: Ben Olson <matthew.olson@intel.com>
Date: Tue, 17 Oct 2023 15:31:51 -0500
Subject: [PATCH] minwidth now accepts a percentage of time with %

---
 README.md     |  3 ++-
 flamegraph.pl | 26 ++++++++++++++++++++++----
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/README.md b/README.md
index 1aa3ecda..ee1b3eee 100644
--- a/README.md
+++ b/README.md
@@ -167,7 +167,8 @@ USAGE: ./flamegraph.pl [options] infile > outfile.svg
 	--subtitle TEXT  # second level title (optional)
 	--width NUM      # width of image (default 1200)
 	--height NUM     # height of each frame (default 16)
-	--minwidth NUM   # omit smaller functions (default 0.1 pixels)
+	--minwidth NUM   # omit smaller functions. In pixels or use "%" for 
+	                 # percentage of time (default 0.1 pixels)
 	--fonttype FONT  # font type (default "Verdana")
 	--fontsize NUM   # font size (default 12)
 	--countname TEXT # count type label (default "samples")
diff --git a/flamegraph.pl b/flamegraph.pl
index d2172b61..c5a7bc3d 100755
--- a/flamegraph.pl
+++ b/flamegraph.pl
@@ -103,7 +103,7 @@
 my $frameheight = 16;           # max height is dynamic
 my $fontsize = 12;              # base text size
 my $fontwidth = 0.59;           # avg width relative to fontsize
-my $minwidth = 0.1;             # min function width, pixels
+my $minwidth = 0.1;             # min function width, pixels or percentage of time
 my $nametype = "Function:";     # what are the names in the data?
 my $countname = "samples";      # what are the counts in the data?
 my $colors = "hot";             # color theme
@@ -134,7 +134,8 @@ sub usage {
 	--subtitle TEXT  # second level title (optional)
 	--width NUM      # width of image (default 1200)
 	--height NUM     # height of each frame (default 16)
-	--minwidth NUM   # omit smaller functions (default 0.1 pixels)
+	--minwidth NUM   # omit smaller functions. In pixels or use "%" for 
+	                 # percentage of time (default 0.1 pixels)
 	--fonttype FONT  # font type (default "Verdana")
 	--fontsize NUM   # font size (default 12)
 	--countname TEXT # count type label (default "samples")
@@ -165,7 +166,7 @@ sub usage {
 	'encoding=s'  => \$encoding,
 	'fontsize=f'  => \$fontsize,
 	'fontwidth=f' => \$fontwidth,
-	'minwidth=f'  => \$minwidth,
+	'minwidth=s'  => \$minwidth,
 	'title=s'     => \$titletext,
 	'subtitle=s'  => \$subtitletext,
 	'nametype=s'  => \$nametype,
@@ -224,6 +225,16 @@ sub usage {
 	die "Notes string can't contain < or >"
 }
 
+# Ensure minwidth is a valid floating-point number,
+# print usage string if not
+my $minwidth_f;
+if ($minwidth =~ /^([0-9.]+)%?$/) {
+	$minwidth_f = $1;
+} else {
+	warn "Value '$minwidth' is invalid for minwidth, expected a float.\n";
+	usage();
+}
+
 # background colors:
 # - yellow gradient: default (hot, java, js, perl)
 # - green gradient: mem
@@ -703,7 +714,14 @@ sub flow {
 $timemax ||= $time;
 
 my $widthpertime = ($imagewidth - 2 * $xpad) / $timemax;
-my $minwidth_time = $minwidth / $widthpertime;
+
+# Treat as a percentage of time if the string ends in a "%".
+my $minwidth_time;
+if ($minwidth =~ /%$/) {
+	$minwidth_time = $timemax * $minwidth_f / 100;
+} else {
+	$minwidth_time = $minwidth_f / $widthpertime;
+}
 
 # prune blocks that are too narrow and determine max depth
 while (my ($id, $node) = each %Node) {
