summaryrefslogtreecommitdiff
path: root/src/pmview/front-ends/mpvis
blob: dd7db785bdb975d3b27665b9899f2fa45821e99b (plain)
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
443
444
445
446
447
448
449
450
451
452
#!/bin/sh
# Copyright (c) 1997-2001 Silicon Graphics, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

tmp=/tmp/$$
trap "rm -f $tmp.*; exit" 0 1 2 3  15
rm -f $tmp.*

. $PCP_DIR/etc/pcp.env
. $PCP_SHARE_DIR/lib/pmview-args

gridspace=120
cpuargs=

algorithm="k"
default_layout=true
force=false
maxrowlen=16
verbose=false
showinst=false
version=2

_usage()
{
    echo >$tmp.msg 'Usage: mpvis [options] [cpuid...]

options:
  -b              use row:column ratio of 1:8 (soft limit)
                  [default, if 64 or more CPUs]
  -i              show CPU numbers
  -r rowlen       maximum number of CPUs per row (soft limit)
  		  [default 16, if less than 64 CPUs]
  -R rowlen       force this number of CPUs per row
  -V              verbose/diagnostic output  

pmview(1) options:'

    _pmview_usage >>$tmp.msg
    echo >>$tmp.msg
    echo 'Default title is: CPU Utilization for Host' >>$tmp.msg
    _pmview_info -f $tmp.msg
}

# generate one row of the scene
#
_do_row()
{
if [ "$1" = "$2" ]
then
    msg="$titleArg\n$1 only"
else
    msg="$titleArg\n$1 to $2"
fi

cat <<End-of-File >>$tmp.conf
    _grid 0 $3 show (
	_baseLabel "$msg"
	_bar $group (
	    _metrics (
End-of-File
$have_idle && echo "                kernel.percpu.cpu.idle[$rowlist]        $max_util \"idle\"" >>$tmp.conf
$have_wait && echo "                kernel.percpu.cpu.wait.total[$rowlist]        $max_util \"wait\"" >>$tmp.conf
$have_intr && echo "                kernel.percpu.cpu.intr[$rowlist]        $max_util \"intr\"" >>$tmp.conf
$have_nice && echo "                kernel.percpu.cpu.nice[$rowlist]  $max_util \"nice\"" >>$tmp.conf
$have_sys  && echo "                kernel.percpu.cpu.sys[$rowlist]         $max_util \"sys\"" >>$tmp.conf
$have_user && echo "                kernel.percpu.cpu.user[$rowlist]        $max_util \"user\"" >>$tmp.conf
cat <<End-of-File >>$tmp.conf
	    )
End-of-File
$showinst && echo "	    _instlabels away ( $labels )" >>$tmp.conf
cat <<End-of-File >>$tmp.conf
	    _colorlist cpu
	    _baseLabel "$msg"
	)
    )
End-of-File
}

# build WM_COMMAND X(1) property for restart after login/logout
#
echo -n "pmview Version 2.1 \"mpvis\"" >$tmp.conf
for arg
do
    echo -n " \"$arg\"" >>$tmp.conf
done

_pmview_args "$@"

if [ "X$otherArgs" != X ]
then
    while getopts "bir:R:v:V?" c $otherArgs
    do
	case $c
	in
	    b)
		algorithm="b"
		default_layout=false
		;;
	    i)
		showinst=true
		gridspace=60
		;;
	    r)
		_pmview_unsigned $c
		maxrowlen=$OPTARG
		default_layout=false
		;;
	    R)
		_pmview_unsigned $c
		maxrowlen=$OPTARG
		default_layout=false
		force=true
		;;

	    v)
		version=$OPTARG
		if [ $version = "1" ]
		then
		    _pmview_warning "$prog: pmview version 1 no longer supported, using version 2"
		    version=2
		elif [ $version != "2" ]
		then
		    _pmview_error "$prog: only version 2 supported for -v"
		    # NOTREACHED
		fi
		;;

	    V)
		verbose=true
		;;
	    ?)
		_usage
		exit 1
		;;
	esac
    done
    set -- $otherArgs
    shift `expr $OPTIND - 1`

    [ $# -gt 0 ] && cpuargs=$@
fi

[ -z "$titleArg" ] && titleArg="SGI PCP : CPU Utilization for Host $host"

_pmview_cache_fetch -I kernel.percpu.cpu.user
_pmview_cache_fetch -v kernel.percpu.cpu.idle \
    kernel.percpu.cpu.wait.total \
    kernel.percpu.cpu.intr \
    kernel.percpu.cpu.nice \
    kernel.percpu.cpu.sys \
    kernel.percpu.cpu.user


# Check that we can get the metrics
#
if _pmview_fetch_indom kernel.percpu.cpu.user
then
    if [ ! -s "$tmp.pmview_result" -o "$number" -lt 1 ]
    then
	_pmview_fetch_fail "get CPU inventory"
    fi
else
    _pmview_fetch_fail "get CPU inventory"
    # NOTREACHED
fi

if [ ! -z "$cpuargs" ]
then
    # restrict based on command line args
    #
    rm -f $tmp.tmp $tmp.msg
    ncpu=0
    for cpu in $cpuargs
    do
	if echo "$cpu" | grep '[.[^]' >/dev/null
	then
	    # assume egrep(1) regular expression
	    #
	    if egrep "$cpu" $tmp.pmview_result >>$tmp.tmp
	    then
	        # found some matches
		#
		:
	    else
		echo "$prog: pattern \"$cpu\" does not match any CPUs ..." >$tmp.msg
	    fi
	elif grep "^$cpu\$" $tmp.pmview_result >/dev/null
	then
	    echo $cpu >>$tmp.tmp
	else
	    echo "$prog: CPU \"$cpu\" not in the CPU inventory" >$tmp.msg
	fi
    done
    if [ -s $tmp.msg ]
    then
	echo "CPUs on host \"$host\" are:" >> $tmp.msg
	tr < $tmp.pmview_result '\012' ' ' | fmt | sed -e "s/^/	/" >> $tmp.msg
	_pmview_error -f $tmp.msg
	# NOTREACHED
    fi
    sort $tmp.tmp | uniq > $tmp.pmview_result
    ncpu=`wc -l $tmp.pmview_result | $PCP_AWK_PROG '{print $1}'`
else
    ncpu=$number
fi

if [ "$ncpu" -ge 64 -a "$default_layout" = true ]
then
    # >= 64p, no -b, -r or -R options ... make -b the default
    #
    algorithm="b"
fi

# sort list
#
if grep cpu: $tmp.pmview_result >/dev/null
then
    # Origin series name style
    sed -e 's/:/./' < $tmp.pmview_result \
    | sort -t. +1n -2 +2n -3 +3 -4 \
    | sed -e 's/\./:/' \
    > $tmp.cpulist
else
    # CPU names for older systems
    sed -e 's/cpu/cpu./' < $tmp.pmview_result \
    | sort -t. +1n -2 \
    | sed -e 's/\.//' \
    > $tmp.cpulist
fi

scale=''
have_idle=false
if _pmview_fetch_values kernel.percpu.cpu.idle
then
  have_idle=true
  [ -z "$scale" ] && scale=`pminfo $namespace $msource -d kernel.percpu.cpu.idle| sed -n '/Semantics:/s/.*Units: //p'`
fi

have_wait=false
if _pmview_fetch_values kernel.percpu.cpu.wait.total
then
  have_wait=true
  [ -z "$scale" ] && scale=`pminfo $namespace $msource -d kernel.percpu.cpu.wait.total | sed -n '/Semantics:/s/.*Units: //p'`
fi

have_intr=false
if _pmview_fetch_values kernel.percpu.cpu.intr
then
# linux 2.6 has wait and intr, but 2.4 does not
  have_intr=`$PCP_AWK_PROG -v found=false '
  	$1 > 0 { found="true" }
	END { print found }' $tmp.pmview_result`
  have_wait=$have_intr
  [ -z "$scale" ] && scale=`pminfo $namespace $msource -d kernel.percpu.cpu.intr | sed -n '/Semantics:/s/.*Units: //p'`
fi

have_nice=false
if _pmview_fetch_values kernel.percpu.cpu.nice
then
  have_nice=true
  [ -z "$scale" ] && scale=`pminfo $namespace $msource -d kernel.percpu.cpu.nice | sed -n '/Semantics:/s/.*Units: //p'`
fi

have_sys=false
if _pmview_fetch_values kernel.percpu.cpu.sys
then
  have_sys=true
  [ -z "$scale" ] && scale=`pminfo $namespace $msource -d kernel.percpu.cpu.sys | sed -n '/Semantics:/s/.*Units: //p'`
fi

have_user=false
if _pmview_fetch_values kernel.percpu.cpu.user
then
  have_user=true
  [ -z "$scale" ] && scale=`pminfo $namespace $msource -d kernel.percpu.cpu.user | sed -n '/Semantics:/s/.*Units: //p'`
fi

case $scale
in
    microsec)
    	max_util=1000000
	;;
    millisec)
    	max_util=1000
	;;
    *)
	_pmview_warning "$prog: cannot determine CPU time units, assuming milliseconds"
	max_util=1000
	;;
esac

# shape the base geometry for the scene
#
if [ $ncpu -le "$maxrowlen" ]
then
    nrows=1
    ncols=$ncpu
elif $force
then
    nrows=`echo $ncpu $maxrowlen | $PCP_AWK_PROG ' \
	{ x = $1 / $2; y = $1 % $2; if (y > 0) ++x; printf "%d\n", x; }'`
    ncols=$maxrowlen
else
    case $algorithm in
	a)	# this algorithm doesn't work at the moment
	    # (the exit condition is not robust enough)
	    nrows=1
	    ncols=1
	    bound=1
	    num=0
	    while [ $num -gt $ncols -o $num -lt $bound ]
	    do
		nrows=`expr $nrows \* 2`
		ncols=`expr $ncpu / $nrows`
		bound=`expr $ncols / 2`
		num=`expr $nrows \* 4`
	    done
	    ncols=`echo $ncpu $nrows | $PCP_AWK_PROG ' { x = $1 / $2; y = $1 % $2; \
			  if (y > 0) ++x; printf "%d\n", x }'`
	    ;;
	b)
	    # use a ratio for rows:columns of 1:8
	    #
	    nrows=`echo $ncpu | $PCP_AWK_PROG ' { x = sqrt ($1 / 8.0);
			    y = int (x); if (y < x) ++y; print y }'`
	    ncols=`expr $ncpu + $nrows - 1`
	    ncols=`expr $ncols / $nrows`
	    ;;
	k)
	    nrows=`expr $ncpu + $maxrowlen - 1`
	    nrows=`expr $nrows / $maxrowlen`
	    ncols=`expr $ncpu + $nrows - 1`
	    ncols=`expr $ncols / $nrows`
    esac
fi

if [ "$ncols" -gt 6 ]
then
    group="_groupByMetric"
else
    group="_groupByInst"
fi

cat <<End-of-File >>$tmp.conf

#
# mpvis
#
# ncpus = $ncpu
# nrows = $nrows
# ncols = $ncols
#
# List:
End-of-File

col=0
rowlist=""
cat $tmp.cpulist | while read cpu
do
    if [ $col -eq 0 ]
    then
	echo -n "$cpu " > $tmp.rowlist
    else
	echo -n "$cpu " >> $tmp.rowlist
    fi
    col=`expr $col + 1`
    if [ "$col" -eq $ncols ]
    then
	echo "# "`cat $tmp.rowlist` >>$tmp.conf
	col=0
	rm -f $tmp.rowlist
    fi
done

[ -s $tmp.rowlist ] && echo "# "`cat $tmp.rowlist` >>$tmp.conf

echo "_gridSpace $gridspace" >>$tmp.conf
echo >>$tmp.conf

colorlist="_colorlist cpu ("
$have_idle && colorlist="$colorlist green2"
$have_wait && colorlist="$colorlist cyan2"
$have_intr && colorlist="$colorlist yellow2"
$have_nice && colorlist="$colorlist rgbi:0.6/1.0/0.7"
$have_sys  && colorlist="$colorlist red2"
$have_user && colorlist="$colorlist blue2"
colorlist="$colorlist )"

echo "$colorlist" >>$tmp.conf
echo "_grid 0 0 _hide ( # outer grid" >>$tmp.conf

# build rows from front-to-back of scene
# fill rows with CPUs from left-to-right
#
y=`expr $nrows \* 2 - 2`
col=0
rowlist=""
labels=""
cat $tmp.cpulist | while read cpu
do
    if [ $col -eq 0 ]
    then
	rowlist=$cpu
	start=$cpu
    else
	rowlist="$rowlist,$cpu"
    fi
    $showinst && labels="$labels \"`echo $cpu | sed -e 's/cpu:*//'`\""
    col=`expr $col + 1`
    echo "$start $cpu $y $rowlist $labels" > $tmp.rowlist
    if [ $col -eq $ncols ]
    then
	_do_row $start $cpu $y
	col=0
	echo -n "" > $tmp.rowlist
	labels=""
	y=`expr $y - 2`
    fi
done

if [ -s $tmp.rowlist ]
then
    read start cpu y rowlist labels < $tmp.rowlist
    # cat $tmp.rowlist
    _do_row $start $cpu $y
fi

echo ")" >>$tmp.conf

if [ $nrows -eq 1 ]
then
    # remove unnecessary _grid for a single row
    #
    sed -e '/^    _grid/d' -e '/^    )/d' <$tmp.conf >$tmp.tmp
    mv $tmp.tmp $tmp.conf
fi

$verbose && cat $tmp.conf

eval $PMVIEW <$tmp.conf $args -title "'$titleArg'" -xrm "'*iconName:mpvis'"

exit