source: trunk/web2/addons/job_monarch/jobstore.php @ 598

Last change on this file since 598 was 598, checked in by ramonb, 15 years ago

jobstore.php:

  • fix for multinode jobs
File size: 15.0 KB
RevLine 
[532]1<?php
2
[550]3ini_set("memory_limit","100M");
4set_time_limit(0);
5
[578]6$c                      = $_POST['c'];
[536]7$clustername            = $c;
8$cluster                = $c;
[532]9
[537]10// Supplied by ExtJS when DataStore has remoteSort: true
11//
[578]12$sortfield              = isset($_POST['sort'] ) ? $_POST['sort'] : "jid";
13$sortorder              = isset($_POST['dir'] ) ? $_POST['dir'] : "ASC"; // ASC or DESC
[537]14
[538]15// Search query from ext.SearchField
16//
17$query                  = isset($_POST['query']) ? $_POST['query'] : null;
18
[543]19// Filter values
20//
21$jid                    = isset($_POST['jid']) ? $_POST['jid'] : null;
[566]22$jids                   = isset($_POST['jids']) ? $_POST['jids'] : null;
[543]23$owner                  = isset($_POST['owner']) ? $_POST['owner'] : null;
24$status                 = isset($_POST['status']) ? $_POST['status'] : null;
25$queue                  = isset($_POST['queue']) ? $_POST['queue'] : null;
[550]26$host                   = isset($_POST['host']) ? $_POST['host'] : null;
[543]27
[566]28if( $jids != null )
29{
30        $jobids = explode( ",", $jids );
31}
32else
33{
34        $jobids = null;
35}
36
[571]37global $c, $clustername, $cluster, $metrics;
[532]38
[537]39// Grid Paging stuff
40//
41//$pstart       = (int) (isset($_POST['start']) ? $_POST['start'] : $_GET['pstart']);
42$pstart = (int) $_POST['start'];
43//$pend = (int) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['plimit']);
44$pend   = (int) $_POST['limit'];
45
[538]46//echo $pend.'p ';
[537]47// Need to fool Ganglia here: or it won't parse XML for our cluster
48//
[578]49$HTTP_POST_VARS['c']    = $c;
50$_GET['c']              = $c;
[536]51
52global $c, $clustername, $cluster;
53
[532]54include_once "./libtoga.php";
55
56$ds             = new DataSource();
57$myxml_data     = &$ds->getData();
58
[549]59session_start();
[578]60unset( $_SESSION['data'] );
61$_SESSION['data']       = &$myxml_data;
[549]62
[569]63global $jobs, $metrics;
[532]64
65$data_gatherer  = new DataGatherer( $clustername );
66$data_gatherer->parseXML( &$myxml_data );
67
68$heartbeat      = &$data_gatherer->getHeartbeat();
69$jobs           = &$data_gatherer->getJobs();
70//$gnodes         = $data_gatherer->getNodes();
71$cpus           = &$data_gatherer->getCpus();
72$use_fqdn       = &$data_gatherer->getUsingFQDN();
73
74// The ext grid script will send  a task field which will specify what it wants to do
75//$task = '';
76
77if( isset($_POST['task']) )
78{
79        $task = $_POST['task'];
80}
81if( isset( $HTTP_POST_VARS['task' ] ) )
82{
83        $task = $HTTP_POST_VARS['task'];
84}
85
86switch($task)
87{
[566]88    case "GETJOBS":
89        getJobs();
[532]90        break;         
[566]91    case "GETNODES":
92        getNodes();
[553]93        break;         
[569]94    case "GETMETRICS":
95        getMetrics();
96        break;         
[532]97    default:
98        echo "{failure:true}";
99        break;
100}
101
[596]102function printCacheHeaders()
103{
104        header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
105        header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
106        header ("Cache-Control: no-cache, must-revalidate");
107        header ("Pragma: no-cache");
108}
109
[569]110function getMetrics( $host=null )
111{
112        global $metrics;
113
114        reset($metrics);
115        if( !$host)
116        {
117          $firsthost = key($metrics);
118        }
119        else
120        {
121          $firsthost = $host;
122        }
123
124        $first_metrics = $metrics[$firsthost];
125
126        $metric_list    = array();
127
128        $metric_count   = 0;
129
130        foreach( $first_metrics as $metricname => $metricval )
131        {
132                $metric         = array();
133                $metric['id']   = $metricname;
134                $metric['name'] = $metricname;
135
136                $metric_list[]  = $metric;
137                $metric_count   = $metric_count + 1;
138        }
139       
140        $results                = array();
141        $results['names']       = $metric_list;
142        $results['total']       = $metric_count;
143
[596]144        printCacheHeaders();
145
[569]146        $jsonresults    = JEncode( $results );
147
148        echo $jsonresults;
149
150        return 0;
151}
152
[538]153function quickSearchJobs( $jobs, $query )
154{
155        $searchresults  = array();
156
157        foreach( $jobs as $jobid => $jobattrs )
158        {
159                if( $query != null )
160                {
161                        if( strpos( $jobattrs['jid'], $query ) !== false )
162                        {
163                                $searchresults[$jobid]  = $jobattrs;
164                        }
165                        if( strpos( $jobattrs['owner'], $query ) !== false )
166                        {
167                                $searchresults[$jobid]  = $jobattrs;
168                        }
169                        if( strpos( $jobattrs['queue'], $query ) !== false )
170                        {
171                                $searchresults[$jobid]  = $jobattrs;
172                        }
173                        if( strpos( $jobattrs['name'], $query ) !== false )
174                        {
175                                $searchresults[$jobid]  = $jobattrs;
176                        }
[563]177                        if( is_array( $jobattrs['nodes'] ) )
178                        {
179                                foreach( $jobattrs['nodes'] as $jattr )
180                                {
181                                        if( strpos( $jattr, $query ) !== false )
182                                        {
183                                                $searchresults[$jobid]  = $jobattrs;
184                                        }
185                                }
186                        }
187                        if( strpos( $jobid, $query ) !== false )
188                        {
189                                $searchresults[$jobid]  = $jobattrs;
190                        }
[538]191                }
192        }
193
194        return $searchresults;
195}
196
[537]197function sortJobs( $jobs, $sortby, $sortorder )
198{
199        $sorted = array();
200
201        $cmp    = create_function( '$a, $b',
202                "global \$sortby, \$sortorder;".
203
204                "if( \$a == \$b ) return 0;".
205
206                "if (\$sortorder==\"DESC\")".
207                        "return ( \$a < \$b ) ? 1 : -1;".
208                "else if (\$sortorder==\"ASC\")".
209                        "return ( \$a > \$b ) ? 1 : -1;" );
210
211        if( isset( $jobs ) && count( $jobs ) > 0 )
212        {
213                foreach( $jobs as $jobid => $jobattrs )
214                {
[578]215                                $state          = $jobattrs['status'];
216                                $user           = $jobattrs['owner'];
217                                $queue          = $jobattrs['queue'];
218                                $name           = $jobattrs['name'];
219                                $req_cpu        = $jobattrs['requested_time'];
220                                $req_memory     = $jobattrs['requested_memory'];
[537]221
[578]222                                $nodes          = $jobattrs['nodes'];
[537]223
[578]224                                $ppn            = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
[564]225
226                                if( $state == 'R' )
227                                {
228                                        $cpus           = count( $nodes ) * $ppn;
229                                }
230                                else
231                                {
232                                        $cpus           = ((int) $nodes ) * $ppn;
233                                }
[578]234                                $queued_time    = (int) $jobattrs['queued_timestamp'];
235                                $start_time     = (int) $jobattrs['start_timestamp'];
[537]236                                $runningtime    = $report_time - $start_time;
237
238                                switch( $sortby )
239                                {
240                                        case "jid":
241                                                $sorted[$jobid] = $jobid;
242                                                break;
243
244                                        case "status":
245                                                $sorted[$jobid] = $state;
246                                                break;
247
248                                        case "owner":
249                                                $sorted[$jobid] = $user;
250                                                break;
251
252                                        case "queue":
253                                                $sorted[$jobid] = $queue;
254                                                break;
255
256                                        case "name":
257                                                $sorted[$jobid] = $name;
258                                                break;
259
260                                        case "requested_time":
261                                                $sorted[$jobid] = timeToEpoch( $req_cpu );
262                                                break;
263
264                                        case "requested_memory":
265                                                $sorted[$jobid] = $req_memory;
266                                                break;
267
268                                        case "ppn":
269                                                $sorted[$jobid] = $ppn;
270                                                break;
[587]271                                        case "nodect":
[564]272                                                if( $state == 'Q' )
273                                                {
274                                                        $sorted[$jobid] = $nodes;
275                                                }
276                                                else
277                                                {
278                                                        $sorted[$jobid] = count( $nodes );
279                                                }
[537]280                                                break;
281                                        case "cpus":
282                                                $sorted[$jobid] = $cpus;
283                                                break;
284
285                                        case "queued_timestamp":
286                                                $sorted[$jobid] = $queued_time;
287                                                break;
288
289                                        case "start_timestamp":
290                                                $sorted[$jobid] = $start_time;
291                                                break;
292
293                                        case "runningtime":
294                                                $sorted[$jobid] = $runningtime;
295                                                break;
[564]296                                        case "nodes":
297                                                if( $state == 'R' )
298                                                {
299                                                        $sorted[$jobid] = $nodes[0];
300                                                }
301                                                else
302                                                {
303                                                        $sorted[$jobid] = $nodes;
304                                                }
[537]305
306                                        default:
307                                                break;
308                                }
309                }
310        }
311
312        if( $sortorder == "ASC" )
313        {
314                asort( $sorted );
315        }
316        else if( $sortorder == "DESC" )
317        {
318                arsort( $sorted );
319        }
320
321        return $sorted;
322}
323
[543]324function filterJobs( $jobs )
325{
[550]326        global $jid, $owner, $queue,  $status, $host, $use_fqdn;
[537]327
[543]328        $filtered_jobs  = array();
[537]329
[543]330        if( isset( $jobs ) && count( $jobs ) > 0 )
331        {
332                foreach( $jobs as $jobid => $jobattrs )
333                {
[578]334                                $state          = $jobattrs['status'];
335                                $user           = $jobattrs['owner'];
336                                $jqueue          = $jobattrs['queue'];
337                                $name           = $jobattrs['name'];
338                                $req_cpu        = $jobattrs['requested_time'];
339                                $req_memory     = $jobattrs['requested_memory'];
[543]340
341                                if( $state == 'R' )
342                                {
[578]343                                        $nodes = count( $jobattrs['nodes'] );
[550]344
345                                        $mynodehosts = array();
[578]346                                        foreach( $jobattrs['nodes'] as $mynode )
[550]347                                        {
[562]348                                                //if( $use_fqdn == 1)
349                                                //{
[578]350                                                //      $mynode = $mynode.".".$jobattrs['domain'];
[562]351                                                //}
[550]352                                                $mynodehosts[]  = $mynode;
353                                        }
[578]354                                        $jobattrs['nodes'] = $mynodehosts;
[543]355                                }
356                                else
357                                {
[578]358                                        $nodes = $jobattrs['nodes'];
[543]359                                }
360
[578]361                                $ppn            = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
[543]362                                $cpus           = $nodes * $ppn;
[578]363                                $queued_time    = (int) $jobattrs['queued_timestamp'];
364                                $start_time     = (int) $jobattrs['start_timestamp'];
[543]365                                $runningtime    = $report_time - $start_time;
[550]366
[578]367                                $domain         = $jobattrs['domain'];
[550]368                                $domain_len     = 0 - strlen( $domain );
369
[543]370                                $keepjob        = true;
371
372                                if( $jid )
373                                {
374                                        if( $jobid != $jid )
375                                        {
376                                                $keepjob        = false;
377                                        }
378                                }
[580]379                                else if( $host )
[550]380                                {
381                                        if( $state == 'R' )
382                                        {
[580]383                                                $jnodes         = $jobattrs['nodes'];
[550]384
[580]385                                                $keepjob        = false;
[550]386
387                                                foreach( $jnodes as $jnode)
388                                                {
389                                                        if( $jnode == $host )
390                                                        {
391                                                                $keepjob = true;
392                                                        }
393                                                }
394                                        }
395                                        else
396                                        {
[580]397                                                $keepjob        = false;
[550]398                                        }
399                                }
[543]400                                if( $owner )
401                                {
402                                        if( $user != $owner )
403                                        {
404                                                $keepjob        = false;
405                                        }
406                                }
407                                if( $queue )
408                                {
409                                        if( $jqueue != $queue )
410                                        {
411                                                $keepjob        = false;
412                                        }
413                                }
414                                if( $status )
415                                {
416                                        if( $state != $status )
417                                        {
418                                                $keepjob        = false;
419                                        }
420                                }
421                                if( $keepjob )
422                                {
423                                        $filtered_jobs[$jobid]  = $jobattrs;
424                                }
425                }
426        }
427
428        return $filtered_jobs;
429}
430
[566]431function getNodes()
[532]432{
[596]433        global $jobs, $jobids, $clustername, $metrics, $jid;
[566]434
435        $display_nodes  = array();
436
[596]437        printCacheHeaders();
438
439        if( !$jobids && !$jid )
[566]440        {
[596]441                printf("no jobid(s)\n");
[566]442                return 1;
443        }
[598]444
[596]445        foreach( $jobs[$jid]['nodes'] as $jobnode )
[566]446        {
[596]447                if( !in_array( $jobnode, $display_nodes) )
[566]448                {
[598]449                        $display_nodes[]        = $jobnode;
[566]450                }
451        }
[571]452
[566]453        $node_results   = array();
454        $result_count   = count( $display_nodes );
[596]455
[598]456        foreach( $display_nodes as $host )
[566]457        {
458                $nr             = array();
459                $nr['c']        = $clustername;
[571]460                $nr['h']        = $host ;
[566]461                $nr['x']        = '5';
462                $nr['v']        = '0';
463
[578]464                $cpus           = $metrics[$host]['cpu_num']['VAL'];
[571]465
466                if ( !$cpus )
467                {
468                        $cpus           = 1;
469                }
470
[578]471                $load_one       = $metrics[$host]['load_one']['VAL'];
[571]472                $load           = ((float) $load_one) / $cpus;
473                $load_color     = load_color($load);
474
475                $nr['l']        = $load_color;
476
[598]477                $job_runtime    = (int) $jobs[$jid]['reported'] - (int) $jobs[$jid]['start_timestamp'];
[571]478                $job_window     = intval($job_runtime * 1.2);
479
480                $nr['jr']       = -$job_window;
[598]481                $nr['js']       = (int) $jobs[$jid]['start_timestamp'];
[571]482
[566]483                $node_results[] = $nr;
484        }
[596]485
486
[566]487        $jsonresults    = JEncode( $node_results );
488
489        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
490}
491
492function getJobs() 
493{
[537]494        global $jobs, $hearbeat, $pstart, $pend;
[550]495        global $sortfield, $sortorder, $query, $host;
[543]496        global $jid, $owner, $queue,  $status;
[532]497
[538]498        $job_count              = count( $jobs );
[532]499
[596]500        printCacheHeaders();
501
[532]502        if( $job_count == 0 )
503        {
[538]504                echo '({"total":"0", "results":""})';
[532]505                return 0;
506        }
507
[538]508        $jobresults             = array();
[532]509
[538]510        $cur_job                = 0;
[537]511
512        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
513
[543]514        if( $query )
[538]515        {
516                $jobs                   = quickSearchJobs( $jobs, $query );
517        }
[550]518        if( $jid || $owner || $queue || $status || $host )
[543]519        {
520                $jobs                   = filterJobs( $jobs );
521        }
[538]522        $result_count           = count( $jobs );
[537]523
524        foreach( $sorted_jobs as $jobid => $jobattrs )
[532]525        {
[535]526                //if( $jobattrs['reported'] != $heartbeat )
527                //{
528                //      continue;
529                //}
[532]530
[538]531                if( ! array_key_exists( $jobid, $jobs ) )
[537]532                {
533                        continue;
534                }
535
[538]536                $jr                     = array();
[534]537                $jr['jid']              = strval( $jobid );
[537]538                $jr['status']           = $jobs[$jobid]['status'];
539                $jr['owner']            = $jobs[$jobid]['owner'];
540                $jr['queue']            = $jobs[$jobid]['queue'];
541                $jr['name']             = $jobs[$jobid]['name'];
542                $jr['requested_time']   = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) );
[534]543
[535]544                if( $jr['status'] == 'R' )
[532]545                {
[578]546                        $nodes          = count( $jobs[$jobid]['nodes'] );
[532]547                }
548                else
549                {
[578]550                        $nodes          = (int) $jobs[$jobid]['nodes'];
[532]551                }
552
[578]553                $jr['ppn']              = strval( $jobs[$jobid]['ppn'] ? $jobs[$jobid]['ppn'] : 1 );
[535]554                $jr['nodect']           = strval( $nodes );
[532]555
[535]556                if( $jr['status'] == 'R' )
[532]557                {
[537]558                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
[532]559                }
[535]560                else
561                {
562                        $jr['nodes']    = "";
563                }
[532]564
[537]565                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
566                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
[534]567
[535]568                if( $jr['status'] == 'R' )
569                {
[537]570                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
[535]571                        $jr['runningtime']      = makeTime( $runningtime );
572                }
573                else
574                {
575                        $jr['runningtime']      = "";
576                }
577
[538]578                if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) )
579                {
580                        $cur_job        = $cur_job + 1;
581                        continue;
582                }
583                else
584                {
585                        $cur_job        = $cur_job + 1;
586                }
[537]587
[534]588                $jobresults[]           = $jr;
[532]589        }
590
591        $jsonresults    = JEncode( $jobresults );
592
[596]593
[537]594        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
[532]595
596        return 0;
597}
598
[538]599// Encodes a SQL array into a JSON formated string: so that Javascript may understand it
600//
[532]601function JEncode( $arr )
602{
[538]603        if( version_compare( PHP_VERSION, "5.2", "<" ) )
[532]604        {   
[538]605                require_once( "./JSON.php" );           //if php<5.2 need JSON class
[532]606
[538]607                $json   = new Services_JSON();          //instantiate new json object
608                $data   = $json->encode( $arr );        //encode the data in json format
[532]609        } 
610        else
611        {
[538]612                $data   = json_encode( $arr );          //encode the data in json format
[532]613        }
614
615        return $data;
616}
617
618?> 
Note: See TracBrowser for help on using the repository browser.