Forum: CFEngine Help
Subject: Re: renicing a process
Author: sauer
Link to topic: https://cfengine.com/forum/read.php?3,25092,25237#msg-25237

So, apparently this is a tad difficult, as "nice" and "priority" are not the 
same thing.  Further complicating things, AIX and HP-UX have completely 
differnet values for the priority field than Linux (and each other), and RHEL5 
has dynamic reprioritization enabled out of the box - so there is a range of 
possible priorities for a given nice value.

So, I ended up with this process selection bundle:

body process_select nice_exact(p){
  priority => irange("$(g.nice_to_priority[$(p)])", 
"$(g.nice_to_priority[$(p)])");
  process_result => "priority";
}


And the "g.nice_to_priority" is an array defined per-OS which maps the desired 
nice value to a priority value.  It doesn't work with the RHEL5 dynamic 
prioritization, of course, but I figure that the cost of just running renice 
periodically is lower than the cost of re-parsing "ps -o nice" myself. :)

For the record, the rest of the code looks essentially like this:

vars:
  "nice"    int    => "5";
  "pidfile" string => "/var/run/randomproc.pid";
  "niceprocs"           slist  => getindices( "nice" );
  "can_proc[$(niceprocs)]"
    string => canonify( "$(niceprocs)" );
  "pid[$(niceprocs)]"
    string => readfile( "$(pidfile[$(niceprocs)])", "15");
classes:
  "have_pid_$(can_proc[$(niceprocs)])"
    expression => fileexists("$(pidfile[$(niceprocs)])");
processes:
  "$(niceprocs)"
    process_select => nice_exact("$(nice[$(niceprocs)])"),
    restart_class  => "renice_$(can_proc[$(niceprocs)])",
    ifvarclass     => "have_pid_$(can_proc[$(niceprocs)])",
    action         => if_elapsed("$(delay)"),
    comment => "Make sure $(niceprocs) has priority $(nice[$(niceprocs)])";
commands:
  "$(renice) $(nice[$(niceprocs)]) -p $(pid[$(niceprocs)])"
    ifvarclass => 
"have_pid_$(can_proc[$(niceprocs)])&renice_$(can_proc[$(niceprocs)])";


Before I open a feature request to add an additional "nice" process selection 
capability (most ps implementations do have a separate "nice" column), am I the 
only person who's ever wanted to make a promise as to the nice value of a 
process?

PS: it'd be super convenient to have a $(match.pid) or some similar variable 
which contains the pid of the process whose name matched in a processes promise 
-- but again, I'm hesitant to open the feature request if I'm the only one who 
could use this.  That's what the have_pid_x class is above; I check to see if 
there's a pid file for the process, and that's good enough to tell if it's 
running or not (in this situation).

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to