Skip to content

Commit

Permalink
added tab separated list (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
justanhduc committed Oct 19, 2022
1 parent fbab5fa commit 2b8525a
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 8 deletions.
22 changes: 22 additions & 0 deletions jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
189 changes: 189 additions & 0 deletions list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
15 changes: 7 additions & 8 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ enum Request {
enum ListFormat {
DEFAULT,
JSON,
TAB
};

struct CommandLine {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2b8525a

Please sign in to comment.