예제 #1
0
파일: m_sco5.c 프로젝트: mwongatemma/pg_top
int
machine_init(struct statics * statics)

{
	ulong		ptr;

	if ((kmem = open(KMEM, O_RDONLY)) == -1)
	{
		perror(KMEM);
		return -1;
	}
	if ((mem = open(MEM, O_RDONLY)) == -1)
	{
		perror(MEM);
		return -1;
	}

	/* get the list of symbols we want to access in the kernel */
	if (nlist(VMUNIX, nlst) == -1)
	{
		fprintf(stderr, "pg_top: nlist failed\n");
		return -1;
	}
	/* make sure they were all found */

	/*
	 * ZZ if (check_nlist(nlst) > 0) return -1;
	 */

	proca = nlst[X_PROC].n_value;

	/* get the symbol values out of kmem */
	(void) getkval(nlst[X_CUR_CPU].n_value, (int *) (&cur_cpu), sizeof(cur_cpu),
				   nlst[X_CUR_CPU].n_name);
	(void) getkval(nlst[X_HZ].n_value, (int *) (&Hz), sizeof(Hz),
				   nlst[X_HZ].n_name);
	(void) getkval(nlst[X_V].n_value, (int *) (&v), sizeof(v),
				   nlst[X_V].n_name);

	/* this is used in calculating WCPU -- calculate it ahead of time */
	logcpu = log(fabs(loaddouble(cur_cpu)));

	/* allocate space for proc structure array and array of pointers */
	bytes = v.v_proc * sizeof(struct proc);
	pbase = (struct proc *) malloc(bytes);
	pref = (struct proc **) malloc(v.v_proc * sizeof(struct proc *));
	if (pbase == (struct proc *) NULL || pref == (struct proc **) NULL)
	{
		fprintf(stderr, "pg_top: cannot allocate sufficient memory\n");
		return -1;
	}

	/* fill in the statics information */
	statics->procstate_names = procstatenames;
	statics->cpustate_names = cpustatenames;
	statics->memory_names = memorynames;
	statics->order_names = ordernames;	/* hops */

	return 0;
}
void
get_system_info(struct system_info *si)

{
    int load_avg[3];
    struct sysinfo s_info;
    struct vmker m_info;
    int i;
    double total = 0;

    /* get the load avarage array */
    getkval(avenrun_offset, (caddr_t)load_avg, sizeof load_avg, "avenrun");

    /* get the sysinfo structure */
    getkval(sysinfo_offset, (caddr_t)&s_info, sizeof s_info, "sysinfo");

    /* get vmker structure */
    getkval(vmker_offset, (caddr_t)&m_info, sizeof m_info, "vmker");

    /* convert load avarages to doubles */
    for (i = 0; i < 3; i++)
	si->load_avg[i] = (double)load_avg[i]/65536.0;

    /* calculate cpu state in percentages */
    for (i = 0; i < CPU_NTIMES; i++) {
	cp_old[i] = cp_time[i];
	cp_time[i] = s_info.cpu[i];
	cp_diff[i] = cp_time[i] - cp_old[i];
	total += cp_diff[i];
    }

    total = total/1000.0;  /* top itself will correct this */
    for (i = 0; i < CPU_NTIMES; i++) {
        cpu_states[i] = cp_diff[i] / total;
    }

    /* calculate memory statistics, scale 4K pages to megabytes */
#define PAGE_TO_MB(a) ((a)*4/1024)
    memory_stats[M_REAL]     = PAGE_TO_MB(m_info.totalmem);
    memory_stats[M_REALFREE] = PAGE_TO_MB(m_info.freemem);
    memory_stats[M_BUFFERS]  = PAGE_TO_MB(m_info.numperm);
    swap_stats[M_VIRTUAL]  = PAGE_TO_MB(m_info.totalvmem);
    swap_stats[M_VIRTFREE] = PAGE_TO_MB(m_info.freevmem);

    /* runnable processes */
    process_states[0] = s_info.runque - old_runque;
    old_runque = s_info.runque;

    si->cpustates = cpu_states;
    si->memory = memory_stats;
    si->swap = swap_stats;
}
예제 #3
0
void
get_system_info (struct system_info *si)
{
  long avenrun[3];
  struct sysinfo sysinfo;
  struct vmtotal total;
  struct anoninfo anoninfo;
  static time_t cp_old[CPUSTATES];
  static time_t cp_diff[CPUSTATES];	/* for cpu state percentages */
  register int i;

  getsysinfo(&sysinfo);

  /* convert cp_time counts to percentages */
  (void) percentages (CPUSTATES, cpu_states, sysinfo.cpu, cp_old, cp_diff);

  /* Find total CPU utilization, as a fraction of 1. */
  total_cpu = (cpu_states[CPU_USER] + cpu_states[CPU_KERNEL]) / 1000.0;

  /* get mpid -- process id of last process */
  (void) getkval (mpid_offset, &(si->last_pid), sizeof (si->last_pid),
		  "mpid");

  /* get load average array */
  (void) getkval (avenrun_offset, (int *) avenrun, sizeof (avenrun), "avenrun");

  /* convert load averages to doubles */
  for (i = 0; i < 3; i++)
    si->load_avg[i] = loaddouble (avenrun[i]);

  /* get total -- systemwide main memory usage structure */
  (void) getkval (total_offset, (int *) (&total), sizeof (total), "total");
  /* convert memory stats to Kbytes */
  memory_stats[0] = pagetok (total.t_rm);
  memory_stats[1] = pagetok (total.t_arm);
  memory_stats[2] = pagetok (total.t_free);
  (void) getkval (anoninfo_offset, (int *) (&anoninfo), sizeof (anoninfo),
		  "anoninfo");
  memory_stats[3] = pagetok (anoninfo.ani_max - anoninfo.ani_free);
  memory_stats[4] = pagetok (anoninfo.ani_max - anoninfo.ani_resv);

  /* set arrays and strings */
  si->cpustates = cpu_states;
  si->memory = memory_stats;
}
예제 #4
0
파일: m_sco5.c 프로젝트: mwongatemma/pg_top
struct region *
get_region(void *ptr)
{
	static struct region reg;
	long		addr = (long) ptr;

	(void) getkval(addr, (struct region *) (&reg),
				   sizeof(struct region), "region");
	return &reg;
}
예제 #5
0
void
get_system_info(struct system_info * si)

{
	int			i;
	int			avenrun[3];
	struct rminfo realmem;
	struct sysinfo sysinfo;
	static time_t cp_old[CPU_STATES];
	static time_t cp_diff[CPU_STATES];	/* for cpu state percentages */
	off_t		fswap;			/* current free swap in blocks */
	off_t		tswap;			/* total swap in blocks */

	(void) getkval(avenrun_offset, (int *) avenrun, sizeof(avenrun), "avenrun");

	for (i = 0; i < 3; i++)
	{
		si->load_avg[i] = loaddouble(avenrun[i]);
		si->load_avg[i] /= 1024.0;
	}

	if ((numcpus = sysmp(MP_NPROCS)) == -1)
	{
		perror("sysmp(MP_NPROCS)");
		return;
	}

	if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == -1)
	{
		perror("sysmp(MP_SAGET,MPSA_RMINFO, ...)");
		return;
	}

	swapctl(SC_GETFREESWAP, &fswap);
	swapctl(SC_GETSWAPTOT, &tswap);

	memory_stats[0] = pagetok(realmem.physmem);
	memory_stats[1] = pagetok(realmem.availrmem);
	memory_stats[2] = pagetok(realmem.freemem);
	memory_stats[3] = tswap / 2;
	memory_stats[4] = fswap / 2;

	if (sysmp(MP_SAGET, MPSA_SINFO, &sysinfo, sizeof(struct sysinfo)) == -1)
	{
		perror("sysmp(MP_SAGET,MPSA_SINFO)");
		return;
	}
	(void) percentages(CPU_STATES, cpu_states, sysinfo.cpu, cp_old, cp_diff);

	si->cpustates = cpu_states;
	si->memory = memory_stats;
	si->last_pid = -1;

	return;
}
예제 #6
0
파일: m_sco5.c 프로젝트: mwongatemma/pg_top
void
get_system_info(struct system_info * si)

{
	long		total;

	/* get process id of the last process */
	(void) getkval(nlst[X_MPID].n_value, &(si->last_pid),
				   sizeof(si->last_pid),
				   nlst[X_MPID].n_name);
	/* get the cp_time array */
	(void) getkval(nlst[X_SYSINFO].n_value, (int *) cp_time, sizeof(cp_time),
				   nlst[X_SYSINFO].n_name);

	/* convert cp_time counts to persentages */
	total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);

	/* sum memory statistics */
	(void) getkval(nlst[X_PHYSMEM].n_value, &memory_stats[0],
				   sizeof(memory_stats[0]), nlst[X_PHYSMEM].n_name);
	(void) getkval(nlst[X_MAXMEM].n_value, &memory_stats[1],
				   sizeof(memory_stats[1]), nlst[X_MAXMEM].n_name);
	(void) getkval(nlst[X_FREEMEM].n_value, &memory_stats[2],
				   sizeof(memory_stats[2]), nlst[X_FREEMEM].n_name);
	(void) getkval(nlst[X_AVAILRMEM].n_value, &memory_stats[3],
				   sizeof(memory_stats[3]), nlst[X_AVAILRMEM].n_name);
	(void) getkval(nlst[X_AVAILSMEM].n_value, &memory_stats[4],
				   sizeof(memory_stats[4]), nlst[X_AVAILSMEM].n_name);
	(void) getkval(nlst[X_NSWAP].n_value, &memory_stats[5],
				   sizeof(memory_stats[5]), nlst[X_NSWAP].n_name);
	memory_stats[0] = bytetok(ctob(memory_stats[0]));	/* clicks -> bytes */
	memory_stats[1] = bytetok(ctob(memory_stats[1]));	/* clicks -> bytes */
	memory_stats[2] = bytetok(ctob(memory_stats[2]));	/* clicks -> bytes */
	memory_stats[3] = bytetok(memory_stats[3] * NBPP);	/* # bytes per page */
	memory_stats[4] = bytetok(memory_stats[4] * NBPP);	/* # bytes per page */
	memory_stats[5] = bytetok(memory_stats[5] * NBPSCTR);		/* # bytes per sector */

	/* set arrays and strings */

	/*
	 * Note: we keep memory_stats as an unsigned long to avoid sign extension
	 * problems when shifting in bytetok. But the module interface requires an
	 * array of signed longs. So we just cast the pointer here and hope for
	 * the best.   --wnl
	 */
	si->cpustates = cpu_states;
	si->memory = (long *) memory_stats;

	tab_avenrun(si->load_avg);	/* philiph */
}
/*
 * Initialize globals, get kernel offsets and stuff...
 */
machine_init(struct statics *statics)

{
    time_t uptime, now;
    struct tms tbuf;

    if ((kmem = open(KMEM, O_RDONLY)) == -1) {
	perror(KMEM);
	return -1;
    }

    /* get kernel symbol offsets */
    if (knlist(nlst, 5, sizeof(struct nlist)) != 0) {
	perror("knlist");
	return -1;
    }
    avenrun_offset = nlst[X_AVENRUN].n_value;
    sysinfo_offset = nlst[X_SYSINFO].n_value;
    vmker_offset   = nlst[X_VMKER].n_value;
    proc_offset    = nlst[X_PROC].n_value;
    v_offset       = nlst[X_V].n_value;

    getkval(v_offset, (caddr_t)&v_info, sizeof v_info, "v");

    ncpus = v_info.v_ncpus;	/* number of cpus */
    nprocs = PROCMASK(PIDMAX);
    if (nprocs > 1024) nprocs = 1024;

    ptsize = nprocs * sizeof (struct proc);
    p_proc = (struct proc *)malloc(ptsize);
    p_info = (struct procsinfo *)malloc(nprocs * sizeof (struct procsinfo));
    pref = (struct procsinfo **)malloc(nprocs * sizeof (struct procsinfo *));

    if (!p_proc || !p_info || !pref) {
	fprintf(stderr, "top: not enough memory\n");
	return -1;
    }

    /* set boot time */
    now = time(NULL);
    uptime = times(&tbuf) / HZ;
    statics->boottime = now - uptime;

    statics->procstate_names = procstatenames;
    statics->cpustate_names = cpustatenames;
    statics->memory_names = memorynames;
    statics->order_names = ordernames;
    statics->swap_names = swapnames;

    return(0);
}
예제 #8
0
static double get_cpu_load()
{
   kernel_fd_type kernel_fd;
   long address = 0;
   static long cpu_time[CPUSTATES];
   static long cpu_old[CPUSTATES];
   static long cpu_diff[CPUSTATES];
   double cpu_states[CPUSTATES];
   double cpu_load;

   if (sge_get_kernel_fd(&kernel_fd)
       && sge_get_kernel_address("cp_time", &address)) {
      getkval(address, (void*)&cpu_time, sizeof(cpu_time), "cp_time"); 
      percentages(CPUSTATES, cpu_states, cpu_time, cpu_old, cpu_diff);
      cpu_load = cpu_states[0] + cpu_states[1] + cpu_states[2];
      if (cpu_load < 0.0) {
         cpu_load = -1.0;
      }
   } else {
      cpu_load = -1.0;
   }
   return cpu_load;
}    
예제 #9
0
getu(register struct proc *p, struct user *u)
{
    register int nbytes, n;
    struct task task;
    struct utask utask;
    struct uthread thread;

    /*
     *  Check if the process is currently loaded or swapped out.  The way we
     *  get the u area is totally different for the two cases.  For this
     *  application, we just don't bother if the process is swapped out.
     */
    /* NEXTSTEP proc.h
     * One structure allocated per active
     * process. It contains all data needed
     * about the process while the
     * process may be swapped out.
     * Other per process data (user.h)
     * is swapped with the process.
     */

    if ((p->p_flag & SLOAD) == 0) {
        /* User info is always in core.
         * #ifdef DOSWAP
         * 		if (lseek(swap, (long)dtob(p->p_swaddr), 0) == -1) {
         * 	    	perror("lseek(swap)");
         * 	    	return(-1);
         * 		}
         * 		if (read(swap, (char *) u, sizeof(struct user)) != sizeof(struct user))  {
         * 	    	perror("read(swap)");
         * 	    	return(-1);
         * 		}
         * 		return (1);
         * #else
         */
        return(-1);
        /*#endif
         */
    }

    /*
     *  Process is currently in memory, we hope!
     */
    if(!getkval(p->task, (int *)&task, sizeof(struct task), "task")) {
#ifdef DEBUG
        perror("getkval(p->task)");
#endif
        /* we can't seem to get to it, so pretend it's swapped out */
        return(-1);
    }

    if(!getkval(task.u_address, (int *)&utask, sizeof(struct utask), "task.u_address")) {
#ifdef DEBUG
        perror("getkval(task->utask)");
#endif
        /* we can't seem to get to it, so pretend it's swapped out */
        return(-1);
    }

    /* Copy utask and uthread info into struct user *u */
    /*  This is incomplete.  Only copied info needed. */
    u->u_procp = utask.uu_procp;
    u->u_ar0 = utask.uu_ar0;
    u->u_ru = utask.uu_ru;
    strcpy(u->u_comm, utask.uu_comm);
    nbytes = strlen(u->u_comm);
    for(n=nbytes; n<MAXCOMLEN; n++)
        u->u_comm[n] = ' ';
    u->u_comm[MAXCOMLEN] = '\0';
    return(0);
}
예제 #10
0
caddr_t get_process_info(struct system_info *si,
                         struct process_select *sel,
                         int (*compare)())
{
    int i, j;
    int total_procs;
    int active_procs;
    struct proc *pp;
    struct task_basic_info taskInfo;
    struct thread_basic_info threadInfo;
    kern_return_t thread_status;
    kern_return_t task_status;
    int threadCount;

    /* these are copied out of sel for speed */
    int show_idle;
    int show_system;
    int show_uid;
    int show_command;

    /* get a pointer to the states summary array */
    si->procstates = process_states;

    /* set up flags which define what we are going to select */
    show_idle = sel->idle;
    show_system = sel->system;
    show_uid = sel->uid != -1;
    show_command = sel->command != NULL;

    (void) getkval(nlst[X_PROC].n_value, (int *)(&proc), sizeof(proc),
                   nlst[X_PROC].n_un.n_name);

    /* count up process states and get pointers to interesting procs */
    total_procs = 0;
    active_procs = 0;
    memset((char *)process_states, 0, sizeof(process_states));
    i = 0;
    j = 0;
    do {
        if(i == 0) {
            /* read first proc structure */
            (void) getkval(proc, (int *)&pbase[i], sizeof(struct proc), "first proc");
        } else {
            (void) getkval(pp->p_nxt, (int *)&pbase[i], sizeof(struct proc), "nxt proc");
        }
        pp = &pbase[i];

        thread_status = thread_stats(pp->p_pid, &threadInfo, &threadCount);
        task_status = task_stats(pp->p_pid, &taskInfo);
        /*
         *  Process slots that are actually in use have a non-zero
         *  status field.  Processes with SSYS set are system
         *  processes---these get ignored unless show_sysprocs is set.
         */
        if (pp->p_stat != 0 &&
                (show_system || ((pp->p_flag & SSYS) == 0)))
        {
            total_procs++;
            /* Using thread info for process states. */
            /*	    	process_states[pp->p_stat]++; */
            if(thread_status==KERN_SUCCESS)
                process_states[threadInfo.run_state]++;
            if ((pp->p_stat != SZOMB) &&
                    (show_idle || (pp->p_stat == SRUN)) &&
                    (!show_uid || pp->p_uid == (uid_t)sel->uid))
            {
                pref[j].p_self = pp;
                if(thread_status==KERN_SUCCESS)
                {
                    pref[j].run_state = threadInfo.run_state;
                    pref[j].flags = threadInfo.flags;
                    pref[j].p_pctcpu = threadInfo.cpu_usage;
                    pref[j].p_cptime = threadInfo.user_time.seconds +
                                       threadInfo.system_time.seconds;
                    pref[j].cur_priority = threadInfo.cur_priority;
                    pref[j].nthreads = threadCount;
                } else {
                    pref[j].run_state = 0;
                    pref[j].flags = 0;
                    pref[j].p_pctcpu = 0;
                    pref[j].p_cptime = 0;
                }
                /* Get processes memory usage and cputime */
                if(task_status==KERN_SUCCESS)
                {
                    pref[j].p_rsize = taskInfo.resident_size/1024;
                    pref[j].p_vsize = taskInfo.virtual_size/1024;
                } else {
                    pref[j].p_rsize = 0;
                    pref[j].p_vsize = 0;
                }
                active_procs++;
                j++;
            }
        }
        i++;
    } while(pp->p_nxt != 0);
    pref[j].p_self = NULL;  /*  End list of processes with NULL */

    /* if requested, sort the "interesting" processes */
    if (compare != NULL)
    {
        qsort((char *)pref, active_procs, sizeof(struct proc_unix), compare);
    }

    /* remember active and total counts */
    si->p_total = total_procs;
    si->p_active = pref_count = active_procs;

    /* pass back a handle */
    handle.list = pref;
    handle.count = active_procs;
    handle.current = 0;
    return((caddr_t)&handle);
}
예제 #11
0
get_system_info(struct system_info *si)
{
    long avenrun[3];
    long total;

    /* get the cp_time array */
    (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),
                   "_cp_time");

    /* get load average array */
    (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun),
                   "_avenrun");

    /* get mpid -- process id of last process */
    (void) getkval(mpid_offset, &(si->last_pid), sizeof(si->last_pid),
                   "_mpid");

    /* convert load averages to doubles */
    {
        register int i;
        for(i=0; i<3; i++)
            si->load_avg[i] = ((double)avenrun[i])/LSCALE;
    }

    /* convert cp_time counts to percentages */
    total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);

    /* sum memory statistics */
    {
        /* get total -- systemwide main memory usage structure */
        /* Does not work on NeXT system.  Use vm_statistics() for paging info. */
        /* struct vmtotal total;
         * (void) getkval(total_offset, (int *)(&total), sizeof(total),
         *	       "_total");
         */
        /* convert memory stats to Kbytes */
        /* memory_stats[0] = -1;
         * memory_stats[1] = pagetok(total.t_arm);
         * memory_stats[2] = pagetok(total.t_rm);
         * memory_stats[3] = -1;
         * memory_stats[4] = pagetok(total.t_avm);
         * memory_stats[5] = pagetok(total.t_vm);
         * memory_stats[6] = -1;
         * memory_stats[7] = pagetok(total.t_free);
         */
        kern_return_t status;
        unsigned int count=HOST_BASIC_INFO_COUNT;
        status = vm_statistics(task_self(), &vm_stats);
#ifdef DEBUG
        if(status != KERN_SUCCESS)
            mach_error("An error calling vm_statistics()!", status);
#endif
        status = host_info(host_self(), HOST_BASIC_INFO, (host_info_t)&host_stats, &count);
#ifdef DEBUG
        if(status != KERN_SUCCESS)
            mach_error("An error calling host_info()!", status);
#endif
        /* convert memory stats to Kbytes */
        memory_stats[0] = pagetok(host_stats.memory_size / vm_stats.pagesize);
        memory_stats[1] = pagetok(vm_stats.active_count);
        memory_stats[2] = pagetok(vm_stats.inactive_count);
        memory_stats[3] = pagetok(vm_stats.wire_count);
        memory_stats[4] = pagetok(vm_stats.free_count);
        if (swappgsin < 0)
        {
            memory_stats[5] = 1;
            memory_stats[6] = 1;
        } else {
            memory_stats[5] = pagetok(((vm_stats.pageins - swappgsin)));
            memory_stats[6] = pagetok(((vm_stats.pageouts - swappgsout)));
        }
        swappgsin = vm_stats.pageins;
        swappgsout = vm_stats.pageouts;
    }

    /* set arrays and strings */
    si->cpustates = cpu_states;
    si->memory = memory_stats;
}
예제 #12
0
machine_init(struct statics *statics)
{
    register int i = 0;
    register int pagesize;

    if ((kmem = open(KMEM, O_RDONLY)) == -1) {
        perror(KMEM);
        return(-1);
    }
    if ((mem = open(MEM, O_RDONLY)) == -1) {
        perror(MEM);
        return(-1);
    }

#ifdef DOSWAP
    if ((swap = open(SWAP, O_RDONLY)) == -1) {
        perror(SWAP);
        return(-1);
    }
#endif

    /* get the list of symbols we want to access in the kernel */
    (void) nlist(VMUNIX, nlst);
    if (nlst[0].n_type == 0)
    {
        fprintf(stderr, "top: nlist failed\n");
        return(-1);
    }

    /* make sure they were all found */
    if (i > 0 && check_nlist(nlst) > 0)
    {
        return(-1);
    }

    /* get the symbol values out of kmem */
    (void) getkval(nlst[X_PROC].n_value, (int *)(&proc), sizeof(proc),
                   nlst[X_PROC].n_un.n_name);
    (void) getkval(nlst[X_NPROC].n_value, &nproc, sizeof(nproc),
                   nlst[X_NPROC].n_un.n_name);
    (void) getkval(nlst[X_HZ].n_value, (int *)(&hz), sizeof(hz),
                   nlst[X_HZ].n_un.n_name);
    /*    (void) getkval(nlst[X_CCPU].n_value, (int *)(&ccpu), sizeof(ccpu),
     *	    			nlst[X_CCPU].n_un.n_name);
     */

    /* stash away certain offsets for later use */
    mpid_offset = nlst[X_MPID].n_value;
    avenrun_offset = nlst[X_AVENRUN].n_value;
    total_offset = nlst[X_TOTAL].n_value;
    cp_time_offset = nlst[X_CP_TIME].n_value;


    /* this is used in calculating WCPU -- calculate it ahead of time */
    /*	ccpu = mach_load_avg();
     *   logcpu = log((double)(ccpu)/LOAD_SCALE);
     */

    /* allocate space for proc structure array and array of pointers */
    bytes = nproc * sizeof(struct proc);
    pbase = (struct proc *)malloc(bytes);
    pref  = (struct proc_unix *)malloc((nproc+1) * sizeof(struct proc_unix *));

    /* Just in case ... */
    if (pbase == (struct proc *)NULL || pref == (struct proc_unix *)NULL)
    {
        fprintf(stderr, "top: can't allocate sufficient memory\n");
        return(-1);
    }

    /* get the page size with "getpagesize" and calculate pageshift from it */
    pagesize = getpagesize();
    pageshift = ceil(log(pagesize)/log(2.0));

    /* we only need the amount of log(2)1024 for our conversion */
    pageshift -= LOG1024;

    /* fill in the statics information */
    statics->procstate_names = procstatenames;
    statics->cpustate_names = cpustatenames;
    statics->memory_names = memorynames;

    /* all done! */
    return(0);
}
예제 #13
0
파일: m_sco5.c 프로젝트: mwongatemma/pg_top
caddr_t
get_process_info(struct system_info * si,
				 struct process_select * sel,
				 int idx)

{
	register int i;
	register int total_procs;
	register int active_procs;
	register struct proc **prefp;
	register struct proc *pp;

/* set up flags of what we are going to select */
/* these are copied out of sel for simplicity */
	int			show_idle = sel->idle;
	int			show_system = sel->system;
	int			show_uid = sel->uid != -1;
	int			show_command = sel->command != NULL;

	/* read all the proc structures in one fell swoop */
	(void) getkval(proca, (int *) pbase, bytes, "proc array");

	/* get a pointer to the states summary array */
	si->procstates = process_states;

	/* count up process states and get pointers to interesting procs */
	total_procs = active_procs = 0;
	memset((char *) process_states, 0, sizeof(process_states));
	prefp = pref;
	for (pp = pbase, i = 0; i < v.v_proc; pp++, i++)
	{
		/*
		 * Place pointers to each valid proc structure in pref[]. Process
		 * slots that are actually in use have a non-zero status field.
		 * Processes with SSYS set are system processes -- these are ignored
		 * unless show_system is set.
		 */
		if (pp->p_stat && (show_system || ((pp->p_flag & SSYS) == 0)))
		{
			total_procs++;
			process_states[pp->p_stat]++;
			if ((pp->p_stat != SZOMB) &&
			(show_idle || (pp->p_stat == SRUN) || (pp->p_stat == SONPROC)) &&
				(!show_uid || pp->p_uid == (ushort) sel->uid))
			{
				*prefp++ = pp;
				active_procs++;
			}
		}
	}

	/* if requested, sort the "interesting" processes */
	qsort((char *) pref, active_procs, sizeof(struct proc *), proc_compares[idx]);

	/* remember active and total counts */
	SI_TOTAL(si) = total_procs;
	SI_ACTIVE(si) = pref_len = active_procs;

	/* pass back a handle */
	handle.next_proc = pref;
	handle.remaining = active_procs;
	return ((caddr_t) & handle);
}
caddr_t
get_process_info(struct system_info *si, struct process_select *sel, int compare_index)

{
    int i, nproc;
    int ptsize_util;
    int active_procs = 0, total_procs = 0;
    struct procsinfo *pp, **p_pref = pref;
    unsigned long pctcpu;
    pid_t procsindex = 0;
    struct proc *p;

    si->procstates = process_states;

    curtime = time(0);

    /* get the procsinfo structures of all running processes */
    nproc = getprocs(p_info, sizeof (struct procsinfo), NULL, 0, 
		     &procsindex, nprocs);
    if (nproc < 0) {
	perror("getprocs");
	quit(1);
    }

    /* the swapper has no cmd-line attached */
    strcpy(p_info[0].pi_comm, "swapper");
    
    /* get proc table */
    ptsize_util = (PROCMASK(p_info[nproc-1].pi_pid)+1) * sizeof(struct proc);
    getkval(proc_offset, (caddr_t)p_proc, ptsize_util, "proc");

    memset(process_states, 0, sizeof process_states);

    /* build a list of pointers to processes to show. walk through the
     * list of procsinfo structures instead of the proc table since the
     * mapping of procsinfo -> proctable is easy, the other way around
     * is cumbersome
     */
    for (pp = p_info, i = 0; i < nproc; pp++, i++) {

	p = &p_proc[PROCMASK(pp->pi_pid)];

	/* AIX marks all runnable processes as ACTIVE. We want to know
	   which processes are sleeping, so check used cpu ticks and adjust
	   status field accordingly
	 */
	if (p->p_stat == SACTIVE && p->p_cpticks == 0)
	    p->p_stat = SIDL;

        if (pp->pi_state && (sel->system || ((pp->pi_flags & SKPROC) == 0))) {
	    total_procs++;
	    process_states[p->p_stat]++;
	    if ( (pp->pi_state != SZOMB) &&
		(sel->idle || p->p_cpticks != 0 || (p->p_stat == SACTIVE))
		&& (sel->uid == -1 || pp->pi_uid == (uid_t)sel->uid)) {
                *p_pref++ = pp;
		active_procs++;
	    }
	}
    }   

    /* the pref array now holds pointers to the procsinfo structures in
     * the p_info array that were selected for display
     */

    /* sort if requested */
    if (si->p_active)
	qsort((char *)pref, active_procs, sizeof (struct procsinfo *), 
	      proc_compares[compare_index]);
    
    si->last_pid = -1;		/* no way to figure out last used pid */
    si->p_total = total_procs;
    si->p_active = pref_len = active_procs;

    handle.next_proc = pref;
    handle.remaining = active_procs;

    return((caddr_t)&handle);
}
예제 #15
0
파일: m_svr4.c 프로젝트: denishpatel/pg_top
caddr_t
get_process_info(
				 struct system_info * si,
				 struct process_select * sel,
				 int x)
{
	register int i;
	register int total_procs;
	register int active_procs;
	register struct prpsinfo **prefp;
	register struct prpsinfo *pp;

	/* these are copied out of sel for speed */
	int			show_idle;
	int			show_system;
	int			show_uid;

	/* Get current number of processes */
	(void) getkval(nproc_offset, (int *) (&nproc), sizeof(nproc), "nproc");

	/* read all the proc structures */
	getptable(pbase);

	/* get a pointer to the states summary array */
	si->procstates = process_states;

	/* set up flags which define what we are going to select */
	show_idle = sel->idle;
	show_system = sel->system;
	show_uid = sel->uid != -1;

	/* count up process states and get pointers to interesting procs */
	total_procs = 0;
	active_procs = 0;
	(void) memset(process_states, 0, sizeof(process_states));
	prefp = pref;

	for (pp = pbase, i = 0; i < nproc; pp++, i++)
	{
		/*
		 * Place pointers to each valid proc structure in pref[]. Process
		 * slots that are actually in use have a non-zero status field.
		 * Processes with SSYS set are system processes---these get ignored
		 * unless show_sysprocs is set.
		 */
		if (pp->pr_state != 0 &&
			(show_system || ((pp->pr_flag & SSYS) == 0)))
		{
			total_procs++;
			process_states[pp->pr_state]++;
			if ((!pp->pr_zomb) &&
				(show_idle || (pp->pr_state == SRUN) || (pp->pr_state == SONPROC)) &&
				(!show_uid || pp->pr_uid == (uid_t) sel->uid))
			{
				*prefp++ = pp;
				active_procs++;
			}
		}
	}

	/* if requested, sort the "interesting" processes */
	qsort((char *) pref, active_procs, sizeof(struct prpsinfo *), proc_compare);

	/* remember active and total counts */
	si->p_total = total_procs;
	si->p_active = active_procs;

	/* pass back a handle */
	handle.next_proc = pref;
	handle.remaining = active_procs;
	return ((caddr_t) & handle);
}
예제 #16
0
파일: m_svr4.c 프로젝트: denishpatel/pg_top
void
get_system_info(struct system_info * si)
{
	long		avenrun[3];
	struct sysinfo sysinfo;
	static struct sysinfo *mpinfo = NULL;		/* array, per-processor
												 * sysinfo structures. */
	struct vmtotal total;
	struct anoninfo anoninfo;
	static long cp_old[CPUSTATES];
	static long cp_diff[CPUSTATES];		/* for cpu state percentages */
	static int	num_cpus;
	static int	fd_cpu = 0;
	register int i;

	if (use_stats == 1)
	{
		if (fd_cpu == 0)
		{
			if ((fd_cpu = open("/stats/cpuinfo", O_RDONLY)) == -1)
			{
				(void) fprintf(stderr, "%s: Open of /stats/cpuinfo failed\n", myname);
				quit(2);
			}
			if (read(fd_cpu, &num_cpus, sizeof(int)) != sizeof(int))
			{
				(void) fprintf(stderr, "%s: Read of /stats/cpuinfo failed\n", myname);
				quit(2);
			}
			close(fd_cpu);
		}
		if (mpinfo == NULL)
		{
			mpinfo = (struct sysinfo *) calloc(num_cpus, sizeof(mpinfo[0]));
			if (mpinfo == NULL)
			{
				(void) fprintf(stderr, "%s: can't allocate space for per-processor sysinfos\n", myname);
				quit(12);
			}
		}
		/* Read the per cpu sysinfo structures into mpinfo struct. */
		read_sysinfos(num_cpus, mpinfo);
		/* Add up all of the percpu sysinfos to get global sysinfo */
		sysinfo_data(num_cpus, &sysinfo, mpinfo);
	}
	else
	{
		(void) getkval(sysinfo_offset, &sysinfo, sizeof(struct sysinfo), "sysinfo");
	}

	/* convert cp_time counts to percentages */
	(void) percentages(CPUSTATES, cpu_states, sysinfo.cpu, cp_old, cp_diff);

	/* get mpid -- process id of last process */
	(void) getkval(mpid_offset, &(si->last_pid), sizeof(si->last_pid),
				   "mpid");

	/* get load average array */
	(void) getkval(avenrun_offset, (int *) avenrun, sizeof(avenrun), "avenrun");

	/* convert load averages to doubles */
	for (i = 0; i < 3; i++)
		si->load_avg[i] = loaddouble(avenrun[i]);

	/* get total -- systemwide main memory usage structure */
	(void) getkval(total_offset, (int *) (&total), sizeof(total), "total");
	/* convert memory stats to Kbytes */
	memory_stats[0] = pagetok(total.t_rm);
	memory_stats[1] = pagetok(total.t_arm);
	memory_stats[2] = pagetok(total.t_free);
	(void) getkval(anoninfo_offset, (int *) (&anoninfo), sizeof(anoninfo),
				   "anoninfo");
	memory_stats[3] = pagetok(anoninfo.ani_max - anoninfo.ani_free);
	memory_stats[4] = pagetok(anoninfo.ani_max - anoninfo.ani_resv);

	/* set arrays and strings */
	si->cpustates = cpu_states;
	si->memory = memory_stats;
}
예제 #17
0
파일: m_svr4.c 프로젝트: denishpatel/pg_top
int
machine_init(struct statics * statics)
{
	static struct var v;

	/* fill in the statics information */
	statics->procstate_names = procstatenames;
	statics->cpustate_names = cpustatenames;
	statics->memory_names = memorynames;

	/* get the list of symbols we want to access in the kernel */
	if (nlist(UNIX, nlst))
	{
		(void) fprintf(stderr, "Unable to nlist %s\n", UNIX);
		return (-1);
	}

	/* make sure they were all found */
	if (check_nlist(nlst) > 0)
		return (-1);

	/* open kernel memory */
	if ((kmem = open(KMEM, O_RDONLY)) == -1)
	{
		perror(KMEM);
		return (-1);
	}

	/* get the symbol values out of kmem */
	/* NPROC Tuning parameter for max number of processes */
	(void) getkval(nlst[X_V].n_value, &v, sizeof(struct var), nlst[X_V].n_name);
	nproc = v.v_proc;

	/* stash away certain offsets for later use */
	mpid_offset = nlst[X_MPID].n_value;
	nproc_offset = nlst[X_NPROC].n_value;
	avenrun_offset = nlst[X_AVENRUN].n_value;
	anoninfo_offset = nlst[X_ANONINFO].n_value;
	total_offset = nlst[X_TOTAL].n_value;
/* JJ this may need to be changed */
	sysinfo_offset = nlst[X_SYSINFO].n_value;

	/* allocate space for proc structure array and array of pointers */
	bytes = nproc * sizeof(struct prpsinfo);
	pbase = (struct prpsinfo *) malloc(bytes);
	pref = (struct prpsinfo **) malloc(nproc * sizeof(struct prpsinfo *));

	/* Just in case ... */
	if (pbase == (struct prpsinfo *) NULL || pref == (struct prpsinfo **) NULL)
	{
		(void) fprintf(stderr, "%s: can't allocate sufficient memory\n", myname);
		return (-1);
	}

	if (!(proc_dir = opendir(PROCFS)))
	{
		(void) fprintf(stderr, "Unable to open %s\n", PROCFS);
		return (-1);
	}

	if (chdir(PROCFS))
	{							/* handy for later on when we're reading it */
		(void) fprintf(stderr, "Unable to chdir to %s\n", PROCFS);
		return (-1);
	}

	/* all done! */
	return (0);
}
예제 #18
0
int
machine_init (struct statics *statics)
  {
    int i;
    static struct var v;

    /* fill in the statics information */
    statics->procstate_names = procstatenames;
    statics->cpustate_names = cpustatenames;
    statics->memory_names = memorynames;

    /* get the list of symbols we want to access in the kernel */
    if (nlist (UNIX, nlst))
      {
	(void) fprintf (stderr, "Unable to nlist %s\n", UNIX);
	return (-1);
      }

    /* make sure they were all found */
    if (check_nlist (nlst) > 0)
      return (-1);

    /* open kernel memory */
    if ((kmem = open (KMEM, O_RDONLY)) == -1)
      {
	perror (KMEM);
	return (-1);
      }

    /* Open the sar driver device node. */
    if ((sar = open(SAR, O_RDONLY)) == -1)
      {
        perror (SAR);
        return (-1);
      }


    /* get the symbol values out of kmem */
    /* NPROC Tuning parameter for max number of processes */
    (void) getkval (nlst[X_V].n_value, &v, sizeof (struct var), nlst[X_V].n_name);
    nproc = v.v_proc;

    /* stash away certain offsets for later use */
    mpid_offset = nlst[X_MPID].n_value;
    nproc_offset = nlst[X_NPROC].n_value;
    avenrun_offset = nlst[X_AVENRUN].n_value;
    anoninfo_offset = nlst[X_ANONINFO].n_value;
    total_offset = nlst[X_TOTAL].n_value;

    /* allocate space for proc structure array and array of pointers */
    bytes = nproc * sizeof (struct prpsinfo);
    pbase = (struct prpsinfo *) malloc (bytes);
    pref = (struct prpsinfo **) malloc (nproc * sizeof (struct prpsinfo *));

    /* Just in case ... */
    if (pbase == (struct prpsinfo *) NULL || pref == (struct prpsinfo **) NULL)
      {
	(void) fprintf (stderr, "%s: can't allocate sufficient memory\n", myname);
	return (-1);
      }

    if (!(procdir = opendir (PROCFS)))
      {
	(void) fprintf (stderr, "Unable to open %s\n", PROCFS);
	return (-1);
      }

    if (chdir (PROCFS))
      {				/* handy for later on when we're reading it */
	(void) fprintf (stderr, "Unable to chdir to %s\n", PROCFS);
	return (-1);
      }

    /* Set up the pointers to the sysinfo data area. */
    spa.uvcp = (caddr_t) &cpu_state[0];
    spa.uvsp = (caddr_t) &cpu_sysinfo[0];

    timedata.lnext = &timedata;
    timedata.llast = &timedata;

    for (i = 0; i < HASHSZ; i++) {
      hash[i].hnext = (timedata_t *)&hash[i];
      hash[i].hlast = (timedata_t *)&hash[i];
    }

    /* all done! */
    return (0);
  }