xref: /freebsd/usr.sbin/pmc/display.hh (revision a53d2e4f5db588cfd59cdebf04536b6ca7a70e4b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2026, Netflix, Inc.
5  *
6  * This software was developed by Ali Mashtizadeh under the sponsorship from
7  * Netflix, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #define COLOR256_GREEN		46
33 #define COLOR256_RED		196
34 #define COLOR256_YELLOW		226
35 
36 void setup_screen();
37 
38 /* Formatting */
39 std::string format_siprefix(uint64_t count);
40 std::string format_binprefix(uint64_t count);
41 std::string format_sample(uint64_t count, uint64_t total);
42 std::string format_percent(uint64_t count, uint64_t total);
43 
44 /* Printing */
45 enum class siunit {
46 	seconds,
47 	// Special
48 	cycles,
49 	percent
50 };
51 
52 void title(const std::string &msg);
53 void header(const std::string &msg);
54 void printval(const std::string &msg, uint64_t val, siunit ui);
55 void printval(const std::string &msg, float val, siunit ui);
56 void printbar(float percent);
57 
58 /* Tables */
59 class field
60 {
61 public:
field()62 	field() { }
63 	field(const std::string &s);
64 	field(int64_t v);
65 	field(float v);
66 	field(int64_t count, int64_t total, bool percent = false);
67 	field(int64_t user_count, int64_t user_total,
68 	    int64_t kernel_count, int64_t kernel_total);
~field()69 	virtual ~field() { }
70 	virtual bool operator>(const field &b) const;
71 	virtual bool operator<(const field &b) const;
72 	virtual std::string to_string();
73 	virtual size_t length();
74 private:
75 	enum class FIELD {
76 		STRING,
77 		INTEGER,
78 		FLOAT,
79 		SAMPLE,
80 		DSAMPLE,
81 		PERCENT
82 	};
83 	FIELD type;
84 	int64_t value[4];
85 	float fvalue;
86 	std::string svalue;
87 };
88 
89 class table
90 {
91 public:
92 	table();
93 	~table();
94 	void addcolumn(const std::string &c, bool alignleft = false);
95 	void addrow(std::vector<field> r);
96 	void sort(int col, bool descending = true);
97 	void print();
98 private:
99 	int sortcol;
100 	bool sortdir;
101 	std::vector<std::string> cols;
102 	std::vector<bool> align;
103 	std::vector<std::vector<field>> rows;
104 };
105 
106