5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
13 #include <sys/types.h>
16 #include <sys/ioctl.h>
21 #include <sys/socket.h>
26 #include <linux/omapfb.h>
30 #define SCREEN_WIDTH 800
31 #define SCREEN_HEIGHT 480
34 #define MEM_SIZE (((WIDTH * HEIGHT * 2 * 2) + 0xfff) & ~0xfff)
37 static struct fb_var_screeninfo g_vi;
38 static uint16_t *g_screen_base, *g_screen;
39 static unsigned int g_old_mem;
40 static int g_fd, g_exit;
42 #define IS_START(buf, str) \
43 !strncmp(buf, str, sizeof(str) - 1)
45 static int setup_layer(void)
47 static const char fbname[] = "/dev/fb2";
48 struct omapfb_plane_info pi;
49 struct omapfb_mem_info mi;
50 struct omapfb_color_key key;
53 g_fd = open(fbname, O_RDWR);
55 fprintf(stderr, "open %s: ", fbname);
60 ret = ioctl(g_fd, OMAPFB_QUERY_PLANE, &pi);
62 perror("ioctl OMAPFB_QUERY_PLANE");
66 ret = ioctl(g_fd, OMAPFB_QUERY_MEM, &mi);
68 perror("ioctl OMAPFB_QUERY_MEM");
72 /* must disable when changing stuff */
75 ret = ioctl(g_fd, OMAPFB_SETUP_PLANE, &pi);
77 perror("ioctl OMAPFB_SETUP_PLANE (d)");
83 if (mi.size < MEM_SIZE) {
85 ret = ioctl(g_fd, OMAPFB_SETUP_MEM, &mi);
87 perror("ioctl SETUP_MEM");
92 pi.pos_x = SCREEN_WIDTH - WIDTH;
95 pi.out_height = HEIGHT;
96 ret = ioctl(g_fd, OMAPFB_SETUP_PLANE, &pi);
98 perror("ioctl OMAPFB_SETUP_PLANE (e1)");
102 ret = ioctl(g_fd, FBIOGET_VSCREENINFO, &g_vi);
104 perror("ioctl FBIOGET_VSCREENINFO");
108 g_vi.xres = g_vi.xres_virtual = WIDTH;
110 g_vi.yres_virtual = HEIGHT * 2;
111 g_vi.bits_per_pixel = 16;
113 ret = ioctl(g_fd, FBIOPUT_VSCREENINFO, &g_vi);
115 perror("ioctl FBIOPUT_VSCREENINFO");
120 ret = ioctl(g_fd, OMAPFB_SETUP_PLANE, &pi);
122 perror("ioctl OMAPFB_SETUP_PLANE (e2)");
126 memset(&key, 0, sizeof(key));
127 key.key_type = OMAPFB_COLOR_KEY_VID_SRC;
128 key.trans_key = 0x07e0;
130 ret = ioctl(g_fd, OMAPFB_SET_COLOR_KEY, &key);
132 perror("ioctl OMAPFB_SET_COLOR_KEY");
139 static int check_layer(void)
141 struct omapfb_plane_info pi;
144 ret = ioctl(g_fd, OMAPFB_QUERY_PLANE, &pi);
146 perror("ioctl OMAPFB_QUERY_PLANE");
151 printf("something disabled the layer (tv-out?), quitting\n");
158 static void remove_layer(void)
160 struct omapfb_plane_info pi;
161 struct omapfb_mem_info mi;
164 memset(&pi, 0, sizeof(pi));
166 ret = ioctl(g_fd, OMAPFB_SETUP_PLANE, &pi);
168 perror("ioctl OMAPFB_SETUP_PLANE (c)");
170 ret = ioctl(g_fd, OMAPFB_QUERY_MEM, &mi);
172 perror("ioctl OMAPFB_QUERY_MEM");
176 if (mi.size != g_old_mem) {
178 ret = ioctl(g_fd, OMAPFB_SETUP_MEM, &mi);
180 perror("ioctl SETUP_MEM");
190 static void handle_signal(int num)
195 static void flip_fb(void)
199 g_vi.yoffset = id * HEIGHT;
200 ioctl(g_fd, FBIOPAN_DISPLAY, &g_vi);
202 g_screen = g_screen_base + id * WIDTH * HEIGHT;
205 #define s_printf(x, y, fmt, ...) \
206 basic_text_out16(g_screen, WIDTH, x, y, fmt, ##__VA_ARGS__);
208 static int read_int_file(const char *fn, int *val)
217 ret = fscanf(f, "%d", val);
225 static void clear(int h)
227 int *p = (int *)g_screen;
228 int l = WIDTH * h / 2;
230 for (; l >= 4; l -= 4, p += 4)
231 p[0] = p[1] = p[2] = p[3] = 0x07e007e0;
232 for (; l > 0; l--, p++)
237 unsigned long idlesum;
244 struct proc_stat last_st;
254 static void collect_proc_stat(void)
260 unsigned long iowait;
264 hz = sysconf(_SC_CLK_TCK);
265 printf("hz: %ld\n", hz);
268 f = fopen("/proc/stat", "r");
272 if (fscanf(f, "cpu %*s %*s %lu %lu %lu",
273 &st.sys, &wait, &iowait) != 3)
276 if (fgets(buf, sizeof(buf), f) == NULL)
278 } while (fscanf(f, "intr %lu", &st.irqs) != 1);
280 if (fgets(buf, sizeof(buf), f) == NULL)
282 } while (fscanf(f, "ctxt %lu", &st.ctxt) != 1);
284 st.idlesum = wait + iowait;
286 stats.cpu_usage = hz - (st.idlesum - stats.last_st.idlesum);
287 stats.cpu_sys = st.sys - stats.last_st.sys;
289 stats.cpu_usage = stats.cpu_usage * 100 / hz;
290 stats.cpu_sys = stats.cpu_sys * 100 / hz;
292 stats.irqs = st.irqs - stats.last_st.irqs;
293 stats.ctxt = st.ctxt - stats.last_st.ctxt;
300 static void collect_mem_stat(void)
302 static const char fn[] = "/proc/meminfo";
303 uint32_t total = 0, free = 0, buffers = 0, cached = 0;
304 uint32_t anonhuge = 0, hugetot = 0, hugefree = 0, hugepgsz = 0;
305 uint32_t free_swap = 0;
313 while (fgets(buf, sizeof(buf), f)) {
314 if (total == 0 && sscanf(buf, "MemTotal: %u", &total) == 1)
316 if (free == 0 && sscanf(buf, "MemFree: %u", &free) == 1)
318 if (buffers == 0 && sscanf(buf, "Buffers: %u", &buffers) == 1)
320 if (cached == 0 && sscanf(buf, "Cached: %u", &cached) == 1)
322 if (free_swap == 0 && sscanf(buf, "SwapFree: %u", &free_swap) == 1)
324 if (anonhuge == 0 && sscanf(buf, "AnonHugePages: %u", &anonhuge) == 1)
326 if (hugetot == 0 && sscanf(buf, "HugePages_Total: %u", &hugetot) == 1)
328 if (hugefree == 0 && sscanf(buf, "HugePages_Free: %u", &hugefree) == 1)
330 if (hugepgsz == 0 && sscanf(buf, "Hugepagesize: %u", &hugepgsz) == 1)
336 hugetot = (hugetot - hugefree) * hugepgsz;
337 free += buffers + cached;
338 stats.mem_free = free >> 10;
339 stats.mem_huge = hugetot >> 10;
340 stats.mem_free_swap = free_swap >> 10;
349 struct io_stat last_st;
354 static void collect_io_stat(void)
356 unsigned long sec_r, sec_w;
357 struct io_stat st = { 0, };
362 for (i = 0; i < 2; i++) {
363 snprintf(buf, sizeof(buf), "/sys/block/mmcblk%d/stat", i);
368 if (fscanf(f, "%*s %*s %lu %*s %*s %*s %lu",
369 &sec_r, &sec_w) == 2)
377 io_stats.read = st.sec_r - io_stats.last_st.sec_r;
378 io_stats.write = st.sec_w - io_stats.last_st.sec_w;
379 // assuming 512 byte sectors..
381 io_stats.write >>= 1;
383 io_stats.last_st = st;
392 struct net_stat last_st;
400 static void collect_net_stat(void)
402 struct net_stat st = { 0, };
403 unsigned long rx, tx;
408 net_stats.down = net_stats.up =
409 net_stats.have_wlan = net_stats.link = net_stats.level = 0;
411 f = fopen("/proc/net/dev", "r");
416 static const char fmt[] =
417 "%255s %lu %*s %*s %*s %*s %*s %*s %lu";
418 if (fscanf(f, fmt, buf, &rx, &tx) != 3)
420 if (IS_START(buf, "lo"))
429 net_stats.down = st.rx - net_stats.last_st.rx;
430 net_stats.up = st.tx - net_stats.last_st.tx;
432 net_stats.last_st = st;
434 f = fopen("/proc/net/wireless", "r");
440 if (fscanf(f, "%255s %*x %d. %d.", buf, &lnk, &lvl) != 3)
442 if (IS_START(buf, "lo"))
444 // first one should be enough
445 net_stats.have_wlan = 1;
446 net_stats.link = lnk;
447 net_stats.level = lvl;
453 static void collect_stats(void)
461 static char cpu_reg_path[64];
463 static void init_stats(void)
470 // find the CPU regulator
471 for (i = found = 0; ; i++) {
472 snprintf(buf, sizeof(buf),
473 "/sys/class/regulator/regulator.%d/name", i);
477 fgets(buf, sizeof(buf), f);
479 if (IS_START(buf, "vdd_mpu")) {
486 snprintf(cpu_reg_path, sizeof(cpu_reg_path),
487 "/sys/class/regulator/regulator.%d/microvolts", i);
489 // do collect so differential stats get sane values
493 static void get_time(int x, int y)
499 gettimeofday(&tv, NULL);
500 t = localtime(&tv.tv_sec);
501 strftime(buf, sizeof(buf), "%T", t);
502 s_printf(x, y, " %s", buf);
505 static void get_cpu(int x, int y)
507 s_printf(x, y, "CPU:%5d%%", stats.cpu_usage);
510 static void get_sys(int x, int y)
512 s_printf(x, y, "SYS:%5d%%", stats.cpu_sys);
515 static void get_irq(int x, int y)
517 s_printf(x, y, "IRQ:%5u", stats.irqs);
520 static void get_cs(int x, int y)
522 s_printf(x, y, "CS:%6u", stats.ctxt);
525 static void get_cpuf(int x, int y)
527 static const char fn[] =
528 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq";
531 if (read_int_file(fn, &f))
533 s_printf(x, y, "f:%7dM", f / 1000);
536 static void get_cpuv(int x, int y)
540 if (!cpu_reg_path[0] || read_int_file(cpu_reg_path, &v))
542 s_printf(x, y, "V:%7.3fV", (float)v / 1000000.0f);
545 static void get_memfree(int x, int y)
547 s_printf(x, y, "M:%7dM", stats.mem_free);
550 static void get_memhuge(int x, int y)
552 s_printf(x, y, "H:%7dM", stats.mem_huge);
555 static void get_memswap(int x, int y)
557 s_printf(x, y, "S:%7dM", stats.mem_free_swap);
560 #define PS_BASE "/sys/class/power_supply/bq27500-0/"
562 static void get_bcap(int x, int y)
564 static const char fnc[] = PS_BASE "capacity";
567 if (read_int_file(fnc, &c))
569 s_printf(x, y, "C:%7d%%", c);
572 static void get_btime(int x, int y)
574 static const char fne[] = PS_BASE "time_to_empty_now";
575 static const char fnf[] = PS_BASE "time_to_full_now";
578 if (read_int_file(fne, &t) || t == 0) {
579 if (read_int_file(fnf, &t))
582 s_printf(x, y, "t:%4d:%02d", t / 3600, t / 60 % 60);
585 static void get_bvolts(int x, int y)
587 static const char fnv[] = PS_BASE "voltage_now";
590 if (read_int_file(fnv, &volt))
593 s_printf(x, y, "V:%7.3fV", (float)volt / 1000000.0f);
596 static void get_bwatts(int x, int y)
598 static const char fnc[] = PS_BASE "current_now";
599 static const char fnv[] = PS_BASE "voltage_now";
600 int w, cur = 0, volt = 0;
602 if (read_int_file(fnc, &cur))
604 if (read_int_file(fnv, &volt))
607 w = (cur / 1000) * (volt / 1000);
608 s_printf(x, y, "P:%7.3fW", (float)w / 1000000.0f);
611 static void get_btemp(int x, int y)
613 static const char fnt[] = PS_BASE "temp";
616 if (read_int_file(fnt, &t))
618 s_printf(x, y, "T:%7.1fC", (float)t / 10.0f);
621 static void get_ioread(int x, int y)
623 s_printf(x, y, "R:%5.1fM/s", (float)io_stats.read / 1000.0f);
626 static void get_iowrite(int x, int y)
628 s_printf(x, y, "W:%5.1fM/s", (float)io_stats.write / 1000.0f);
631 static void get_netdown(int x, int y)
633 s_printf(x, y, "D:%5uK/s", net_stats.down / 1000);
636 static void get_netup(int x, int y)
638 s_printf(x, y, "U:%5uK/s", net_stats.up / 1000);
641 static void get_netsgnl(int x, int y)
643 s_printf(x, y, "S:%5ddBm", net_stats.level);
646 int main(int argc, char *argv[])
648 static const char socket_name[] = "\0livestats";
649 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
651 pthread_condattr_t condattr;
652 struct sockaddr_un sun;
655 int y = 0, y_max = 0;
659 // look for other instance
660 sock = socket(PF_UNIX, SOCK_STREAM, 0);
662 perror("socket PF_UNIX");
666 memset(&sun, 0, sizeof(sun));
667 sun.sun_family = AF_UNIX;
668 memcpy(sun.sun_path, socket_name, sizeof(socket_name));
670 ret = connect(sock, (struct sockaddr *)&sun, sizeof(sun));
672 printf("other instance detected, sending quit command\n");
673 ret = send(sock, "quit", 4, 0);
680 // bind/listen for quit command
681 ret = bind(sock, (struct sockaddr *)&sun, sizeof(sun));
688 ret = listen(sock, 1);
696 pfd.events = POLLIN | POLLPRI;
699 // clean up on kill too
700 signal(SIGTERM, handle_signal);
702 ret = pthread_condattr_init(&condattr);
703 ret |= pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
704 ret |= pthread_cond_init(&cond, &condattr);
705 pthread_condattr_destroy(&condattr);
707 fprintf(stderr, "cond init failed\n");
715 g_screen_base = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE,
716 MAP_SHARED, g_fd, 0);
717 if (g_screen_base == MAP_FAILED) {
721 g_screen = g_screen_base;
726 pthread_mutex_lock(&mutex);
727 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
729 perror("clock_gettime CLOCK_MONOTONIC");
745 get_time (0, y += Y_STEP);
747 get_cpu (0, y += Y_STEP);
748 get_sys (0, y += Y_STEP);
749 get_irq (0, y += Y_STEP);
750 get_cs (0, y += Y_STEP);
751 get_cpuf (0, y += Y_STEP);
752 get_cpuv (0, y += Y_STEP);
754 s_printf(0, y += Y_STEP, "mem");
755 get_memfree(0, y += Y_STEP);
756 get_memswap(0, y += Y_STEP);
758 get_memhuge(0, y += Y_STEP);
760 s_printf(0, y += Y_STEP, "i/o");
761 get_ioread (0, y += Y_STEP);
762 get_iowrite(0, y += Y_STEP);
764 s_printf(0, y += Y_STEP, "net");
765 get_netdown(0, y += Y_STEP);
766 get_netup (0, y += Y_STEP);
767 if (net_stats.have_wlan)
768 get_netsgnl(0, y += Y_STEP);
770 s_printf(0, y += Y_STEP, "batt");
771 get_bcap (0, y += Y_STEP);
772 get_btime (0, y += Y_STEP);
773 get_bvolts (0, y += Y_STEP);
774 get_bwatts (0, y += Y_STEP);
775 get_btemp (0, y += Y_STEP);
779 if (check_layer() != 0)
781 ret = poll(&pfd, 1, 0);
783 printf("poll returned %d\n", ret);
788 pthread_cond_timedwait(&cond, &mutex, &ts);
791 munmap(g_screen_base, MEM_SIZE);
798 // vim:expandtab:sw=2:ts=2