slstatus

status monitor
git clone git://git.suckless.org/slstatus
Log | Files | Refs | README | LICENSE

cpu.c (3434B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 
      6 #include "../slstatus.h"
      7 #include "../util.h"
      8 
      9 #if defined(__linux__)
     10 	#define CPU_FREQ "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
     11 
     12 	const char *
     13 	cpu_freq(const char *unused)
     14 	{
     15 		uintmax_t freq;
     16 
     17 		/* in kHz */
     18 		if (pscanf(CPU_FREQ, "%ju", &freq) != 1)
     19 			return NULL;
     20 
     21 		return fmt_human(freq * 1000, 1000);
     22 	}
     23 
     24 	const char *
     25 	cpu_perc(const char *unused)
     26 	{
     27 		static long double a[7];
     28 		long double b[7], sum;
     29 
     30 		memcpy(b, a, sizeof(b));
     31 		/* cpu user nice system idle iowait irq softirq */
     32 		if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
     33 		           &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6])
     34 		    != 7)
     35 			return NULL;
     36 
     37 		if (b[0] == 0)
     38 			return NULL;
     39 
     40 		sum = (b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) -
     41 		      (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]);
     42 
     43 		if (sum == 0)
     44 			return NULL;
     45 
     46 		return bprintf("%d", (int)(100 *
     47 		               ((b[0] + b[1] + b[2] + b[5] + b[6]) -
     48 		                (a[0] + a[1] + a[2] + a[5] + a[6])) / sum));
     49 	}
     50 #elif defined(__OpenBSD__)
     51 	#include <sys/param.h>
     52 	#include <sys/sched.h>
     53 	#include <sys/sysctl.h>
     54 
     55 	const char *
     56 	cpu_freq(const char *unused)
     57 	{
     58 		int freq, mib[2];
     59 		size_t size;
     60 
     61 		mib[0] = CTL_HW;
     62 		mib[1] = HW_CPUSPEED;
     63 
     64 		size = sizeof(freq);
     65 
     66 		/* in MHz */
     67 		if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
     68 			warn("sysctl 'HW_CPUSPEED':");
     69 			return NULL;
     70 		}
     71 
     72 		return fmt_human(freq * 1E6, 1000);
     73 	}
     74 
     75 	const char *
     76 	cpu_perc(const char *unused)
     77 	{
     78 		int mib[2];
     79 		static uintmax_t a[CPUSTATES];
     80 		uintmax_t b[CPUSTATES], sum;
     81 		size_t size;
     82 
     83 		mib[0] = CTL_KERN;
     84 		mib[1] = KERN_CPTIME;
     85 
     86 		size = sizeof(a);
     87 
     88 		memcpy(b, a, sizeof(b));
     89 		if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) {
     90 			warn("sysctl 'KERN_CPTIME':");
     91 			return NULL;
     92 		}
     93 		if (b[0] == 0)
     94 			return NULL;
     95 
     96 		sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) -
     97 		      (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]);
     98 
     99 		if (sum == 0)
    100 			return NULL;
    101 
    102 		return bprintf("%d", 100 *
    103 		               ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +
    104 		                 a[CP_INTR]) -
    105 		                (b[CP_USER] + b[CP_NICE] + b[CP_SYS] +
    106 		                 b[CP_INTR])) / sum);
    107 	}
    108 #elif defined(__FreeBSD__)
    109 	#include <devstat.h>
    110 	#include <sys/param.h>
    111 	#include <sys/sysctl.h>
    112 
    113 	const char *
    114 	cpu_freq(const char *unused)
    115 	{
    116 		int freq;
    117 		size_t size;
    118 
    119 		size = sizeof(freq);
    120 		/* in MHz */
    121 		if (sysctlbyname("hw.clockrate", &freq, &size, NULL, 0) < 0 || !size) {
    122 			warn("sysctlbyname 'hw.clockrate':");
    123 			return NULL;
    124 		}
    125 
    126 		return fmt_human(freq * 1E6, 1000);
    127 	}
    128 
    129 	const char *
    130 	cpu_perc(const char *unused)
    131 	{
    132 		size_t size;
    133 		static long a[CPUSTATES];
    134 		long b[CPUSTATES], sum;
    135 
    136 		size = sizeof(a);
    137 		memcpy(b, a, sizeof(b));
    138 		if (sysctlbyname("kern.cp_time", &a, &size, NULL, 0) < 0 || !size) {
    139 			warn("sysctlbyname 'kern.cp_time':");
    140 			return NULL;
    141 		}
    142 		if (b[0] == 0)
    143 			return NULL;
    144 
    145 		sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) -
    146 		      (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]);
    147 
    148 		if (sum == 0)
    149 			return NULL;
    150 
    151 		return bprintf("%d", 100 *
    152 		               ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +
    153 		                 a[CP_INTR]) -
    154 		                (b[CP_USER] + b[CP_NICE] + b[CP_SYS] +
    155 		                 b[CP_INTR])) / sum);
    156 	}
    157 #endif