From 2b8525a68b9550c40b696da8f9361c0171c8c532 Mon Sep 17 00:00:00 2001 From: Duc Nguyen Date: Wed, 19 Oct 2022 11:14:50 +0900 Subject: [PATCH] added tab separated list (#14) --- jobs.c | 22 +++++++ list.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 15 +++-- main.h | 3 + 4 files changed, 221 insertions(+), 8 deletions(-) diff --git a/jobs.c b/jobs.c index fc7e26b..f679c3d 100755 --- a/jobs.c +++ b/jobs.c @@ -532,6 +532,28 @@ void s_list(int s, enum ListFormat listFormat) { cJSON_Delete(jobs); free(buffer); } + else if (listFormat == TAB) { + /* Show Queued or Running jobs */ + p = firstjob; + while (p != 0) { + if (p->state != HOLDING_CLIENT) { + buffer = joblist_line_plain(p); + send_list_line(s, buffer); + free(buffer); + } + p = p->next; + } + + p = first_finished_job; + + /* Show Finished jobs */ + while (p != 0) { + buffer = joblist_line_plain(p); + send_list_line(s, buffer); + free(buffer); + p = p->next; + } + } } #ifndef CPU diff --git a/list.c b/list.c index a8315e2..9cdb16c 100755 --- a/list.c +++ b/list.c @@ -309,6 +309,184 @@ char *jobgpulist_line(const struct Job *p) { } #endif +static char *plainprint_noresult(const struct Job *p) { + const char *jobstate; + const char *output_filename; + int maxlen; + char *line; + /* 20 chars should suffice for a string like "[int,int,..]&& " */ + char dependstr[20] = ""; + + jobstate = jstate2string(p->state); + output_filename = ofilename_shown(p); + + maxlen = 4 + 1 + 10 + 1 + 20 + 1 + 8 + 1 + + 25 + 1 + 5 + 1 + strlen(p->command) + 20; /* 20 is the margin for errors */ + + if (p->label) + maxlen += 3 + strlen(p->label); + + if (p->depend_on_size) { + maxlen += sizeof(dependstr); + int pos = 0; + if (p->depend_on[0] == -1) + pos += snprintf(&dependstr[pos], sizeof(dependstr), "[ "); + else + pos += snprintf(&dependstr[pos], sizeof(dependstr), "[%i", p->depend_on[0]); + + for (int i = 1; i < p->depend_on_size; i++) { + if (p->depend_on[i] == -1) + pos += snprintf(&dependstr[pos], sizeof(dependstr), ", "); + else + pos += snprintf(&dependstr[pos], sizeof(dependstr), ",%i", p->depend_on[i]); + } + pos += snprintf(&dependstr[pos], sizeof(dependstr), "]&& "); + } + + line = (char *) malloc(maxlen); + if (line == NULL) + error("Malloc for %i failed.\n", maxlen); + + if (p->label) +#ifdef CPU + snprintf(line, maxlen, "%i\t%s\t%s\t%s\t%s\t%s\t[%s]\t%s\n", + p->jobid, + jobstate, + output_filename, + "", + "", + dependstr, + p->label, + p->command); +#else + snprintf(line, maxlen, "%i\t%s\t%s\t%s\t%s\t%d\t%s\t[%s]\t%s\n", + p->jobid, + jobstate, + output_filename, + "", + "", + p->num_gpus, + dependstr, + p->label, + p->command); +#endif + else +#ifdef CPU + snprintf(line, maxlen, "%i\t%s\t%s\t%s\t%s\t%s\t\t%s\n", + p->jobid, + jobstate, + output_filename, + "", + "", + dependstr, + p->command); +#else + snprintf(line, maxlen, "%i\t%s\t%s\t%s\t%s\t%d\t%s\t\t%s\n", + p->jobid, + jobstate, + output_filename, + "", + "", + p->num_gpus, + dependstr, + p->command); +#endif + + return line; +} + +static char *plainprint_result(const struct Job *p) { + const char *jobstate; + int maxlen; + char *line; + const char *output_filename; + /* 20 chars should suffice for a string like "[int,int,..]&& " */ + char dependstr[20] = ""; + float real_ms = p->result.real_ms; + char *unit = time_rep(&real_ms); + + jobstate = jstate2string(p->state); + output_filename = ofilename_shown(p); + + maxlen = 4 + 1 + 10 + 1 + 20 + 1 + 8 + 1 + + 25 + 1 + + 5 + 1 + strlen(p->command) + 20; /* 20 is the margin for errors */ + + if (p->label) + maxlen += 3 + strlen(p->label); + + if (p->depend_on_size) { + maxlen += sizeof(dependstr); + int pos = 0; + if (p->depend_on[0] == -1) + pos += snprintf(&dependstr[pos], sizeof(dependstr), "[ "); + else + pos += snprintf(&dependstr[pos], sizeof(dependstr), "[%i", p->depend_on[0]); + + for (int i = 1; i < p->depend_on_size; i++) { + if (p->depend_on[i] == -1) + pos += snprintf(&dependstr[pos], sizeof(dependstr), ", "); + else + pos += snprintf(&dependstr[pos], sizeof(dependstr), ",%i", p->depend_on[i]); + } + pos += snprintf(&dependstr[pos], sizeof(dependstr), "]&& "); + } + + line = (char *) malloc(maxlen); + if (line == NULL) + error("Malloc for %i failed.\n", maxlen); + + if (p->label) +#ifdef CPU + snprintf(line, maxlen, "%i\t%s\t%s\t%i\t%.2f\t%s\t%s\t[%s]\t%s\n", + p->jobid, + jobstate, + output_filename, + p->result.errorlevel, + real_ms, + unit, + dependstr, + p->label, + p->command); +#else + snprintf(line, maxlen, "%i\t%s\t%s\t%i\t%.2f\t%s\t%d\t%s\t[%s]\t%s\n", + p->jobid, + jobstate, + output_filename, + p->result.errorlevel, + real_ms, + unit, + p->num_gpus, + dependstr, + p->label, + p->command); +#endif + else +#ifdef CPU + snprintf(line, maxlen, "%i\t%s\t%s\t%i\t%.2f\t%s\t%s\t\t%s\n", + p->jobid, + jobstate, + output_filename, + p->result.errorlevel, + real_ms, + unit, + dependstr, + p->command); +#else + snprintf(line, maxlen, "%i\t%s\t%s\t%i\t%.2f\t%s\t%d\t%s\t\t%s\n", + p->jobid, + jobstate, + output_filename, + p->result.errorlevel, + real_ms, + unit, + p->num_gpus, + dependstr, + p->command); +#endif + + return line; +} + char *joblist_line(const struct Job *p) { char *line; @@ -320,6 +498,17 @@ char *joblist_line(const struct Job *p) { return line; } +char *joblist_line_plain(const struct Job *p) { + char *line; + + if (p->state == FINISHED) + line = plainprint_result(p); + else + line = plainprint_noresult(p); + + return line; +} + char *joblistdump_torun(const struct Job *p) { int maxlen; char *line; diff --git a/main.c b/main.c index f3eb45d..93b6f20 100755 --- a/main.c +++ b/main.c @@ -406,18 +406,17 @@ void parse_opts(int argc, char **argv) { fprintf(stderr, "-M can only be used when listing jobs.\n"); exit(-1); } - enum ListFormat format; - if (strcmp(optarg, "default") == 0) { - format = DEFAULT; - } - else if (strcmp(optarg, "json") == 0) { - format = JSON; - } + + if (strcmp(optarg, "default") == 0) + command_line.list_format = DEFAULT; + else if (strcmp(optarg, "json") == 0) + command_line.list_format = JSON; + else if (strcmp(optarg, "tab") == 0) + command_line.list_format = TAB; else { fprintf(stderr, "Invalid argument for option M: %s.\n", optarg); exit(-1); } - command_line.list_format = format; break; case '?': fprintf(stderr, "Wrong option %c.\n", optopt); diff --git a/main.h b/main.h index 5740a53..491b879 100755 --- a/main.h +++ b/main.h @@ -96,6 +96,7 @@ enum Request { enum ListFormat { DEFAULT, JSON, + TAB }; struct CommandLine { @@ -484,6 +485,8 @@ char *jobgpulist_header(); char *joblist_line(const struct Job *p); +char *joblist_line_plain(const struct Job *p); + char *joblistdump_torun(const struct Job *p); char *joblistdump_headers();