Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Linux, try to use /proc/loadavg to get instaneous load (#2218) #2268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,36 @@ double GetLoadAverage() {
}
#else
double GetLoadAverage() {
# if defined(__linux__)
// Only try to open the file once.
static int fd = -1;
entrope marked this conversation as resolved.
Show resolved Hide resolved
if (fd == -1) {
fd = open("/proc/loadavg", O_RDONLY);
if (fd < 0) {
fd = -2;
}
entrope marked this conversation as resolved.
Show resolved Hide resolved
}

if (fd >= 0) {
char buf[65], *p;
ssize_t nbr = pread(fd, buf, sizeof(buf) - 1, 0);
// Load average should be at least "1 5 9 n/", where we want the "n".
if (nbr > 7) {
buf[nbr] = '\0';
p = strchr(buf, ' ');
if (p) {
p = strchr(p + 1, ' ');
}
if (p) {
p = strchr(p + 1, ' ');
}
if (p && (unsigned(p[1]) - '0' <= 9)) {
return atoi(p + 1);
}
}
}
# endif

double loadavg[3] = { 0.0f, 0.0f, 0.0f };
if (getloadavg(loadavg, 3) < 0) {
// Maybe we should return an error here or the availability of
Expand Down