File: sysview.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (442 lines) | stat: -rw-r--r-- 8,915 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

/*
 * Systems statistics viewing application
 *  (C) Javier Velasco	'97 (almost '98)
 *	fjvelasco@sinix.net
 *
 *  This application was developed on an INDIGO2 Extreme and makes use of
 *  a SGI system call (sginap) that sleeps the process for a given number of
 *  clock ticks. For other UNIX systems, this call should be substituted for 
 *  another proper call.
 *  The default number of ticks between samples is 20. This can be changed
 *  through the mouse right button menu.
 *
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysmp.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <unistd.h>

/* GL includes */
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>


/*
 * Macros
 */
#define GRID		0x22
#define ZGRID		0x23
#define XGRID		0x24
#define YGRID		0x25

float lastx=0;	/* mouse track */
float lasty=0;

void *font1 = GLUT_BITMAP_9_BY_15; /* used fonts */
void *font2 = GLUT_BITMAP_8_BY_13;

long nPeriod=20;	    /* default sampling rate in ticks */
GLsizei nWidth, nHeight;    /* current window size */
GLboolean bMotion=False;

struct sysinfo SysInfo, LastSysInfo;	/* system information */

/*
 * Mouse motion track
 */
void MouseMove(int x, int y)
{
    if(bMotion)
	{
	lastx = x;
	lasty = y;
	glutPostRedisplay();    
	}
}

/*
 * 3D Perspective projection setup
 */
void Make3DLook(void)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, 4.0/3.0, 0.0, 500.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();    
}

/*
 * 2D Orthographic projections setup
 */
void Make2DLook(void)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, nWidth, nHeight, 0);
  glMatrixMode(GL_MODELVIEW);
}

/*
 * Toggle line antialias 
 */
void ToggleAAlias(void)
{
    if(glIsEnabled(GL_LINE_SMOOTH))
	{
	glDisable(GL_LINE_SMOOTH);	    
	glDisable(GL_BLEND);	    
	}
    else
	{
	glEnable(GL_LINE_SMOOTH);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    	glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
	}
}


/*
 * Draw a string
 */
void glPuts(GLint x, GLint y, char *string, void *font)
{
  int len, i;

  glRasterPos2i(x, y);
  len = (int) strlen(string);
  for (i = 0; i < len; i++) {
    glutBitmapCharacter(font, string[i]);
  }
}

/*
 * Draw the 3d grid
 */
void DrawGrid(void)
{
    glCallList(GRID);
}

/*
 * Display the legend
 */
void Legend(void)
{
    glPuts(5, 40, "User", font2);    
    glPushMatrix();
    glPushAttrib(GL_CURRENT_BIT);
    glTranslatef(10.0, 50.0, 0.0);
    glColor3f(0.0, 0.0, 1.0);
    glRecti(0, 0, 20, 20);
    glPopAttrib();
    glPopMatrix();
    
    glPuts(5, 90, "Kernel", font2);    
    glPushMatrix();
    glPushAttrib(GL_CURRENT_BIT);
    glTranslatef(10.0, 100.0, 0.0);
    glColor3f(1.0, 0.0, 0.0);
    glRecti(0, 0, 20, 20);
    glPopAttrib();
    glPopMatrix();

    glPuts(5, 140, "Intr", font2);    
    glPushMatrix();
    glPushAttrib(GL_CURRENT_BIT);
    glTranslatef(10.0, 150.0, 0.0);
    glColor3f(1.0, 1.0, 0.0);
    glRecti(0, 0, 20, 20);
    glPopAttrib();
    glPopMatrix();

    glPuts(5, 190, "Idle", font2);    
    glPushMatrix();
    glPushAttrib(GL_CURRENT_BIT);
    glTranslatef(10.0, 200.0, 0.0);
    glColor3f(0.0, 1.0, 0.0);
    glRecti(0, 0, 20, 20);
    glPopAttrib();
    glPopMatrix();
    
    glPuts(5, 240, "Wait", font2);    
    glPushMatrix();
    glPushAttrib(GL_CURRENT_BIT);
    glTranslatef(10.0, 250.0, 0.0);
    glColor3f(0.0, 1.0, 1.0);
    glRecti(0, 0, 20, 20);
    glPopAttrib();
    glPopMatrix();
    
    glPuts(nWidth-30, nHeight-30, "0", font1); 
    glPuts(nWidth-45, 45, "100", font1); 
}



/*
 * Construct the grid display list
 */
void MakeGrid(void)
{
    int i;
    
    /* let's divide the grid in three pieces */
    glNewList(ZGRID, GL_COMPILE_AND_EXECUTE);
    glBegin(GL_LINES);
    for(i=0;i<=30;i+=3)
	{
	glVertex2d(i, 0);
	glVertex2d(i, 30);
	glVertex2d(0, i);
	glVertex2d(30, i);
	}
    glEnd();
    glEndList();
    
        
    glNewList(XGRID, GL_COMPILE_AND_EXECUTE);
    glPushMatrix();
    glRotatef(90.0, 1.0, 0.0, 0.0);    
    glBegin(GL_LINES);
    for(i=0;i<=6;i+=3)
	{
	glVertex2d(0, i);
	glVertex2d(30, i);
	}
    for(i=0;i<=30;i+=3)
	{
	glVertex2d(i, 0);
	glVertex2d(i, 6);
	} 
    glEnd();
    glPopMatrix();
    glEndList();

    glNewList(YGRID, GL_COMPILE_AND_EXECUTE);
    glPushMatrix();
    glRotatef(-90.0, 0.0, 1.0, 0.0);    
    glBegin(GL_LINES);
    for(i=0;i<=30;i+=3)
	{
	glVertex2d(0, i);
	glVertex2d(6, i);
	}
    for(i=0;i<=6;i+=3)
	{
	glVertex2d(i, 0);
	glVertex2d(i, 30);
	} 
    glEnd();
    glPopMatrix();
    glEndList();

    /* now call them all */
    glNewList(GRID, GL_COMPILE_AND_EXECUTE);
	glLineWidth(2.0);
	glColor3f(0.7, 0.7, 0.7);
	glCallList(ZGRID);
	glCallList(XGRID);
	glCallList(YGRID);
	glLineWidth(1.0);
    glEndList();
}

/*
 * System statistics collection routine
 */
void ComputeStatistics(void)
{
    memcpy(&LastSysInfo, &SysInfo, sizeof(SysInfo));
    sysmp(MP_SAGET, MPSA_SINFO, &SysInfo, sizeof(SysInfo));
    sginap(nPeriod);
    glutPostRedisplay();
}

/*
 * Draw a coloured cube for each one of the monitored parameters
 */
void Draw3DStatistics(void)
{
    GLdouble fSize;

    glPushMatrix();
    glTranslatef(0.0, 0.0, 1.5);
    /* Time in user mode */
    glPushMatrix();
    fSize = SysInfo.cpu[CPU_USER] - LastSysInfo.cpu[CPU_USER] ;
    fSize = fSize*30/nPeriod;
    glTranslatef(3.0, fSize/2, 0.0);
    glScalef(6.0, fSize, 3.0);
    glColor3f(0.0, 0.0, 1.0);
    glutSolidCube(1.0);
    glColor3f(0.0, 0.0, 0.0);
    glutWireCube(1.0);
    glPopMatrix();
    
    /* Time in kernel mode */
    glPushMatrix();
    fSize = SysInfo.cpu[CPU_KERNEL] - LastSysInfo.cpu[CPU_KERNEL] ;
    fSize = fSize*30/nPeriod;
    glTranslatef(9.0, fSize/2, 0.0);
    glScalef(6.0, fSize, 3.0);
    glColor3f(1.0, 0.0, 0.0);
    glutSolidCube(1.0);
    glColor3f(0.0, 0.0, 0.0);
    glutWireCube(1.0);
    glPopMatrix();

   /* Time in interrupt mode */
    glPushMatrix();
    fSize = SysInfo.cpu[CPU_INTR] - LastSysInfo.cpu[CPU_INTR] ;
    fSize = fSize*30/nPeriod;
    glTranslatef(15.0, fSize/2, 0.0);
    glScalef(6.0, fSize, 3.0);
    glColor3f(1.0, 1.0, 0.0);
    glutSolidCube(1.0);
    glColor3f(0.0, 0.0, 0.0);
    glutWireCube(1.0);
    glPopMatrix();
  
   /* Time in idle */
    glPushMatrix();
    fSize = SysInfo.cpu[CPU_IDLE] - LastSysInfo.cpu[CPU_IDLE] ;
    fSize = fSize*30/nPeriod;
    glTranslatef(21.0, fSize/2, 0.0);
    glScalef(6.0, fSize, 3.0);
    glColor3f(0.0, 1.0, 0.0);
    glutSolidCube(1.0);
    glColor3f(0.0, 0.0, 0.0);
    glutWireCube(1.0);
    glPopMatrix();
  
   /* Time in wait mode */
    glPushMatrix();
    fSize = SysInfo.cpu[CPU_WAIT] - LastSysInfo.cpu[CPU_WAIT] ;
    fSize = fSize*30/nPeriod;
    glTranslatef(27.0, fSize/2, 0.0);
    glScalef(6.0, fSize, 3.0);
    glColor3f(0.0, 1.0, 1.0);
    glutSolidCube(1.0);
    glColor3f(0.0, 0.0, 0.0);
    glutWireCube(1.0);
    glPopMatrix();
        
    glPopMatrix();
}


/*
 * Resize routine
 */
void Resize3DWindow(int newWidth, int newHeight)
{
    glViewport(0, 0, (GLint)newWidth, (GLint)newHeight);
    nWidth = newWidth;
    nHeight = newHeight;
    Make3DLook();
    glClear(GL_COLOR_BUFFER_BIT);
}


/*
 * Display routine
 */
void Doit3D(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    Make2DLook();
    glColor3f(1.0, 1.0, 0.0);
    glPuts(15, 15, "CPU Activity", font1);
    Legend();
    Make3DLook();
    glPushMatrix();
    glTranslatef(-15.0, -15.0, -50.0);
    glRotatef (lastx, 0.0, 1.0, 0.0);
    glRotatef (lasty, 1.0, 0.0, 0.0);
    DrawGrid();
    Draw3DStatistics();
    glFlush();
    glPopMatrix();
    glutSwapBuffers();
}

/*
 * Menus routines
 */
void SelectSampleRate(int pick)
{
    nPeriod = pick;
}

void MainMenu(int pick)
{
    switch(pick)  
	{
	case 0:
	    bMotion = !bMotion;
	    break;
	case 1:
	    ToggleAAlias();
	    break;	    
	case 2:
	    exit(0);
	    break;	    
	}
}

/*
 * Menu creation
 */
void SetUpMenu(void)
{
    int SampleMenu;
    
    SampleMenu = glutCreateMenu(SelectSampleRate);
    glutAddMenuEntry("1", 1);
    glutAddMenuEntry("5", 5);
    glutAddMenuEntry("10", 10);
    glutAddMenuEntry("20", 20);
    glutCreateMenu(MainMenu);
    glutAddMenuEntry("Mouse Motion", 0);
    glutAddMenuEntry("Line antialias", 1);
    glutAddSubMenu("Sample rate", SampleMenu);
    glutAddMenuEntry("Quit", 2);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

}

/*
 * MAIN STUFF
 */
int main(int argc,  char **argv)
{
    GLenum type;
    
    type = GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH;
    glutInit(&argc, argv);
    glutInitDisplayMode(type);
    glutCreateWindow(argv[0]);    
    SetUpMenu();
    
    MakeGrid();

    glutReshapeFunc(Resize3DWindow);
    glutDisplayFunc(Doit3D);
    glutMotionFunc(MouseMove);
    glutIdleFunc(ComputeStatistics);
    glutMainLoop();
    return(0);
}