ports/119923: [patch] sysutil/ascpu does not work FreeBSD 8.0-CURRENT
KOIE Hidetaka
koie at suri.co.jp
Wed Jan 23 14:00:06 UTC 2008
>Number: 119923
>Category: ports
>Synopsis: [patch] sysutil/ascpu does not work FreeBSD 8.0-CURRENT
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-ports-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: change-request
>Submitter-Id: current-users
>Arrival-Date: Wed Jan 23 14:00:05 UTC 2008
>Closed-Date:
>Last-Modified:
>Originator: KOIE Hidetaka
>Release: 8.0-CURRENT
>Organization:
surigiken
>Environment:
FreeBSD guriandgura 8.0-CURRENT FreeBSD 8.0-CURRENT #1: Wed Jan 16 16:41:01 JST 2008 koie at guriandgura:/usr/obj/usr/src/sys/GURIANDGURA amd64
>Description:
ascpu exits 1, because kvm_nlist(kd, nlst={{"_cp_time"}, {0}}) is faild.
>How-To-Repeat:
koie at guriandgura% ascpu
kvm_open: kvm_nlist: No such file or directory
ascpu : cannot get nlist
koie at guriandgura%
>Fix:
sysctl cp_time/cp_times should be used.
(If sysctl is only used, no need to set the setgid bit by do-install in Makefile.)
the option -cpu will be accepted by this patch.
Patch attached with submission follows:
--- ascpu_x.c.orig2 2008-01-23 18:36:30.354893235 +0900
+++ ascpu_x.c 2008-01-23 18:36:51.855717860 +0900
@@ -19,6 +19,11 @@
#include <fcntl.h>
#include <kvm.h>
#include <sys/types.h>
+#include <err.h>
+#include <string.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
#endif
#ifdef __hpux__
@@ -82,6 +87,8 @@
int update_request = 0;
#ifdef __FreeBSD__
+static int cp_time_mib[2];
+static int cp_times_mib[2];
static kvm_t *kd;
static struct nlist nlst[] = {
{"_cp_time"}, {0}
@@ -555,12 +562,45 @@
memcpy(&last, &fresh, sizeof(last));
#ifdef __FreeBSD__
- if (kvm_read(kd, nlst[0].n_value, &fresh, sizeof(fresh)) != sizeof(fresh)) {
- printf("ascpu : cannot read kvm\n");
- exit(1);
+ if (state.cpu_number >= 0 && cp_times_mib[0] != 0) {
+ long cp_times[MAX_CPU][CPUSTATES];
+ size_t cp_times_len = sizeof(cp_times);
+ int error = sysctl(cp_times_mib, 2, cp_times, &cp_times_len, NULL, 0);
+ if (error) {
+ printf("ascpu: cannot sysctl cp_times\n");
+ exit(1);
+ }
+
+ long *cp_time = cp_times[state.cpu_number];
+ fresh.load[0] = cp_time[CP_USER];
+ fresh.load[1] = cp_time[CP_NICE];
+ fresh.load[2] = cp_time[CP_SYS] + cp_time[CP_INTR];
+ fresh.load[3] = cp_time[CP_IDLE];
+ } else if (state.cpu_number == -1 && cp_time_mib[0] != 0) {
+ long cp_time[CPUSTATES];
+ size_t cp_time_len = sizeof(cp_time);
+ int error = sysctl(cp_time_mib, 2, cp_time, &cp_time_len, NULL, 0);
+ if (error) {
+ printf("ascpu: cannot sysctl cp_time\n");
+ exit(1);
+ }
+
+ fresh.load[0] = cp_time[CP_USER];
+ fresh.load[1] = cp_time[CP_NICE];
+ fresh.load[2] = cp_time[CP_SYS] + cp_time[CP_INTR];
+ fresh.load[3] = cp_time[CP_IDLE];
+ } else {
+ if (nlst[0].n_type == 0) {
+ printf("ascpu : cannot get nlist\n");
+ exit(1);
+ }
+ if (kvm_read(kd, nlst[0].n_value, &fresh, sizeof(fresh)) != sizeof(fresh)) {
+ printf("ascpu : cannot read kvm\n");
+ exit(1);
+ }
+ /* compatible with Linux(overwrite 'interrupt' with 'idle' field) */
+ fresh.load[3] = fresh.load[4];
}
- /* compatible with Linux(overwrite 'interrupt' with 'idle' field) */
- fresh.load[3] = fresh.load[4];
#endif
#ifdef __hpux__
@@ -1094,14 +1134,12 @@
ascpu_cleanup();
}
#ifdef __FreeBSD__
- if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) {
- printf("ascpu : cannot open kvm\n");
- exit(1);
- }
- kvm_nlist(kd, nlst);
- if (nlst[0].n_type == 0) {
- printf("ascpu : cannot get nlist\n");
- exit(1);
+ size_t len = 2;
+ sysctlnametomib("kern.cp_times", cp_times_mib, &len);
+ len = 2;
+ sysctlnametomib("kern.cp_time", cp_time_mib, &len);
+ if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) != NULL) {
+ kvm_nlist(kd, nlst);
}
#endif
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the freebsd-ports-bugs
mailing list