Next version of JABA
[jabaws.git] / engine / compbio / engine / cluster / drmaa / StatisticManager.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.engine.cluster.drmaa;\r
20 \r
21 import java.util.Date;\r
22 import java.util.Map;\r
23 \r
24 import org.ggf.drmaa.DrmaaException;\r
25 import org.ggf.drmaa.JobInfo;\r
26 \r
27 import compbio.util.Util;\r
28 \r
29 public class StatisticManager {\r
30 \r
31         private final Map<String, String> usage;\r
32         final JobInfo jobinfo;\r
33         private static String newLine = "\n";\r
34 \r
35         public StatisticManager(JobInfo status) throws DrmaaException {\r
36                 assert status != null;\r
37                 // TODO this is potentially unsafe may need reviewing\r
38                 this.jobinfo = status;\r
39                 Map<String, String> resourcesUsage = status.getResourceUsage();\r
40                 this.usage = resourcesUsage;\r
41         }\r
42 \r
43         public String getJobId() throws DrmaaException {\r
44                 return jobinfo.getJobId();\r
45         }\r
46 \r
47         public boolean hasExited() throws DrmaaException {\r
48                 return jobinfo.hasExited();\r
49         }\r
50 \r
51         public boolean hasSignaled() throws DrmaaException {\r
52                 return jobinfo.hasSignaled();\r
53         }\r
54 \r
55         public boolean hasDump() throws DrmaaException {\r
56                 return jobinfo.hasCoreDump();\r
57         }\r
58 \r
59         public String termSignal() throws DrmaaException {\r
60                 return jobinfo.getTerminatingSignal();\r
61         }\r
62 \r
63         public boolean wasAborted() throws DrmaaException {\r
64                 return jobinfo.wasAborted();\r
65         }\r
66 \r
67         public String getSubmissionTime() {\r
68                 return usage.get(submission_time);\r
69         }\r
70 \r
71         public String getVMem() {\r
72                 return usage.get(vmem);\r
73         }\r
74 \r
75         public String getMaxVMem() {\r
76                 return usage.get(maxvmem);\r
77         }\r
78 \r
79         public String getUsedSysTime() {\r
80                 return usage.get(ru_stime);\r
81         }\r
82 \r
83         public String getUsedUserTime() {\r
84                 return usage.get(ru_utime);\r
85         }\r
86 \r
87         public String getCalculationTime() {\r
88                 return usage.get(ru_wallclock);\r
89         }\r
90 \r
91         public String getEndTime() {\r
92                 return usage.get(end_time);\r
93         }\r
94 \r
95         public String getStartTime() {\r
96                 return usage.get(start_time);\r
97         }\r
98 \r
99         public String getIOWait() {\r
100                 return usage.get(iow);\r
101         }\r
102 \r
103         public String getCPUUsageTime() {\r
104                 return usage.get(cpu);\r
105         }\r
106 \r
107         public String getDataTransfered() {\r
108                 return usage.get(io);\r
109         }\r
110 \r
111         public String getJobPriority() {\r
112                 return usage.get(priority);\r
113         }\r
114 \r
115         public String getExitStatus() {\r
116                 return usage.get(exit_status);\r
117         }\r
118 \r
119         public String getAllStats() throws DrmaaException {\r
120 \r
121                 String stats = "JobID: " + getJobId() + newLine;\r
122                 stats += getExecutionStat() + newLine;\r
123                 stats += getTimeStat() + newLine;\r
124                 stats += getCPUTimeStat() + newLine;\r
125                 stats += getMemoryStat() + newLine;\r
126 \r
127                 return stats;\r
128         }\r
129 \r
130         public String getExecutionStat() throws DrmaaException {\r
131                 String stats = "Priority:" + getJobPriority() + newLine;\r
132                 /* report how job finished */\r
133                 if (wasAborted()) {\r
134                         stats += "job \"" + getJobId() + "\" was aborted (never ran)"\r
135                                         + newLine;\r
136                 } else if (hasExited()) {\r
137                         stats += "job \"" + getJobId()\r
138                                         + "\" finished regularly with exit status "\r
139                                         + getExitStatus() + newLine;\r
140                 } else if (hasSignaled()) {\r
141                         stats += "job \"" + getJobId() + "\" finished due to signal "\r
142                                         + termSignal() + newLine;\r
143                 } else {\r
144                         stats += "job \"" + getJobId()\r
145                                         + "\" finished with unclear conditions" + newLine;\r
146                 }\r
147                 stats += "Has Core Dump: " + hasDump() + newLine;\r
148                 return stats;\r
149         }\r
150 \r
151         public String getMemoryStat() {\r
152                 String stats = "Data transfered: " + getDataTransfered() + newLine;\r
153                 stats += "IO wait: " + getIOWait() + newLine;\r
154                 stats += "Max Virtual Memory: " + getMaxVMem() + newLine;\r
155                 stats += "Virtual Memory: " + getVMem() + newLine;\r
156                 return stats;\r
157         }\r
158 \r
159         public String getCPUTimeStat() {\r
160                 String stats = "CPU time (s): " + getCPUUsageTime() + newLine;\r
161                 stats += "Sys time (s): " + getUsedSysTime() + newLine;\r
162                 stats += "User time (s): " + getUsedUserTime() + newLine;\r
163                 return stats;\r
164         }\r
165 \r
166         public String getTimeStat() {\r
167                 String stats = "Submission time: " + formatTime(getSubmissionTime())\r
168                                 + newLine;\r
169                 stats += "Calculation time (s): " + getCalculationTime() + newLine;\r
170                 stats += "Start time: " + formatTime(getStartTime()) + newLine;\r
171                 stats += "End time: " + formatTime(getEndTime()) + newLine;\r
172                 return stats;\r
173         }\r
174 \r
175         public Map<String, String> getRawUsage() {\r
176                 return usage;\r
177         }\r
178 \r
179         @Override\r
180         public String toString() {\r
181                 String stat = "";\r
182                 try {\r
183                         stat = getAllStats();\r
184                 } catch (DrmaaException e) {\r
185                         // Should not happen\r
186                         throw new RuntimeException("exception during toString execution! ",\r
187                                         e);\r
188                 }\r
189                 return stat;\r
190         }\r
191 \r
192         /**\r
193          * Convert grid engine time format End time: 1250701672.0000 to a readable\r
194          * time representation as defined in Util.dataf\r
195          */\r
196         String formatTime(String time) {\r
197                 int dotIdx = time.indexOf(".");\r
198                 if (dotIdx > 0) {\r
199                         // get rid of .0000 part\r
200                         time = time.substring(0, dotIdx);\r
201                         Date d = new Date();\r
202                         d.setTime(Long.parseLong(time + "000"));\r
203                         time = Util.datef.format(d);\r
204                 } // else do not know how to format\r
205                 return time;\r
206         }\r
207 \r
208         /**\r
209          * see man 5 accounting on sun grid engine installed workstation see also\r
210          * man 2 getrusage\r
211          * \r
212          */\r
213         String signal = "signal";\r
214         String submission_time = "submission_time"; // Submission time (GMT unix\r
215         // time stamp).\r
216         String vmem = "vmem";\r
217         String maxvmem = "maxvmem"; // The maximum vmem size in bytes.\r
218         String ru_stime = "ru_stime"; // system time used\r
219         String ru_utime = "ru_utime"; // user time used\r
220         String ru_wallclock = "ru_wallclock"; // Difference between end_time and\r
221         // start_time\r
222         String end_time = "end_time"; // End time (GMT unix time stamp).\r
223         String start_time = "start_time"; // Start time (GMT unix time stamp).\r
224 \r
225         String iow = "iow"; // The io wait time in seconds.\r
226         String cpu = "cpu"; // The cpu time usage in seconds.\r
227         String io = "io"; // The amount of data transferred in input/output\r
228         // operations.\r
229 \r
230         String priority = "priority";\r
231         /*\r
232          * Priority value assigned to the job corresponding to the priority\r
233          * parameter in the queue configuration\r
234          */\r
235         String exit_status = "exit_status";\r
236         /*\r
237          * Exit status of the job script (or Sun Grid Engine specific status in case\r
238          * of certain error conditions). The exit status is determined by following\r
239          * the normal shell conventions. If the command terminates normally the\r
240          * value of the command is its exit status. However, in the case that the\r
241          * command exits abnormally, a value of 0200 (octal), 128 (decimal) is added\r
242          * to the value of the command to make up the exit status. For example: If a\r
243          * job dies through signal 9 (SIGKILL) then the exit status becomes 128 + 9\r
244          * = 137.\r
245          */\r
246 \r
247         // Could not find a discription for these\r
248         String acct_cpu = "acct_cpu";\r
249         String acct_mem = "acct_mem";\r
250         String acct_iow = "acct_iow";\r
251         String acct_io = "acct_io";\r
252         String acct_maxvmem = "acct_maxvmem";\r
253 \r
254         /*\r
255          * Appears to be not reported String ru_idrss=0.000; // integral unshared\r
256          * data size String ru_inblock=0.000; String ru_ismrss=0.0000 String\r
257          * ru_majflt=0.0000 String ru_msgrcv=0.0000 String ru_isrss="ru_isrss" //\r
258          * integral unshared stack size String ru_nswap=0.0000 String\r
259          * ru_oublock=0.0000 String ru_msgsnd=0.0000 String ru_nsignals=0.0000\r
260          * String ru_maxrss="ru_maxrss"; // maximum resident set size // Too\r
261          * technical to be useful String ru_nivcsw=524.0000 // involuntary context\r
262          * switches String ru_minflt=9668.0000 // page reclaims String\r
263          * ru_nvcsw=97.0000 // voluntary context switches String\r
264          * ru_ixrss="ru_ixrss"; // integral shared memory size String mem="mem"; //\r
265          * The integral memory usage in Gbytes cpu seconds.\r
266          */\r
267 \r
268 }\r