Thank you both for your comments.

An updated patch, in the first one I missed handling the -n flag :(

# ./sysctl kern.cp_time
kern.cp_time=23725,0,189448,89073,27969981
# ./sysctl kern.cp_time2
kern.cp_time2.0=30675,0,204640,342526,27687398
kern.cp_time2.1=13907,0,119539,2129,28140911
kern.cp_time2.2=19826,0,162028,3620,28090129
kern.cp_time2.3=30492,0,271589,8022,27962123
# ./sysctl kern.cp_time2.0
kern.cp_time2.0=30675,0,204640,342527,27687581
# ./sysctl kern.cp_time2.1
kern.cp_time2.1=13907,0,119539,2129,28141226
# ./sysctl kern.cp_time2.2
kern.cp_time2.2=19826,0,162029,3620,28090527
# ./sysctl kern.cp_time2.3
kern.cp_time2.3=30493,0,271594,8022,27962683
# ./sysctl kern.cp_time2.4
sysctl: kern.cp_time2.4: third level '4' too large
# ./sysctl hw.ncpu
hw.ncpu=4
# ./sysctl kern.cp_time2.-1
sysctl: kern.cp_time2.-1: third level '-1' too small
# ./sysctl kern.cp_time2.a
sysctl: kern.cp_time2.a: third level 'a' invalid
# ./sysctl kern.cp_time2.
sysctl: kern.cp_time2.: third level '' invalid
# ./sysctl kern.cp_time2.0.0.0
sysctl: kern.cp_time2.0.0.0: third level '0.0.0' invalid
# ./sysctl -n kern.cp_time2
30678,0,204669,342565,27691239
13907,0,119546,2129,28144816
19827,0,162053,3620,28094016
30495,0,271635,8023,27965986
# ./sysctl -n kern.cp_time2.0
30678,0,204669,342573,27691470
# ./sysctl -a 2>&1 | egrep -i cp_time
kern.cp_time=23728,0,189500,89091,27977016
kern.cp_time2.0=30680,0,204701,342594,27694212
kern.cp_time2.1=13907,0,119553,2129,28147846
kern.cp_time2.2=19828,0,162082,3621,28097021
kern.cp_time2.3=30499,0,271666,8023,27968987
# ./sysctl -A 2>&1 | egrep -i cp_time
kern.cp_time=23729,0,189504,89092,27977436
kern.cp_time2.0=30681,0,204707,342597,27694629
kern.cp_time2.1=13908,0,119554,2129,28148270
kern.cp_time2.2=19828,0,162088,3621,28097441
kern.cp_time2.3=30500,0,271671,8023,27969406

-- 
Andrei.

Index: src/sbin/sysctl/sysctl.c
===================================================================
RCS file: /cvs/src/sbin/sysctl/sysctl.c,v
retrieving revision 1.211
diff -u -p -u -r1.211 sysctl.c
--- src/sbin/sysctl/sysctl.c    18 Apr 2015 18:28:37 -0000      1.211
+++ src/sbin/sysctl/sysctl.c    28 Oct 2015 16:18:43 -0000
@@ -215,6 +215,7 @@ int sysctl_emul(char *, char *, int);
 #ifdef CPU_CHIPSET
 int sysctl_chipset(char *, char **, int *, int, int *);
 #endif
+int sysctl_cptime2(char *, char **, int *, int, int *);
 void vfsinit(void);

 char *equ = "=";
@@ -412,6 +413,9 @@ parse(char *string, int flags)
                        special |= LONGARRAY;
                        lal = CPUSTATES;
                        break;
+               case KERN_CPTIME2:
+                       sysctl_cptime2(string, &bufp, mib, flags,
&type);
+                       return;
                case KERN_SEMINFO:
                        len = sysctl_seminfo(string, &bufp, mib, flags,
                        &type);
                        if (len < 0)
@@ -2759,6 +2763,84 @@ sysctl_emul(char *string, char *newval,
        return (0);


+}
+
+int
+sysctl_cptime2(char *string, char **bufpp, int mib[], int flags, int
*typep)
+{
+       int local_mib[2], ncpu, i, cpu;
+       size_t len;
+       u_int64_t cp_time2[CPUSTATES];
+       char *second, *third;
+       const char *errstr;
+
+       local_mib[0] = CTL_HW;
+       local_mib[1] = HW_NCPU;
+       len = sizeof(ncpu);
+       if (sysctl(local_mib, 2, &ncpu, &len, NULL, 0) == -1) {
+               err(1, "%s can't get number of cpus (hw.ncpu)", string);
+               return (0);
+       }
+
+       len = sizeof(cp_time2);
+       second = strchr(string, '.');
+       if (!second) {
+               errx(1, "%s: can't get mib second level name", string);
+               return (0);
+       }
+       second++;
+       third = strchr(second, '.');
+       if (!third) {
+               for (i = 0; i < ncpu; i++) {
+                       mib[2] = i;
+                       if (sysctl(mib, 3, &cp_time2, &len, NULL, 0) ==
-1) {
+                               warn("%s.%d can't get cpu states",
string, i);
+                               continue;
+                       }
+
+                       if (!nflag)
+                               (void)printf("%s.%d=", string, i);
+                       (void)printf("%ld,%ld,%ld,%ld,%ld\n",
+                               cp_time2[CP_USER],
+                               cp_time2[CP_NICE],
+                               cp_time2[CP_SYS],
+                               cp_time2[CP_INTR],
+                               cp_time2[CP_IDLE]
+                       );
+               }
+       }
+       else {
+               third++;
+               if (ncpu > 1) {
+                       cpu = strtonum(third, 0, ncpu - 1, &errstr);
+               }
+               else {
+                       cpu = strtonum(third, 0, 0, &errstr);
+               }
+               if (errstr) {
+                       errx(1, "%s: third level '%s' %s", string,
third,
+                               errstr);
+                       return (0);
+               }
+
+               mib[2] = cpu;
+               if (sysctl(mib, 3, &cp_time2, &len, NULL, 0) == -1) {
+                       warn("%s can't get cpu states", string);
+                       return (0);
+               }
+
+               if (!nflag)
+                       (void)printf("%s=", string);
+               (void)printf("%ld,%ld,%ld,%ld,%ld\n",
+                       cp_time2[CP_USER],
+                       cp_time2[CP_NICE],
+                       cp_time2[CP_SYS],
+                       cp_time2[CP_INTR],
+                       cp_time2[CP_IDLE]
+               );
+       }
+
+       return (1);
 }

 static int
Index: src/sbin/sysctl/sysctl.8
===================================================================
RCS file: /cvs/src/sbin/sysctl/sysctl.8,v
retrieving revision 1.187
diff -u -p -u -r1.187 sysctl.8
--- src/sbin/sysctl/sysctl.8    3 Oct 2015 09:17:13 -0000       1.187
+++ src/sbin/sysctl/sysctl.8    28 Oct 2015 16:18:43 -0000
@@ -153,6 +153,7 @@ and a few require a kernel compiled with
 .It kern.malloc.kmemnames Ta string Ta no
 .It kern.malloc.kmemstat.<name> Ta string Ta no
 .It kern.cp_time Ta struct Ta no
+.It kern.cp_time2 Ta struct Ta no
 .It kern.nchstats Ta struct Ta no
 .It kern.forkstat Ta struct Ta no
 .It kern.nselcoll Ta integer Ta no

Reply via email to