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 f6ed57b5 2024-01-04 benni #include <sys/time.h>
18 8f16cb3e 2024-01-04 benni #include <sys/ioctl.h>
19 f6ed57b5 2024-01-04 benni #include <sys/sched.h>
20 8f16cb3e 2024-01-04 benni #include <sys/sensors.h>
21 f6ed57b5 2024-01-04 benni #include <sys/sysctl.h>
22 f6ed57b5 2024-01-04 benni #include <machine/apmvar.h>
24 f6ed57b5 2024-01-04 benni #include <err.h>
25 f6ed57b5 2024-01-04 benni #include <fcntl.h>
26 8f16cb3e 2024-01-04 benni #include <limits.h>
27 f6ed57b5 2024-01-04 benni #include <locale.h>
28 f6ed57b5 2024-01-04 benni #include <ncurses.h>
29 f6ed57b5 2024-01-04 benni #include <stdbool.h>
30 8f16cb3e 2024-01-04 benni #include <stdint.h>
31 8f16cb3e 2024-01-04 benni #include <stdio.h>
32 f6ed57b5 2024-01-04 benni #include <stdlib.h>
33 f6ed57b5 2024-01-04 benni #include <string.h>
34 f6ed57b5 2024-01-04 benni #include <unistd.h>
36 8f16cb3e 2024-01-04 benni #define MAXSENSORS 16
37 8f16cb3e 2024-01-04 benni #define LOGLEN 512
38 8f16cb3e 2024-01-04 benni #define MIN(a, b) ((a) < (b) ? (a) : (b))
40 8f16cb3e 2024-01-04 benni static int power_sensors[MAXSENSORS];
41 8f16cb3e 2024-01-04 benni static int cputemp_sensor = -1;
42 8f16cb3e 2024-01-04 benni static int num_power_sensors = 0;
43 8f16cb3e 2024-01-04 benni static int entries = 0;
46 8f16cb3e 2024-01-04 benni G_CPUUSAGE,
47 8f16cb3e 2024-01-04 benni G_CPUSPEED,
54 8f16cb3e 2024-01-04 benni struct graph {
55 8f16cb3e 2024-01-04 benni WINDOW *win;
56 8f16cb3e 2024-01-04 benni int log[LOGLEN];
60 8f16cb3e 2024-01-04 benni struct display {
61 8f16cb3e 2024-01-04 benni struct graph g[NGRAPH];
63 8f16cb3e 2024-01-04 benni struct cpu_usage *cpus;
64 8f16cb3e 2024-01-04 benni int fd_apm, ncpu;
65 8f16cb3e 2024-01-04 benni int ngraph;
68 8f16cb3e 2024-01-04 benni bool running;
71 8f16cb3e 2024-01-04 benni struct cpu_usage {
72 8f16cb3e 2024-01-04 benni struct cpustats new;
73 8f16cb3e 2024-01-04 benni struct cpustats old;
74 8f16cb3e 2024-01-04 benni struct cpustats diffs;
77 8f16cb3e 2024-01-04 benni static void
78 8f16cb3e 2024-01-04 benni find_sensors (void)
80 8f16cb3e 2024-01-04 benni for (int i = 0; i < MAXSENSORS; ++i) {
81 8f16cb3e 2024-01-04 benni const int mib[3] = { CTL_HW, HW_SENSORS, i };
82 8f16cb3e 2024-01-04 benni struct sensordev dev;
83 8f16cb3e 2024-01-04 benni size_t sdlen = sizeof dev;
85 8f16cb3e 2024-01-04 benni if (sysctl (mib, 3, &dev, &sdlen, NULL, 0) == -1)
88 8f16cb3e 2024-01-04 benni if (dev.maxnumt[SENSOR_WATTS] > 0) {
89 8f16cb3e 2024-01-04 benni power_sensors[num_power_sensors++] = i;
92 8f16cb3e 2024-01-04 benni if (dev.maxnumt[SENSOR_TEMP] > 0
93 8f16cb3e 2024-01-04 benni && memcmp (dev.xname, "cpu", 3) == 0
94 8f16cb3e 2024-01-04 benni && cputemp_sensor == -1) {
95 8f16cb3e 2024-01-04 benni cputemp_sensor = i;
100 8f16cb3e 2024-01-04 benni static int
101 8f16cb3e 2024-01-04 benni num_cpu (void)
103 8f16cb3e 2024-01-04 benni const int mib[] = { CTL_HW, HW_NCPU };
104 8f16cb3e 2024-01-04 benni int ncpu = 0;
105 8f16cb3e 2024-01-04 benni size_t size = sizeof ncpu;
107 8f16cb3e 2024-01-04 benni if (sysctl (mib, 2, &ncpu, &size, NULL, 0) < 0)
110 8f16cb3e 2024-01-04 benni return ncpu;
113 8f16cb3e 2024-01-04 benni static int64_t
114 8f16cb3e 2024-01-04 benni percentages (struct cpustats *out, struct cpu_usage *cpu)
116 8f16cb3e 2024-01-04 benni int64_t total_change = 0;
118 8f16cb3e 2024-01-04 benni for (int i = 0; i < 6; ++i) {
119 8f16cb3e 2024-01-04 benni int64_t change = cpu->new.cs_time[i] - cpu->old.cs_time[i];
120 8f16cb3e 2024-01-04 benni if (change < 0)
121 8f16cb3e 2024-01-04 benni change = INT64_MAX - cpu->old.cs_time[i] - cpu->new.cs_time[i];
123 8f16cb3e 2024-01-04 benni cpu->diffs.cs_time[i] = change;
124 8f16cb3e 2024-01-04 benni total_change += change;
125 8f16cb3e 2024-01-04 benni cpu->old.cs_time[i] = cpu->new.cs_time[i];
128 8f16cb3e 2024-01-04 benni if (total_change == 0)
129 8f16cb3e 2024-01-04 benni total_change = 1;
131 8f16cb3e 2024-01-04 benni for (int i = 0; i < 6; ++i) {
132 8f16cb3e 2024-01-04 benni out->cs_time[i] = (cpu->diffs.cs_time[i] * 1000 + total_change / 2) / total_change;
135 8f16cb3e 2024-01-04 benni return total_change;
138 8f16cb3e 2024-01-04 benni static int
139 8f16cb3e 2024-01-04 benni cpuspeed (void)
141 8f16cb3e 2024-01-04 benni const int mib[2] = { CTL_HW, HW_CPUSPEED };
142 8f16cb3e 2024-01-04 benni int speed;
143 8f16cb3e 2024-01-04 benni size_t len = sizeof speed;
145 8f16cb3e 2024-01-04 benni if (sysctl (mib, 2, &speed, &len, NULL, 0) == -1)
146 8f16cb3e 2024-01-04 benni return -1;
148 8f16cb3e 2024-01-04 benni return speed;
151 8f16cb3e 2024-01-04 benni static int
152 8f16cb3e 2024-01-04 benni cputemp (void)
154 8f16cb3e 2024-01-04 benni const int mib[5] = { CTL_HW, HW_SENSORS, cputemp_sensor, SENSOR_TEMP, 0 };
155 8f16cb3e 2024-01-04 benni struct sensor sensor;
156 8f16cb3e 2024-01-04 benni size_t len = sizeof sensor;
159 8f16cb3e 2024-01-04 benni ret = sysctl (mib, 5, &sensor, &len, NULL, 0);
161 8f16cb3e 2024-01-04 benni return (ret == -1 || sensor.status & SENSOR_FINVALID) ? -1 : sensor.value / 10000000;
164 8f16cb3e 2024-01-04 benni static int
165 8f16cb3e 2024-01-04 benni cpu (int ncpu, struct cpu_usage *cpus)
167 8f16cb3e 2024-01-04 benni uint64_t sum = 0;
169 8f16cb3e 2024-01-04 benni for (int i = 0; i < ncpu; ++i) {
170 8f16cb3e 2024-01-04 benni const int mib[] = { CTL_KERN, KERN_CPUSTATS, i };
171 8f16cb3e 2024-01-04 benni struct cpustats tmp;
172 8f16cb3e 2024-01-04 benni size_t size = sizeof tmp;
174 8f16cb3e 2024-01-04 benni if (sysctl (mib, 3, &cpus[i].new, &size, NULL, 0) < 0)
177 8f16cb3e 2024-01-04 benni percentages (&tmp, &cpus[i]);
178 8f16cb3e 2024-01-04 benni sum += 1000 - tmp.cs_time[5];
181 8f16cb3e 2024-01-04 benni return sum / ncpu / 10;
184 8f16cb3e 2024-01-04 benni static int
185 8f16cb3e 2024-01-04 benni bat (int fd)
187 8f16cb3e 2024-01-04 benni struct apm_power_info info;
189 8f16cb3e 2024-01-04 benni if (fd < 0 || ioctl (fd, APM_IOC_GETPOWER, &info) != 0)
192 8f16cb3e 2024-01-04 benni return info.battery_life;
195 8f16cb3e 2024-01-04 benni static int
196 8f16cb3e 2024-01-04 benni power (void)
198 8f16cb3e 2024-01-04 benni int64_t total = 0;
200 8f16cb3e 2024-01-04 benni for (int i = 0; i < num_power_sensors; ++i) {
201 8f16cb3e 2024-01-04 benni const int mib[5] = { CTL_HW, HW_SENSORS, power_sensors[i], SENSOR_WATTS, 0 };
202 8f16cb3e 2024-01-04 benni struct sensor sensor;
203 8f16cb3e 2024-01-04 benni size_t slen = sizeof sensor;
205 8f16cb3e 2024-01-04 benni if (sysctl (mib, 5, &sensor, &slen, NULL, 0) == -1)
208 8f16cb3e 2024-01-04 benni if (sensor.flags & SENSOR_FINVALID)
211 8f16cb3e 2024-01-04 benni total += sensor.value;
214 8f16cb3e 2024-01-04 benni return total / 1000;
217 8f16cb3e 2024-01-04 benni static void
218 8f16cb3e 2024-01-04 benni create_win (struct display *dpy)
220 8f16cb3e 2024-01-04 benni int j = 0, w, h, wh;
222 8f16cb3e 2024-01-04 benni dpy->ngraph = 0;
223 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
224 8f16cb3e 2024-01-04 benni if (!dpy->g[i].hide)
225 8f16cb3e 2024-01-04 benni ++dpy->ngraph;
228 8f16cb3e 2024-01-04 benni getmaxyx (stdscr, h, w);
229 8f16cb3e 2024-01-04 benni dpy->width = MIN (w - 2, LOGLEN);
231 8f16cb3e 2024-01-04 benni wh = h / dpy->ngraph;
233 8f16cb3e 2024-01-04 benni if (entries > dpy->width) {
234 8f16cb3e 2024-01-04 benni const int diff = entries - dpy->width;
235 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
236 8f16cb3e 2024-01-04 benni struct graph *g = &dpy->g[i];
237 8f16cb3e 2024-01-04 benni memmove (g->log, g->log + diff, dpy->width * sizeof (g->log[0]));
239 8f16cb3e 2024-01-04 benni entries = dpy->width;
242 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
243 8f16cb3e 2024-01-04 benni struct graph *g = &dpy->g[i];
244 8f16cb3e 2024-01-04 benni if (g->win != NULL)
245 8f16cb3e 2024-01-04 benni delwin (g->win);
246 8f16cb3e 2024-01-04 benni g->win = g->hide ? NULL : newwin (wh, w, 1 + wh * j++, 0);
250 8f16cb3e 2024-01-04 benni static void
251 8f16cb3e 2024-01-04 benni draw_graph (struct graph *g, const char *fmt, int min, int max)
255 8f16cb3e 2024-01-04 benni if (g->hide || g->win == NULL)
258 8f16cb3e 2024-01-04 benni getmaxyx (g->win, h, w);
262 8f16cb3e 2024-01-04 benni wclear (g->win);
264 8f16cb3e 2024-01-04 benni for (int i = 0; i < entries; ++i) {
265 8f16cb3e 2024-01-04 benni int v = g->log[i];
266 8f16cb3e 2024-01-04 benni if (v < min)
268 8f16cb3e 2024-01-04 benni if (v > max)
272 8f16cb3e 2024-01-04 benni if (min < max) {
273 8f16cb3e 2024-01-04 benni for (int i = 0; i < entries; ++i) {
274 8f16cb3e 2024-01-04 benni int v = g->log[i];
275 8f16cb3e 2024-01-04 benni int y = (int)((float)(v - min) / (max - min) * h);
276 8f16cb3e 2024-01-04 benni mvwaddch (g->win, h - y + 1, i + 1, 'X');
280 8f16cb3e 2024-01-04 benni box (g->win, 0, 0);
282 8f16cb3e 2024-01-04 benni mvwprintw (g->win, 0, 3, fmt, g->log[entries - 1]);
283 8f16cb3e 2024-01-04 benni wprintw (g->win, " (min: %d/max: %d)", min, max);
287 8f16cb3e 2024-01-04 benni static void
288 8f16cb3e 2024-01-04 benni draw (struct display *dpy)
292 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_CPUUSAGE], "CPU: %d%%", 0, 100);
293 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_CPUTEMP], "CPU.: %d C", 0, 100);
294 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_CPUSPEED], "CPU: %d MHz", INT_MAX, INT_MIN);
295 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_BATTERY], "BAT: %d%%", 0, 100);
296 8f16cb3e 2024-01-04 benni draw_graph (&dpy->g[G_POWER], "PWR: %dmW", INT_MAX, INT_MIN);
298 8f16cb3e 2024-01-04 benni mvprintw (0, 0, "Panels: ");
299 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i) {
300 8f16cb3e 2024-01-04 benni addch (dpy->g[i].hide ? ' ' : ('1' + i));
303 8f16cb3e 2024-01-04 benni printw (" %c delay=%d", dpy->running ? 'R' : 'S', dpy->delay);
305 8f16cb3e 2024-01-04 benni refresh ();
306 8f16cb3e 2024-01-04 benni for (int i = 0; i < NGRAPH; ++i)
307 8f16cb3e 2024-01-04 benni wnoutrefresh (dpy->g[i].win);
308 8f16cb3e 2024-01-04 benni doupdate ();
309 8f16cb3e 2024-01-04 benni move (0, 0);
313 8f16cb3e 2024-01-04 benni static void
314 8f16cb3e 2024-01-04 benni update_graph (int w, struct graph *g, int value)
316 8f16cb3e 2024-01-04 benni if (entries == w) {
317 8f16cb3e 2024-01-04 benni memmove (g->log, g->log + 1, (LOGLEN - 1) * sizeof (g->log[0]));
318 8f16cb3e 2024-01-04 benni g->log[entries - 1] = value;
320 8f16cb3e 2024-01-04 benni g->log[entries] = value;
322 8f16cb3e 2024-01-04 benni fprintf (stderr, "entries=%d, w=%d, value=%d\n", entries, w, value);
325 8f16cb3e 2024-01-04 benni static void
326 8f16cb3e 2024-01-04 benni update (struct display *dpy)
328 8f16cb3e 2024-01-04 benni int w = dpy->width;
330 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_CPUUSAGE], cpu (dpy->ncpu, dpy->cpus));
331 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_CPUTEMP], cputemp ());
332 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_CPUSPEED], cpuspeed ());
333 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_BATTERY], bat (dpy->fd_apm));
334 8f16cb3e 2024-01-04 benni update_graph (w, &dpy->g[G_POWER], power ());
336 8f16cb3e 2024-01-04 benni entries++;
337 8f16cb3e 2024-01-04 benni if (entries > w)
338 8f16cb3e 2024-01-04 benni entries = w;
342 8f16cb3e 2024-01-04 benni main (int argc, char *argv[])
344 8f16cb3e 2024-01-04 benni struct display dpy;
346 c440ada7 2024-01-04 benni // pledge(2) doesn't work, because apmtop(1) needs sysctl(2).
347 c440ada7 2024-01-04 benni unveil ("/usr/share/terminfo", "r");
348 c440ada7 2024-01-04 benni unveil (NULL, NULL);
350 f6ed57b5 2024-01-04 benni // Configure the "display".
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 f6ed57b5 2024-01-04 benni // Configure ncurses.
360 8f16cb3e 2024-01-04 benni setlocale (LC_ALL, "");
361 8f16cb3e 2024-01-04 benni initscr ();
362 8f16cb3e 2024-01-04 benni cbreak ();
363 8f16cb3e 2024-01-04 benni noecho ();
364 8f16cb3e 2024-01-04 benni intrflush (stdscr, FALSE);
365 8f16cb3e 2024-01-04 benni keypad (stdscr, TRUE);
366 8f16cb3e 2024-01-04 benni halfdelay (dpy.delay);
368 f6ed57b5 2024-01-04 benni // Bootstrap the process.
369 8f16cb3e 2024-01-04 benni entries = 0;
370 8f16cb3e 2024-01-04 benni create_win (&dpy);
371 8f16cb3e 2024-01-04 benni update (&dpy);
372 8f16cb3e 2024-01-04 benni draw (&dpy);
374 8f16cb3e 2024-01-04 benni while (1) {
375 8f16cb3e 2024-01-04 benni struct graph *g;
378 8f16cb3e 2024-01-04 benni ch = getch ();
380 8f16cb3e 2024-01-04 benni switch (ch) {
382 8f16cb3e 2024-01-04 benni goto done;
383 8f16cb3e 2024-01-04 benni case KEY_RESIZE:
384 8f16cb3e 2024-01-04 benni create_win (&dpy);
391 8f16cb3e 2024-01-04 benni g = &dpy.g[ch - '1'];
392 8f16cb3e 2024-01-04 benni if (dpy.ngraph == 1 && !g->hide)
394 8f16cb3e 2024-01-04 benni g->hide = !g->hide;
395 8f16cb3e 2024-01-04 benni create_win (&dpy);
398 8f16cb3e 2024-01-04 benni ++dpy.delay;
399 8f16cb3e 2024-01-04 benni halfdelay (dpy.delay);
402 8f16cb3e 2024-01-04 benni if (dpy.delay > 1)
403 8f16cb3e 2024-01-04 benni --dpy.delay;
404 8f16cb3e 2024-01-04 benni halfdelay (dpy.delay);
407 8f16cb3e 2024-01-04 benni dpy.running = !dpy.running;
410 8f16cb3e 2024-01-04 benni if (dpy.running)
411 8f16cb3e 2024-01-04 benni update (&dpy);
415 8f16cb3e 2024-01-04 benni draw (&dpy);
419 8f16cb3e 2024-01-04 benni endwin ();