Description:
[grader] box.cc now reports running status when die git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@172 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r46:31f3935d33aa - - 2 files changed: 61 inserted, 47 deleted

@@ -54,29 +54,57
54 54 #define long_seek(f,o,w) llseek(f,o,w)
55 55 #endif
56 56
57 + int max_mem_used = 0;
58 +
59 + void print_running_stat(double wall_time,
60 + double user_time,
61 + double system_time,
62 + int mem_usage)
63 + {
64 + fprintf(stderr,"%.4lfr%.4lfu%.4lfs%dm\n",
65 + wall_time, user_time, system_time, mem_usage);
66 + }
67 +
57 68 static void NONRET
58 69 box_exit(void)
59 70 {
60 - if (box_pid > 0)
61 - {
62 - if (is_ptraced)
63 - ptrace(PTRACE_KILL, box_pid);
64 - kill(-box_pid, SIGKILL);
65 - kill(box_pid, SIGKILL);
66 - }
67 - exit(1);
68 - }
71 + if (box_pid > 0) {
72 + if (is_ptraced)
73 + ptrace(PTRACE_KILL, box_pid);
74 + kill(-box_pid, SIGKILL);
75 + kill(box_pid, SIGKILL);
76 + }
69 77
70 - static void
71 - box_kill(void)
72 - {
73 - if (box_pid > 0)
74 - {
75 - if (is_ptraced)
76 - ptrace(PTRACE_KILL, box_pid);
77 - kill(-box_pid, SIGKILL);
78 - kill(box_pid, SIGKILL);
79 - }
78 + struct timeval total;
79 + int wall;
80 + struct rusage rus;
81 + int stat;
82 + pid_t p;
83 +
84 + // wait so that we can get information
85 + p = wait4(box_pid, &stat, WUNTRACED, &rus);
86 + if (p < 0) {
87 + fprintf(stderr,"wait4: error\n");
88 + print_running_stat(0,0,0,max_mem_used);
89 + } else if (p != box_pid) {
90 + fprintf(stderr,"wait4: unknown pid %d exited!\n", p);
91 + print_running_stat(0,0,0,max_mem_used);
92 + } else {
93 + if (!WIFEXITED(stat))
94 + fprintf(stderr,"wait4: unknown status\n");
95 + struct timeval total;
96 + int wall;
97 + wall = time(NULL) - start_time;
98 + timeradd(&rus.ru_utime, &rus.ru_stime, &total);
99 +
100 + print_running_stat((double)wall,
101 + (double) rus.ru_utime.tv_sec +
102 + ((double) rus.ru_utime.tv_usec/1000000.0),
103 + (double) rus.ru_stime.tv_sec +
104 + ((double) rus.ru_stime.tv_usec/1000000.0),
105 + max_mem_used);
106 + }
107 + exit(1);
80 108 }
81 109
82 110 static void NONRET __attribute__((format(printf,1,2)))
@@ -89,16 +117,6
89 117 box_exit();
90 118 }
91 119
92 - static void __attribute__((format(printf,1,2)))
93 - die_report(char *msg, ...)
94 - {
95 - va_list args;
96 - va_start(args, msg);
97 - vfprintf(stderr, msg, args);
98 - fputc('\n', stderr);
99 - box_kill();
100 - }
101 -
102 120 static void __attribute__((format(printf,1,2)))
103 121 log(char *msg, ...)
104 122 {
@@ -349,12 +367,10
349 367 if (verbose > 1)
350 368 fprintf(stderr, "[timecheck: %d seconds]\n", sec);
351 369 if (sec > timeout) {
352 - die_report("Time limit exceeded.");
370 + die("Time limit exceeded.");
353 371 }
354 372 }
355 373
356 - int max_mem_used = 0;
357 -
358 374 static void
359 375 check_memory_usage()
360 376 {
@@ -452,13 +468,12
452 468 // report OK and statistics
453 469 fprintf(stderr,"OK\n");
454 470
455 - fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n",
456 - wall,
457 - (double) rus.ru_utime.tv_sec +
458 - ((double) rus.ru_utime.tv_usec/1000000.0),
459 - (double) rus.ru_stime.tv_sec +
460 - ((double) rus.ru_stime.tv_usec/1000000.0),
461 - max_mem_used);
471 + print_running_stat((double) wall,
472 + (double) rus.ru_utime.tv_sec +
473 + ((double) rus.ru_utime.tv_usec/1000000.0),
474 + (double) rus.ru_stime.tv_sec +
475 + ((double) rus.ru_stime.tv_usec/1000000.0),
476 + max_mem_used);
462 477 /*
463 478 (%.4lf sec real (%d), %d sec wall, %d syscalls, %d kb)\n",
464 479 (double) total.tv_sec + ((double)total.tv_usec / 1000000.0),
@@ -478,13 +493,12
478 493 int wall;
479 494 wall = time(NULL) - start_time;
480 495 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
481 - fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n",
482 - wall,
483 - (double) rus.ru_utime.tv_sec +
484 - ((double) rus.ru_utime.tv_usec/1000000.0),
485 - (double) rus.ru_stime.tv_sec +
486 - ((double) rus.ru_stime.tv_usec/1000000.0),
487 - max_mem_used);
496 + print_running_stat((double) wall,
497 + (double) rus.ru_utime.tv_sec +
498 + ((double) rus.ru_utime.tv_usec/1000000.0),
499 + (double) rus.ru_stime.tv_sec +
500 + ((double) rus.ru_stime.tv_usec/1000000.0),
501 + max_mem_used);
488 502 exit(0);
489 503 }
490 504 if (WIFSTOPPED(stat))
@@ -241,7 +241,7
241 241 :graded_at= => nil,
242 242 :compiler_message= => '',
243 243 :grader_comment= => '',
244 - :running_stat= => /[Ee]xit.*status.*10.*0\.0 sec/m,
244 + :running_stat= => /[Ee]xit.*status.*10.*0\.0\d* sec/m,
245 245 :output_file_name= => lambda { |fname|
246 246 File.exists?(fname).should be_true
247 247 },
You need to be logged in to leave comments. Login now