1
2 #include "os/IRIX.h"
3 #include <sys/sysinfo.h>
4 #include <sys/swap.h>
5
6 #define SWAP_BLKSIZE 512 /* better if we extract the block size from
7 system call but I don't know how */
8
9 /* Make sure /proc is mounted */
OS_initialize()10 char* OS_initialize(){
11 struct statvfs svfs;
12
13 static char* no_proc = "/proc unavailable";
14 if( statvfs("/proc", &svfs) == -1 ){
15 return no_proc;
16 }
17
18 return NULL;
19 }
20
21
22 /* FIXME we should get minimum info like process ID and ownership from
23 file stat-- does this work for IOCTL-proc? Does it for FS-proc? It
24 does on linux... */
25
OS_get_table()26 void OS_get_table(){
27 DIR *procdir;
28 struct dirent *procdirp;
29 int psdata;
30 int pagesize;
31 char pathbuf[MAXPATHLEN];
32
33 struct prpsinfo psbuf;
34 struct rminfo rminfo;
35 off_t tswap; /* total swap in blocks */
36 uint totswap; /* total swap size in pages */
37
38 /* variables to hold some values for bless_into_proc */
39 char state[20];
40 char pctcpu[7];
41 char pctmem[7];
42
43 if ( (pagesize = getpagesize()) == 0 ) return;
44
45 if( (procdir = opendir( "/proc/pinfo" )) == NULL ) return;
46
47 sysmp(MP_SAGET, MPSA_RMINFO, &rminfo, sizeof(rminfo));
48 swapctl(SC_GETSWAPTOT, &tswap);
49
50 while( (procdirp = readdir(procdir)) != NULL ){
51
52 /* Only look at this file if it's a proc id; that is, all numbers */
53 if( strtok(procdirp->d_name, "0123456789") != NULL ){ continue; }
54
55 /* Construct path of the form /proc/proc_number */
56 strcpy( pathbuf, "/proc/pinfo/");
57 strcat( pathbuf, procdirp->d_name );
58
59 if( (psdata = open( pathbuf, O_RDONLY )) == -1 ) continue;
60
61 if( ioctl(psdata, PIOCPSINFO, &psbuf) == -1 ) continue;
62
63 close(psdata);
64
65 /* translate process state */
66 #if defined(sgi) && (IRIX_VERSION >= 0x006005)
67 switch( psbuf.pr_sname )
68 {
69 case 'S':
70 strcpy(state, SLEEP);
71 break;
72 case '0':
73 /* FALLTHROUGH */
74 case 'R':
75 strcpy(state, RUN);
76 break;
77 case 'Z':
78 strcpy(state, ZOMBIE);
79 break;
80 case 'T':
81 strcpy(state, STOP);
82 break;
83 case 'X':
84 strcpy(state, XBRK);
85 break;
86 }
87 #else
88 switch( psbuf.pr_state)
89 {
90 case SSLEEP:
91 strcpy(state, SLEEP);
92 break;
93 case SRUN:
94 strcpy(state, RUN);
95 break;
96 case SZOMB:
97 strcpy(state, ZOMBIE);
98 break;
99 case SSTOP:
100 strcpy(state, STOP);
101 break;
102 case SIDL:
103 strcpy(state, IDLE);
104 break;
105 case SXBRK:
106 strcpy(state, XBRK);
107 break;
108 }
109 #endif
110
111 /* These seem to be 2 bytes, 1st byte int part, 2nd byte fractional */
112 /* Perl can handle stringy numbers of the form 1.5 */
113
114 sprintf( pctcpu, "%3.2f",
115 (float) ((psbuf.pr_time.tv_sec) * 100) / (( time(NULL) - psbuf.pr_start.tv_sec ) * 100));
116
117 totswap = tswap * SWAP_BLKSIZE / pagesize;
118 sprintf( pctmem, "%5.2f",
119 ((double)psbuf.pr_size)/(double)(rminfo.physmem+totswap)*100);
120
121 bless_into_proc( Format,
122 Fields,
123
124 psbuf.pr_uid, /* uid */
125 psbuf.pr_gid, /* gid */
126 psbuf.pr_pid, /* pid */
127 psbuf.pr_ppid, /* ppid */
128 psbuf.pr_spid, /* spid */
129 psbuf.pr_pgrp, /* pgrp */
130 psbuf.pr_sid, /* sess */
131 psbuf.pr_sonproc, /* cpuid */
132
133 psbuf.pr_pri, /* priority */
134 psbuf.pr_ttydev, /* ttynum */
135 psbuf.pr_flag, /* flags */
136 psbuf.pr_time.tv_sec, /* time */
137 psbuf.pr_ctime.tv_sec, /* ctime */
138 psbuf.pr_qtime.tv_sec, /* qtime */
139 psbuf.pr_size * pagesize, /* size (bytes) */
140 psbuf.pr_rssize * pagesize, /* rss (bytes) */
141 psbuf.pr_wchan, /* wchan */
142
143 psbuf.pr_fname, /* fname */
144
145 psbuf.pr_start.tv_sec, /* start */
146
147 pctcpu, /* pctcpu */
148 state, /* state */
149 pctmem, /* pctmem */
150 psbuf.pr_psargs, /* cmndline */
151 psbuf.pr_clname /* clname */
152 );
153
154 }
155 closedir(procdir);
156 }
157