]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/cytune.c
1 /* cytune.c -- Tune Cyclades driver
3 * Copyright 1995 Nick Simicich (njs@scifi.emi.net)
5 * Modifications by Rik Faith (faith@cs.unc.edu)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the Nick Simicich
18 * 4. Neither the name of the Nick Simicich nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY NICK SIMICICH AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL NICK SIMICICH OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
45 #include <linux/tty.h>
47 #include <linux/tqueue.h>
48 #include <linux/cyclades.h>
53 # include <linux/version.h>
54 # if LINUX_VERSION_CODE > 66056
59 /* Until it gets put in the kernel,
63 struct cyclades_control
{
64 struct cyclades_monitor c
;
69 unsigned long threshold_value
;
70 unsigned long timeout_value
;
72 struct cyclades_control
*cmon
;
75 #define mvtime(tvpto, tvpfrom) (((tvpto)->tv_sec = (tvpfrom)->tv_sec),(tvpto)->tv_usec = (tvpfrom)->tv_usec)
78 inline double dtime(struct timeval
* tvpnew
, struct timeval
* tvpold
) {
80 diff
= (double)tvpnew
->tv_sec
- (double)tvpold
->tv_sec
;
81 diff
+= ((double)tvpnew
->tv_usec
- (double)tvpold
->tv_usec
)/1000000;
85 static int global_argc
, global_optind
;
86 static char ***global_argv
;
88 void summary(int signal
) {
89 struct cyclades_control
*cc
;
98 optind
= global_optind
;
101 for(i
= optind
; i
< argc
; i
++) {
103 cc
= &cmon
[cmon_index
];
104 fprintf(stderr
, "File %s, For threshold value %lu, Maximum characters "
105 "in fifo were %d,\nand the maximum transfer rate in "
106 "characters/second was %f\n",
115 cc
= &cmon
[cmon_index
];
116 if (cc
->threshold_value
> 0 && signal
!= -1) {
117 fprintf(stderr
, "File %s, For threshold value %lu and timrout value %lu,"
118 " Maximum characters "
119 "in fifo were %d,\nand the maximum transfer rate in "
120 "characters/second was %f\n",
121 argv
[cmon_index
+optind
],
129 cc
->threshold_value
= 0;
130 cc
->timeout_value
= 0;
133 static int query
= 0;
134 static int interval
= 1;
137 static int set_val
= -1;
140 static int set_def
= 0;
141 static int set_def_val
= -1;
142 static int get_def
= 0;
144 static int set_time
= 0;
145 static int set_time_val
= -1;
147 static int set_def_time
= 0;
148 static int set_def_time_val
= -1;
151 int main(int argc
, char *argv
[]) {
153 struct timeval lasttime
, thistime
;
154 struct timezone tz
= {0,0};
159 struct cyclades_monitor cywork
;
164 unsigned long threshold_value
;
165 unsigned long timeout_value
;
171 global_argc
= argc
; /* For signal routine. */
172 global_argv
= &argv
; /* For signal routine. */
174 while (EOF
!= (i
= getopt(argc
, argv
,
181 interval
= atoi(optarg
);
183 fprintf(stderr
, "Invalid interval value: %s\n",optarg
);
189 set_val
= atoi(optarg
);
190 if(set_val
<= 0 || set_val
> 12) {
191 fprintf(stderr
, "Invalid set value: %s\n",optarg
);
197 set_def_val
= atoi(optarg
);
198 if(set_def_val
< 0 || set_def_val
> 12) {
199 fprintf(stderr
, "Invalid default value: %s\n",optarg
);
205 set_time_val
= atoi(optarg
);
206 if(set_time_val
<= 0 || set_time_val
> 255) {
207 fprintf(stderr
, "Invalid set time value: %s\n",optarg
);
213 set_def_time_val
= atoi(optarg
);
214 if(set_def_time_val
< 0 || set_def_time_val
> 255) {
215 fprintf(stderr
, "Invalid default time value: %s\n",optarg
);
219 case 'g': ++get
; break;
220 case 'G': ++get_def
; break;
225 numfiles
= argc
- optind
;
226 if(errflg
|| (numfiles
== 0)
227 || (!query
&& !set
&& !set_def
&&
228 !get
&& !get_def
&& !set_time
&& !set_def_time
) ||
229 (set
&& set_def
) || (set_time
&& set_def_time
) ||
232 "Usage: %s [-q [-i interval]]"
233 " ([-s value]|[-S value]) ([-t value]|[-T value])"
234 " [-g|-G] file [file...]\n",
239 global_optind
= optind
; /* For signal routine. */
241 if (set
|| set_def
) {
242 for(i
= optind
;i
< argc
;i
++) {
243 file
= open(argv
[i
],O_RDONLY
);
245 fprintf(stderr
, "Can't open %s: %s\n",argv
[i
],strerror(errno
));
249 set
? CYSETTHRESH
: CYSETDEFTHRESH
,
250 set
? set_val
: set_def_val
)) {
251 fprintf(stderr
, "Can't set %s to threshold %d: %s\n",
252 argv
[i
],set
?set_val
:set_def_val
,strerror(errno
));
257 if (set_time
|| set_def_time
) {
258 for(i
= optind
;i
< argc
;i
++) {
259 file
= open(argv
[i
],O_RDONLY
);
261 fprintf(stderr
, "Can't open %s: %s\n",argv
[i
],strerror(errno
));
265 set_time
? CYSETTIMEOUT
: CYSETDEFTIMEOUT
,
266 set_time
? set_time_val
: set_def_time_val
)) {
267 fprintf(stderr
, "Can't set %s to time threshold %d: %s\n",
268 argv
[i
],set_time
?set_time_val
:set_def_time_val
,strerror(errno
));
274 if (get
|| get_def
) {
275 for(i
= optind
;i
< argc
;i
++) {
276 file
= open(argv
[i
],O_RDONLY
);
278 fprintf(stderr
, "Can't open %s: %s\n",argv
[i
],strerror(errno
));
281 if(ioctl(file
, get
? CYGETTHRESH
: CYGETDEFTHRESH
, &threshold_value
)) {
282 fprintf(stderr
, "Can't get threshold for %s: %s\n",
283 argv
[i
],strerror(errno
));
286 if(ioctl(file
, get
? CYGETTIMEOUT
: CYGETDEFTIMEOUT
, &timeout_value
)) {
287 fprintf(stderr
, "Can't get timeout for %s: %s\n",
288 argv
[i
],strerror(errno
));
291 printf("%s: %ld %s threshold and %ld %s timeout\n",
292 argv
[i
], threshold_value
,
293 get
?"current":"default",
295 get
?"current":"default");
299 if(!query
) return 0; /* must have been something earlier */
301 /* query stuff after this line */
303 cmon
= (struct cyclades_control
*) malloc(sizeof (struct cyclades_control
)
306 perror("malloc failed");
309 if(signal(SIGINT
, summary
)||
310 signal(SIGQUIT
, summary
)||
311 signal(SIGTERM
, summary
)) {
312 perror("Can't set signal handler");
315 if(gettimeofday(&lasttime
,&tz
)) {
316 perror("gettimeofday failed");
319 for(i
= optind
; i
< argc
; i
++) {
320 cmon_index
= i
- optind
;
321 cmon
[cmon_index
].cfile
= open(argv
[i
], O_RDONLY
);
322 if(-1 == cmon
[cmon_index
].cfile
) {
323 fprintf(stderr
, "Can't open %s: %s\n",argv
[i
],strerror(errno
));
326 if(ioctl(cmon
[cmon_index
].cfile
, CYGETMON
, &cmon
[cmon_index
].c
)) {
327 fprintf(stderr
, "Can't issue CYGETMON on %s: %s\n",
328 argv
[i
],strerror(errno
));
332 if(ioctl(cmon
[cmon_index
].cfile
, CYGETTHRESH
, &threshold_value
)) {
333 fprintf(stderr
, "Can't get threshold for %s: %s\n",
334 argv
[i
],strerror(errno
));
337 if(ioctl(cmon
[cmon_index
].cfile
, CYGETTIMEOUT
, &timeout_value
)) {
338 fprintf(stderr
, "Can't get timeout for %s: %s\n",
339 argv
[i
],strerror(errno
));
346 if(gettimeofday(&thistime
,&tz
)) {
347 perror("gettimeofday failed");
350 diff
= dtime(&thistime
, &lasttime
);
351 mvtime(&lasttime
, &thistime
);
353 for(i
= optind
; i
< argc
; i
++) {
354 cmon_index
= i
- optind
;
355 if(ioctl(cmon
[cmon_index
].cfile
, CYGETMON
, &cywork
)) {
356 fprintf(stderr
, "Can't issue CYGETMON on %s: %s\n",
357 argv
[i
],strerror(errno
));
360 if(ioctl(cmon
[cmon_index
].cfile
, CYGETTHRESH
, &threshold_value
)) {
361 fprintf(stderr
, "Can't get threshold for %s: %s\n",
362 argv
[i
],strerror(errno
));
365 if(ioctl(cmon
[cmon_index
].cfile
, CYGETTIMEOUT
, &timeout_value
)) {
366 fprintf(stderr
, "Can't get timeout for %s: %s\n",
367 argv
[i
],strerror(errno
));
371 xfer_rate
= cywork
.char_count
/diff
;
373 xmit_rate
= cywork
.send_count
/diff
;
376 if(threshold_value
!= cmon
[cmon_index
].threshold_value
||
377 timeout_value
!= cmon
[cmon_index
].timeout_value
) {
379 /* Note that the summary must come before the setting of */
380 /* threshold_value */
381 cmon
[cmon_index
].threshold_value
= threshold_value
;
382 cmon
[cmon_index
].timeout_value
= timeout_value
;
384 /* Don't record this first cycle after change */
385 if(xfer_rate
> cmon
[cmon_index
].maxtran
)
386 cmon
[cmon_index
].maxtran
= xfer_rate
;
388 if(xmit_rate
> cmon
[cmon_index
].maxxmit
)
389 cmon
[cmon_index
].maxxmit
= xmit_rate
;
391 if(cywork
.char_max
> cmon
[cmon_index
].maxmax
)
392 cmon
[cmon_index
].maxmax
= cywork
.char_max
;
396 printf("%s: %lu ints, %lu/%lu chars; "
397 "fifo: %lu thresh, %lu tmout, "
398 "%lu max, %lu now\n",
400 cywork
.int_count
,cywork
.char_count
,cywork
.send_count
,
401 threshold_value
,timeout_value
,
402 cywork
.char_max
,cywork
.char_last
);
403 printf(" %f int/sec; %f rec, %f send (char/sec)\n",
404 cywork
.int_count
/diff
,
408 printf("%s: %lu ints, %lu chars; "
409 "fifo: %lu thresh, %lu tmout, "
410 "%lu max, %lu now\n",
412 cywork
.int_count
,cywork
.char_count
,
413 threshold_value
,timeout_value
,
414 cywork
.char_max
,cywork
.char_last
);
415 printf(" %f int/sec; %f rec (char/sec)\n",
416 cywork
.int_count
/diff
,
419 memcpy(&cmon
[cmon_index
].c
, &cywork
, sizeof (struct cyclades_monitor
));