2012-03-02 17:59:36 +05:30
|
|
|
/*
|
|
|
|
* File for parsing top-level /proc entities.
|
2023-02-07 15:39:48 +05:30
|
|
|
*
|
|
|
|
* Copyright © 2011-2023 Jim Warner <james.warner@comcast.net>
|
|
|
|
* Copyright © 2011-2023 Craig Small <csmall@dropbear.xyz>
|
|
|
|
* Copyright © 1998-2008 Albert Cahalan
|
|
|
|
* Copyright © 1992-1998 Michael K. Johnson <johnsonm@redhat.com>
|
2012-03-02 17:59:36 +05:30
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2013-05-09 05:04:44 +05:30
|
|
|
#include <stdbool.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2002-02-02 05:10:38 +05:30
|
|
|
#include <locale.h>
|
2015-06-24 17:46:16 +05:30
|
|
|
#include <errno.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2017-08-19 18:35:22 +05:30
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
2021-01-19 11:30:00 +05:30
|
|
|
#include "misc.h"
|
2015-06-24 17:46:16 +05:30
|
|
|
#include "procps-private.h"
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
|
|
|
|
#define LOADAVG_FILE "/proc/loadavg"
|
|
|
|
|
|
|
|
/* evals 'x' twice */
|
|
|
|
#define SET_IF_DESIRED(x,y) do{ if(x) *(x) = (y); }while(0)
|
|
|
|
|
2014-07-15 22:47:02 +05:30
|
|
|
/* return minimum of two values */
|
2017-08-19 18:35:22 +05:30
|
|
|
#ifndef __CYGWIN__
|
2014-07-15 22:47:02 +05:30
|
|
|
#define MIN(x,y) ((x) < (y) ? (x) : (y))
|
2017-08-19 18:35:22 +05:30
|
|
|
#endif
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2015-06-29 21:46:52 +05:30
|
|
|
/*
|
2015-06-24 17:46:16 +05:30
|
|
|
* procps_hertz_get:
|
|
|
|
*
|
|
|
|
*
|
2002-02-02 04:17:29 +05:30
|
|
|
* Some values in /proc are expressed in units of 1/HZ seconds, where HZ
|
|
|
|
* is the kernel clock tick rate. One of these units is called a jiffy.
|
|
|
|
* The HZ value used in the kernel may vary according to hacker desire.
|
|
|
|
*
|
2015-06-24 17:46:16 +05:30
|
|
|
* On some architectures, the kernel provides an ELF note to indicate
|
|
|
|
* HZ.
|
2002-02-02 04:17:29 +05:30
|
|
|
*
|
2015-06-24 17:46:16 +05:30
|
|
|
* Returns:
|
|
|
|
* The discovered or assumed hertz value
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
2015-06-24 17:46:16 +05:30
|
|
|
PROCPS_EXPORT long procps_hertz_get(void)
|
|
|
|
{
|
|
|
|
long hz;
|
2011-01-19 17:20:26 +05:30
|
|
|
|
|
|
|
#ifdef _SC_CLK_TCK
|
2015-06-24 17:46:16 +05:30
|
|
|
if ((hz = sysconf(_SC_CLK_TCK)) > 0)
|
2015-07-01 17:38:02 +05:30
|
|
|
return hz;
|
2011-01-19 17:20:26 +05:30
|
|
|
#endif
|
2002-02-02 04:17:29 +05:30
|
|
|
#ifdef HZ
|
2015-06-24 17:46:16 +05:30
|
|
|
return(HZ);
|
2002-02-02 04:17:29 +05:30
|
|
|
#endif
|
2015-06-24 17:46:16 +05:30
|
|
|
/* Last resort, assume 100 */
|
|
|
|
return 100;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2015-06-24 17:46:16 +05:30
|
|
|
|
2015-07-01 17:17:30 +05:30
|
|
|
/*
|
|
|
|
* procps_loadavg:
|
|
|
|
* @av1: location to store 1 minute load average
|
|
|
|
* @av5: location to store 5 minute load average
|
|
|
|
* @av15: location to store 15 minute load average
|
|
|
|
*
|
|
|
|
* Find the 1,5 and 15 minute load average of the system
|
|
|
|
*
|
|
|
|
* Returns: 0 on success <0 on error
|
|
|
|
*/
|
|
|
|
PROCPS_EXPORT int procps_loadavg(
|
|
|
|
double *restrict av1,
|
|
|
|
double *restrict av5,
|
|
|
|
double *restrict av15)
|
2015-06-24 17:46:16 +05:30
|
|
|
{
|
2002-02-02 04:17:29 +05:30
|
|
|
double avg_1=0, avg_5=0, avg_15=0;
|
2021-09-28 10:30:00 +05:30
|
|
|
locale_t tmplocale;
|
2022-05-31 17:33:22 +05:30
|
|
|
int retval=0;
|
2022-05-31 17:02:03 +05:30
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if ((fp = fopen(LOADAVG_FILE, "r")) == NULL)
|
|
|
|
return -errno;
|
2013-03-31 10:30:00 +05:30
|
|
|
|
2021-09-28 10:30:00 +05:30
|
|
|
tmplocale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0);
|
|
|
|
uselocale(tmplocale);
|
2022-05-31 17:02:03 +05:30
|
|
|
if (fscanf(fp, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3)
|
2015-07-01 17:17:30 +05:30
|
|
|
retval = -ERANGE;
|
2021-09-28 10:30:00 +05:30
|
|
|
|
2022-06-02 12:06:40 +05:30
|
|
|
fclose(fp);
|
2021-09-28 10:30:00 +05:30
|
|
|
uselocale(LC_GLOBAL_LOCALE);
|
|
|
|
freelocale(tmplocale);
|
2002-02-02 04:17:29 +05:30
|
|
|
SET_IF_DESIRED(av1, avg_1);
|
|
|
|
SET_IF_DESIRED(av5, avg_5);
|
|
|
|
SET_IF_DESIRED(av15, avg_15);
|
2015-06-24 17:46:16 +05:30
|
|
|
return retval;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2003-06-08 22:58:06 +05:30
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-16 12:33:57 +05:30
|
|
|
#define PROCFS_PID_MAX "/proc/sys/kernel/pid_max"
|
|
|
|
#define DEFAULT_PID_LENGTH 5
|
2003-07-03 10:50:19 +05:30
|
|
|
|
2016-04-16 12:33:57 +05:30
|
|
|
/*
|
|
|
|
* procps_pid_length
|
|
|
|
*
|
|
|
|
* Return the length of the maximum possible pid.
|
|
|
|
*
|
|
|
|
* Returns either the strlen of PROCFS_PID_MAX or the
|
|
|
|
* best-guess DEFAULT_PID_LENGTH
|
|
|
|
*/
|
|
|
|
PROCPS_EXPORT unsigned int procps_pid_length(void)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char pidbuf[24];
|
2021-09-28 10:30:00 +05:30
|
|
|
static __thread int pid_length=0;
|
2016-04-16 12:33:57 +05:30
|
|
|
|
|
|
|
if (pid_length)
|
|
|
|
return pid_length;
|
|
|
|
|
|
|
|
pid_length = DEFAULT_PID_LENGTH;
|
|
|
|
if ((fp = fopen(PROCFS_PID_MAX, "r")) != NULL) {
|
2016-04-17 10:30:00 +05:30
|
|
|
if (fgets(pidbuf, sizeof(pidbuf), fp) != NULL) {
|
|
|
|
pid_length = strlen(pidbuf);
|
|
|
|
if (pidbuf[pid_length-1] == '\n')
|
|
|
|
--pid_length;
|
2016-04-16 12:33:57 +05:30
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
return pid_length;
|
2003-07-03 10:50:19 +05:30
|
|
|
}
|
2011-07-14 16:46:02 +05:30
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-06-29 18:01:36 +05:30
|
|
|
/* procps_cpu_count:
|
|
|
|
*
|
|
|
|
* Returns the number of CPUs that are currently online.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
long procps_cpu_count(void)
|
|
|
|
{
|
2017-11-15 10:30:00 +05:30
|
|
|
long cpus;
|
2015-06-29 18:01:36 +05:30
|
|
|
|
|
|
|
cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
if (cpus < 1)
|
|
|
|
return 1;
|
|
|
|
return cpus;
|
2011-07-14 16:46:02 +05:30
|
|
|
}
|
2015-06-29 18:01:36 +05:30
|
|
|
|