File: xsysinfo.c

package info (click to toggle)
xsysinfo 1.7-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 180 kB
  • ctags: 118
  • sloc: ansic: 787; sh: 129; makefile: 44
file content (361 lines) | stat: -rw-r--r-- 10,373 bytes parent folder | download | duplicates (4)
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
/*
 * xsysinfo: X application for displaying some kernel parameters
 *
 * $Id: xsysinfo.c,v 1.9 1993/02/12 11:40:16 gabor Exp $
 *
 */

/* 
 * Written by Gabor Herr <herr@iti.informatik.th-darmstadt.de>.
 *
 * Copyright (c) 1992, 1993 by Gabor Herr, all rights reserved.
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that may name is not used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission. I make no representations
 * about the suitability of this software for any purpose. It is
 * provided "as is" without express or implied warranty.
 */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>
#include "BarGauge.h"
#include <stdio.h>
#include "sysinfo.h"

#define norm(x,max)	((max) == 0 ? 0 : (double)(x) / (max))

/* Application Ressources: */
typedef struct {
	Boolean show_title;
	Boolean show_labels;
	Boolean show_loadavg;
	Boolean show_load;
	Boolean show_mem;
	Boolean show_swap;
	Boolean smp_kernel;
} AppData, *AppDataPtr;

static AppData appres;

static XtResource application_resources[] = {
	{ "showTitle", "ShowTitle", XtRBoolean, sizeof(Boolean),
	  XtOffset(AppDataPtr,show_title), XtRImmediate, (XtPointer) True },
	{ "showLabels", "ShowLabels", XtRBoolean, sizeof(Boolean),
	  XtOffset(AppDataPtr,show_labels), XtRImmediate, (XtPointer) True },
	{ "showLoadAvg", "ShowLoadAvg", XtRBoolean, sizeof(Boolean),
	  XtOffset(AppDataPtr,show_loadavg), XtRImmediate, (XtPointer) True },
	{ "showLoad", "ShowLoad", XtRBoolean, sizeof(Boolean),
	  XtOffset(AppDataPtr,show_load), XtRImmediate, (XtPointer) True },
	{ "showMem", "ShowMem", XtRBoolean, sizeof(Boolean),
	  XtOffset(AppDataPtr,show_mem), XtRImmediate, (XtPointer) True },
	{ "showSwap", "ShowSwap", XtRBoolean, sizeof(Boolean),
	  XtOffset(AppDataPtr,show_swap), XtRImmediate, (XtPointer) True },
	{ "smpKernel", "SmpKernel", XtRBoolean, sizeof(Boolean),
	  XtOffset(AppDataPtr,smp_kernel), XtRImmediate, (XtPointer) True }
};

/* Command line options */
static XrmOptionDescRec options[] = {

	{ "-update", "*.bar.update", XrmoptionSepArg, NULL },

	{ "-title",   "*showTitle", XrmoptionNoArg, (XtPointer) "True" },
	{ "-notitle", "*showTitle", XrmoptionNoArg, (XtPointer) "False" },

	{ "-labels",   "*showLabels", XrmoptionNoArg, (XtPointer) "True" },
	{ "-nolabels", "*showLabels", XrmoptionNoArg, (XtPointer) "False" },

	{ "-loadavg",   "*showLoadAvg", XrmoptionNoArg, (XtPointer) "True" },
	{ "-noloadavg", "*showLoadAvg", XrmoptionNoArg, (XtPointer) "False" },

	{ "-load",   "*showLoad", XrmoptionNoArg, "True" },
	{ "-noload", "*showLoad", XrmoptionNoArg, "False" },

	{ "-mem",   "*showMem", XrmoptionNoArg, (XtPointer) "True" },
	{ "-nomem", "*showMem", XrmoptionNoArg, (XtPointer) "False" },

	{ "-swap",   "*showSwap", XrmoptionNoArg, (XtPointer) "True" },
	{ "-noswap", "*showSwap", XrmoptionNoArg, (XtPointer) "False" },

	{ "-smp",   "*smpKernel", XrmoptionNoArg, (XtPointer) "True" },
	{ "-nosmp", "*smpKernel", XrmoptionNoArg, (XtPointer) "False" }
};

static String fallback_resources[] = {
	"*.title.label:		Linux System Info",
	"*.loadavg*label:	CPU Load Avg:",
	"*.load*label:		CPU Load:",
	"*.mem*label:		Memory:  ",
	"*.swap*label:		Swap:    ",
	NULL
};

static XtAppContext context;
static struct meminfo swap[MAX_SWAPFILES];
static int swapfiles;
static int memupdate_freq = 0;

static void getloadavgCB( widget, client_data, call_data )
	Widget widget;
XtPointer client_data;
XtPointer call_data;
{
	XgBarGaugeValues *gauge = (XgBarGaugeValues *) call_data;
	gauge->values[0] = get_loadavg();  
}

static void getloadCB( widget, client_data, call_data )
	Widget widget;
XtPointer client_data;
XtPointer call_data;
{
	struct load load;
	XgBarGaugeValues *gauge = (XgBarGaugeValues *) call_data;

	load.cpu = (char *)malloc(7*sizeof(char));
	strcpy(load.cpu, (char *)client_data);
	get_load(&load);

	gauge->values[0] = norm(load.user, load.total);
	gauge->values[1] = norm(load.system, load.total);
	gauge->values[2] = norm(load.nice, load.total);
}

static void update_meminfo()
{
	static int count = 0;

	if ( count-- > 0 )
		return;

	count = memupdate_freq;
  
	get_meminfo( &swapfiles, swap );
}

static void getmemCB( widget, client_data, call_data )
	Widget widget;
XtPointer client_data;
XtPointer call_data;
{
	XgBarGaugeValues *gauge = (XgBarGaugeValues *) call_data;
	struct meminfo *mem = swap;

	update_meminfo();
  
	if (mem->buffers >= 0) {
		/* New style /proc/meminfo format */
    
		gauge->values[0] = norm(mem->total - mem->free - mem->cache -
					mem->buffers,mem->total);
		gauge->values[1] = norm(mem->cache + mem->buffers,mem->total);
	} else {
		/* Old style /proc/meminfo format (no buffers-entry) */
		gauge->values[0] = norm(mem->total - mem->free - mem->cache,
					mem->total);
		gauge->values[1] = norm(mem->cache,mem->total);
	}
}

static void getswapCB( widget, client_data, call_data )
	Widget widget;
XtPointer client_data;
XtPointer call_data;
{
	XgBarGaugeValues *gauge = (XgBarGaugeValues *) call_data;
	int i;
	int total;

	update_meminfo();
  
	if ( gauge->num_values != swapfiles )
		XtVaSetValues( widget, XtNsegments, swapfiles, NULL);

	for ( total = i = 0; i < gauge->num_values; i++ ) 
		total += swap[i + 1].total;

	for ( i = 0; i < gauge->num_values; i++ ) 
		gauge->values[i] = norm(swap[i + 1].total - swap[i + 1].free,
					total);
}

static Widget create_gauge( name, parent, segs, valueproc, client_data )
	String name;
Widget parent;
XtCallbackProc valueproc;
int segs;
XtPointer client_data;
{
	Widget gauge;
	Widget bar;
	Widget label;
  
	gauge = XtCreateManagedWidget( name, formWidgetClass, parent, NULL, 0 );
 
	if (appres.show_labels) {
		label = XtVaCreateManagedWidget( "name",labelWidgetClass,gauge,
						 XtNright, XawChainLeft,
						 NULL);

		bar = XtVaCreateManagedWidget( "bar",barGaugeWidgetClass,gauge,
					       XtNsegments, segs,
					       XtNfromHoriz, label,
					       XtNleft, XawChainLeft,
					       NULL);
	} else {
		bar = XtVaCreateManagedWidget( "bar",barGaugeWidgetClass,gauge,
					       XtNsegments, segs,
					       XtNleft, XawChainLeft,
					       NULL);
	}

	XtAddCallback( bar, XtNgetValue, valueproc, client_data );

	return bar;
}

static void create_app( top )
	Widget top;
{
	Widget form;
	Widget above_widget = NULL;
	Widget this;
	Widget bar;
	char *bufptr;
	int total_procs, i;
  
	form = XtCreateManagedWidget( "form", formWidgetClass, top, NULL, 0 );

	if (appres.show_title)
		above_widget = XtVaCreateManagedWidget( "title",
							labelWidgetClass, form,
							XtNright, XawChainRight,
							NULL);

	if (appres.show_loadavg)	{
		bar  = create_gauge( "loadavg", form, 1, getloadavgCB, NULL );
		XtVaSetValues( bar,
			       XtNautoAdjust, True,
			       NULL );
		this = XtParent(bar);
		XtVaSetValues( this,
			       XtNfromVert, above_widget,
			       XtNright,    XawChainRight,
			       NULL );
		above_widget = this;
	}
    
	if (appres.show_load) {
		total_procs = get_total_procs();
		if(!appres.smp_kernel || total_procs == 1) {
			bufptr = (char *)malloc(7*sizeof(char));
			sprintf(bufptr, "cpu ");
			this = XtParent(create_gauge( "load", form, 3, getloadCB, bufptr ));
			XtVaSetValues( this,
				       XtNfromVert, above_widget,
				       XtNright,    XawChainRight,
				       NULL );
			above_widget = this;
		}
		else {
			for( i = 0; i < total_procs; i ++) {
				bufptr = (char *)malloc(7*sizeof(char));
				sprintf(bufptr, "cpu%d ", i);

				this = XtParent(create_gauge( "load", form, 3, getloadCB, bufptr ));
				XtVaSetValues( this,
					       XtNfromVert, above_widget,
					       XtNright,    XawChainRight,
					       NULL );
				above_widget = this;
			}
		}
	}
    
	if (appres.show_mem)	{		
		this = XtParent(create_gauge( "mem", form, 2, getmemCB, NULL ));
		XtVaSetValues( this,
			       XtNfromVert, above_widget,
			       XtNright,    XawChainRight,
			       NULL );
		above_widget = this;
		memupdate_freq++;
	}
    
	if (appres.show_swap && swapfiles > 0)	{		
		this = XtParent(create_gauge( "swap", form, swapfiles,
					      getswapCB, NULL ));
		XtVaSetValues( this,
			       XtNfromVert, above_widget,
			       XtNright,    XawChainRight,
			       NULL );
		above_widget = this;
		memupdate_freq++;
	}

	if (above_widget == NULL)
		XtAppError( context,"All gauges disabled, nothing to show.\n" );
}

static void show_usage()
{
	fprintf(stderr,"xsysinfo version 1.7\n\n");
	fprintf(stderr,"usage: xsysinfo [options...]\n\n");
	fprintf(stderr,"where options include:\n");
#ifdef USE_MILLI_SECONDS       /* (ThMO) */
	fprintf(stderr,"-update <n>                     set update rate to <n> milli-seconds\n");
#else
	fprintf(stderr,"-update <n>                     set update rate to <n> seconds\n");
#endif
	fprintf(stderr,"-title   (-notitle)		show (don't show) title string\n");
	fprintf(stderr,"-labels  (-nolabels)		show (don't show) gauge labels\n");
	fprintf(stderr,"-loadavg (-noloadavg)		show (don't show) CPU load average value\n");
	fprintf(stderr,"-load    (-noload)		show (don't show) CPU load value\n");
	fprintf(stderr,"-mem     (-nomem)		show (don't show) Memory info\n");
	fprintf(stderr,"-swap    (-noswap)		show (don't show) Swap info\n");
	fprintf(stderr,"-smp     (-nosmp)               smp (not smp) kernel\n");
	fprintf(stderr,"-help				show this message\n");
	fprintf(stderr,"\nor one of the standard toolkit options.\n\n");
}

main( argc, argv )
	int argc;
char **argv;
{
	Arg args[1];
	Widget toplevel;

	toplevel = XtAppInitialize( &context, "XSysinfo",
				    options, XtNumber(options),
				    &argc, argv,
				    fallback_resources,
				    NULL, 0 );

	if (argc != 1) {
		show_usage();
		exit(1);
	}
			      
	XtGetApplicationResources( toplevel, &appres,
				   application_resources,
				   XtNumber( application_resources ),
				   NULL, 0 );
			      
	if (sysinfo_init() != 0) {
		fprintf(stderr,	"Proc filesystem isn't mounted on `/proc'.\n");
		exit(1);
	}
	update_meminfo();

	create_app( toplevel );

	XtRealizeWidget(toplevel);
	XtAppMainLoop(context);
}