Automatically adjusting process priorities under Linux

前端 未结 7 1400
眼角桃花
眼角桃花 2021-02-20 11:52

I\'m trying to write a program that automatically sets process priorities based on a configuration file (basically path - priority pairs).

I thought the best solution wo

相关标签:
7条回答
  • 2021-02-20 12:25

    Does anyone of you have any ideas on how to complete this task?

    As an idea, consider using apparmor in complain-mode. That would log certain messages to syslog, which you could listen to.

    0 讨论(0)
  • 2021-02-20 12:26

    If the processes in question are started by executing an executable file with a known path, you can use the inotify mechanism to watch for events on that file. Executing it will trigger an I_OPEN and an I_ACCESS event.

    Unfortunately, this won't tell you which process caused the event to trigger, but you can then check which /proc/*/exe are a symlink to the executable file in question and renice the process id in question.

    E.g. here is a crude implementation in Perl using Linux::Inotify2 (which, on Ubuntu, is provided by the liblinux-inotify2-perl package):

    perl -MLinux::Inotify2 -e '
      use warnings;
      use strict;
      my $x = shift(@ARGV);
      my $w = new Linux::Inotify2;
      $w->watch($x, IN_ACCESS, sub
      {
        for (glob("/proc/*/exe"))
        {
          if (-r $_ && readlink($_) eq $x && m#^/proc/(\d+)/#)
          {
            system(@ARGV, $1)
          }
        }
      });
      1 while $w->poll
    ' /bin/ls renice
    

    You can of course save the Perl code to a file, say onexecuting, prepend a first line #!/usr/bin/env perl, make the file executable, put it on your $PATH, and from then on use onexecuting /bin/ls renice.

    Then you can use this utility as a basis for implementing various policies for renicing executables. (or doing other things).

    0 讨论(0)
  • 2021-02-20 12:27

    There's another point of attack you might consider: replace the system's dynamic linker with a modified one which applies your logic. (See this paper for some nice examples of what's possible from the largely neglected art of linker hacking).

    Where this approach will have problems is with purely statically linked binaries. I doubt there's much on a modern system which actually doesn't link something dynamically (things like busybox-static being the obvious exceptions, although you might regard the ability to get a minimal shell outside of your controls as a feature when it all goes horribly wrong), so this may not be a big deal. On the other hand, if the priority policies are intended to bring some order to an overloaded shared multi-user system then you might see smart users preparing static-linked versions of apps to avoid linker-imposed priorities.

    0 讨论(0)
  • 2021-02-20 12:30

    Sometimes polling is a necessity, and even more optimal in the end -- believe it or not. It depends on a lot of variables.

    If the polling overhead is low-enough, it far exceeds the added complexity, cost, and RISK of developing your own style kernel hooks to get notified of the changes you need. That said, when hooks or notification events are available, or can be easily injected, they should certainly be used if the situation calls.

    This is classic programmer 'perfection' thinking. As engineers, we strive for perfection. This is the real world though and sometimes compromises must be made. Ironically, the more perfect solution may be the less efficient one in some cases.

    I develop a similar 'process and process priority optimization automation' tool for Windows called Process Lasso (not an advertisement, its free). I had a similar choice to make and have a hybrid solution in place. Kernel mode hooks are available for certain process related events in Windows (creation and destruction), but they not only aren't exposed at user mode, but also aren't helpful at monitoring other process metrics. I don't think any OS is going to natively inform you of any change to any process metric. The overhead for that many different hooks might be much greater than simple polling.

    Lastly, considering the HIGH frequency of process changes, it may be better to handle all changes at once (polling at interval) vs. notification events/hooks, which may have to be processed many more times per second.

    You are RIGHT to stay away from scripts. Why? Because they are slow(er). Of course, the linux scheduler does a fairly good job at handling CPU bound threads by downgrading their priority and rewarding (upgrading) the priority of I/O bound threads -- so even in high loads a script should be responsive I guess.

    0 讨论(0)
  • 2021-02-20 12:33

    If you've settled for a polling solution, most of the features you want to implement already exist in the Automatic Nice Daemon. You can configure nice levels for processes based on process name, user and group. It's even possible to adjust process priorities dynamically based on how much CPU time it has used so far.

    0 讨论(0)
  • 2021-02-20 12:37

    Sure, just iterate through /proc/nnn/exe to get the pathname of the running image. Only use the ones with slashes, the others are kernel procs.

    Check to see if you have already processed that one, otherwise look up the new priority in your configuration file and use renice(8) to tweak its priority.

    0 讨论(0)
提交回复
热议问题