2 c440ada7 2024-01-04 benni * Copyright (c) 2024 Benjamin Stürz <benni@stuerz.xyz>
4 c440ada7 2024-01-04 benni * Permission to use, copy, modify, and distribute this software for any
5 c440ada7 2024-01-04 benni * purpose with or without fee is hereby granted, provided that the above
6 c440ada7 2024-01-04 benni * copyright notice and this permission notice appear in all copies.
8 c440ada7 2024-01-04 benni * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c440ada7 2024-01-04 benni * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c440ada7 2024-01-04 benni * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c440ada7 2024-01-04 benni * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c440ada7 2024-01-04 benni * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c440ada7 2024-01-04 benni * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c440ada7 2024-01-04 benni * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 8f16cb3e 2024-01-04 benni #include <sys/types.h>
17 8f16cb3e 2024-01-04 benni #include <machine/apmvar.h>
18 8f16cb3e 2024-01-04 benni #include <sys/signal.h>
19 8f16cb3e 2024-01-04 benni #include <sys/sched.h>
20 8f16cb3e 2024-01-04 benni #include <sys/sysctl.h>
21 8f16cb3e 2024-01-04 benni #include <sys/ioctl.h>
22 8f16cb3e 2024-01-04 benni #include <sys/sensors.h>
24 8f16cb3e 2024-01-04 benni #include <stdbool.h>
25 8f16cb3e 2024-01-04 benni #include <ncurses.h>
26 8f16cb3e 2024-01-04 benni #include <locale.h>
27 8f16cb3e 2024-01-04 benni #include <limits.h>
28 8f16cb3e 2024-01-04 benni #include <unistd.h>
29 8f16cb3e 2024-01-04 benni #include <string.h>
30 8f16cb3e 2024-01-04 benni #include <stdint.h>
31 8f16cb3e 2024-01-04 benni #include <stdlib.h>
32 8f16cb3e 2024-01-04 benni #include <stdio.h>
33 8f16cb3e 2024-01-04 benni #include <fcntl.h>
34 8f16cb3e 2024-01-04 benni #include <time.h>
35 8f16cb3e 2024-01-04 benni #include <err.h>
37 8f16cb3e 2024-01-04 benni #define MAXSENSORS 16
38 8f16cb3e 2024-01-04 benni #define LOGLEN 512
39 8f16cb3e 2024-01-04 benni #define MIN(a, b) ((a) < (b) ? (a) : (b))
41 8f16cb3e 2024-01-04 benni static int power_sensors[MAXSENSORS];
42 8f16cb3e 2024-01-04 benni static int cputemp_sensor = -1;
43 8f16cb3e 2024-01-04 benni static int num_power_sensors = 0;
44 8f16cb3e 2024-01-04 benni static int entries = 0;
47 8f16cb3e 2024-01-04 benni G_CPUUSAGE,
48 8f16cb3e 2024-01-04 benni G_CPUSPEED,
55 8f16cb3e 2024-01-04 benni struct graph {
56 8f16cb3e 2024-01-04 benni WINDOW *win;
57 8f16cb3e 2024-01-04 benni int log[LOGLEN];
61 8f16cb3e 2024-01-04 benni struct display {
62 8f16cb3e 2024-01-04 benni struct graph g[NGRAPH];
64 8f16cb3e 2024-01-04 benni struct cpu_usage *cpus;
65 8f16cb3e 2024-01-04 benni int fd_apm, ncpu;
66 8f16cb3e 2024-01-04 benni int ngraph;
69 8f16cb3e 2024-01-04 benni bool running;
72 8f16cb3e 2024-01-04 benni struct cpu_usage {
73 8f16cb3e 2024-01-04 benni struct cpustats new;
74 8f16cb3e 2024-01-04 benni struct cpustats old;
75 8f16cb3e 2024-01-04 benni struct cpustats diffs;
78 8f16cb3e 2024-01-04 benni static void
79 8f16cb3e 2024-01-04 benni find_sensors (void)
81 8f16cb3e 2024-01-04 benni for (int i = 0; i < MAXSENSORS; ++i) {
82 8f16cb3e 2024-01-04 benni const int mib[3] = { CTL_HW, HW_SENSORS, i };
83 8f16cb3e 2024-01-04 benni struct sensordev dev;
84 8f16cb3e 2024-01-04 benni size_t sdlen = sizeof dev;
86 8f16cb3e 2024-01-04 benni if (sysctl (mib, 3, &dev, &sdlen, NULL, 0) == -1)
89 8f16cb3e 2024-01-04 benni if (dev.maxnumt[SENSOR_WATTS] > 0) {
90 8f16cb3e 2024-01-04 benni power_sensors[num_power_sensors++] = i;
93 8f16cb3e 2024-01-04 benni if (dev.maxnumt[SENSOR_TEMP] > 0
94 8f16cb3e 2024-01-04 benni && memcmp (dev.xname, "cpu", 3) == 0
95 8f16cb3e 2024-01-04 benni && cputemp_sensor == -1) {
96 8f16cb3e 2024-01-04 benni cputemp_sensor = i;
101 8f16cb3e 2024-01-04 benni static int
102 8f16cb3e 2024-01-04 benni num_cpu (void)
104 8f16cb3e 2024-01-04 benni const int mib[] = { CTL_HW, HW_NCPU };
105 8f16cb3e 2024-01-04 benni int ncpu = 0;
106 8f16cb3e 2024-01-04 benni size_t size = sizeof ncpu;
108 8f16cb3e 2024-01-04 benni if (sysctl (mib, 2, &ncpu, &size, NULL, 0) < 0)
111 8f16cb3e 2024-01-04 benni return ncpu;
114 8f16cb3e 2024-01-04 benni static int64_t
115 8f16cb3e 2024-01-04 benni percentages (struct cpustats *out, struct cpu_usage *cpu)
117 8f16cb3e 2024-01-04 benni int64_t total_change = 0;
119 8f16cb3e 2024-01-04 benni for (int i = 0; i < 6; ++i) {
120 8f16cb3e 2024-01-04 benni int64_t change = cpu->new.cs_time[i] - cpu->old.cs_time[i];
121 8f16cb3e 2024-01-04 benni if (change < 0)
122 8f16cb3e 2024-01-04 benni change = INT64_MAX - cpu->old.cs_time[i] - cpu->new.cs_time[i];
124 8f16cb3e 2024-01-04 benni cpu->diffs.cs_time[i] = change;
125 8f16cb3e 2024-01-04 benni total_change += change;
126 8f16cb3e 2024-01-04 benni cpu->old.cs_time[i] = cpu->new.cs_time[i];
129 8f16cb3e 2024-01-04 benni if (total_change == 0)
130 8f16cb3e 2024-01-04 benni total_change = 1;
132 8f16cb3e 2024-01-04 benni for (int i = 0; i < 6; ++i) {
133 8f16cb3e 2024-01-04 benni out->cs_time[i] = (cpu->diffs.cs_time[i] * 1000 + total_change / 2) / total_change;
136 8f16cb3e 2024-01-04 benni return total_change;
139 8f16cb3e 2024-01-04 benni static int
140 8f16cb3e 2024-01-04 benni cpuspeed (void)
142 8f16cb3e 2024-01-04 benni const int mib[2] = { CTL_HW, HW_CPUSPEED };
143 8f16cb3e 2024-01-04 benni int speed;
144 8f16cb3e 2024-01-04 benni size_t len = sizeof speed;
146 8f16cb3e 2024-01-04 benni if (sysctl (mib, 2, &speed, &len, NULL, 0) == -1)
147 8f16cb3e 2024-01-04 benni return -1;
149 8f16cb3e 2024-01-04 benni return speed;
152 8f16cb3e 2024-01-04 benni static int
153 8f16cb3e 2024-01-04 benni cputemp (void)
155 8f16cb3e 2024-01-04 benni const int mib[5] = { CTL_HW, HW_SENSORS, cputemp_sensor, SENSOR_TEMP, 0 };
156 8f16cb3e 2024-01-04 benni struct sensor sensor;
157 8f16cb3e 2024-01-04 benni size_t len = sizeof sensor;
160 8f16cb3e 2024-01-04 benni ret = sysctl (mib, 5, &sensor, &len, NULL, 0);
162 8f16cb3e 2024-01-04 benni return (ret == -1 || sensor.status & SENSOR_FINVALID) ? -1 : sensor.value / 10000000;
165 8f16cb3e 2024-01-04 benni static int
166 8f16cb3e 2024-01-04 benni cpu (int ncpu, struct cpu_usage *cpus)
168 8f16cb3e 2024-01-04 benni uint64_t sum = 0;
170 8f16cb3e 2024-01-04 benni for (int i = 0; i < ncpu; ++i) {
171 8f16cb3e 2024-01-04 benni const int mib[] = { CTL_KERN, KERN_CPUSTATS, i };
172 8f16cb3e 2024-01-04 benni struct cpustats tmp;
173 8f16cb3e 2024-01-04 benni size_t size = sizeof tmp;
175 8f16cb3e 2024-01-04 benni if (sysctl (mib, 3, &cpus[i].new, &size, NULL, 0) < 0)
178 8f16cb3e 2024-01-04 benni percentages (&tmp, &cpus[i]);
179 8f16cb3e 2024-01-04 benni sum += 1000 - tmp.cs_time[5];
182 8f16cb3e 2024-01-04 benni return sum / ncpu / 10;
185 8f16cb3e 2024-01-04 benni static int
186 8f16cb3e 2024-01-04 benni bat (int fd)
188 8f16cb3e 2024-01-04 benni struct apm_power_info info;
190 8f16cb3e 2024-01-04 benni if (fd < 0 || ioctl (fd, APM_IOC_GETPOWER, &info) != 0)
193 8f16cb3e 2024-01-04 benni return info.battery_life;
196 8f16cb3e 2024-01-04 benni static int
197 8f16cb3e 2024-01-04 benni power (void)
199 8f16cb3e 2024-01-04 benni int64_t total = 0;
201 8f16cb3e 2024-01-04 benni for (int i = 0; i < num_power_sensors; ++i) {
202 8f16cb3e 2024-01-04 benni const int mib[5] = { CTL_HW, HW_SENSORS, power_sensors[i], SENSOR_WATTS, 0 };
203 8f16cb3e 2024-01-04 benni struct sensor sensor;
204 8f16cb3e 2024-01-04 benni size_t slen = sizeof sensor;
206 8f16cb3e 2024-01-04 benni if (sysctl (mib, 5, &sensor, &slen, NULL, 0) == -1)
209 8f16cb3e 2024-01-04 benni if (sensor.flags & SENSOR_FINVALID)
212 8f16cb3e 2024-01-04 benni total += sensor.value;
215 8f16cb3e 2024-01-04 benni return total / 1000;
218 8f16cb3e 2024-01-04 benni static void
219 8f16cb3e 2024-01-04 benni create_win (struct display *dpy)
221 8f16cb3e 2024-01-04 benni int j = 0, w, h, wh;
223 8f16cb3e 2024-01-04 benni dpy->ngraph = 0;
224 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
225 8f16cb3e 2024-01-04 benni if (!dpy->g[i].hide)
226 8f16cb3e 2024-01-04 benni ++dpy->ngraph;
229 8f16cb3e 2024-01-04 benni getmaxyx (stdscr, h, w);
230 8f16cb3e 2024-01-04 benni dpy->width = MIN (w - 2, LOGLEN);
232 8f16cb3e 2024-01-04 benni wh = h / dpy->ngraph;
234 8f16cb3e 2024-01-04 benni if (entries > dpy->width) {
235 8f16cb3e 2024-01-04 benni const int diff = entries - dpy->width;
236 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
237 8f16cb3e 2024-01-04 benni struct graph *g = &dpy->g[i];
238 8f16cb3e 2024-01-04 benni memmove (g->log, g->log + diff, dpy->width * sizeof (g->log[0]));
240 8f16cb3e 2024-01-04 benni entries = dpy->width;
243 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
244 8f16cb3e 2024-01-04 benni struct graph *g = &dpy->g[i];
245 8f16cb3e 2024-01-04 benni if (g->win != NULL)
246 8f16cb3e 2024-01-04 benni delwin (g->win);
247 8f16cb3e 2024-01-04 benni g->win = g->hide ? NULL : newwin (wh, w, 1 + wh * j++, 0);
251 8f16cb3e 2024-01-04 benni static void
252 8f16cb3e 2024-01-04 benni draw_graph (struct graph *g, const char *fmt, int min, int max)
256 8f16cb3e 2024-01-04 benni if (g->hide || g->win == NULL)
259 8f16cb3e 2024-01-04 benni getmaxyx (g->win, h, w);
263 8f16cb3e 2024-01-04 benni wclear (g->win);
265 8f16cb3e 2024-01-04 benni for (int i = 0; i < entries; ++i) {
266 8f16cb3e 2024-01-04 benni int v = g->log[i];
267 8f16cb3e 2024-01-04 benni if (v < min)
269 8f16cb3e 2024-01-04 benni if (v > max)
273 8f16cb3e 2024-01-04 benni if (min < max) {
274 8f16cb3e 2024-01-04 benni for (int i = 0; i < entries; ++i) {
275 8f16cb3e 2024-01-04 benni int v = g->log[i];
276 8f16cb3e 2024-01-04 benni int y = (int)((float)(v - min) / (max - min) * h);
277 8f16cb3e 2024-01-04 benni mvwaddch (g->win, h - y + 1, i + 1, 'X');
281 8f16cb3e 2024-01-04 benni box (g->win, 0, 0);
283 8f16cb3e 2024-01-04 benni mvwprintw (g->win, 0, 3, fmt, g->log[entries - 1]);
284 8f16cb3e 2024-01-04 benni wprintw (g->win, " (min: %d/max: %d)", min, max);
288 8f16cb3e 2024-01-04 benni static void
289 8f16cb3e 2024-01-04 benni draw (struct display *dpy)
293 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_CPUUSAGE], "CPU: %d%%", 0, 100);
294 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_CPUTEMP], "CPU.: %d C", 0, 100);
295 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_CPUSPEED], "CPU: %d MHz", INT_MAX, INT_MIN);
296 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_BATTERY], "BAT: %d%%", 0, 100);
297 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_POWER], "PWR: %dmW", INT_MAX, INT_MIN);
299 8f16cb3e 2024-01-04 benni mvprintw (0, 0, "Panels: ");
300 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
301 8f16cb3e 2024-01-04 benni addch (dpy->g[i].hide ? ' ' : ('1' + i));
304 8f16cb3e 2024-01-04 benni printw (" %c delay=%d", dpy->running ? 'R' : 'S', dpy->delay);
306 8f16cb3e 2024-01-04 benni refresh ();
307 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i)
308 8f16cb3e 2024-01-04 benni wnoutrefresh (dpy->g[i].win);
309 8f16cb3e 2024-01-04 benni doupdate ();
310 8f16cb3e 2024-01-04 benni move (0, 0);
314 8f16cb3e 2024-01-04 benni static void
315 8f16cb3e 2024-01-04 benni update_graph (int w, struct graph *g, int value)
317 8f16cb3e 2024-01-04 benni if (entries == w) {
318 8f16cb3e 2024-01-04 benni memmove (g->log, g->log + 1, (LOGLEN - 1) * sizeof (g->log[0]));
319 8f16cb3e 2024-01-04 benni g->log[entries - 1] = value;
321 8f16cb3e 2024-01-04 benni g->log[entries] = value;
323 8f16cb3e 2024-01-04 benni fprintf (stderr, "entries=%d, w=%d, value=%d\n", entries, w, value);
326 8f16cb3e 2024-01-04 benni static void
327 8f16cb3e 2024-01-04 benni update (struct display *dpy)
329 8f16cb3e 2024-01-04 benni int w = dpy->width;
331 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_CPUUSAGE], cpu (dpy->ncpu, dpy->cpus));
332 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_CPUTEMP], cputemp ());
333 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_CPUSPEED], cpuspeed ());
334 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_BATTERY], bat (dpy->fd_apm));
335 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_POWER], power ());
337 8f16cb3e 2024-01-04 benni entries++;
338 8f16cb3e 2024-01-04 benni if (entries > w)
339 8f16cb3e 2024-01-04 benni entries = w;
343 8f16cb3e 2024-01-04 benni main (int argc, char *argv[])
345 8f16cb3e 2024-01-04 benni struct display dpy;
347 c440ada7 2024-01-04 benni // pledge(2) doesn't work, because apmtop(1) needs sysctl(2).
348 c440ada7 2024-01-04 benni unveil ("/usr/share/terminfo", "r");
349 c440ada7 2024-01-04 benni unveil (NULL, NULL);
351 8f16cb3e 2024-01-04 benni memset (&dpy, 0, sizeof (dpy));
352 8f16cb3e 2024-01-04 benni dpy.ncpu = num_cpu ();
353 8f16cb3e 2024-01-04 benni dpy.cpus = calloc (dpy.ncpu, sizeof (struct cpu_usage));
354 8f16cb3e 2024-01-04 benni dpy.fd_apm = open ("/dev/apm", O_RDONLY);
355 8f16cb3e 2024-01-04 benni dpy.delay = 10;
356 8f16cb3e 2024-01-04 benni dpy.running = true;
357 8f16cb3e 2024-01-04 benni find_sensors ();
359 8f16cb3e 2024-01-04 benni setlocale (LC_ALL, "");
360 8f16cb3e 2024-01-04 benni initscr ();
361 8f16cb3e 2024-01-04 benni cbreak ();
362 8f16cb3e 2024-01-04 benni noecho ();
363 8f16cb3e 2024-01-04 benni intrflush (stdscr, FALSE);
364 8f16cb3e 2024-01-04 benni keypad (stdscr, TRUE);
365 8f16cb3e 2024-01-04 benni halfdelay (dpy.delay);
367 8f16cb3e 2024-01-04 benni entries = 0;
368 8f16cb3e 2024-01-04 benni create_win (&dpy);
369 8f16cb3e 2024-01-04 benni update (&dpy);
370 8f16cb3e 2024-01-04 benni draw (&dpy);
372 8f16cb3e 2024-01-04 benni while (1) {
373 8f16cb3e 2024-01-04 benni struct graph *g;
376 8f16cb3e 2024-01-04 benni ch = getch ();
378 8f16cb3e 2024-01-04 benni switch (ch) {
380 8f16cb3e 2024-01-04 benni goto done;
381 8f16cb3e 2024-01-04 benni case KEY_RESIZE:
382 8f16cb3e 2024-01-04 benni create_win (&dpy);
389 8f16cb3e 2024-01-04 benni g = &dpy.g[ch - '1'];
390 8f16cb3e 2024-01-04 benni if (dpy.ngraph == 1 && !g->hide)
392 8f16cb3e 2024-01-04 benni g->hide = !g->hide;
393 8f16cb3e 2024-01-04 benni create_win (&dpy);
396 8f16cb3e 2024-01-04 benni ++dpy.delay;
397 8f16cb3e 2024-01-04 benni halfdelay (dpy.delay);
400 8f16cb3e 2024-01-04 benni if (dpy.delay > 1)
401 8f16cb3e 2024-01-04 benni --dpy.delay;
402 8f16cb3e 2024-01-04 benni halfdelay (dpy.delay);
405 8f16cb3e 2024-01-04 benni dpy.running = !dpy.running;
408 8f16cb3e 2024-01-04 benni if (dpy.running)
409 8f16cb3e 2024-01-04 benni update (&dpy);
413 8f16cb3e 2024-01-04 benni draw (&dpy);
417 8f16cb3e 2024-01-04 benni endwin ();