author | viric@mandarina |
Wed, 18 Feb 2009 23:25:21 +0100 | |
changeset 267 | 11631dd11ff8 |
parent 259 | ba3827aa8f25 |
permissions | -rw-r--r-- |
125 | 1 |
/* |
2 |
Task Spooler - a task queue system for the unix user |
|
267 | 3 |
Copyright (C) 2007-2009 LluĂs Batlle i Rossell |
125 | 4 |
|
5 |
Please find the license in the provided COPYING file. |
|
6 |
*/ |
|
117 | 7 |
#include <stdio.h> |
8 |
#include <stdlib.h> |
|
9 |
#include <string.h> |
|
146 | 10 |
#include <sys/time.h> |
117 | 11 |
#include "main.h" |
12 |
||
219 | 13 |
/* From jobs.c */ |
14 |
extern int busy_slots; |
|
15 |
extern int max_slots; |
|
16 |
||
120 | 17 |
char * joblistdump_headers() |
18 |
{ |
|
19 |
char * line; |
|
20 |
||
21 |
line = malloc(600); |
|
150 | 22 |
snprintf(line, 600, "#!/bin/sh\n# - task spooler (ts) job dump\n" |
120 | 23 |
"# This file has been created because a SIGTERM killed\n" |
24 |
"# your queue server.\n" |
|
25 |
"# The finished commands are listed first.\n" |
|
26 |
"# The commands running or to be run are stored as you would\n" |
|
27 |
"# probably run them. Take care - some quotes may have got" |
|
28 |
" broken\n\n"); |
|
29 |
||
30 |
return line; |
|
31 |
} |
|
32 |
||
117 | 33 |
char * joblist_headers() |
34 |
{ |
|
35 |
char * line; |
|
36 |
||
37 |
line = malloc(100); |
|
229
fac290a405e6
Changing the list output from [slot=x/x] to [run=x/x] to
llbatlle@taga
parents:
219
diff
changeset
|
38 |
snprintf(line, 100, "%-4s %-10s %-20s %-8s %-14s %s [run=%i/%i]\n", |
117 | 39 |
"ID", |
40 |
"State", |
|
41 |
"Output", |
|
42 |
"E-Level", |
|
43 |
"Times(r/u/s)", |
|
219 | 44 |
"Command", |
45 |
busy_slots, |
|
46 |
max_slots); |
|
117 | 47 |
|
48 |
return line; |
|
49 |
} |
|
50 |
||
51 |
static int max(int a, int b) |
|
52 |
{ |
|
53 |
if (a > b) |
|
54 |
return a; |
|
55 |
return b; |
|
56 |
} |
|
57 |
||
58 |
static const char * ofilename_shown(const struct Job *p) |
|
59 |
{ |
|
60 |
const char * output_filename; |
|
61 |
||
156 | 62 |
if (p->state == SKIPPED) |
63 |
{ |
|
64 |
output_filename = "(no output)"; |
|
65 |
} else if (p->store_output) |
|
117 | 66 |
{ |
67 |
if (p->state == QUEUED) |
|
68 |
{ |
|
69 |
output_filename = "(file)"; |
|
70 |
} else |
|
71 |
{ |
|
72 |
if (p->output_filename == 0) |
|
73 |
/* This may happen due to concurrency |
|
74 |
* problems */ |
|
75 |
output_filename = "(...)"; |
|
76 |
else |
|
77 |
output_filename = p->output_filename; |
|
78 |
} |
|
79 |
} else |
|
80 |
output_filename = "stdout"; |
|
81 |
||
156 | 82 |
|
117 | 83 |
return output_filename; |
84 |
} |
|
85 |
||
86 |
static char * print_noresult(const struct Job *p) |
|
87 |
{ |
|
88 |
const char * jobstate; |
|
89 |
const char * output_filename; |
|
90 |
int maxlen; |
|
91 |
char * line; |
|
259 | 92 |
/* 18 chars should suffice for a string like "[int]&& " */ |
93 |
char dependstr[18] = ""; |
|
117 | 94 |
|
95 |
jobstate = jstate2string(p->state); |
|
96 |
output_filename = ofilename_shown(p); |
|
97 |
||
98 |
maxlen = 4 + 1 + 10 + 1 + max(20, strlen(output_filename)) + 1 + 8 + 1 |
|
99 |
+ 14 + 1 + strlen(p->command) + 20; /* 20 is the margin for errors */ |
|
100 |
||
170
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
101 |
if (p->label) |
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
102 |
maxlen += 3 + strlen(p->label); |
259 | 103 |
if (p->do_depend) |
104 |
{ |
|
105 |
maxlen += sizeof(dependstr); |
|
106 |
if (p->depend_on == -1) |
|
107 |
snprintf(dependstr, sizeof(dependstr), "&& "); |
|
108 |
else |
|
109 |
snprintf(dependstr, sizeof(dependstr), "[%i]&& ", p->depend_on); |
|
110 |
} |
|
170
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
111 |
|
117 | 112 |
line = (char *) malloc(maxlen); |
113 |
if (line == NULL) |
|
114 |
error("Malloc for %i failed.\n", maxlen); |
|
115 |
||
150 | 116 |
if (p->label) |
155 | 117 |
snprintf(line, maxlen, "%-4i %-10s %-20s %-8s %14s %s[%s]%s\n", |
150 | 118 |
p->jobid, |
119 |
jobstate, |
|
120 |
output_filename, |
|
121 |
"", |
|
122 |
"", |
|
259 | 123 |
dependstr, |
150 | 124 |
p->label, |
125 |
p->command); |
|
126 |
else |
|
155 | 127 |
snprintf(line, maxlen, "%-4i %-10s %-20s %-8s %14s %s%s\n", |
150 | 128 |
p->jobid, |
129 |
jobstate, |
|
130 |
output_filename, |
|
131 |
"", |
|
132 |
"", |
|
259 | 133 |
dependstr, |
150 | 134 |
p->command); |
117 | 135 |
|
136 |
return line; |
|
137 |
} |
|
138 |
||
139 |
static char * print_result(const struct Job *p) |
|
140 |
{ |
|
141 |
const char * jobstate; |
|
142 |
int maxlen; |
|
143 |
char * line; |
|
144 |
const char * output_filename; |
|
259 | 145 |
/* 18 chars should suffice for a string like "[int]&& " */ |
146 |
char dependstr[18] = ""; |
|
117 | 147 |
|
148 |
jobstate = jstate2string(p->state); |
|
149 |
output_filename = ofilename_shown(p); |
|
150 |
||
151 |
maxlen = 4 + 1 + 10 + 1 + max(20, strlen(output_filename)) + 1 + 8 + 1 |
|
152 |
+ 14 + 1 + strlen(p->command) + 20; /* 20 is the margin for errors */ |
|
153 |
||
170
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
154 |
if (p->label) |
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
155 |
maxlen += 3 + strlen(p->label); |
259 | 156 |
if (p->do_depend) |
157 |
{ |
|
158 |
maxlen += sizeof(dependstr); |
|
159 |
if (p->depend_on == -1) |
|
160 |
snprintf(dependstr, sizeof(dependstr), "&& "); |
|
161 |
else |
|
162 |
snprintf(dependstr, sizeof(dependstr), "[%i]&& ", p->depend_on); |
|
163 |
} |
|
170
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
164 |
|
117 | 165 |
line = (char *) malloc(maxlen); |
166 |
if (line == NULL) |
|
167 |
error("Malloc for %i failed.\n", maxlen); |
|
168 |
||
150 | 169 |
if (p->label) |
155 | 170 |
snprintf(line, maxlen, "%-4i %-10s %-20s %-8i %0.2f/%0.2f/%0.2f %s[%s]" |
150 | 171 |
"%s\n", |
172 |
p->jobid, |
|
173 |
jobstate, |
|
174 |
output_filename, |
|
175 |
p->result.errorlevel, |
|
176 |
p->result.real_ms, |
|
177 |
p->result.user_ms, |
|
178 |
p->result.system_ms, |
|
259 | 179 |
dependstr, |
150 | 180 |
p->label, |
181 |
p->command); |
|
182 |
else |
|
155 | 183 |
snprintf(line, maxlen, "%-4i %-10s %-20s %-8i %0.2f/%0.2f/%0.2f %s%s\n", |
150 | 184 |
p->jobid, |
185 |
jobstate, |
|
186 |
output_filename, |
|
187 |
p->result.errorlevel, |
|
188 |
p->result.real_ms, |
|
189 |
p->result.user_ms, |
|
190 |
p->result.system_ms, |
|
259 | 191 |
dependstr, |
150 | 192 |
p->command); |
117 | 193 |
|
194 |
return line; |
|
195 |
} |
|
196 |
||
197 |
char * joblist_line(const struct Job *p) |
|
198 |
{ |
|
199 |
char * line; |
|
200 |
||
201 |
if (p->state == FINISHED) |
|
202 |
line = print_result(p); |
|
203 |
else |
|
204 |
line = print_noresult(p); |
|
205 |
||
206 |
return line; |
|
207 |
} |
|
120 | 208 |
|
209 |
char * joblistdump_torun(const struct Job *p) |
|
210 |
{ |
|
211 |
int maxlen; |
|
212 |
char * line; |
|
213 |
||
214 |
maxlen = 10 + strlen(p->command) + 20; /* 20 is the margin for errors */ |
|
215 |
||
216 |
line = (char *) malloc(maxlen); |
|
217 |
if (line == NULL) |
|
218 |
error("Malloc for %i failed.\n", maxlen); |
|
219 |
||
150 | 220 |
snprintf(line, maxlen, "ts %s\n", p->command); |
120 | 221 |
|
222 |
return line; |
|
223 |
} |