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>
18 #include <sys/ioctl.h>
19 #include <sys/sched.h>
20 #include <sys/sensors.h>
21 #include <sys/sysctl.h>
22 #include <machine/apmvar.h>
38 #define MIN(a, b) ((a) < (b) ? (a) : (b))
40 static int power_sensors[MAXSENSORS];
41 static int cputemp_sensor = -1;
42 static int num_power_sensors = 0;
43 static int entries = 0;
61 struct graph g[NGRAPH];
63 struct cpu_usage *cpus;
74 struct cpustats diffs;
80 for (int i = 0; i < MAXSENSORS; ++i) {
81 const int mib[3] = { CTL_HW, HW_SENSORS, i };
83 size_t sdlen = sizeof dev;
85 if (sysctl (mib, 3, &dev, &sdlen, NULL, 0) == -1)
88 if (dev.maxnumt[SENSOR_WATTS] > 0) {
89 power_sensors[num_power_sensors++] = i;
92 if (dev.maxnumt[SENSOR_TEMP] > 0
93 && memcmp (dev.xname, "cpu", 3) == 0
94 && cputemp_sensor == -1) {
103 const int mib[] = { CTL_HW, HW_NCPU };
105 size_t size = sizeof ncpu;
107 if (sysctl (mib, 2, &ncpu, &size, NULL, 0) < 0)
114 percentages (struct cpustats *out, struct cpu_usage *cpu)
116 int64_t total_change = 0;
118 for (int i = 0; i < 6; ++i) {
119 int64_t change = cpu->new.cs_time[i] - cpu->old.cs_time[i];
121 change = INT64_MAX - cpu->old.cs_time[i] - cpu->new.cs_time[i];
123 cpu->diffs.cs_time[i] = change;
124 total_change += change;
125 cpu->old.cs_time[i] = cpu->new.cs_time[i];
128 if (total_change == 0)
131 for (int i = 0; i < 6; ++i) {
132 out->cs_time[i] = (cpu->diffs.cs_time[i] * 1000 + total_change / 2) / total_change;
141 const int mib[2] = { CTL_HW, HW_CPUSPEED };
143 size_t len = sizeof speed;
145 if (sysctl (mib, 2, &speed, &len, NULL, 0) == -1)
154 const int mib[5] = { CTL_HW, HW_SENSORS, cputemp_sensor, SENSOR_TEMP, 0 };
155 struct sensor sensor;
156 size_t len = sizeof sensor;
159 ret = sysctl (mib, 5, &sensor, &len, NULL, 0);
161 return (ret == -1 || sensor.status & SENSOR_FINVALID) ? -1 : sensor.value / 10000000;
165 cpu (int ncpu, struct cpu_usage *cpus)
169 for (int i = 0; i < ncpu; ++i) {
170 const int mib[] = { CTL_KERN, KERN_CPUSTATS, i };
172 size_t size = sizeof tmp;
174 if (sysctl (mib, 3, &cpus[i].new, &size, NULL, 0) < 0)
177 percentages (&tmp, &cpus[i]);
178 sum += 1000 - tmp.cs_time[5];
181 return sum / ncpu / 10;
187 struct apm_power_info info;
189 if (fd < 0 || ioctl (fd, APM_IOC_GETPOWER, &info) != 0)
192 return info.battery_life;
200 for (int i = 0; i < num_power_sensors; ++i) {
201 const int mib[5] = { CTL_HW, HW_SENSORS, power_sensors[i], SENSOR_WATTS, 0 };
202 struct sensor sensor;
203 size_t slen = sizeof sensor;
205 if (sysctl (mib, 5, &sensor, &slen, NULL, 0) == -1)
208 if (sensor.flags & SENSOR_FINVALID)
211 total += sensor.value;
218 create_win (struct display *dpy)
223 for (int i = 0; i < NGRAPH; ++i) {
228 getmaxyx (stdscr, h, w);
229 dpy->width = MIN (w - 2, LOGLEN);
231 wh = h / dpy->ngraph;
233 if (entries > dpy->width) {
234 const int diff = entries - dpy->width;
235 for (int i = 0; i < NGRAPH; ++i) {
236 struct graph *g = &dpy->g[i];
237 memmove (g->log, g->log + diff, dpy->width * sizeof (g->log[0]));
239 entries = dpy->width;
242 for (int i = 0; i < NGRAPH; ++i) {
243 struct graph *g = &dpy->g[i];
246 g->win = g->hide ? NULL : newwin (wh, w, 1 + wh * j++, 0);
251 draw_graph (struct graph *g, const char *fmt, int min, int max)
255 if (g->hide || g->win == NULL)
258 getmaxyx (g->win, h, w);
264 for (int i = 0; i < entries; ++i) {
273 for (int i = 0; i < entries; ++i) {
275 int y = (int)((float)(v - min) / (max - min) * h);
276 mvwaddch (g->win, h - y + 1, i + 1, 'X');
282 mvwprintw (g->win, 0, 3, fmt, g->log[entries - 1]);
283 wprintw (g->win, " (min: %d/max: %d)", min, max);
288 draw (struct display *dpy)
292 draw_graph (&dpy->g[G_CPUUSAGE], "CPU: %d%%", 0, 100);
293 draw_graph (&dpy->g[G_CPUTEMP], "CPU.: %d C", 0, 100);
294 draw_graph (&dpy->g[G_CPUSPEED], "CPU: %d MHz", INT_MAX, INT_MIN);
295 draw_graph (&dpy->g[G_BATTERY], "BAT: %d%%", 0, 100);
296 draw_graph (&dpy->g[G_POWER], "PWR: %dmW", INT_MAX, INT_MIN);
298 mvprintw (0, 0, "Panels: ");
299 for (int i = 0; i < NGRAPH; ++i) {
300 addch (dpy->g[i].hide ? ' ' : ('1' + i));
303 printw (" %c delay=%d", dpy->running ? 'R' : 'S', dpy->delay);
306 for (int i = 0; i < NGRAPH; ++i)
307 wnoutrefresh (dpy->g[i].win);
314 update_graph (int w, struct graph *g, int value)
317 memmove (g->log, g->log + 1, (LOGLEN - 1) * sizeof (g->log[0]));
318 g->log[entries - 1] = value;
320 g->log[entries] = value;
322 fprintf (stderr, "entries=%d, w=%d, value=%d\n", entries, w, value);
326 update (struct display *dpy)
330 update_graph (w, &dpy->g[G_CPUUSAGE], cpu (dpy->ncpu, dpy->cpus));
331 update_graph (w, &dpy->g[G_CPUTEMP], cputemp ());
332 update_graph (w, &dpy->g[G_CPUSPEED], cpuspeed ());
333 update_graph (w, &dpy->g[G_BATTERY], bat (dpy->fd_apm));
334 update_graph (w, &dpy->g[G_POWER], power ());
342 main (int argc, char *argv[])
346 // pledge(2) doesn't work, because apmtop(1) needs sysctl(2).
347 unveil ("/usr/share/terminfo", "r");
350 // Configure the "display".
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 // Configure ncurses.
360 setlocale (LC_ALL, "");
364 intrflush (stdscr, FALSE);
365 keypad (stdscr, TRUE);
366 halfdelay (dpy.delay);
368 // Bootstrap the process.
391 g = &dpy.g[ch - '1'];
392 if (dpy.ngraph == 1 && !g->hide)
399 halfdelay (dpy.delay);
404 halfdelay (dpy.delay);
407 dpy.running = !dpy.running;