Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
There is a smallish (8 node) computing cluster where I work, and there has recently been some contention over sharing CPU time. Three seperate groups "bought into" the cluster in differing ammounts (5 shares for one group, 2 shares for a second, and one share for a third). Each group thus "owns" that many nodes, and should get first priority on those they own. However, if they are not running anything on their node(s), the spare CPU cycles are fair game for the other groups. Thus was born the Autonicer. Currently, It requires BSD-style 'ps' for collecting data, and does not take into account how long the processes has been running (although, ps does to some extent).
#!/usr/bin/perl -wT delete $ENV{PATH}; use strict; # Script to auto-renice jobs based on who is running them, and where. # This is based on unix group membership, so make sure that # /etc/groups is correct. # # "Hogs" are processes that are using lots of CPU, don't # belong to root and are not already niced. # Who "owns" each node. The key should be the value returned # by 'hostname', and the value should be a valid unix group. my %nodes = ( 'node0' => 'groupA', 'node1' => 'groupA', 'node2' => 'groupA', 'node3' => 'groupB', 'node4' => 'groupC', 'node5' => 'groupA', 'node6' => 'groupA', 'node7' => 'groupA' ); #location and arguments to ps my $ps = "/bin/ps --noheader -eo user,pid,pcpu"; # processes taking more than this %cpu are considered my $hog_level = 25; #This is the new nice level that procs are set to. my $nice_level = 19; my $node=`/bin/uname -n`; chomp $node; my (%cpu_hogs, @procs); ###################################################################### +######### sub get_hogs { # find and return a pid=>user hash of process hogs my $level=shift || 75; my @procs=@_; my (%hogs,$pid,$user,$name,$cpu,$priority,$time); foreach (@procs) { ($user,$pid,$cpu) = split (/\s+/, $_); #skip root-owned processes next if $user eq "root"; #print STDERR "[$_] : [$pid] - [$user] - [$cpu]\n"; if ($cpu >= $level) { $hogs{$pid}=$user; } } return %hogs; } sub get_procs { # One proc per line... return split(/\n/, `$ps | sed 's/^ *//'`); } sub user_valid_on_this_node { my $user=shift; my $node=shift; $user =~ /^([\w]+)/; $user=$1; my @output=split(/\s+/,`/usr/bin/groups $user | sed 's/^[^:]*://'` +); my $temp=$nodes{$node}; #print STDERR "checking $user on [$temp]($node) against ", join("_ +", @output),"\n"; if (grep (/$temp/, @output) ) { return 1; } else { return 0; } } #nice down the processes if they aren't nice already sub make_nice { my %hogs=@_; my ($pid, $user, $prio); while (($pid,$user)=each(%hogs)) { $prio=getpriority(0,$pid); print STDERR "checking $user [$pid], prio=$prio\n"; if ($prio < $nice_level && !user_valid_on_this_node($user,$nod +e) ) { setpriority(0,$pid,$nice_level); print STDERR "Reniced [$pid] to ", getpriority(0,$pid),"\n +"; } } } ###################################################################### #get a process list @procs=get_procs(); # Find the hogs %cpu_hogs=get_hogs($hog_level, @procs); # nice the poeple who should be niced make_nice(%cpu_hogs); exit 0;

Edited: ~Thu Jul 25 21:08:54 2002 (GMT) by footpad: Added <READMORE> tag


In reply to Auto-renicer by hawson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-25 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found