savonet / liquidsoap

Liquidsoap is a statically typed scripting general-purpose language with dedicated operators and backend for all thing media, streaming, file generation, automation, HTTP backend and more.
http://liquidsoap.info
GNU General Public License v2.0
1.36k stars 121 forks source link

Feature Request: Get the number of file descriptors #3953

Open vitoyucepi opened 1 month ago

vitoyucepi commented 1 month ago

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:

Describe alternatives you've considered

N/A

Additional context

3156

toots commented 1 month ago

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