diff options
author | Martin Baulig <martin@src.gnome.org> | 1998-05-23 23:05:08 +0000 |
---|---|---|
committer | Martin Baulig <martin@src.gnome.org> | 1998-05-23 23:05:08 +0000 |
commit | 109f8dc6080cfee3e1331c9ec09bb7f5c20bbdbf (patch) | |
tree | f2e3ae98f86f7651c64e6fc26b9c2e313983fb40 /sysdeps | |
parent | 37e161bcecf014c2035905d6c0b4f92c83c86895 (diff) | |
download | libgtop-109f8dc6080cfee3e1331c9ec09bb7f5c20bbdbf.tar.gz |
Started porting. Getting CPU usage now working. Took a lot of code from
* sysdeps/sun4/{open.c, cpu.c}: Started porting.
Getting CPU usage now working. Took a lot of code
from top-3.4.
Diffstat (limited to 'sysdeps')
-rw-r--r-- | sysdeps/sun4/cpu.c | 68 | ||||
-rw-r--r-- | sysdeps/sun4/open.c | 149 |
2 files changed, 217 insertions, 0 deletions
diff --git a/sysdeps/sun4/cpu.c b/sysdeps/sun4/cpu.c index cda12977..e5efea4d 100644 --- a/sysdeps/sun4/cpu.c +++ b/sysdeps/sun4/cpu.c @@ -22,10 +22,78 @@ #include <config.h> #include <glibtop/cpu.h> +static const unsigned long _glibtop_sysdeps_cpu = +(1 << GLIBTOP_CPU_TOTAL) + (1 << GLIBTOP_CPU_USER) + +(1 << GLIBTOP_CPU_NICE) + (1 << GLIBTOP_CPU_SYS) + +(1 << GLIBTOP_CPU_IDLE) + (1 << GLIBTOP_CPU_FREQUENCY); + /* Provides information about cpu usage. */ void glibtop_get_cpu__r (glibtop *server, glibtop_cpu *buf) { + long cp_time [CPUSTATES], mp_time [NCPU][CPUSTATES]; + int i; + memset (buf, 0, sizeof (glibtop_cpu)); + + /* !!! THE FOLLOWING CODE RUNS SGID KMEM - CHANGE WITH CAUTION !!! */ + + setregid (server->machine.gid, server->machine.egid); + + /* get the cp_time array */ + + (void) _glibtop_getkval (server, _glibtop_nlist [X_CP_TIME].n_value, (int *) cp_time, + sizeof (cp_time), _glibtop_nlist [X_CP_TIME].n_name); + +#ifdef MULTIPROCESSOR + /* get the mp_time array as well */ + + if (server->machine.ncpu > 1) { + (void) _glibtop_getkval (server, _glibtop_nlist [X_MP_TIME].n_value, (int *) mp_time, + sizeof (mp_time), _glibtop_nlist [X_MP_TIME].n_name); + } +#endif + + if (setregid (server->machine.egid, server->machine.gid)) + _exit (1); + + /* !!! END OF SUID ROOT PART !!! */ + +#ifdef MULTIPROCESSOR + /* If we have multiple processors, we add the times for each of them and set + frequency to 100 times the number of the processors. */ + + /* [FIXME]: I had no machine with more than one processor to test this code !!! */ + + if (server->machine.ncpu > 1) { + for (i = 0; i < server->machine.ncpu; i++) { + buf->user += mp_time [i][CP_USER]; + buf->nice += mp_time [i][CP_NICE]; + buf->sys += mp_time [i][CP_SYS]; + buf->idle += mp_time [i][CP_IDLE]; + buf->frequency += 100; + } + } else { + buf->user = cp_time [CP_USER]; + buf->nice = cp_time [CP_NICE]; + buf->sys = cp_time [CP_SYS]; + buf->idle = cp_time [CP_IDLE]; + buf->frequency = 100; + } +#else + buf->user = cp_time [CP_USER]; + buf->nice = cp_time [CP_NICE]; + buf->sys = cp_time [CP_SYS]; + buf->idle = cp_time [CP_IDLE]; + buf->frequency = 100; +#endif + + /* Calculate total time. */ + + buf->total = buf->user + buf->nice + buf->sys + buf->idle; + + /* Now we can set the flags. */ + + buf->flags = _glibtop_sysdeps_cpu; } diff --git a/sysdeps/sun4/open.c b/sysdeps/sun4/open.c index c2d58069..94cc5a44 100644 --- a/sysdeps/sun4/open.c +++ b/sysdeps/sun4/open.c @@ -19,13 +19,162 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include <glibtop.h> #include <glibtop/open.h> +struct nlist _glibtop_nlist[] = { +#ifdef i386 + { "avenrun" }, /* 0 */ + { "ccpu" }, /* 1 */ + { "mpid" }, /* 2 */ + { "nproc" }, /* 3 */ + { "proc" }, /* 4 */ + { "total" }, /* 5 */ + { "cp_time" }, /* 6 */ + { "pages" }, /* 7 */ + { "epages" }, /* 8 */ +#else + { "_avenrun" }, /* 0 */ + { "_ccpu" }, /* 1 */ + { "_mpid" }, /* 2 */ + { "_nproc" }, /* 3 */ + { "_proc" }, /* 4 */ + { "_total" }, /* 5 */ + { "_cp_time" }, /* 6 */ + { "_pages" }, /* 7 */ + { "_epages" }, /* 8 */ +#ifdef MULTIPROCESSOR + { "_ncpu" }, + { "_mp_time" }, +#endif +#endif + { 0 } +}; + /* Opens pipe to gtop server. Returns 0 on success and -1 on error. */ +/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */ + void glibtop_open (glibtop *server, const char *program_name) { + /* !!! WE ARE ROOT HERE - CHANGE WITH CAUTION !!! */ + memset (server, 0, sizeof (glibtop)); server->name = program_name; + + server->machine.uid = getuid (); + server->machine.euid = geteuid (); + server->machine.gid = getgid (); + server->machine.egid = getegid (); + + /* initialize the kernel interface */ + + server->machine.kd = kvm_open (NULL, NULL, NULL, O_RDONLY, "libgtop"); + + if (server->machine.kd == NULL) + glibtop_error__r (server, "kvm_open: %s", strerror (errno)); + + /* get the list of symbols we want to access in the kernel */ + + server->machine.nlist_count = kvm_nlist (server->machine.kd, _glibtop_nlist); + + if (server->machine.nlist_count < 0) + glibtop_error__r (server, "nlist: %s", strerror (errno)); + +#ifdef MULTIPROCESSOR + /* were ncpu and xp_time not found in the nlist? */ + + if ((server->machine.nlist_count > 0) && (_glibtop_nlist[X_NCPU].n_type == 0) && + (_glibtop_nlist[X_MP_TIME].n_type == 0)) { + /* we were compiled on an MP system but we are not running on one */ + /* so we will pretend this didn't happen and set ncpu = 1 */ + server->machine.nlist_count -= 2; + server->machine.ncpu = 1; + } +#endif + +#ifdef solbourne + { + unsigned int status, type; + + /* Get the number of CPUs on this system. */ + syscall(SYS_getcpustatus, &status, &server->machine.ncpu, &type); + } +#endif + + /* Make sure all of the symbols were found. */ + + if ((server->machine.nlist_count > 0) && + (_glibtop_check_nlist (server, _glibtop_nlist) > 0)) + _exit (1); + + /* Drop priviledges. */ + + if (setreuid (server->machine.euid, server->machine.uid)) + _exit (1); + + if (setregid (server->machine.egid, server->machine.gid)) + _exit (1); + + /* !!! END OF SUID ROOT PART !!! */ + + /* Our effective uid is now those of the user invoking the server, + so we do no longer have any priviledges. + */ + + /* NOTE: On SunOS, we do not need to be suid root, we just need to be sgid kmem. + * The server will only use setegid() to get back it's priviledges, so it + * will fail if it is suid root and not sgid kmem. + */ +} + +/* Used internally. Returns number of symbols that cannot be found in the nlist. */ + +int +_glibtop_check_nlist (void *server, register struct nlist *nlst) +{ + register int not_found; + + /* check to see if we got ALL the symbols we requested */ + /* this will write one line to stderr for every symbol not found */ + + not_found = 0; + + while (nlst->n_name != NULL) { + +#ifdef i386 + if (nlst->n_value == 0) { + glibtop_error__r (server, "kernel: no symbol named `%s'", nlst->n_name); + not_found++; + } +#else + if (nlst->n_type == 0) { + glibtop_error__r (server, "kernel: no symbol named `%s'", nlst->n_name); + not_found++; + } +#endif + + nlst++; + } + + return not_found; +} + +/* Used internally. Fetches value from kernel. */ + +int +_glibtop_getkval (void *void_server, unsigned long offset, int *ptr, int size, char *refstr) +{ + glibtop *server = (glibtop *) void_server; + + if (kvm_read (server->machine.kd, offset, ptr, size) != size) + { + if (*refstr == '!') return 0; + + glibtop_error__r (server, "kvm_read(%s): %s", + refstr, strerror (errno)); + } + + return 1; } |