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

Feature Request: Get the number of file descriptors #3953

Open
vitoyucepi opened this issue Jun 8, 2024 · 1 comment
Open

Feature Request: Get the number of file descriptors #3953

vitoyucepi opened this issue Jun 8, 2024 · 1 comment

Comments

@vitoyucepi
Copy link
Collaborator

Is your feature request related to a problem? Please describe.

I'd like to know the number of file descriptors the liquidsoap process has opened and the maximum number of file descriptors the OS will allow liquidsoap to open.

Describe the solution you'd like

Add new functions to the process module, for example:

  • process.fd_open() or process.fd.open().
  • process.fd_max() or process.fd.max().
  • process.fd() -> {"open": int, "max": int}.

Describe alternatives you've considered

N/A

Additional context

#3156

@toots
Copy link
Member

toots commented Jun 11, 2024

This is a good idea.

Counting open FDs is platform dependent but seems pretty straight forward on at least linux and macos.

Here's some code for macos:

#include <libproc.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/proc_info.h>
#include <unistd.h>

int main() {
  int bufferSize = proc_pidinfo(getpid(), PROC_PIDLISTFDS, 0, 0, 0);
  struct proc_fdinfo *procFDInfo = (struct proc_fdinfo *)malloc(bufferSize);

  proc_pidinfo(getpid(), PROC_PIDLISTFDS, 0, procFDInfo, bufferSize);

  int numberOfProcFDs = bufferSize / PROC_PIDLISTFD_SIZE;

  int i;
  for (i = 0; i < numberOfProcFDs; i++) {
    if (procFDInfo[i].proc_fdtype == PROX_FDTYPE_VNODE) {
      printf("Open FD %d\n", procFDInfo[i].proc_fd);
    }
  }

  return 0;
}

And some for linux is here: https://stackoverflow.com/questions/6583158/finding-open-file-descriptors-for-a-process-linux-c-code

#include <dirent.h>
#include <stdio.h>
#include <unistd.h>

int main() {
  int fd_count;
  char buf[64];
  struct dirent *dp;

  snprintf(buf, 64, "/proc/%i/fd/", getpid());

  fd_count = 0;
  DIR *dir = opendir(buf);
  while ((dp = readdir(dir)) != NULL) {
    fd_count++;
  }
  closedir(dir);
  return fd_count;
}

This could be implemented directly as a c stubs inside the liquidsoap code, see for instance: https://github.com/savonet/liquidsoap/blob/main/src/core/tools/unix_c.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants