2 * Copyright (c) 2024 Benjamin Stürz <benni@stuerz.xyz>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <sys/types.h>
17 #include <machine/apmvar.h>
18 #include <sys/signal.h>
19 #include <sys/sched.h>
20 #include <sys/sysctl.h>
21 #include <sys/ioctl.h>
22 #include <sys/sensors.h>
39 #define MIN(a, b) ((a) < (b) ? (a) : (b))
41 static int power_sensors[MAXSENSORS];
42 static int cputemp_sensor = -1;
43 static int num_power_sensors = 0;
44 static int entries = 0;
62 struct graph g[NGRAPH];
64 struct cpu_usage *cpus;
75 struct cpustats diffs;
81 for (int i = 0; i < MAXSENSORS; ++i) {
82 const int mib[3] = { CTL_HW, HW_SENSORS, i };
84 size_t sdlen = sizeof dev;
86 if (sysctl (mib, 3, &dev, &sdlen, NULL, 0) == -1)
89 if (dev.maxnumt[SENSOR_WATTS] > 0) {
90 power_sensors[num_power_sensors++] = i;
93 if (dev.maxnumt[SENSOR_TEMP] > 0
94 && memcmp (dev.xname, "cpu", 3) == 0
95 && cputemp_sensor == -1) {
104 const int mib[] = { CTL_HW, HW_NCPU };
106 size_t size = sizeof ncpu;
108 if (sysctl (mib, 2, &ncpu, &size, NULL, 0) < 0)
115 percentages (struct cpustats *out, struct cpu_usage *cpu)
117 int64_t total_change = 0;
119 for (int i = 0; i < 6; ++i) {
120 int64_t change = cpu->new.cs_time[i] - cpu->old.cs_time[i];
122 change = INT64_MAX - cpu->old.cs_time[i] - cpu->new.cs_time[i];
124 cpu->diffs.cs_time[i] = change;
125 total_change += change;
126 cpu->old.cs_time[i] = cpu->new.cs_time[i];
129 if (total_change == 0)
132 for (int i = 0; i < 6; ++i) {
133 out->cs_time[i] = (cpu->diffs.cs_time[i] * 1000 + total_change / 2) / total_change;
142 const int mib[2] = { CTL_HW, HW_CPUSPEED };
144 size_t len = sizeof speed;
146 if (sysctl (mib, 2, &speed, &len, NULL, 0) == -1)
155 const int mib[5] = { CTL_HW, HW_SENSORS, cputemp_sensor, SENSOR_TEMP, 0 };
156 struct sensor sensor;
157 size_t len = sizeof sensor;
160 ret = sysctl (mib, 5, &sensor, &len, NULL, 0);
162 return (ret == -1 || sensor.status & SENSOR_FINVALID) ? -1 : sensor.value / 10000000;
166 cpu (int ncpu, struct cpu_usage *cpus)
170 for (int i = 0; i < ncpu; ++i) {
171 const int mib[] = { CTL_KERN, KERN_CPUSTATS, i };
173 size_t size = sizeof tmp;
175 if (sysctl (mib, 3, &cpus[i].new, &size, NULL, 0) < 0)
178 percentages (&tmp, &cpus[i]);
179 sum += 1000 - tmp.cs_time[5];
182 return sum / ncpu / 10;
188 struct apm_power_info info;
190 if (fd < 0 || ioctl (fd, APM_IOC_GETPOWER, &info) != 0)
193 return info.battery_life;
201 for (int i = 0; i < num_power_sensors; ++i) {
202 const int mib[5] = { CTL_HW, HW_SENSORS, power_sensors[i], SENSOR_WATTS, 0 };
203 struct sensor sensor;
204 size_t slen = sizeof sensor;
206 if (sysctl (mib, 5, &sensor, &slen, NULL, 0) == -1)
209 if (sensor.flags & SENSOR_FINVALID)
212 total += sensor.value;
219 create_win (struct display *dpy)
224 for (int i = 0; i < NGRAPH; ++i) {
229 getmaxyx (stdscr, h, w);
230 dpy->width = MIN (w - 2, LOGLEN);
232 wh = h / dpy->ngraph;
234 if (entries > dpy->width) {
235 const int diff = entries - dpy->width;
236 for (int i = 0; i < NGRAPH; ++i) {
237 struct graph *g = &dpy->g[i];
238 memmove (g->log, g->log + diff, dpy->width * sizeof (g->log[0]));
240 entries = dpy->width;
243 for (int i = 0; i < NGRAPH; ++i) {
244 struct graph *g = &dpy->g[i];
247 g->win = g->hide ? NULL : newwin (wh, w, 1 + wh * j++, 0);
252 draw_graph (struct graph *g, const char *fmt, int min, int max)
256 if (g->hide || g->win == NULL)
259 getmaxyx (g->win, h, w);
265 for (int i = 0; i < entries; ++i) {
274 for (int i = 0; i < entries; ++i) {
276 int y = (int)((float)(v - min) / (max - min) * h);
277 mvwaddch (g->win, h - y + 1, i + 1, 'X');
283 mvwprintw (g->win, 0, 3, fmt, g->log[entries - 1]);
284 wprintw (g->win, " (min: %d/max: %d)", min, max);
289 draw (struct display *dpy)
293 draw_graph (&dpy->g[G_CPUUSAGE], "CPU: %d%%", 0, 100);
294 draw_graph (&dpy->g[G_CPUTEMP], "CPU.: %d C", 0, 100);
295 draw_graph (&dpy->g[G_CPUSPEED], "CPU: %d MHz", INT_MAX, INT_MIN);
296 draw_graph (&dpy->g[G_BATTERY], "BAT: %d%%", 0, 100);
297 draw_graph (&dpy->g[G_POWER], "PWR: %dmW", INT_MAX, INT_MIN);
299 mvprintw (0, 0, "Panels: ");
300 for (int i = 0; i < NGRAPH; ++i) {
301 addch (dpy->g[i].hide ? ' ' : ('1' + i));
304 printw (" %c delay=%d", dpy->running ? 'R' : 'S', dpy->delay);
307 for (int i = 0; i < NGRAPH; ++i)
308 wnoutrefresh (dpy->g[i].win);
315 update_graph (int w, struct graph *g, int value)
318 memmove (g->log, g->log + 1, (LOGLEN - 1) * sizeof (g->log[0]));
319 g->log[entries - 1] = value;
321 g->log[entries] = value;
323 fprintf (stderr, "entries=%d, w=%d, value=%d\n", entries, w, value);
327 update (struct display *dpy)
331 update_graph (w, &dpy->g[G_CPUUSAGE], cpu (dpy->ncpu, dpy->cpus));
332 update_graph (w, &dpy->g[G_CPUTEMP], cputemp ());
333 update_graph (w, &dpy->g[G_CPUSPEED], cpuspeed ());
334 update_graph (w, &dpy->g[G_BATTERY], bat (dpy->fd_apm));
335 update_graph (w, &dpy->g[G_POWER], power ());
343 main (int argc, char *argv[])
347 // pledge(2) doesn't work, because apmtop(1) needs sysctl(2).
348 unveil ("/usr/share/terminfo", "r");
351 memset (&dpy, 0, sizeof (dpy));
352 dpy.ncpu = num_cpu ();
353 dpy.cpus = calloc (dpy.ncpu, sizeof (struct cpu_usage));
354 dpy.fd_apm = open ("/dev/apm", O_RDONLY);
359 setlocale (LC_ALL, "");
363 intrflush (stdscr, FALSE);
364 keypad (stdscr, TRUE);
365 halfdelay (dpy.delay);
389 g = &dpy.g[ch - '1'];
390 if (dpy.ngraph == 1 && !g->hide)
397 halfdelay (dpy.delay);
402 halfdelay (dpy.delay);
405 dpy.running = !dpy.running;