110154Sache/* 210154Sache * gloadavg.c - get load average for Linux 310154Sache * Copyright (C) 1993 Thomas Koenig 410154Sache * 510154Sache * Redistribution and use in source and binary forms, with or without 610154Sache * modification, are permitted provided that the following conditions 710154Sache * are met: 810154Sache * 1. Redistributions of source code must retain the above copyright 910154Sache * notice, this list of conditions and the following disclaimer. 1010154Sache * 2. The name of the author(s) may not be used to endorse or promote 1110154Sache * products derived from this software without specific prior written 1210154Sache * permission. 1310154Sache * 1410154Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 1510154Sache * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1610154Sache * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1710154Sache * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 1810154Sache * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1910154Sache * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2010154Sache * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2110154Sache * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2210154Sache * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2310154Sache * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2410154Sache */ 2510154Sache 2631306Scharnier#ifndef lint 2731306Scharnierstatic const char rcsid[] = 2850476Speter "$FreeBSD$"; 2931306Scharnier#endif /* not lint */ 3031306Scharnier 3110154Sache#ifndef __FreeBSD__ 3210154Sache#define _POSIX_SOURCE 1 3310154Sache 3410154Sache/* System Headers */ 3510154Sache 3610154Sache#include <stdio.h> 3710154Sache#else 3810154Sache#include <stdlib.h> 3910154Sache#endif 4010154Sache 4110154Sache/* Local headers */ 4210154Sache 4310154Sache#include "gloadavg.h" 4410154Sache 4531306Scharnier/* Global functions */ 4610154Sache 47170768Syarvoid perr(const char *fmt, ...); 4810154Sache 4910154Sachedouble 5010154Sachegloadavg(void) 5110154Sache/* return the current load average as a floating point number, or <0 for 5210154Sache * error 5310154Sache */ 5410154Sache{ 5510154Sache double result; 5610154Sache#ifndef __FreeBSD__ 5710154Sache FILE *fp; 5810154Sache 5910154Sache if((fp=fopen(PROC_DIR "loadavg","r")) == NULL) 6010154Sache result = -1.0; 6110154Sache else 6210154Sache { 6310154Sache if(fscanf(fp,"%lf",&result) != 1) 6410154Sache result = -1.0; 6510154Sache fclose(fp); 6610154Sache } 6710154Sache#else 6810154Sache if (getloadavg(&result, 1) != 1) 6931306Scharnier perr("error in getloadavg"); 7010154Sache#endif 7110154Sache return result; 7210154Sache} 73