[11] | 1 | # ---------------------------------------------------------------------- |
---|
| 2 | # COMPONENT: tool - represents an entire tool |
---|
| 3 | # |
---|
| 4 | # This object represents an entire tool defined by Rappture. |
---|
| 5 | # Each tool resides in an installation directory with other tool |
---|
| 6 | # resources (libraries, examples, etc.). Each tool is defined by |
---|
| 7 | # its inputs and outputs, which are tied to various widgets in the |
---|
| 8 | # GUI. Each tool tracks the inputs, knows when they're changed, |
---|
| 9 | # and knows how to run itself to produce new results. |
---|
| 10 | # ====================================================================== |
---|
| 11 | # AUTHOR: Michael McLennan, Purdue University |
---|
[115] | 12 | # Copyright (c) 2004-2005 Purdue Research Foundation |
---|
| 13 | # |
---|
| 14 | # See the file "license.terms" for information on usage and |
---|
| 15 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
[11] | 16 | # ====================================================================== |
---|
| 17 | package require BLT |
---|
| 18 | |
---|
| 19 | itcl::class Rappture::Tool { |
---|
[22] | 20 | inherit Rappture::ControlOwner |
---|
[11] | 21 | |
---|
[22] | 22 | constructor {xmlobj installdir args} { |
---|
[1929] | 23 | Rappture::ControlOwner::constructor "" |
---|
[22] | 24 | } { # defined below } |
---|
[11] | 25 | |
---|
[2164] | 26 | destructor { # defined below } |
---|
| 27 | |
---|
[11] | 28 | public method installdir {} { return $_installdir } |
---|
| 29 | |
---|
| 30 | public method run {args} |
---|
| 31 | public method abort {} |
---|
[2078] | 32 | public method reset {} |
---|
[11] | 33 | |
---|
[1003] | 34 | protected method _mkdir {dir} |
---|
[23] | 35 | protected method _output {data} |
---|
| 36 | |
---|
[2164] | 37 | private variable _origxml "" ;# copy of original XML (for reset) |
---|
[11] | 38 | private variable _installdir "" ;# installation directory for this tool |
---|
[23] | 39 | private variable _outputcb "" ;# callback for tool output |
---|
[11] | 40 | private common job ;# array var used for blt::bgexec jobs |
---|
[158] | 41 | private common jobnum 0 ;# counter for unique job number |
---|
[413] | 42 | |
---|
[724] | 43 | # get global resources for this tool session |
---|
| 44 | public proc resources {{option ""}} |
---|
| 45 | |
---|
| 46 | public common _resources |
---|
[903] | 47 | public proc setAppName {name} { set _resources(-appname) $name } |
---|
| 48 | public proc setHubName {name} { set _resources(-hubname) $name } |
---|
| 49 | public proc setHubURL {name} { set _resources(-huburl) $name } |
---|
| 50 | public proc setSession {name} { set _resources(-session) $name } |
---|
| 51 | public proc setJobPrt {name} { set _resources(-jobprotocol) $name } |
---|
| 52 | public proc setResultDir {name} { set _resources(-resultdir) $name } |
---|
[11] | 53 | } |
---|
[413] | 54 | |
---|
| 55 | # must use this name -- plugs into Rappture::resources::load |
---|
| 56 | proc tool_init_resources {} { |
---|
| 57 | Rappture::resources::register \ |
---|
[1929] | 58 | application_name Rappture::Tool::setAppName \ |
---|
| 59 | application_id Rappture::Tool::setAppId \ |
---|
| 60 | hub_name Rappture::Tool::setHubName \ |
---|
| 61 | hub_url Rappture::Tool::setHubURL \ |
---|
| 62 | session_token Rappture::Tool::setSession \ |
---|
| 63 | job_protocol Rappture::Tool::setJobPrt \ |
---|
| 64 | results_directory Rappture::Tool::setResultDir |
---|
[413] | 65 | } |
---|
[2164] | 66 | |
---|
[11] | 67 | # ---------------------------------------------------------------------- |
---|
| 68 | # CONSTRUCTOR |
---|
| 69 | # ---------------------------------------------------------------------- |
---|
| 70 | itcl::body Rappture::Tool::constructor {xmlobj installdir args} { |
---|
| 71 | if {![Rappture::library isvalid $xmlobj]} { |
---|
[1929] | 72 | error "bad value \"$xmlobj\": should be Rappture::Library" |
---|
[11] | 73 | } |
---|
| 74 | set _xmlobj $xmlobj |
---|
| 75 | |
---|
[2164] | 76 | # stash a copy of the original XML for later "reset" operations |
---|
| 77 | set _origxml [Rappture::LibraryObj ::#auto "<?xml version=\"1.0\"?><run/>"] |
---|
| 78 | $_origxml copy "" from $_xmlobj "" |
---|
| 79 | |
---|
[11] | 80 | if {![file exists $installdir]} { |
---|
[1929] | 81 | error "directory \"$installdir\" doesn't exist" |
---|
[11] | 82 | } |
---|
| 83 | set _installdir $installdir |
---|
| 84 | |
---|
| 85 | eval configure $args |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | # ---------------------------------------------------------------------- |
---|
[2164] | 89 | # DESTRUCTOR |
---|
| 90 | # ---------------------------------------------------------------------- |
---|
| 91 | itcl::body Rappture::Tool::destructor {} { |
---|
| 92 | itcl::delete object $_origxml |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | # ---------------------------------------------------------------------- |
---|
[724] | 96 | # USAGE: resources ?-option? |
---|
[640] | 97 | # |
---|
| 98 | # Clients use this to query information about the tool. |
---|
| 99 | # ---------------------------------------------------------------------- |
---|
[724] | 100 | itcl::body Rappture::Tool::resources {{option ""}} { |
---|
[640] | 101 | if {$option == ""} { |
---|
[1929] | 102 | return [array get _resources] |
---|
[640] | 103 | } |
---|
[731] | 104 | if {[info exists _resources($option)]} { |
---|
[1929] | 105 | return $_resources($option) |
---|
[640] | 106 | } |
---|
[731] | 107 | return "" |
---|
[640] | 108 | } |
---|
| 109 | |
---|
| 110 | # ---------------------------------------------------------------------- |
---|
[23] | 111 | # USAGE: run ?<path1> <value1> <path2> <value2> ...? ?-output <callbk>? |
---|
[11] | 112 | # |
---|
| 113 | # This method causes the tool to run. All widgets are synchronized |
---|
| 114 | # to the current XML representation, and a "driver.xml" file is |
---|
| 115 | # created as the input for the run. That file is fed to the tool |
---|
| 116 | # according to the <tool><command> string, and the job is executed. |
---|
| 117 | # |
---|
[23] | 118 | # Any "<path> <value>" arguments are used to override the current |
---|
| 119 | # settings from the GUI. This is useful, for example, when filling |
---|
| 120 | # in missing simulation results from the analyzer. |
---|
| 121 | # |
---|
| 122 | # If the -output argument is included, then the next arg is a |
---|
| 123 | # callback command for output messages. Any output that comes in |
---|
| 124 | # while the tool is running is sent back to the caller, so the user |
---|
| 125 | # can see progress running the tool. |
---|
| 126 | # |
---|
[11] | 127 | # Returns a list of the form {status result}, where status is an |
---|
| 128 | # integer status code (0=success) and result is the output from the |
---|
| 129 | # simulator. Successful output is something like {0 run1293921.xml}, |
---|
| 130 | # where 0=success and run1293921.xml is the name of the file containing |
---|
| 131 | # results. |
---|
| 132 | # ---------------------------------------------------------------------- |
---|
| 133 | itcl::body Rappture::Tool::run {args} { |
---|
| 134 | global errorInfo |
---|
| 135 | |
---|
[674] | 136 | # |
---|
| 137 | # Make sure that we save the proper application name. |
---|
| 138 | # Actually, the best place to get this information is |
---|
| 139 | # straight from the "installtool" script, but just in |
---|
| 140 | # case we have an older tool, we should insert the |
---|
| 141 | # tool name from the resources config file. |
---|
| 142 | # |
---|
[726] | 143 | if {[info exists _resources(-appname)] |
---|
[1929] | 144 | && "" != $_resources(-appname) |
---|
| 145 | && "" == [$_xmlobj get tool.name]} { |
---|
| 146 | $_xmlobj put tool.name $_resources(-appname) |
---|
[413] | 147 | } |
---|
| 148 | |
---|
[11] | 149 | # sync all widgets to the XML tree |
---|
| 150 | sync |
---|
| 151 | |
---|
| 152 | # if there are any args, use them to override parameters |
---|
[23] | 153 | set _outputcb "" |
---|
[11] | 154 | foreach {path val} $args { |
---|
[1929] | 155 | if {$path == "-output"} { |
---|
| 156 | set _outputcb $val |
---|
| 157 | } else { |
---|
| 158 | $_xmlobj put $path.current $val |
---|
| 159 | } |
---|
[11] | 160 | } |
---|
| 161 | |
---|
| 162 | foreach item {control output error} { set job($item) "" } |
---|
| 163 | |
---|
| 164 | # write out the driver.xml file for the tool |
---|
| 165 | set file "driver[pid].xml" |
---|
| 166 | set status [catch { |
---|
[1929] | 167 | set fid [open $file w] |
---|
| 168 | puts $fid "<?xml version=\"1.0\"?>" |
---|
| 169 | puts $fid [$_xmlobj xml] |
---|
| 170 | close $fid |
---|
[11] | 171 | } result] |
---|
| 172 | |
---|
[1286] | 173 | # set limits for cpu time |
---|
[158] | 174 | set limit [$_xmlobj get tool.limits.cputime] |
---|
| 175 | if {"" == $limit || [catch {Rappture::rlimit set cputime $limit}]} { |
---|
[1929] | 176 | Rappture::rlimit set cputime 900 ;# 15 mins by default |
---|
[158] | 177 | } |
---|
| 178 | |
---|
[11] | 179 | # execute the tool using the path from the tool description |
---|
| 180 | if {$status == 0} { |
---|
[1929] | 181 | set cmd [$_xmlobj get tool.command] |
---|
| 182 | regsub -all @tool $cmd $_installdir cmd |
---|
| 183 | regsub -all @driver $cmd $file cmd |
---|
| 184 | regsub -all {\\} $cmd {\\\\} cmd |
---|
| 185 | set cmd [string trimleft $cmd " "] |
---|
[2753] | 186 | if { $cmd == "" } { |
---|
| 187 | puts stderr "cmd is empty" |
---|
| 188 | return [list 1 "Command is empty.\n\nThere is no command specified by\n\n <command>\n </command>\n\nin the tool.xml file."] |
---|
| 189 | } |
---|
[1929] | 190 | # if job_protocol is "submit", then use use submit command |
---|
| 191 | if {[resources -jobprotocol] == "submit"} { |
---|
| 192 | set cmd [linsert $cmd 0 submit --local] |
---|
| 193 | } |
---|
[731] | 194 | |
---|
[1929] | 195 | # starting job... |
---|
| 196 | Rappture::rusage mark |
---|
[158] | 197 | |
---|
[1929] | 198 | if {0 == [string compare -nocase -length 5 $cmd "ECHO "] } { |
---|
| 199 | set status 0; |
---|
| 200 | set job(output) [string range $cmd 5 end] |
---|
| 201 | } else { |
---|
[2753] | 202 | set status [catch { |
---|
| 203 | eval blt::bgexec \ |
---|
| 204 | ::Rappture::Tool::job(control) \ |
---|
| 205 | -keepnewline yes \ |
---|
| 206 | -killsignal SIGTERM \ |
---|
| 207 | -onoutput [list [itcl::code $this _output]] \ |
---|
| 208 | -output ::Rappture::Tool::job(output) \ |
---|
| 209 | -error ::Rappture::Tool::job(error) \ |
---|
| 210 | $cmd |
---|
| 211 | } result] |
---|
[2743] | 212 | |
---|
[2744] | 213 | if { $status != 0 } { |
---|
[3032] | 214 | # We're here because the exec-ed program failed |
---|
[2753] | 215 | if { $::Rappture::Tool::job(control) != "" } { |
---|
[3032] | 216 | foreach { token pid code mesg } \ |
---|
| 217 | $::Rappture::Tool::job(control) break |
---|
| 218 | if { $token == "EXITED" } { |
---|
| 219 | # This means that the program exited normally but |
---|
| 220 | # returned a non-zero exitcode. Consider this an |
---|
| 221 | # invalid result from the program. Append the stderr |
---|
| 222 | # from the program to the message. |
---|
[3116] | 223 | set result \ |
---|
| 224 | "Program finished: exit code is $code\n\n" |
---|
[2977] | 225 | append result $::Rappture::Tool::job(error) |
---|
[3032] | 226 | } elseif { $token == "abort" } { |
---|
| 227 | # The user pressed the abort button. |
---|
| 228 | set result "Program terminated by user.\n\n" |
---|
| 229 | append result $::Rappture::Tool::job(output) |
---|
| 230 | } else { |
---|
| 231 | # Abnormal termination |
---|
| 232 | set result "Abnormal program termination: $mesg\n\n" |
---|
| 233 | append result $::Rappture::Tool::job(output) |
---|
[2753] | 234 | } |
---|
| 235 | } |
---|
| 236 | return [list $status $result] |
---|
[2744] | 237 | } |
---|
| 238 | } |
---|
[1929] | 239 | # ...job is finished |
---|
| 240 | array set times [Rappture::rusage measure] |
---|
[158] | 241 | |
---|
[1929] | 242 | if {[resources -jobprotocol] != "submit"} { |
---|
| 243 | set id [$_xmlobj get tool.id] |
---|
| 244 | set vers [$_xmlobj get tool.version.application.revision] |
---|
| 245 | set simulation simulation |
---|
| 246 | if { $id != "" && $vers != "" } { |
---|
| 247 | set pid [pid] |
---|
| 248 | set simulation ${pid}_${id}_r${vers} |
---|
| 249 | } |
---|
| 250 | puts stderr "MiddlewareTime: job=[incr jobnum] event=$simulation start=$times(start) walltime=$times(walltime) cputime=$times(cputime) status=$status" |
---|
[637] | 251 | |
---|
[1929] | 252 | # |
---|
| 253 | # Scan through stderr channel and look for statements that |
---|
| 254 | # represent grid jobs that were executed. The statements |
---|
| 255 | # look like this: |
---|
| 256 | # |
---|
| 257 | # MiddlewareTime: job=1 event=simulation start=3.001094 ... |
---|
| 258 | # |
---|
| 259 | set subjobs 0 |
---|
| 260 | while {[regexp -indices {(^|\n)MiddlewareTime:( +[a-z]+=[^ \n]+)+(\n|$)} $job(error) match]} { |
---|
| 261 | foreach {p0 p1} $match break |
---|
| 262 | if {[string index $job(error) $p0] == "\n"} { incr p0 } |
---|
[637] | 263 | |
---|
[1929] | 264 | catch {unset data} |
---|
| 265 | array set data { |
---|
| 266 | job 1 |
---|
| 267 | event simulation |
---|
| 268 | start 0 |
---|
| 269 | walltime 0 |
---|
| 270 | cputime 0 |
---|
| 271 | status 0 |
---|
| 272 | } |
---|
| 273 | foreach arg [lrange [string range $job(error) $p0 $p1] 1 end] { |
---|
| 274 | foreach {key val} [split $arg =] break |
---|
| 275 | set data($key) $val |
---|
| 276 | } |
---|
| 277 | set data(job) [expr {$jobnum+$data(job)}] |
---|
| 278 | set data(event) "subsimulation" |
---|
| 279 | set data(start) [expr {$times(start)+$data(start)}] |
---|
[734] | 280 | |
---|
[1929] | 281 | set stmt "MiddlewareTime:" |
---|
| 282 | foreach key {job event start walltime cputime status} { |
---|
| 283 | # add required keys in a particular order |
---|
| 284 | append stmt " $key=$data($key)" |
---|
| 285 | unset data($key) |
---|
| 286 | } |
---|
| 287 | foreach key [array names data] { |
---|
| 288 | # add anything else that the client gave -- venue, etc. |
---|
| 289 | append stmt " $key=$data($key)" |
---|
| 290 | } |
---|
| 291 | puts stderr $stmt |
---|
| 292 | incr subjobs |
---|
[734] | 293 | |
---|
[1929] | 294 | # done -- remove this statement |
---|
| 295 | set job(error) [string replace $job(error) $p0 $p1] |
---|
| 296 | } |
---|
| 297 | incr jobnum $subjobs |
---|
| 298 | } |
---|
[637] | 299 | |
---|
[11] | 300 | } else { |
---|
[1929] | 301 | set job(error) "$result\n$errorInfo" |
---|
[11] | 302 | } |
---|
| 303 | if {$status == 0} { |
---|
[1929] | 304 | file delete -force -- $file |
---|
[11] | 305 | } |
---|
| 306 | |
---|
| 307 | # see if the job was aborted |
---|
| 308 | if {[regexp {^KILLED} $job(control)]} { |
---|
[1929] | 309 | return [list 0 "ABORT"] |
---|
[11] | 310 | } |
---|
| 311 | |
---|
| 312 | # |
---|
| 313 | # If successful, return the output, which should include |
---|
| 314 | # a reference to the run.xml file containing results. |
---|
| 315 | # |
---|
| 316 | if {$status == 0} { |
---|
[1929] | 317 | set result [string trim $job(output)] |
---|
| 318 | if {[regexp {=RAPPTURE-RUN=>([^\n]+)} $result match file]} { |
---|
| 319 | set status [catch {Rappture::library $file} result] |
---|
| 320 | if {$status != 0} { |
---|
| 321 | global errorInfo |
---|
| 322 | set result "$result\n$errorInfo" |
---|
| 323 | } |
---|
[903] | 324 | |
---|
[1929] | 325 | # if there's a results_directory defined in the resources |
---|
| 326 | # file, then move the run.xml file there for storage |
---|
| 327 | if {[info exists _resources(-resultdir)] |
---|
| 328 | && "" != $_resources(-resultdir)} { |
---|
| 329 | catch { |
---|
| 330 | if {![file exists $_resources(-resultdir)]} { |
---|
| 331 | _mkdir $_resources(-resultdir) |
---|
| 332 | } |
---|
| 333 | file rename -force -- $file $_resources(-resultdir) |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | } else { |
---|
| 337 | set status 1 |
---|
| 338 | set result "Can't find result file in output.\nDid you call Rappture |
---|
[903] | 339 | ::result in your simulator?" |
---|
[1929] | 340 | } |
---|
| 341 | return [list $status $result] |
---|
[11] | 342 | } elseif {"" != $job(output) || "" != $job(error)} { |
---|
[1929] | 343 | return [list $status [string trim "$job(output)\n$job(error)"]] |
---|
[11] | 344 | } |
---|
| 345 | return [list $status $result] |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | # ---------------------------------------------------------------------- |
---|
[1003] | 349 | # USAGE: _mkdir <directory> |
---|
| 350 | # |
---|
| 351 | # Used internally to create the <directory> in the file system. |
---|
| 352 | # The parent directory is also created, as needed. |
---|
| 353 | # ---------------------------------------------------------------------- |
---|
| 354 | itcl::body Rappture::Tool::_mkdir {dir} { |
---|
| 355 | set parent [file dirname $dir] |
---|
| 356 | if {"." != $parent && "/" != $parent} { |
---|
[1929] | 357 | if {![file exists $parent]} { |
---|
| 358 | _mkdir $parent |
---|
| 359 | } |
---|
[1003] | 360 | } |
---|
| 361 | file mkdir $dir |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | |
---|
| 365 | # ---------------------------------------------------------------------- |
---|
[11] | 366 | # USAGE: abort |
---|
| 367 | # |
---|
| 368 | # Clients use this during a "run" to abort the current job. |
---|
| 369 | # Kills the job and forces the "run" method to return. |
---|
| 370 | # ---------------------------------------------------------------------- |
---|
| 371 | itcl::body Rappture::Tool::abort {} { |
---|
| 372 | set job(control) "abort" |
---|
| 373 | } |
---|
[23] | 374 | |
---|
| 375 | # ---------------------------------------------------------------------- |
---|
[2078] | 376 | # USAGE: reset |
---|
| 377 | # |
---|
| 378 | # Resets all input values to their defaults. Sometimes used just |
---|
| 379 | # before a run to reset to a clean state. |
---|
| 380 | # ---------------------------------------------------------------------- |
---|
| 381 | itcl::body Rappture::Tool::reset {} { |
---|
[2164] | 382 | $_xmlobj copy "" from $_origxml "" |
---|
[2078] | 383 | foreach path [Rappture::entities -as path $_xmlobj input] { |
---|
| 384 | if {[$_xmlobj element -as type $path.default] ne ""} { |
---|
| 385 | set defval [$_xmlobj get $path.default] |
---|
[2135] | 386 | $_xmlobj put $path.current $defval |
---|
[2078] | 387 | } |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | |
---|
[2135] | 391 | # ---------------------------------------------------------------------- |
---|
[23] | 392 | # USAGE: _output <data> |
---|
| 393 | # |
---|
| 394 | # Used internally to send each bit of output <data> coming from the |
---|
| 395 | # tool onto the caller, so the user can see progress. |
---|
| 396 | # ---------------------------------------------------------------------- |
---|
| 397 | itcl::body Rappture::Tool::_output {data} { |
---|
| 398 | if {[string length $_outputcb] > 0} { |
---|
[2079] | 399 | uplevel #0 $_outputcb [list $data] |
---|
[23] | 400 | } |
---|
| 401 | } |
---|