-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus_writing.cpp
217 lines (205 loc) · 6.47 KB
/
status_writing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "status_writing.hpp"
#include <chrono>
#include <cstdint>
#include <format>
#include <iostream>
#include <sstream>
#include "functions.hpp"
#include "output_manager.hpp"
#include "status_manager.hpp"
namespace tsp {
void print_job_stdout(Status_Manager sm_ro, uint32_t id) {
auto details = sm_ro.get_job_details_by_id(id);
if (details.stat.status) {
std::cout << sm_ro.get_job_stdout(id);
} else {
std::ifstream stream{get_tmp() /
(std::string(out_file_template) + details.uuid)};
std::cout << stream.rdbuf();
}
};
void print_job_stderr(Status_Manager sm_ro, uint32_t id) {
auto details = sm_ro.get_job_details_by_id(id);
if (details.stat.status) {
std::cout << sm_ro.get_job_stderr(id);
} else {
std::ifstream stream{get_tmp() /
(std::string(err_file_template) + details.uuid)};
std::cout << stream.rdbuf();
}
};
void format_jobs_table(std::vector<tsp::job_stat> jobs) {
// Not finished
std::cout << "ID | State | ExitStat | Run Time | Command\n";
std::cout << "=====================================================\n";
for (const auto &info : jobs) {
if (!info.etime) {
std::string state{!info.stime ? "queued" : "running"};
std::printf("%-5d %10s %s\n", info.id,
state.c_str(), info.cmd.c_str());
} else {
std::printf(
"%-5d finished %10d%14s %s\n", info.id, info.status.value(),
format_hh_mm_ss(info.etime.value() - info.stime.value()).c_str(),
info.cmd.c_str());
}
}
}
void format_jobs_gh_md(std::vector<tsp::job_stat> jobs) {
std::cout << "## Case timings\nCase | Time | Success?\n---- | ----: | ----\n";
for (auto &info : jobs) {
if (!info.etime) {
continue;
}
std::string cmd;
auto ss = std::stringstream{info.cmd};
std::string tok;
bool save = (info.cmd.find("python3") == std::string::npos);
while (getline(ss, tok, ' ')) {
if (save) {
cmd.append(tok);
cmd.append(" ");
}
if (tok.find("python3") != std::string::npos) {
save = true;
}
}
if (info.category) {
cmd = std::format("{}: {}", info.category.value(), cmd);
}
info.cmd = cmd;
}
std::sort(jobs.begin(), jobs.end(),
[](tsp::job_stat a, tsp::job_stat b) { return a.cmd < b.cmd; });
for (const auto &info : jobs) {
if (!info.etime) {
continue;
}
std::cout << info.cmd << " | "
<< format_hh_mm_ss(info.etime.value() - info.stime.value())
<< " | " << (info.status == 0 ? "Yes\n" : "No\n");
}
}
void print_job_detail(Status_Manager sm_ro, uint32_t id) {
auto info = sm_ro.get_job_details_by_id(id);
// Expects /etc/localtime to be symlink, therefore
// broken on Gadi
auto tz = std::chrono::current_zone();
// Finished
std::string runtime{format_hh_mm_ss(info.stat.etime.value_or(now()) -
info.stat.stime.value_or(now()))};
if (!info.stat.stime) {
std::cout << "Status: Queued\n";
} else if (!info.stat.etime) {
std::cout << "Status: Running\n";
} else {
std::cout << "Status: Finished with exit status "
<< info.stat.status.value() << "\n";
}
std::cout << "Command: " << info.stat.cmd << "\n";
std::cout << "Slots required: " << info.slots << "\n";
std::chrono::system_clock::time_point tp{
std::chrono::microseconds{info.stat.qtime}};
std::cout << "Enqueue time: " << tz->to_local(tp) << "\n";
if (info.stat.stime) {
std::chrono::system_clock::time_point tp{
std::chrono::microseconds{info.stat.stime.value()}};
std::cout << "Start time: " << tz->to_local(tp) << "\n";
}
if (info.stat.etime) {
std::chrono::system_clock::time_point tp{
std::chrono::microseconds{info.stat.etime.value()}};
std::cout << "End time: " << tz->to_local(tp) << "\n";
}
if (info.stat.stime) {
std::cout << "Time run: " << runtime << "\n";
std::cout << "TSP process pid: " << info.pid.value() << "\n";
}
std::cout << "Internal UUID: " << info.uuid << std::endl;
};
void print_jobs_list(Status_Manager sm_ro, ListCategory c) {
format_jobs_table(sm_ro.get_job_stats_by_category(c));
}
void print_github_summary(Status_Manager sm_ro) {
format_jobs_gh_md(sm_ro.get_job_stats_by_category(ListCategory::all));
};
void print_time(Status_Manager sm_ro, TimeCategory c, uint32_t jobid) {
auto stat = sm_ro.get_job_by_id(jobid);
switch (c) {
case TimeCategory::queue:
std::cout << format_hh_mm_ss(stat.stime.value_or(now()) - stat.qtime);
break;
case TimeCategory::run:
if (stat.stime) {
std::cout << format_hh_mm_ss(stat.etime.value_or(now()) -
stat.stime.value());
} else {
std::cout << "0.000";
}
break;
case TimeCategory::total:
std::cout << format_hh_mm_ss(stat.etime.value_or(now()) - stat.qtime);
break;
}
std::cout << std::endl;
}
int do_writer_action(Action a, uint32_t jobid) {
auto sm_ro = Status_Manager(false);
switch (a) {
case Action::info:
print_job_detail(sm_ro, jobid);
break;
case Action::stdout:
print_job_stdout(sm_ro, jobid);
break;
case Action::stderr:
print_job_stderr(sm_ro, jobid);
break;
default:
die_with_err("Error! Jobid supplied for action that cannot take jobid", -1);
}
return EXIT_SUCCESS;
};
int do_writer_action(Action a) {
auto sm_ro = Status_Manager(false);
switch (a) {
case Action::github_summary:
print_github_summary(sm_ro);
break;
case Action::info:
case Action::stdout:
case Action::stderr:
return do_writer_action(a, sm_ro.get_last_job_id());
break;
default:
die_with_err("Error! 'list' action requested without a category", -1);
}
return EXIT_SUCCESS;
}
int do_writer_action(Action a, ListCategory c) {
auto sm_ro = Status_Manager(false);
switch (a) {
case Action::list:
print_jobs_list(sm_ro, c);
break;
default:
die_with_err("Error! List category supplied for non-list action", -1);
}
return EXIT_SUCCESS;
};
int do_writer_action(Action a, TimeCategory c, uint32_t jobid) {
auto sm_ro = Status_Manager(false);
switch (a) {
case Action::print_time:
print_time(sm_ro, c, jobid);
break;
default:
die_with_err("Error! Time category supplied for non-print_time action", -1);
}
return EXIT_SUCCESS;
};
int do_writer_action(Action a, TimeCategory c) {
auto sm_ro = Status_Manager(false);
return do_writer_action(a, c, sm_ro.get_last_job_id());
};
} // namespace tsp