| 42 | |
| 43 | #ifdef __linux__ |
| 44 | static int count_runnable() { |
| 45 | const int BUFSIZE = 1024; |
| 46 | char buf[BUFSIZE]; |
| 47 | int fd = open("/proc/loadavg", O_RDONLY); |
| 48 | if (fd == -1) { |
| 49 | perror("alive-jobserver: open"); |
| 50 | exit(-1); |
| 51 | } |
| 52 | int len = read(fd, buf, BUFSIZE); |
| 53 | if (len == -1) { |
| 54 | perror("alive-jobserver: read"); |
| 55 | exit(-1); |
| 56 | } |
| 57 | close(fd); |
| 58 | int spaces = 0, pos; |
| 59 | for (pos = 0; pos < len; ++pos) { |
| 60 | if (buf[pos] == ' ') |
| 61 | ++spaces; |
| 62 | if (spaces == 3) |
| 63 | break; |
| 64 | } |
| 65 | assert(spaces == 3); |
| 66 | ++pos; |
| 67 | char *endp; |
| 68 | int runnable = strtol(&buf[pos], &endp, 10); |
| 69 | assert(*endp == '/'); |
| 70 | return runnable - 1; |
| 71 | } |
| 72 | |
| 73 | static int count_tokens(int pipefd) { |
| 74 | int flags = fcntl(pipefd, F_GETFL, 0); |