Skip to content

Commit

Permalink
Adopt an efficient implementation for getloadavg
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGarfield committed Jan 25, 2025
1 parent 9a1bba0 commit 04e1790
Showing 1 changed file with 8 additions and 39 deletions.
47 changes: 8 additions & 39 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

#ifdef __IMPL_GETLOADAVG
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/sysinfo.h>
#endif

#if defined(__linux__) && !defined(_SC_NPROCESSORS_ONLN)
Expand Down Expand Up @@ -149,48 +148,18 @@ pid_t get_pid_max(void)
#ifdef __IMPL_GETLOADAVG
int __getloadavg(double *loadavg, int nelem)
{
int fd, i;
char buffer[65], *ptr;
ssize_t bytesread;
struct sysinfo si;
int i;

if (nelem < 0)
{
return -1;
}
else if (nelem == 0)
{
return 0;
}
else if (nelem > 3)
{
if (nelem <= 0)
return nelem ? -1 : 0;
sysinfo(&si);
if (nelem > 3)
nelem = 3;
}

if ((fd = open("/proc/loadavg", O_RDONLY)) < 0)
{
return -1;
}

bytesread = read(fd, buffer, sizeof(buffer) - 1);
close(fd);
if (bytesread <= 0)
{
return -1;
}
buffer[bytesread - 1] = '\0';

ptr = buffer;

for (i = 0; i < nelem; i++)
{
char *endptr;
errno = 0;
loadavg[i] = strtod(ptr, &endptr);
if (errno != 0 || ptr == endptr)
{
return -1;
}
ptr = endptr;
loadavg[i] = 1.0 / (1 << SI_LOAD_SHIFT) * si.loads[i];
}

return nelem;
Expand Down

0 comments on commit 04e1790

Please sign in to comment.